
var Survey = {
	//SURVEY STATUS
	setSurveyStatus :function(status){
		//set the survey status cookie
		////alert("setting survey status to: " + status);
		Cookie.set(cookie_name_status,status, 86400000 *365);
	},
	
	getSurveyStatus: function(){
		//get the survey status cookie
		return Cookie.get(cookie_name_status);	
	},
	
	//TIMEOUT FUNCTIONS
	setSurveyTimeout: function(){
		
		//set the timout cookie time
		var d = new Date();
      	d.setTime(d.getTime() + (1000 * timeout_update_amount));
		Cookie.erase(cookie_name_timeout);
		//document.getElementById("date_holder").innerHTML ="time:" + d;
		Cookie.set(cookie_name_timeout,d, 86400000 *(1000 * timeout_update_amount));
		
	},
	
	getSurveyTimeout: function(){
		//fetch the current time from the cookie
		return Cookie.get(cookie_name_timeout);	
	},
	
	gotoSurvey: function(){
		//load the survey
		window.moveTo((screen.width/2)-500,(screen.height/2)-400);
		window.resizeTo(800,600);
		self.focus();
		clearInterval(check_holder);
		window.location = "http://survey.organik.co.uk/go.php?survey_id=" + encoded_survey_id
		
	},
	
	openSurveyWindow: function(){
		//OPEN THE MONITOING PAGE
		win_ref = window.open(monitor_page,'monitor','height=600,width=800,scrollbars=yes,resizable=yes');
		//if (!win_ref.opener) win_ref.opener = self;
		//if (win_ref.focus()) {window.focus}
		window.focus();
		
	},
	checkMe:  function(){
			//alert(Survey.getSurveyTimeout());
			var cookie_val= this.getSurveyStatus();
			timeout_holder = setInterval(Survey.setSurveyTimeout,timeout_update_interval*10);
			if(cookie_val == 1){
				this.openSurveyWindow();
				
			}
				
	},
	assessSurveyOption: function(element,top){
		
		//Show the user the option to take the survey 
		//if the cokie has not been set or is set to take, but not taken
		var cookie_val= this.getSurveyStatus();
		if( cookie_val == 0 || cookie_val == null){
			//show the popup
			////alert("show the popup");
			var myStyle = document.getElementById(element).style;
			myStyle.top= top + 'px';
			Effect.Appear(element, {duration :0.5 , from:0.0, to:0.9})
		}else if(cookie_val == 1){
			//waiting to start the survey
			////alert("waiting to start the survey");
			//check the window is open
			if (!win_ref.closed){
				//REMOVE://alert ("It's closed!");
				this.openSurveyWindow();
				
			}else{
				////alert ("It's still open!");
			}
			//start timer
			timeout_holder = setInterval(Survey.setSurveyTimeout,timeout_update_interval*10);
			
		}else{
			//REMOVE://alert("uncaught: " + cookie_val);
		}
	},
	
	assessShowSurvey: function(){
		self.blur();
		//opener.focus();
		var cookie_val= this.getSurveyStatus();
		if(cookie_val == 1){
			check_holder = setInterval(this.checkSurveyTimeout,timeout_update_interval*1000);
		}
	},
	
	checkSurveyTimeout: function(){
		var now      = new Date();
		var time_out = new Date();
		time_out.setTime(Date.parse(Survey.getSurveyTimeout()));
		//Calculate difference btw the two dates, and convert to days
		var difference = (time_out.getTime()-now.getTime());
		//document.getElementById("date_holder").innerHTML =  difference;
		if(difference < 1){
			Survey.gotoSurvey();
		}
	}
}

