$(function() {
	// generic document.ready handler
	// 
	// adds a handler to the 'change' link, opening the popup	
	$("#changeLink").click (function () {showWelcome (); return false;});
	
	
	if ($("#columnContainer").length > 0) {
		$("#columnContainer").equalHeights(true);
		
	}
	
	$(".ques").click ( function () { 
		var alt = $(this).attr('alt');
		if (alt == "") {
			showHelp (null);
		}
		else {
			showHelp (alt);
		}
		
	});
	var undefined;
	if(undefined===window.notHome){
		window.notHome = true;
	}

	$("#logolink img").hover(
		 function()
		 {
		 	if (notHome) {
			  this.src = this.src.replace("_off","_on");
		 	}
		 },
		 function()
		 {
		  this.src = this.src.replace("_on","_off");
		 }
		);
	
	
});

function openPopup (item, width, height) {

 
 window.open ($(item).attr('href'), "contentwindow","location=0,width=" + width + ",height=" + height); 
	
}

$.fn.equalHeights = function(px) {
	$(this).each(function(){
	
		var currentTallest = 0;
		$(this).children().each(function(i){
			if ($(this).height() > currentTallest) { currentTallest = $(this).height();  }
		});
		if (!px || !Number.prototype.pxToEm) currentTallest = currentTallest.pxToEm(); //use ems unless px is specified
		// for ie6, set height since min-height isn't supported
		if ($.browser.msie && $.browser.version == 6.0) { $(this).children().css({'height': currentTallest}); }
		$(this).children().css({'min-height': currentTallest}); 
	});
	return this;
};


function showOverlay (href, width, height) {
	if ($("#dialog").length > 0) {
		$('#dialog').jqm({modal: true,overlay:40, target: '#jqmContent', toTop: true});
		$('#dialog').width (width);
		$('#dialog').height (height);
		var halfWidth = Math.round (width / 2);
		var heightVal = height;
		if (height < 400) {
			
			heightVal = 400;
		}
		var halfHeight = Math.round (heightVal / 2);
		$('#dialog').css ("margin-left",  "-" + halfWidth + "px");
		//$('#dialog').css ("margin-top",  "-" + halfHeight + "px");
		$('#dialog').css ("padding",  "0");
		var $modalContent = $("#jqmContent");
		$modalContent.html('').attr('src', href);
	//	alert ($('#dialog').jqmShow());	
		$('#dialog').jqmShow();
	}
	else
	{
		alert ("Dialog was not found...");
	}
}
function viewedContent (contentID) {
	// alert ("Content ID is " + contentID);
	 	 var cookieName = "viewedcontent" + contentID;
	 	 var date = new Date();
	 	 // set cookie for 2 hours
         date.setTime(date.getTime() + (2 * 60 * 60 * 1000));
         $.cookie(cookieName, '1', { path: '/', expires: date });
}

function showHelp (action) {
	var urlToOpen = helpURL;
	if (action != null) {
		urlToOpen += "#" + action;
	}
	showOverlay (urlToOpen, 708,420);
}

function showWelcome () {
	//alert ("SHOWING WELCOME");
	//console.log ("Showing welcome...");
	showOverlay ( welcomeURL, 708,272)	;
	return false;
}
function closeOverlay () {
	
	var $modalContent = $("#jqmContent");
	setTimeout (function () {

		$modalContent.html('').attr('src', "about:blank");
	}, 500
	)
	//console.log ("Hiding overlay...");
	$('#dialog').jqmHide();
}

Number.prototype.pxToEm = String.prototype.pxToEm = function(settings){
	//set defaults
	settings = jQuery.extend({
		scope: 'body',
		reverse: false
	}, settings);
	
	var pxVal = (this == '') ? 0 : parseFloat(this);
	var scopeVal;
	var getWindowWidth = function(){
		var de = document.documentElement;
		return self.innerWidth || (de && de.clientWidth) || document.body.clientWidth;
	};	
	
	/* When a percentage-based font-size is set on the body, IE returns that percent of the window width as the font-size. 
		For example, if the body font-size is 62.5% and the window width is 1000px, IE will return 625px as the font-size. 	
		When this happens, we calculate the correct body font-size (%) and multiply it by 16 (the standard browser font size) 
		to get an accurate em value. */
				
	if (settings.scope == 'body' && $.browser.msie && (parseFloat($('body').css('font-size')) / getWindowWidth()).toFixed(1) > 0.0) {
		var calcFontSize = function(){		
			return (parseFloat($('body').css('font-size'))/getWindowWidth()).toFixed(3) * 16;
		};
		scopeVal = calcFontSize();
	}
	else { scopeVal = parseFloat(jQuery(settings.scope).css("font-size")); };
			
	var result = (settings.reverse == true) ? (pxVal * scopeVal).toFixed(2) + 'px' : (pxVal / scopeVal).toFixed(2) + 'em';
	return result;
};


