var CoreKedemOptimization = { // execute global functions in the optimization process
	init : function (){
		$('#youtubeFrameSmall img').click(function(){
			// track the Small youtube video openning rate
			 _gaq.push(['_trackPageview', '/en/video/viewYoutubeProduct/']);
		});
		
		$('.buyNow button, .buyNowTrigger button').click(function(){
			KedemHerbs.loader.show();
			window.location = "/en/cart/content/";
		});
		
		// Journal de Montreal popup		
		$('.enlargeJDM').click(function(){
			_gaq.push(['_trackPageview', '/en/modal/viewJournalDeMontreal/']);
			var content = "<div class='modal_Event'><div class='close'></div><img src='/_Assets/Images/Optimization/jdm-enlarged.png' width='449' height='550'></div>";
			ModalWindow.init(content);
		});
		
		// Return Policy
		$('.view-return-policy').click(function(){
			var contextual = $(this).attr('alt');
			_gaq.push(['_trackPageview', '/en/modal/viewReturnPolicy/']);
			var content = "<div class='modal_Event view-return-policy-enlarged'><div class='close'></div><p><b>ECZEMA TREATMENT RETURN POLICY</b></p><p>If you don't start seeing visible results in a before & after picture comparison within 3 weeks, you may return the unused portion to us for a full refund.</p><p> Anytime beyond 40 days from the date of purchase of merchandise (3 weeks of usage, plus around 20 days shipping time - 10 days each way - ) no returns will be accepted.</p><p>"+contextual+" Any usage beyound this quantity will void the refund. <p>A return will not be accepted if a before and after comparison picture is missing from the returning package.</p></div>";
			ModalWindow.init(content);
		});
		
	}
}


var $_GET = {
	values : [],
	init : function(){		
		var urlHalves = String(document.location).split('?');
		if(urlHalves[1]){
			var urlVars = urlHalves[1].split('&');
			for(var i=0; i<=(urlVars.length); i++){
				if(urlVars[i]){
					var urlVarPair = urlVars[i].split('=');
					$_GET.values[urlVarPair[0]] = urlVarPair[1];
				}
			}
		}
	}
}

var Timing = {
	calculateTiming : function(totalSecondsToTheEnd){
		var HoursToTheEnd = Math.floor(totalSecondsToTheEnd / 3600);
		var totalSecondsToTheEnd = totalSecondsToTheEnd - (HoursToTheEnd * 3600);
		var MinutesToTheEnd = Math.floor(totalSecondsToTheEnd / 60);
		var totalSecondsToTheEnd = totalSecondsToTheEnd - (MinutesToTheEnd * 60);
		if (HoursToTheEnd < 10) HoursToTheEnd ="0"+HoursToTheEnd;
		if (MinutesToTheEnd < 10) MinutesToTheEnd =  "0"+MinutesToTheEnd;
		if (totalSecondsToTheEnd < 10) totalSecondsToTheEnd =  "0"+totalSecondsToTheEnd;				
		
		return (HoursToTheEnd+''+ MinutesToTheEnd+'' +totalSecondsToTheEnd+'');
	},
	init : function(){
		
		var output = Timing.calculateTiming(SpecialOffer.totalSecondsToTheEnd);
		SpecialOffer.totalSecondsToTheEnd = SpecialOffer.totalSecondsToTheEnd-1;
		if (SpecialOffer.totalSecondsToTheEnd < 0){
			CookieManager.erase('promoCode');
			var path = window.location.href;
			path = path.replace('&fshipex=true','');
			path = path.replace('fshipex=true','');
			window.location.href=path;
		}
			
		// update HTML content
		$('.hours_dash .digit:nth-child(even)').text(output.substring(0,1));
		$('.hours_dash .digit:nth-child(odd)').text(output.substring(1,2));	
		$('.minutes_dash .digit:nth-child(even)').text(output.substring(2,3));
		$('.minutes_dash .digit:nth-child(odd)').text(output.substring(3,4));		
		$('.seconds_dash .digit:nth-child(even)').text(output.substring(4,5));
		$('.seconds_dash .digit:nth-child(odd)').text(output.substring(5,6));
		
		SpecialOffer.timer =  setTimeout("Timing.init()", 1000);
	}	
}


var SpecialOffer = {
	unixTime : 0,
	positionObject : function(id){
		var objectW = $('#'+id).width();
		var windowW = $(window).width();
		$('#'+id).css('left',((windowW-objectW)/2)+'px').show().animate({bottom:'0'},700);
	},
	rePositionObject : function(id){
		var objectW = $('#'+id).width();
		var windowW = $(window).width();
		$('#'+id).css('left',((windowW-objectW)/2)+'px');
	},	
	
	createFreeShipping : function(){
		// create the object
		var content = "<div id='specialOfferContainer'><div id='freeShipping'></div><div id='timerContainer'><div class='dash hours_dash'><span class='dash_title'>hours</span><div class='digit'>0</div><div class='digit'>0</div></div><div class='dash minutes_dash'><span class='dash_title'>minutes</span><div class='digit'>0</div><div class='digit'>0</div></div><div class='dash seconds_dash'><span class='dash_title'>seconds</span><div class='digit'>0</div><div class='digit'>0</div></div></div><div id='close'></div></div>";
		$('body').append(content);
	},
	
	removeObject : function(obj){		
		var $parent = $(obj).parent();
		$parent.animate({bottom:'-125'},700,function(){$(this).remove()});
	},
	
	

	process : function(countDisplays){
		var expires = CookieManager.read('fsex');
		if (expires != "") {
			SpecialOffer.totalSecondsToTheEnd = expires - SpecialOffer.unixtime;
			if (SpecialOffer.totalSecondsToTheEnd > 0){
				// record the display
				var displayed = CookieManager.read('promoShownCount');
 				if (!displayed) displayed=1;
 				else displayed = parseInt(displayed) + 1;
				CookieManager.create('promoShownCount',displayed);

				if (!countDisplays || (countDisplays && displayed <=2) ){
					// create the object
					SpecialOffer.createFreeShipping();
					// position object
					SpecialOffer.positionObject('specialOfferContainer');			
					$(window).resize(function(){SpecialOffer.rePositionObject('specialOfferContainer');});
									
					// update object content
					Timing.init();
				}				

			}
			else {
				CookieManager.erase('promoCode');
			}
					        
		        // close the module
		        $("#specialOfferContainer #close").click(function(){
		        	SpecialOffer.removeObject(this);
		        	SpecialOffer.timer = clearTimeout(SpecialOffer.timer);
		        });       
		}
		
	},

	init : function () {		
		// determine the current time
		var foo = new Date; // Generic JS date object
		var unixtime_ms = foo.getTime(); // Returns milliseconds since the epoch
		SpecialOffer.unixtime = parseInt(unixtime_ms / 1000);
		
		// see if the ccookie has ben previously planted
		var expires = CookieManager.read('fsex');
		if ( $_GET.values['fshipex'] == "true" && expires == ""){
			expires = SpecialOffer.unixtime + (24*60*60 - 17);
			CookieManager.create('fsex',expires);
			CookieManager.create('promoCode','NOSHIP24');
		}
	}
}


$(document).ready(function(){
	$_GET.init();
	SpecialOffer.init();
	CoreKedemOptimization.init();
});
