$(document).ready(function() {
						  
						  
					
	// HACK TO MAKE PRIVACY POLICY AN OVERLAY LINK WITHOUT NEEDING TO ADJUST STYLING OF DYNAMICALLY GENERATED LINK	  
	$(".gsom_widget a").addClass("ovl privacy");
	
	
	
	
	
	
	initCommentValidation();			   
	overlayButtonInit();
	menuInit();
	falshText();
	cufonText();
	
	
						   
    //DEFINITION LISTS (FAQs & FEATURES)
    $(".toggle dt").hover(
		function() {
		    $(this).addClass("hover");
		    $(this).css("cursor", "pointer");
		}, function() {
		    $(this).removeClass("hover");
		}
	);

    $(".toggle dt").toggle(
		function() {
		    $(this).next("dd").slideDown();
		    $(this).addClass("open");
		}, function() {
		    $(this).next("dd").slideUp();
		    $(this).removeClass("open");
		}
	);
	
	$(".blog-content ul li.current").show();
	
	if ($(".blog-content ul li").length > 1) {
		
		newsTickerCurrent = 1;
		newsTickerLength = $(".blog-content ul li").length;		
        $(".blog-content ul").everyTime(8000, newsTicker);
    }
	
}
);
   
   
String.prototype.prepare = function() {
	return this.replace(/^\s+|\s+$/g,""); 
}

String.prototype.trim = function() {
	return this.replace(/^\s+|\s+$/g,"");
}
   
