﻿var JSON = JSON || {};

JSON.stringify = JSON.stringify || function(obj)
{
	var t = typeof (obj);
	if (t != "object" || obj === null)
	{
		// simple data type
		if (t == "string") obj = '"' + obj + '"';
		return String(obj);
	}
	else
	{
		// recurse array or object 
		var n, v, json = [], arr = (obj && obj.constructor == Array);
		for (n in obj)
		{
			v = obj[n]; t = typeof (v);

			if (t == "string") v = '"' + v + '"';
			else if (t == "object" && v !== null) v = JSON.stringify(v);

			json.push((arr ? "" : '"' + n + '":') + String(v));
		}

		return (arr ? "[" : "{") + String(json) + (arr ? "]" : "}");
	}
};

JSON.parse = JSON.parse || function(str)
{
	if (str === "") str = '""';
	eval("var p=" + str + ";");
	return p;
};

// Returns the first n charachters of the string str
function Left(str, n)
{
	if (n <= 0)
		return "";
	else if (n > String(str).length)
		return str;
	else
		return String(str).substring(0, n);
}

// Returns the last n charachters of the string str
function Right(str, n)
{
	if (n <= 0)
		return "";
	else if (n > String(str).length)
		return str;
	else
	{
		var iLen = String(str).length;
		return String(str).substring(iLen, iLen - n);
	}
}

// Forces a particular element (instanceId) to have the highest z-index when compared to all others that share the css class (className) 
function BringElementToTop(instanceId, className)
{
	var zmax = 0;

	//var combos = $('.comboWrapper');
	var collection = $('.' + className);
	for (var n = 0; n < collection.length; n++)
	{
		//var siblingId = collection[n].id;
		//var zIndex = $('#' + siblingId).css('z-index');
		var sibling = collection[n];
		var zIndex = $(sibling).css('z-index');
		zmax = parseInt(zIndex > zmax ? zIndex : zmax);
	}

	zmax = zmax + 1;
	$('#' + instanceId).css('z-index', zmax);

}

var CustomEvent = function()
{
	//name of the event
	this.eventName = arguments[0];

	//function to call on event fire
	var eventAction = null;

	//subscribe a function to the event
	this.subscribe = function(fn)
	{
		eventAction = fn;
	};

	//fire the event
	this.fire = function(sender, eventArgs)
	{
		if (eventAction != null)
		{
			eventAction(sender, eventArgs);
		}
	};
};

function areArraysEqual(array1, array2)
{
	var temp = new Array();
	if ((!array1[0]) || (!array2[0]))
	{ // If either is not an array
		return false;
	}
	if (array1.length != array2.length)
	{
		return false;
	}
	// Put all the elements from array1 into a "tagged" array
	for (var i = 0; i < array1.length; i++)
	{
		key = (typeof array1[i]) + "~" + array1[i];
		// Use "typeof" so a number 1 isn't equal to a string "1".
		if (temp[key]) { temp[key]++; } else { temp[key] = 1; }
		// temp[key] = # of occurrences of the value (so an element could appear multiple times)
	}
	// Go through array2 - if same tag missing in "tagged" array, not equal
	for (var i = 0; i < array2.length; i++)
	{
		key = (typeof array2[i]) + "~" + array2[i];
		if (temp[key])
		{
			if (temp[key] == 0) { return false; } else { temp[key]--; }
			// Subtract to keep track of # of appearances in array2
		} 
		else	
		{ // Key didn't appear in array1, arrays are not equal.
			return false;
		}
	}
	// If we get to this point, then every generated key in array1 showed up the exact same
	// number of times in array2, so the arrays are equal.
	return true;
}
function ActivateMouseOver(selectClass, mouseOverClass, mouseDownClass)
{
	$('.' + selectClass).bind('mouseenter', function() { $(this).addClass(mouseOverClass); }); //mouse over
	$('.' + selectClass).bind('mousedown', function() { $(this).removeClass(mouseOverClass); }); //mouse down
	$('.' + selectClass).bind('mousedown', function() { $(this).addClass(mouseDownClass); });
	$('.' + selectClass).bind('mouseup', function() { $(this).removeClass(mouseDownClass); }); //mouse up
	$('.' + selectClass).bind('mouseup', function() { $(this).addClass(mouseOverClass); });
	$('.' + selectClass).bind('mouseleave', function() { $(this).removeClass(mouseOverClass); }); //mouse out
	$('.' + selectClass).bind('mouseleave', function() { $(this).removeClass(mouseDownClass); }); //mouse out outside of button
}