//Set cookie time if one doesn't exist - this will mark the start time that the user first visited the site
var cookieStartDate = $.cookie('start');
if(cookieStartDate == null)
{
	var startDate = new Date();	
	cookieStartDate = startDate.toGMTString();
	$.cookie('start',cookieStartDate,  { expires: 365, path: '/' } );
}

//Has Survey been shown previously?
var intervalId = null;
if(!checkSurveyOpened())
	intervalId = SetTimer();


function SetTimer()
{
	var intv = window.setInterval(function(){ 
		displaySurveyCheck();
	}, 30000);   //Check every 30 seconds (30000)
	window.setTimeout(function(){
		stopChecking(intv);
	}, 3600000);  //Been on same page for 360 secs so cancel interval (360000)
	return intv;
}

function checkSurveyOpened()
{
	var cookieSurvey = $.cookie('survey');
	if(cookieSurvey == null)
		return false;
	return true;
}

function displaySurveyCheck()
{
	//If time since user arrived is > x mins 
	var timespan = new Date().getTime() - new Date(cookieStartDate).getTime();
	var secondsTimeSpan = Math.floor( timespan / 1000 );
	
	if(secondsTimeSpan >= 60)
	{
		var nowDate = new Date();
		$.cookie('survey' , true , {  expires: 365, path: '/' } );
		clearInterval(intervalId);
		window.open('http://www.surveymonkey.com/s/9GV3T79', 'popup', 'status=1,width=600,height=600,scrollbars=1');
	}
}

function stopChecking(a)
{
	clearInterval(a);
}