function initCommentValidation() {
	if($('#commentform').length>0) {
		$('#commentform').submit(function() {
			var comment = $('#comment').val().prepare();
			var author = $('#author').val().prepare();
			var email = $('#email').val().prepare();
			var url = $('#url').val().prepare();
			var valid = true;
			var message = "";
			if(comment=='') message="Please enter a comment";
			if((!urlValid(url))&&(url!='')) message="Please enter a valid URL for your website";
			if(email=='') message="Please enter your email address";
			if(!emailAddressValid(email)&&(email!='')) message="Please enter a valid email address";
			if(author=='') message="Please enter your name";
			if(message.length>0) {
				alert(message);
				return false;
			} else {
				return true;
			}
		});
	}
}
   
   
function encodeMyHtml() {
     encodedHtml = escape(encodeHtml.htmlToEncode.value);
     encodedHtml = encodedHtml.replace(/\//g,"%2F");
     encodedHtml = encodedHtml.replace(/\?/g,"%3F");
     encodedHtml = encodedHtml.replace(/=/g,"%3D");
     encodedHtml = encodedHtml.replace(/&/g,"%26");
     encodedHtml = encodedHtml.replace(/@/g,"%40");
     encodeHtml.htmlEncoded.value = encodedHtml;
} 

function cufonText(){
	Cufon.replace('#global-nav li ', {color:'white',display:'block',hover:{color:'#FC7F25'}});
	Cufon.replace('#global-nav li a.current ', {color:'#fc7f25',display:'block',hover:{color:'#FC7F25'}});	
}

function menuInit(){
	$('#global-footer > ul > li').hover(function() {
		if (obj) {
			obj.find('#footer-menu').hide();
			obj = null;
		} //if
		
		$(this).find('#footer-menu').show();
	}, function() {
		obj = $(this);
		setTimeout(
			"checkHover()",
			400);
	});
}

var obj = null;

function checkHover() {
	if (obj) {
		obj.find('#footer-menu').hide();	
	} //if
} //checkHover



function falshText() {
	if(typeof sIFR == "function"){
	
	// This is the preferred "named argument" syntax
	
		sIFR.replaceElement(named({sSelector:"H1", sFlashSrc:"/flash/Trebuchet.swf", sColor:"#942414", sLinkColor:"#942414", sBgColor:"#FFFEEF", sHoverColor:"#942414", nPaddingTop:0, nPaddingBottom:0, sFlashVars:"textalign=left&offsetTop=0",sWmode:"transparent"}));
		
		sIFR.replaceElement(named({sSelector:".h1", sFlashSrc:"/flash/Trebuchet.swf", sColor:"#ee7100", sLinkColor:"#942414", sBgColor:"#FFFEEF", sHoverColor:"#942414", nPaddingTop:0, nPaddingBottom:0, sFlashVars:"textalign=left&offsetTop=0",sWmode:"transparent"}));
		
		sIFR.replaceElement(named({sSelector:".f2", sFlashSrc:"/flash/Trebuchet.swf", sColor:"#ee7100", sLinkColor:"#942414", sBgColor:"#FFFEEF", sHoverColor:"#942414", nPaddingTop:0, nPaddingBottom:0, sFlashVars:"textalign=left&offsetTop=0",sWmode:"transparent",sWmode:"transparent"}));
		
		sIFR.replaceElement(named({sSelector:".ps-header h2", sFlashSrc:"/flash/SuperGrotesk.swf", sColor:"#FFFFFF", sLinkColor:"#FFFFFF", sBgColor:"#FFFEEF", sHoverColor:"#FFFFFF", nPaddingTop:0, nPaddingBottom:0, sFlashVars:"textalign=left&offsetTop=0",sWmode:"transparent",sCase:"upper"}));
		
		sIFR.replaceElement(named({sSelector:".did-you-know .f3", sFlashSrc:"/flash/SuperGrotesk.swf", sColor:"#FFFFFF", sLinkColor:"#FFFFFF", sBgColor:"#FFFEEF", sHoverColor:"#FFFFFF", nPaddingTop:0, nPaddingBottom:0, sFlashVars:"textalign=center&offsetTop=0",sWmode:"transparent",sCase:"upper" }));
		
		sIFR.replaceElement(named({sSelector:".f3green", sFlashSrc:"/flash/SuperGrotesk.swf", sColor:"#009649", sLinkColor:"#009649", sBgColor:"#FFFEEF", sHoverColor:"#009649", nPaddingTop:0, nPaddingBottom:0, sFlashVars:"textalign=left&offsetTop=0",sWmode:"transparent",sCase:"upper" }));
		
		sIFR.replaceElement(named({sSelector:".f3red", sFlashSrc:"/flash/SuperGrotesk.swf", sColor:"#a33038", sLinkColor:"#a33038", sBgColor:"#FFFEEF", sHoverColor:"#a33038", nPaddingTop:0, nPaddingBottom:0, sFlashVars:"textalign=left&offsetTop=0",sWmode:"transparent",sCase:"upper" }));
		
		sIFR.replaceElement(named({sSelector:".f3blue", sFlashSrc:"/flash/SuperGrotesk.swf", sColor:"#00808c", sLinkColor:"#00808c", sBgColor:"#FFFEEF", sHoverColor:"#00808c", nPaddingTop:0, nPaddingBottom:0, sFlashVars:"textalign=left&offsetTop=0",sWmode:"transparent",sCase:"upper" }));
		
		sIFR.replaceElement(named({sSelector:".f3purple", sFlashSrc:"/flash/SuperGrotesk.swf", sColor:"#673A7B", sLinkColor:"#673A7B", sBgColor:"#FFFEEF", sHoverColor:"#673A7B", nPaddingTop:0, nPaddingBottom:0, sFlashVars:"textalign=left&offsetTop=0",sWmode:"transparent",sCase:"upper" }));
		
		sIFR.replaceElement(named({sSelector:".f3white", sFlashSrc:"/flash/SuperGrotesk.swf", sColor:"#FFFFFF", sLinkColor:"#FFFFFF", sBgColor:"#FFFEEF", sHoverColor:"#FFFFFF", nPaddingTop:0, nPaddingBottom:0, sFlashVars:"textalign=left&offsetTop=0",sWmode:"transparent",sCase:"upper" }));
		
		sIFR.replaceElement(named({sSelector:".widgettitle", sFlashSrc:"/flash/SuperGrotesk.swf", sColor:"#ee7100", sLinkColor:"#00808c", sBgColor:"#FFFEEF", sHoverColor:"#00808c", nPaddingTop:0, nPaddingBottom:0,sFlashVars:"textalign=left&offsetTop=0",sWmode:"transparent",sCase:"upper" }));		
		
		
		//sIFR.replaceElement(named({sSelector:".oval-122w h3", sFlashSrc:"/flash/SuperGrotesk.swf", sColor:"#ffffff", sLinkColor:"#ffffff", sBgColor:"#FFFEEF", sHoverColor:"#ffffff", nPaddingTop:0, nPaddingBottom:0, sFlashVars:"textalign=center&offsetTop=0&resize=false",sWmode:"transparent",sCase:"upper" }));
		
		sIFR.replaceElement(named({sSelector:"h3", sFlashSrc:"/flash/SuperGrotesk.swf", sColor:"#ee7100", sLinkColor:"#942414", sBgColor:"#FFFEEF", sHoverColor:"#942414", nPaddingTop:0, nPaddingBottom:0, sFlashVars:"textalign=left&offsetTop=0",sWmode:"transparent",sCase:"upper" }));
		
		//sIFR.replaceElement(named({sSelector:"#global-nav li", sFlashSrc:"/flash/SuperGrotesk.swf", sColor:"#ffffff", sLinkColor:"#ffffff", sBgColor:"#FFFEEF", sHoverColor:"#6a6a6a", nPaddingTop:0, nPaddingBottom:0, sFlashVars:"textalign=left&offsetTop=0",sWmode:"transparent",sCase:"upper" }));

		
	};
}

//BIND OVERLAY LAUNCH FUNCTIONS
function overlayButtonInit() {
	
	$("a.ovl").unbind("click");
	
	$("a.ovl").each(
		function() {

			var thisOverlay = $(this).attr("class");
			thisOverlay = thisOverlay.substr(4);

			$(this).click(
				function() {
				

					launchOverlay(thisOverlay);

					return false;

				}
			);

		}
	);

}

function launchOverlay(thisOverlay) {


	$("#overlay").remove();
	$("body").prepend("<div id=\"overlay\"></div>");

    //$("#overlay").load("/Privacy/default.aspx #ovl", null, function() {
    
    
  
		$("#overlay").load("/overlays/index.php?overlay="+thisOverlay+" #ovl", null,  function() {
	    	closeOverlay();
		});	

	
	
	
	
	
	//alert("/" + thisOverlay + "/Default.aspx #ovl");

}

function closeOverlay() {
	$("#ovl .close a").unbind("click");
	$("#ovl .close a").click(
		function() {

			/*$("#overlay").fadeOut(400, function() {*/
				$("#overlay").remove();				
			/*});*/

			return false;

		}
	);

}

//BOOKMARKS

function overlay_share_init() {
	$("a.delicious").attr("href", "http://del.icio.us/post?url=" + encodeURIComponent(document.URL) + "&title=" + encodeURIComponent(document.title));
	$("a.digg").attr("href", "http://digg.com/submit?url=" + encodeURIComponent(document.URL) + "&title=" + encodeURIComponent(document.title));
	$("a.facebook").attr("href", "http://www.facebook.com/sharer.php?u=" + encodeURIComponent(document.URL));
	$("a.stumbleupon").attr("href", "http://www.stumbleupon.com/submit?url=" + encodeURIComponent(document.URL) + "&title=" + encodeURIComponent(document.title));
	$("a.twitter").attr("href", "http://twitter.com/home?status=Currently reading " + encodeURIComponent(document.URL));
}

//ACTION TRACKING
function trackAction(action, page, title, media, type) {
	/*
	if(page) {
		page = page.substr(page.indexOf(""));
	}
	*/
	var track_path = "/" + action + "/" + page + "/" + title;

	if(media) track_path += "/" + media;
	if(type) track_path += "/" + type;

	track_path = unescape(track_path);
	pageTracker._trackPageview(track_path);

}

//HOMEPAGE NEWS TICKER
function newsTicker() {
	
	$(".blog-content ul li.current").removeClass("current").fadeOut(500, function() {
		
		newsTickerCurrent++;
		
		if(newsTickerCurrent > newsTickerLength) {
			newsTickerCurrent = 1;
		}
		
		$(".blog-content ul li#entry-" + newsTickerCurrent).fadeIn(500).addClass("current");
		
	});
	
}


function urlValid(url) {
	var urlFilter = /(ftp|http|https):\/\/(\w+:{0,1}\w*@)?(\S+)(:[0-9]+)?(\/|\/([\w#!:.?+=&%@!\-\/]))?/
	return(urlFilter.test(url));
}

function emailAddressValid(emailAddress) {
	var emailFilter = /^([\w-]+(?:\.[\w-]+)*)@((?:[\w-]+\.)*\w[\w-]{0,66})\.([a-z]{2,6}(?:\.[a-z]{2})?)$/i;
	return(emailFilter.test(emailAddress));
}

