/* lightbox script for survey request */
var Survey = {

	options: {},

	load: function() {
		var survey = Cookie.read('survey');
		Survey.depth =  Cookie.read('session_depth') || 0;
		var showsurvey = Cookie.read('show_survey');
		var rand = Math.random();
		var lottery = Math.floor(rand * 100); // 0-99
		Survey.uniqueKey = Math.floor(rand * 100000000) + "";
		Survey.getURLOptions();
		Survey.options.depth = Survey.options.depth || 0;
		Survey.options.exclude = (typeof navigator.userAgent !== 'undefined') && navigator.userAgent.indexOf("KTXN") > 1 ? true : false;
		console.log(survey);
        console.log(Survey.depth);
		if(Survey.options.displayType=="lightbox"){		
			if ((!(survey) && !(Survey.options.exclude) && Survey.depth == Survey.options.depth) || Survey.options.debugMode){
				if(lottery < Survey.options.selectionPercent || Survey.options.debugMode) {
					//run lightbox callback function if selected
					window.setTimeout(Survey.options.lightboxCallback,1000);
					//set expiration date
					Survey.setSurveyCookie();
				}
			}	
		}
		if(Survey.options.displayType=="slide"){
			if(!(survey) && showsurvey && !(Survey.options.debugMode)) $("#surveyinvite").show(); //persist survey until yes/no are selected
			else if(typeof(showsurvey)=='undefined' && Survey.depth >= Survey.options.depth  || Survey.options.debugMode){
				if(lottery < Survey.options.selectionPercent  || Survey.options.debugMode ){
				Survey.options.slideCallback(); //slide on first time only		
				Cookie.write({name: 'show_survey', value: true, path: '/'});	
				}
				else{
					Cookie.write({name: 'show_survey', value: false, path: '/'});
				}
			}
			$("#surveyyes").click(function(){ Survey.startSurvey(true, Survey.options.slideSurveyURL); $("#surveyinvite").slideUp(700); Survey.setSurveyCookie();});
			$("#surveyno").click(function(){ $("#surveyinvite").slideUp(700);Survey.setSurveyCookie();});
		}
		Survey.setSessionDepth();
	}
}

Survey.setSessionDepth = function (){
	Survey.depth++;
	Cookie.write({
		name: 'session_depth',
        value: Survey.depth,
		path: '/'
	});	
}

Survey.getURLOptions = function (){
	querystring = window.location.search;
	if(querystring.indexOf("debug")>=0) Survey.options.debugMode = true; 
	if(querystring.indexOf("survey=a")>=0) Survey.options.displayType = 'slide';
	if(querystring.indexOf("survey=b")>=0) Survey.options.displayType = 'lightbox';
}

Survey.setSurveyCookie = function(){
	//write exemption cookie
	Cookie.write({
		name: 'survey',
        value: true,
		expires: Survey.options.exemptionDays,
		path: '/'
	});
}
	
Survey.startLightbox =  function() {
	    var link  = document.createElement('a');
	    link.setAttribute('href',Survey.options.lightboxURL);
	    link.setAttribute('title','Survey');
	    jQuery.openDOMWindow(Survey.options.lightboxParameters); 
	}

Survey.startSurvey = function(start, surveyurl) {
    if(start) {
    	if(parent.pageTracker && parent.pageTracker._trackEvent && Survey.options.trackEvent)
    		parent.pageTracker._trackEvent(
    			Survey.options.trackEvent.category,
    			Survey.options.trackEvent.action,
    			Survey.options.trackEvent.label,
    			Survey.options.trackEvent.value
    		);
		//select random url if given multiple url choices
		if( typeof(surveyurl) == 'object' && (surveyurl instanceof Array) ) {
			srandlength = surveyurl.length;
			var srand = Math.random();
	        var sindex = Math.floor(srand * srandlength);
			url = surveyurl[sindex];
		} else {
			url = surveyurl;
		}
    	if(Survey.options.addUniqueKey) url = url + '?c='+Survey.uniqueKey;
    	//,'height='+screen.availHeight+',width='+screen.availWidth+',resizable=yes,scrollbars=yes,status=yes'
    	surveywindow = window.open(url,'survey');
    }
    
   if(Survey.options.displayType=="lightbox"){	 jQuery.closeDOMWindow(); }
}

// jQuery onload event
$(window).load(Survey.load);


var Cookie = {
    read: function(key) {
        var cookie = Cookie.getCookie();
        return cookie[key];
    },
	write: function(hash) {
		var cookie_string = expires = '';
		days = hash.expires || 0;
		if (days) {
			var date = new Date();
			date.setDate(date.getDate()+days);
			expires = "; expires="+date.toUTCString();
		}
		path = hash.path || '/'; 
		document.cookie = hash.name+"="+hash.value+expires+"; path="+path;
	},
	getCookie: function() {
		var cookie = {};
		var cookie_array = document.cookie.split('; ');
		for(var i=0;i<cookie_array.length;i++) {
			var c = cookie_array[i].match(/(.*?)=(.*)/);
			if(c) cookie[c[1]] = c[2];
		}
		return cookie;
	}
}
