
//popup settings
//0 means disabled; 1 means enabled;
var popupStatus = 0;

//loading popup with jQuery
function loadPopup(){
	//loads popup only if it is disabled
	if(popupStatus==0){
		$("#backgroundPopup").css({
			"opacity": "0.7"
		});
		$("#backgroundPopup").fadeIn("slow");
		$("#popupContact").fadeIn("slow");
		popupStatus = 1;
	}
}

//loading popup thankyou with jQuery
function loadPopup2(){
	//loads popup only if it is disabled
	if(popupStatus==0){
		$("#backgroundPopup").css({
			"opacity": "0.7"
		});
		$("#backgroundPopup").fadeIn("slow");
		$("#popupThankYou").fadeIn("slow");
		popupStatus = 2;
	}
}

//disabling popup with jQuery
function disablePopup(){
	//disables popup only if it is enabled
	if(popupStatus==1){
		$("#backgroundPopup").fadeOut("slow");
		$("#popupContact").fadeOut("slow");		
		popupStatus = 0;
	}else if(popupStatus==2){
		$("#backgroundPopup").fadeOut("slow");
		$("#popupThankYou").fadeOut("slow");
		popupStatus = 0;
	}
}

//centering popup
function centerPopup(){
	//request data for centering
	var windowWidth = document.documentElement.clientWidth;
	var windowHeight = document.documentElement.clientHeight;
	var popupHeight = $("#popupContact").height();
	var popupWidth = $("#popupContact").width();
	//centering
	$("#popupContact").css({
		"position": "absolute",
		"top": windowHeight/2-popupHeight/2,
		"left": windowWidth/2-popupWidth/2
	});
	
	$("#popupThankYou").css({
		"position": "absolute",
		"top": windowHeight/2-popupHeight/2,
		"left": windowWidth/2-popupWidth/2
	});
	
	//only need force for IE6
	
	$("#backgroundPopup").css({
		"height": windowHeight
	});
	
}