/**
*
*  Base64 encode / decode
*  http://www.webtoolkit.info/
*
**/
 
var Base64 = {
 
	// private property
	_keyStr : "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/=",
 
	// public method for encoding
	encode : function (input) {
		var output = "";
		var chr1, chr2, chr3, enc1, enc2, enc3, enc4;
		var i = 0;
 
		input = Base64._utf8_encode(input);
 
		while (i < input.length) {
 
			chr1 = input.charCodeAt(i++);
			chr2 = input.charCodeAt(i++);
			chr3 = input.charCodeAt(i++);
 
			enc1 = chr1 >> 2;
			enc2 = ((chr1 & 3) << 4) | (chr2 >> 4);
			enc3 = ((chr2 & 15) << 2) | (chr3 >> 6);
			enc4 = chr3 & 63;
 
			if (isNaN(chr2)) {
				enc3 = enc4 = 64;
			} else if (isNaN(chr3)) {
				enc4 = 64;
			}
 
			output = output +
			this._keyStr.charAt(enc1) + this._keyStr.charAt(enc2) +
			this._keyStr.charAt(enc3) + this._keyStr.charAt(enc4);
 
		}
 
		return output;
	},
 
	// public method for decoding
	decode : function (input) {
		var output = "";
		var chr1, chr2, chr3;
		var enc1, enc2, enc3, enc4;
		var i = 0;
 
		input = input.replace(/[^A-Za-z0-9\+\/\=]/g, "");
 
		while (i < input.length) {
 
			enc1 = this._keyStr.indexOf(input.charAt(i++));
			enc2 = this._keyStr.indexOf(input.charAt(i++));
			enc3 = this._keyStr.indexOf(input.charAt(i++));
			enc4 = this._keyStr.indexOf(input.charAt(i++));
 
			chr1 = (enc1 << 2) | (enc2 >> 4);
			chr2 = ((enc2 & 15) << 4) | (enc3 >> 2);
			chr3 = ((enc3 & 3) << 6) | enc4;
 
			output = output + String.fromCharCode(chr1);
 
			if (enc3 != 64) {
				output = output + String.fromCharCode(chr2);
			}
			if (enc4 != 64) {
				output = output + String.fromCharCode(chr3);
			}
 
		}
 
		output = Base64._utf8_decode(output);
 
		return output;
 
	},
 
	// private method for UTF-8 encoding
	_utf8_encode : function (string) {
		string = string.replace(/\r\n/g,"\n");
		var utftext = "";
 
		for (var n = 0; n < string.length; n++) {
 
			var c = string.charCodeAt(n);
 
			if (c < 128) {
				utftext += String.fromCharCode(c);
			}
			else if((c > 127) && (c < 2048)) {
				utftext += String.fromCharCode((c >> 6) | 192);
				utftext += String.fromCharCode((c & 63) | 128);
			}
			else {
				utftext += String.fromCharCode((c >> 12) | 224);
				utftext += String.fromCharCode(((c >> 6) & 63) | 128);
				utftext += String.fromCharCode((c & 63) | 128);
			}
 
		}
 
		return utftext;
	},
 
	// private method for UTF-8 decoding
	_utf8_decode : function (utftext) {
		var string = "";
		var i = 0;
		var c = c1 = c2 = 0;
 
		while ( i < utftext.length ) {
 
			c = utftext.charCodeAt(i);
 
			if (c < 128) {
				string += String.fromCharCode(c);
				i++;
			}
			else if((c > 191) && (c < 224)) {
				c2 = utftext.charCodeAt(i+1);
				string += String.fromCharCode(((c & 31) << 6) | (c2 & 63));
				i += 2;
			}
			else {
				c2 = utftext.charCodeAt(i+1);
				c3 = utftext.charCodeAt(i+2);
				string += String.fromCharCode(((c & 15) << 12) | ((c2 & 63) << 6) | (c3 & 63));
				i += 3;
			}
 
		}
 
		return string;
	}
 
}