$(document).ready(function () {

    overlayURLCheck();
    overlayButtonInit();
    menuInit();
    falshText();
    cufonText();
    whyBuyInitDropDownInit();

    //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");
		}
	);

    $(".closesearch").click(function () {
        slideStoreSearchUp();
    });
    $(".restartsearch").click(function () {
        slideStoreSearchDown();
    });

    $(".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);
    }

    $(".fb-wrapper a").mouseenter(function () {
        $(".fb-wrapper .fb-cta").fadeIn('fast');
    });
    $(".fb-wrapper a").mouseleave(function () {
        $(".fb-wrapper .fb-cta").fadeOut('fast');
    });

}
);
function SendTofriend() {
    var Referrer = document.URL;
    var yourname = $('#su_yourname').attr('value');
    var email = $('#su_email').attr('value');
    var friendemail = $('#su_friend_email').attr('value');
    var message = $('#su_message').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/ReferAFriend.ashx", { FriendEmail: friendemail, YourEmail: email, YourName: yourname, Message: message, Referrer: Referrer, firstOptin: firstOptin, secondOptin: secondOptin },
	function (data) {
	    var close = false;
	    $("#errorMsgYourName").hide();
	    $("#errorMsgEmail").hide();
	    $("#errorMsgFiendEmail").hide();
	    $("#su_yourname_error").hide();
	    $("#su_email_error").hide();
	    $("#su_friendemail_error").hide();
	    var errorMsg = "";
	    if (data.match('saved') == 'saved') {
	        $("#sent").css("display", "block");
	        //close window
	        $("#overlay").animate({ opacity: 1 }, 1500, function () { $("#overlay").remove(); });
	    } else {
	        if (data.match('YourNameError') == 'YourNameError') {
	            $("#errorMsgYourName").show();
	            $("#su_yourname_error").show();
	        }
	        if (data.match('YourEmailError') == 'YourEmailError') {
	            $("#errorMsgEmail").show();
	            $("#su_email_error").show();
	        }
	        if (data.match('FriendEmailError') == 'FriendEmailError') {
	            $("#errorMsgFiendEmail").show();
	            $("#su_friendemail_error").show();
	        }
	        if (data.match('noOptins') == 'noOptins')
	            $("#errorMsgOptin").show();
	    }
	});
}

function whyBuyInitDropDownInit() {
    var $selector = $("select.why-buy-selector");
    if ($selector.length > 0) {
        $(".why-buy-other-field").hide();
        var otherValue = $("input.why-buy-other-value").val();
        var value = $('select.why-buy-selector').val();
        if (value == otherValue)
            $("input.why-buy-other-field").show();
        $selector.change(function () {
            whyBuyDropDownChanged();
        });
    }
}

function whyBuyDropDownChanged() {
    var $selector = $("select.why-buy-selector");
    var $otherValue = $('input.why-buy-other-value');
    var $otherField = $('input.why-buy-other-field');
    var otherValue = $otherValue.val();
    var value = $selector.val();
    if (value == otherValue)
        $otherField.slideDown();
    else {
        $otherField.val("");
        $otherField.slideUp();
    }
}


function whyBuyGetValue() {
    var otherValue = $("input.why-buy-other-value").val();
    var value = $('select.why-buy-selector').val();
    if (otherValue == value)
        return "";
    else
        return value;
}


function SignUp(){
    var firstname = $('#su_firstname').val();
    var lastname = $('#su_lastname').val();
	var email = $('#su_email').val();
	
    var whyBuy = whyBuyGetValue();
    if (whyBuy.length == 0)
        whyBuy = $('#su_whyBuyOther').val();

	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;

    $(".error-descrip").hide();
    $("#sent").hide();
    $("span.error").hide();


    $.post("../ajax/CommunitySignUp.ashx", { firstname: firstname, lastname: lastname, email: email, firstOptin: firstOptin, secondOptin: secondOptin, whyBuy: whyBuy },
	function (data) {
	    var close = false;

	    var errorMsg = "";
	    if (data.match('saved') == 'saved') {
	        $("#sent").css("display", "block");
	        //close window
	        $("#overlay").animate({ opacity: 1 }, 1500, function () { $("#overlay").remove(); });
	    } else {
	        if (data.match('whyBuyError')) {
	            $("#errorMsgFirstname").show();
	            $("#su_firstname_error").show();
	        }
	        if (data.match('firstNameError')) {
	            $("#errorMsgFirstname").show();
	            $("#su_firstname_error").show();
	        }
	        if (data.match('lastNameError')) {
	            $("#errorMsgLastname").show();
	            $("#su_lastname_error").show();
	        }
	        if (data.match('emailError')) {
	            $("#errorMsgEmail").show();
	            $("#su_email_error").show();
	        }
	        if (data.match('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;
		} 
		$(this).find('#footer-menu').show();
	}, function() {
		obj = $(this);
		setTimeout(
			"checkHover()",
			400);
	});
}

var obj = null;

function checkHover() {
	if (obj)
		obj.find('#footer-menu').hide();	
} //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:"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" }));
	};
}

function overlayURLCheck() {
	var checkOverlayURL = document.URL.substr(document.URL.lastIndexOf("#"));
    if(checkOverlayURL.substr(checkOverlayURL.indexOf("#"), 1) == "#") {
        checkOverlayURL = checkOverlayURL.substr(1);
        if(checkOverlayURL.substr(checkOverlayURL.lastIndexOf("?"), 1) == "?")
            checkOverlayURL = checkOverlayURL.substr(0, checkOverlayURL.lastIndexOf("?"));
        if (checkOverlayURL == "privacy" || checkOverlayURL == "accessibility" || checkOverlayURL == "Share" || checkOverlayURL == "Community-Signup" || checkOverlayURL == "EmailSignup")
            launchOverlay(checkOverlayURL);
    }
}

//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("/" + thisOverlay + "/Default.aspx #ovl", null,  function() {
    	closeOverlay();
	});	
}

function closeOverlay() {
    $("#ovl #close-overlay").unbind("click");
    $("#ovl #close-overlay").click(
		function() {
		    $("#overlay").remove();
		    return false;
		}
	);
    $("#ovl .close a").unbind("click");
	$("#ovl .close a").click(
		function() {
			$("#overlay").remove();				
			return false;
		}
	);
	$("#ovl .cancel a").click(
		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) {
	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");
	});
}

