function initDebug(){
	$("body").append('<div id="debug" style=""><h3>Debugging Panel</h3></div>');
	$('div#debug').append('<a href="#" onclick="resetCookiesUsed()">Reset Cookies Used</a> - If you reset the cookies used for the dyanamic functionalities, please make sure to go back to the homepage of the parse you want to test again so that the appropriate cookies can be set again.<br />');	
	$('div#debug').append('<a href="/product/p90x.do?tnt=P90X_MS_B1">go to Parse B home</a><br />');
	$('div#debug').append('<a href="/product/p90x.do?tnt=P90X_MS_C1">go to Parse C home</a><br />');
};

function resetCookiesUsed(){
	setCookie('P90XcsPhoneNumber','',-1);
	setCookie('P90XtestHomeURL','',-1);
	setCookie('P90XtestATC','',-1);
	setCookie('P90XdelayPopID','',-1);
	setCookie('P90XdelayPopBool','',-1);
	/*alert("Cookies used have been reset.");*/
	return false;
};

function setCookie(name,value,days) {
	if (days) {
		var date = new Date();
		date.setTime(date.getTime()+(days*24*60*60*1000));
		var expires = "; expires="+date.toGMTString();
	}
	else var expires = "";
	document.cookie = name+"="+value+expires+"; path=/";
}

function getCookie(name) {
	var nameEQ = name + "=";
	var ca = document.cookie.split(';');
	for(var i=0;i < ca.length;i++) {
		var c = ca[i];
		while (c.charAt(0)==' ') c = c.substring(1,c.length);
		if (c.indexOf(nameEQ) == 0) return c.substring(nameEQ.length,c.length);
	}
	return null;
};

function initDelayPop(){
	var theUrl = '/product/code/P90X-TB.do?actionkey=ADDTOBASKET';
	if(getCookie('P90XtestATC')!='' && getCookie('P90XtestATC')!=null){
		theUrl = "/product/code/"+getCookie('P90XtestATC')+".do?actionkey=ADDTOBASKET";
	}
	
	var delayPopText = '<div id="P90XDelayPop" style="width:380px; height:250px; display:none; background-image:url(/images/beachbody/en_us/products/programs/p90x/sellingsafety/delay_pop_2.jpg);" class="delayPop"><a href="'+theUrl+'" class="atc" onClick="logLink(\'landing:p90x:web:fakepop:1:buy\');">>Order P90X Now</a><a href="#" class="closeBtn">Close</a></div>';
	$("body").append(delayPopText);
};


function delayPop(id, millisecs){
	// for this function to work, the layer should be made already and have the height and width set via css or something. also add the class "delayPop".
	// the close functionality will be taken care of if you add the class "closeBtn" on the close button (<a>) on the popup.
	
	setTimeout("delayPopShow('#"+id+"')", millisecs);
}

function delayPopPrep(){
	$(".delayPop").each(function(){
			$(this).appendTo("body");
			$(this).css({
				"display" : "none",
				"position" : "absolute",
				"z-index" : 10002
			});
	});
	$(".delayPop a.closeBtn").click(function(){
			$(this).parents("div.delayPop").eq(0).fadeOut(200);		
			setCookie('P90XdelayPopBool','closed',1);
			return false;
	});
};

function delayPopShow(id){
	popScrollTop = $(document).scrollTop() +  ($(window).height()/2) - ($(id).height()/2);
	popScrollLeft = $(document).scrollLeft() + ($(window).width()/2) - ($(id).width()/2);
	
	$(id).css({
		"top" : popScrollTop + "px",
		"left" : popScrollLeft + "px"
	}).fadeIn(200, function(){delayPopListener(this);});
};

function delayPopListener(target){
	//$(target).fadeOut(10000);
	setCookie('P90XdelayPopBool','opened',1);
};

function getQueryVariable(variable) {
	var query = window.location.search.substring(1);
	var vars = query.split("&");
	for (var i=0;i<vars.length;i++) {
		var pair = vars[i].split("=");
		if (pair[0] == variable) {return pair[1];}
	}
	return null;
}

