$(document).ready(function() {
						   
	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);
    }
	
}
);

function SignUp(){
    var firstname = $('#su_firstname').attr('value');
	var lastname = $('#su_lastname').attr('value');
	var email = $('#su_email').attr('value');
	var firstOptin;

    var firstOptin_checked = $("#su_updates:checked").length;
    if (firstOptin_checked == 1) {
        firstOptin = true;
    }
    else {
        firstOptin = false;
    }
   
    var secondOptin;
    var secondOptin_checked = $("#su_consumer:checked").length;
    if (secondOptin_checked == 1) {
        secondOptin = true;
    }
    else {
        secondOptin = false;
    }


    $.post("../ajax/CommunitySignUp.ashx", { firstname: firstname, lastname: lastname, email: email, firstOptin: firstOptin, secondOptin: secondOptin },
	function(data) {
	    //alert(data.match('saved') == 'saved');
	    var close = false;
	    $("#errorMsgFirstname").hide();
	    $("#errorMsgLastname").hide();
	    $("#errorMsgEmail").hide();
	    $("#errorMsgOptin").hide();
	    $("#sent").hide();
	    $("#su_firstname_error").hide();
	    $("#su_lastname_error").hide();
	    $("#su_email_error").hide();
	    var errorMsg = "";
	    if (data.match('saved') == 'saved') {
	        document.getElementById("sent").style.display = 'block';
	        //close window
	        $("#overlay").animate({ opacity: 1 }, 1500, function() { $("#overlay").remove(); });
	    } else {

	        if (data.match('firstNameError') == 'firstNameError') {
	            $("#errorMsgFirstname").show();
	            $("#su_firstname_error").show();
	        }
	        if (data.match('lastNameError') == 'lastNameError') {
	            $("#errorMsgLastname").show();
	            $("#su_lastname_error").show();
	        }
	        if (data.match('emailError') == 'emailError') {

	            $("#errorMsgEmail").show();
	            $("#su_email_error").show();
	        }
	        if (data.match('noOptins') == 'noOptins') {
	            $("#errorMsgOptin").show();

	        }
	    }
	});
	
} 
	
   
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'}});	
	Cufon.replace('.storesearch-table .header ', {color:'white',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:".comp-form h2", sFlashSrc:"/flash/SuperGrotesk.swf", sColor:"#ffdc88", sLinkColor:"#ffdc88", sBgColor:"#a32f38", sHoverColor:"#ffdc88", nPaddingTop:0, nPaddingBottom:0, sFlashVars:"textalign=left&offsetTop=0",sWmode:"transparent",sCase:"upper"}));
		
		sIFR.replaceElement(named({sSelector:".comp-footer h2", sFlashSrc:"/flash/SuperGrotesk.swf", sColor:"#a33038", sLinkColor:"#f57c21", sBgColor:"#a32f38", sHoverColor:"#f57c21", 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:".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("/" + thisOverlay + "/Default.aspx #ovl", null,  function() {
    	closeOverlay();
	});	
	//alert("/" + thisOverlay + "/Default.aspx #ovl");

}

function closeOverlay() {
    $("#ovl #close-overlay").unbind("click");


    $("#ovl #close-overlay").click(
		function() {

		    /*$("#overlay").fadeOut(400, function() {*/
		    $("#overlay").remove();
		    /*});*/

		    return false;

		}
	);
    $("#ovl .close a").unbind("click");

	
	$("#ovl .close a").click(
		function() {

			/*$("#overlay").fadeOut(400, function() {*/
				$("#overlay").remove();				
			/*});*/

			return false;

		}
	);
	
	$("#ovl .cancel 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");
		
	});
	
}