//CONTROLLING EVENTS IN jQuery
$(document).ready(function(){

sEttIngS__1_en = '0';	
	
	// enable/disable script
	// enabled: sEttIng5__1_en = '1';
	// disables: sEttIng5__1_en = '0';
	if (sEttIngS__1_en=='1'){
	
		var value = $.readCookie('Timers');
		
		var threemin= 180000;
		var tenmin= 600000;
		var difference= 780000;
		
		var tfirst= threemin;
		var tsecond= tenmin;
		var tdiff= difference;
		
		var pageOpen = new Date();
		var timeSpent = $.readCookie('TimeSpent');
		if (timeSpent==null){
			$.setCookie('TimeSpent','0', {});
			$timeSpent=0; 
		}
		else {
			timeSpent = parseInt(timeSpent);
		}
		
	
		if (timeSpent < threemin){
			if (! value){
				tfirst = threemin - timeSpent;
				tdiff = difference - timeSpent;
			}
			if (value=='t1' || value=='t2'){				
				tsecond = tenmin - timeSpent;
			}
		}
		if (timeSpent < tenmin && timeSpent > threemin){			
			if (value=='t1'||value=='t2')
			{				
				tsecond = tenmin - timeSpent;				
			}
		}
		if (timeSpent > tenmin){
			if (value=='t1' || value =='t2'){
				var multiple = 0;
				while (multiple <= timeSpent){
					multiple+= tenmin;
				}								
				tsecond = multiple - timeSpent;				
			}		
		}	
		
		//alert ("tSpent: "+ timeSpent  + "\ntFirst: "+ tfirst + "\ntSecond: " + tsecond + "\ntDiff: "+ tdiff);	
			
		if (! value ){        
	        $(this).oneTime(tfirst,function(){		
				//alert('3 minutes have passed');	
				centerPopup();			
				loadPopup();			
				
				//setting cookie and value t1
				var cookie = $.setCookie('Timers', 't1', {
	        		duration: 10 // in days
	    		});	    	    					
			});	
			
			// second timer for the 10 mins loop.
	    	// using tdiff to account for the time difference between
	    	// the popups
	    	$(this).oneTime(tdiff,function(){
	    			var execValue = $.readCookie('Timers');
	    			if (execValue=='s'){
	    				$(this).stopTime();
	    			}
	    			else {
	    				// get back to standard timer
	    				$(this).stopTime();
	    				centerPopup();				
						loadPopup();		
						//create standard timer for same page
						$(this).everyTime(tsecond,function(){
							var execValue = $.readCookie('Timers');
							if (execValue=='s'){
			    				$(this).stopTime();
			    			}
			    			else {		    				
								centerPopup();				
								loadPopup();
								// if we have a timer != 30 secs, we stop the current one and
			    				// create a new, static timer (for time differences when changing pages)
			    				// get back to the regulat timer		    				
								if (tsecond!=tenmin){
									tsecond = tenmin;
									$(this).stopTime();								
									$(this).everyTime(tsecond,function(){
										var execValue = $.readCookie('Timers');
										if (execValue=='s'){
						    				$(this).stopTime();
						    			}
						    			else {		    				
											centerPopup();				
											loadPopup();
						    			}																					
									});
								}
			    			}
						});
	    			}
			});	
		}
	    else {    	
	    	if (value == 't1' || value == 't2' ){
	    		//activating second timer    		
	    			    		
	    		// stop other timers and prepare for loop
	    		$(this).stopTime();
	    		$(this).everyTime(tsecond,function(){
	    			var execValue = $.readCookie('Timers');
	    			if (execValue=='s'){
	    				$(this).stopTime();
	    			}
	    			else {		
	    				$.delCookie('Timers');
	    				var cookie = $.setCookie('Timers', 't2', {
	        				duration: 10 // in days
	    				});		
	    				centerPopup();
						loadPopup();
						// if we have a timer != 30 secs, we stop the current one and
	    				// create a new, static timer (for time differences when changing pages)
	    				// get back to the regulat timer		    				
						if (tsecond!=tenmin){
							tsecond = tenmin;
							$(this).stopTime();								
							$(this).everyTime(tsecond,function(){
								var execValue = $.readCookie('Timers');
								if (execValue=='s'){
				    				$(this).stopTime();
				    			}
				    			else {		    				
									centerPopup();				
									loadPopup();
				    			}																					
							});
						}
	    			}
				});	
	    	}
	    	// cookie has any other combination (s)/user has sent the info
	    	else { $(this).stopTime(); }
	    }
	    
		//LOADING POPUP
		//Click the button event!
		$("#button").click(function(){
			//centering with css
			centerPopup();
			//load popup
			loadPopup();
		});
					
		//CLOSING POPUP
		//Click the x event!
		$("#popupContactClose").click(function(){		
			disablePopup();			
		});
		
		$("#popupSendForm").click(function(){
			var textName 	= document.getElementById("popupContactInName").value;
			var textPhone 	= document.getElementById("popupContactInPhone").value;
			var textTime	= document.getElementById("popupContactInTime").value;		
			var ajxFile = "../../sendmail.php";		
					
			$.delCookie('Timers');
	    	//reset cookie,set it to send
	    	var cookie = $.setCookie('Timers', 's', {
	        		duration: 10 // in days
	    	});
			
			disablePopup();		
			
			$.post(ajxFile, { name: textName, phone: textPhone, time: textTime});		
			loadPopup2();
		});
	
		$("#popupContactClose2").click(function(){		
			disablePopup();			
		});	
		
		//Click out event!
		$("#backgroundPopup").click(function(){
			disablePopup();
		});
		//Press Escape event!
		$(document).keypress(function(e){
			if(e.keyCode==27 && (popupStatus==1||popupStatus==2)){
				disablePopup();
			}
		});
	
		$(window).unload( function () { 
			pageClose = new Date();
			
			hours    = (pageClose.getHours() - pageOpen.getHours());
			minutes = (pageClose.getMinutes() - pageOpen.getMinutes());
			seconds = (pageClose.getSeconds() - pageOpen.getSeconds());
			time = (seconds + (minutes * 60) + (hours * 60 * 60));
			
			var previousTimeSpent = $.readCookie('TimeSpent');
			if (previousTimeSpent==null){
				$.setCookie('TimeSpent', time*1000, {});		
			}
			else {
				$.delCookie('TimeSpent');
				$.setCookie('TimeSpent', time*1000+parseInt(previousTimeSpent), {});		
			}
		});
	}
});
