// ---------------------------------------------------------------------------------------------------- //
// -- Utility
// ---------------------------------------------------------------------------------------------------- //
//
// jQuery on an empty object, we are going to use this as our Queue
var ajaxQueue = jQuery({})
//
var Utility = {
	// Helper function for printing out debug messages
	trace : function(s) {
		//
		return false;
		var row = 1;
		//
		// Log to Firebug console if available
		if(window.console != undefined && typeof window.console.log == 'function') {
			console.log(s);
		}
		//
		// Log to output div if debug console is not available
		if(jQuery('#output').get(0) != undefined) {
			if(row >= 1000) {
				var r = row;
			} else if(row >= 100) {
				var r = '&nbsp;' + row;
			} else if(row >= 10) {
				var r = '&nbsp;&nbsp;' + row;
			} else {
				var r = '&nbsp;&nbsp;&nbsp;' + row;
			}
			// write s to the "output" window
			jQuery('#output').html(jQuery('#output').html() + r + ': ' + s + '<br />').get(0).scrollTop += 10000;
			//
			row++;
		}
	},
	//
	// Extracts the value of a variable from the browser query string
	getQueryStringVar : function(ref) {
		var value;
		var pageURL = window.location.toString();
		var queryString = pageURL.substring(pageURL.indexOf("?") + 1);
		//
		var vars = queryString.split("&");
		var pair;
		for(var i = 0, n = vars.length; i < n; i++) {
			pair = vars[i].split("=");
			if(pair[0] == ref) {
				value = pair[1];
				break;
			}
		}
		//
		return value;
	},
	// Extracts the browser query string and converts to an Object
	getQueryStringObject : function() {
		//
		var obj = {};
		var pageURL = window.location.toString();
		var queryString = pageURL.substring(pageURL.indexOf("?") + 1);
		//
		var vars = queryString.split("&");
		var pair;
		for(var i = 0, n = vars.length; i < n; i++) {
			pair = vars[i].split("=");
			obj[pair[0]] = pair[1];
		}
		//
		return obj;
	},
	//
	/*_this.overlay = Utility.createOverlay('body', {
					rgb : '#000000',
					opacity : opacity
	});*/
	createOverlay : function(id, settings) {
		var _this = this;
		//
		var div;
		var wWidth;
		var wHeight;
		var oWidth;
		var oHeight;
		var depth;
		var positioning;
		//
		if(id == 'body') {
			div = id;
			//
			wWidth = jQuery(window).width();
			wHeight = jQuery(window).height();
			oWidth = '100%';
			oHeight = '100%';
			positioning = 'fixed';
			depth = 1000;
			//
		} else {
			div = '#' + id;
			wWidth = jQuery(div).width();
			wHeight = jQuery(div).height();
			oWidth = '100%';
			oHeight = '100%';
			positioning = 'absolute';
			depth = 0;
		}
		//
		// create a new div for the overlay
		var overlay = jQuery('<div id="overlay" />').get();
		var bg = jQuery('<div id="overlay-bg" />').get();
		//
		// ensure the overlay fills the div and is above all other content
		jQuery(overlay).css({
			display : 'block',
			width : oWidth,
			height : oHeight,
			position : positioning,
			left : 0,
			top : 0,
			overflow : 'hidden',
			'z-index' : depth,
			right : 0,
                        bottom : 0
		});
		// ensure the bg fills the overlay
		jQuery(bg).css({
			display : 'block',
			position : 'absolute',
			width : '100%',
			height : '100%',
			'background-color' : settings.rgb,
			opacity : settings.opacity
		});
		//
		// APPEND
		jQuery(div).append(overlay);
		jQuery(overlay).append(bg);
		//
		//alert(jQuery(overlay).width() + " : " + jQuery(overlay).height() + " : " + jQuery('body').width() + " : " + jQuery('body').height() + " : "+ screen.width+" : "+screen.height);
		//
		return overlay;
	},
	//
	killOverlay : function() {
		// remove all listeners
		jQuery('#overlay').unbind();
		// remove overlay
		jQuery('#overlay').remove();
	},
	//
	addSimpleSwitchRollover : function(e) {
		//
		var h = jQuery(e).height();
		if(jQuery(e).css('background-position')) {
			var posY = jQuery(e).css('background-position').split(' ')[1];
		} else {
			var posY = jQuery(e).css('background-position-y');
		}
		var startY = parseInt(posY.substring(0, posY.indexOf('px')));
		var span = jQuery(e).find('span').first();
		//
		jQuery(e).hover(function() {
			var y = startY - (h * 2);
			var sy = (startY - h) - (h * 2);
			var s = span;
			jQuery(this).css({
				'background-position' : 'left ' + y + 'px'
			});
			jQuery(s).css({
				'background-position' : 'right ' + sy + 'px'
			});
		}, function() {
			var y = startY;
			var sy = startY - h;
			var s = span;
			jQuery(this).css({
				'background-position' : 'left ' + y + 'px'
			});
			jQuery(s).css({
				'background-position' : 'right ' + sy + 'px'
			});
		});
	},
	//
	getAbsolutePositionValues : function(rect, cw, ch) {
		// if x and y values have not been set default to zero
		var infoX = (String(rect.x).length > 0) ? String(rect.x).split(":") : [0];
		var infoY = (String(rect.y).length > 0) ? String(rect.y).split(":") : [0];
		// establish the values the panel will be spaced from a particular border
		var alignSpaceX = (infoX[infoX.length - 1] == "center") ? 0 : Number(infoX[infoX.length - 1]);
		var alignSpaceY = (infoY[infoY.length - 1] == "center") ? 0 : Number(infoY[infoY.length - 1]);
		// establish the border we will position the panel relative to... if no border specified use top,left
		var alignX = (infoX.length == 1 && infoX[0] != "center") ? "left" : infoX[0];
		var alignY = (infoY.length == 1 && infoY[0] != "center") ? "top" : infoY[0];
		//
		// convert %s to pixel values
		var width = (String(rect.w).indexOf("%") == -1) ? rect.w : (Number(String(rect.w).substring(0, String(rect.w).indexOf("%"))) * 0.01) * cw;
		var height = (String(rect.h).indexOf("%") == -1) ? rect.h : (Number(String(rect.h).substring(0, String(rect.h).indexOf("%"))) * 0.01) * ch;
		//
		var posX;
		var posY;
		//
		switch (alignX) {
			case "left":
				posX = alignSpaceX;
				break;
			case "right":
				posX = cw - width - alignSpaceX;
				break;
			case "center":
				posX = Math.round((cw / 2) - (width / 2));
				break;
		}
		//
		switch (alignY) {
			case "top":
				posY = alignSpaceY;
				break;
			case "bottom":
				posY = ch - height - alignSpaceY;
				break;
			case "center":
				posY = Math.round((ch / 2) - (height / 2));
				break;
		}
		//
		return {
			x : posX,
			y : posY,
			w : width,
			h : height
		};
	},
	//
	changePage : function(ref) {
		var pageURL = window.location.toString();
		var queryString = (pageURL.indexOf("?") != -1) ? pageURL.substring(pageURL.indexOf("?")) : "";
		//
		window.location = ref + queryString;
	},
	pathmodifier: function(){
		var pageURL = window.location.toString();
		var pathmodifier = (pageURL.indexOf("option") != -1) ? '../' : "";
		return pathmodifier;
	}
}