$(document).ready(function(){
	/**
	 * risk free trial */
	if (getCookie('P90XRFT') == "true") {
		var codeBlock = "<div class=\"new-cta\"><p class=\"col-left\">Try it <br /><strong>risk free</strong><br /> for just</p><p class=\"price\">$14.95</p><p class=\"col-right\">with <br /><strong>free <br /> shipping</strong></p><div style=\"clear: both;\"></div><a href=\"/product/code/P90XDVD2158.do?actionkey=ADDTOBASKET\" title=\"Order P90X Now\" target=\"_parent\"><img src=\"/images/beachbody/en_us/products/programs/p90x/sellingsafety/cta-buy-btn.jpg\" alt=\"Order P90X Now\" /></a><p class=\"dp-info\">Just pay $14.95 plus applicable taxes when you order today. <br />After your 30-day trial, you will automatically be billed 3 monthly payments of $39.95 or choose one payment at checkout.</p></div>";
		$(".cta").each( function() {
			$(this).replaceWith(codeBlock);
		});
		
		$(".en-espanol").hide();
		
		if (getCookie('RDIO') != "true") {
			$("a[href=/product/p90x.do]").attr("href", "/product/p90x-riskfree-trial.do");
		}

		$(".mbg").text("30-Day Money Back Guarantee");
		$(".mbg").attr("href", "/text/products/programs/p90x/sellingsafety/rf-mbg.html");
	}
		
	if(getCookie('P90XcsPhoneNumber')!='' && getCookie('P90XcsPhoneNumber')!=null){
		$("span.phone").text(getCookie('P90XcsPhoneNumber'));
	}
	if(getCookie('P90XtestHomeURL')!='' && getCookie('P90XtestHomeURL')!=null){
		$("h1 a").attr("href", getCookie('P90XtestHomeURL'));
	}
	if(getCookie('P90XtestATC')!='' && getCookie('P90XtestATC')!=null){
		$("a.atc").attr("href", "/product/code/"+getCookie('P90XtestATC')+".do?actionkey=ADDTOBASKET");
	}
	if(getCookie('P90XdelayPopID')!='' && getCookie('P90XdelayPopID')!=null && (getCookie('P90XdelayPopBool')=='' || getCookie('P90XdelayPopBool')==null) ){
		initDelayPop();
		delayPopPrep();
		delayPop('P90XDelayPop', 90000);
	}else if(getCookie('P90XdelayPopID')!='' && getCookie('P90XdelayPopID')!=null && getCookie('P90XdelayPopBool')=='opened'){
		initDelayPop();
		delayPopPrep();
		delayPop('P90XDelayPop', 90000);
	}else if(getCookie('P90XdelayPopBool')=='closed' || getCookie('P90XdelayPopBool')=='true'){
		
	}
	
													 
	Shadowbox.init({
    initialWidth: 622, viewportPadding:0, handleOversize:"drag" 	});							 
	
	
  initAllSlideShows();// INITIALIZE SLIDESHOW
	
	$("#homeSubSlideshow div.nav:last").mouseenter(function(){
		$("#homeSlideshow div.nav:last").children("a.stop").eq(0).click();																									
	}).mouseleave(function(){
		$("#homeSlideshow div.nav:last").children("a.start").eq(0).click();
	});
	
	
	$("div.success-stories #maincontent .jslideshow .slide a").mouseover(function(){
		$(this).addClass("hover");																																 
	}).mouseout(function(){
		$(this).removeClass("hover");
	});
	
	$("div.success-stories #maincontent .jslideshow .slide img").reflect({height:.3, opacity:0.5});
	
	$("div.faq #maincontent .main div.answer").hide();
	$("div.faq #maincontent .main h3").toggle(
	function(){
		$(this).addClass("active");
		$(this).next("div.answer").slideDown(200);													
	}, function(){
		$(this).removeClass("active");
		$(this).next("div.answer").slideUp(200);																							
	});
	
	var bvReviewURL = "/product/p90x-reviews-comments.do";
	$(".bvRatingStarClass").addClass(bvRatingStarClass);
	$(".bvRatingMessage").html(bvRating.substr(0,4) + " of 5 (<a href="+bvReviewURL+">" + bvReviewCount + " reviews</a>)");
	$(".bvReviewSubmission").attr("href", bvReviewSubmissionUrl);
	$(".bvCustomerReviews").attr('href', bvReviewURL);
	
	/*
	$("span.bvRatingMessage > a, .bvCustomerReviews").click( function() {
			reviewsPop(this.href);
			return false;
	});
	*/
		
		
		
	$(".press #maincontent .main .content .item a").each(function(){
		if($(this).attr("href").indexOf("http://")!=-1 && $(this).attr("href").indexOf("javascript:")==-1){
			$(this).click(function(){
				pressPop($(this).attr("href"));
				return false;
			});
		};
	
	});
    
}); 


		
function reviewsPop(href){window.open(href, 'custReviews', 'width=980, height=500, left=100, top=100, toolbar=no, location=no, scrollbars=yes, status=yes, resizable=yes, fullscreen=no');}

function pressPop(href){window.open(href, 'In the Press', 'width=980, height=500, left=100, top=100, toolbar=no, location=no, scrollbars=yes, status=yes, resizable=yes, fullscreen=no');}


