/*
-- Random Survey Popup v1.2 --
by Andrew Murphy - 2011/06/21
copyright Delvinia Interactive Inc. - 2011
http://www.delvinia.com/
Requires jQuery v1.4.1
*/

// Randomization Properties
//
var MIN_SIP_VISITS = 3;  // minimum number of survey invitation pages that the user must visit before they may begin recieving invitations

var RANDOM_SIZE = 5;  // 1 in RANDOM_SIZE chance of the popup occouring

  jQuery.noConflict();
// Initialization
//
jQuery(document).ready(windowOnload);
function windowOnload()
{	
	// check for the cookie which tracks how many survey initializing pages (sip) the user has visited
	var sip_visits;
	if(jQuery.cookie("sip_visits"))
	{
		sip_visits = parseInt(jQuery.cookie("sip_visits"));
	}
	else
	{
		sip_visits = 0;
	}
	
	// check if they have already been shown the survey invitation or not
	var survey_invited = jQuery.cookie("survey_invited");
	
	// DEBUGGING SCRIPTS - START
	jQuery("#sipVisitsInput").attr("value", sip_visits);
	jQuery("#surveyInvitedInput").attr("value", survey_invited);
	jQuery("#rndNumInput").attr("value", "");
	// DEBUGGING SCRIPTS - END

	// if they have visited more than MIN_SIP_VISITS pages and have NOT been invited to take the survey
	var chance = null;
	if(sip_visits >= MIN_SIP_VISITS && !survey_invited)
	{
		// generates a random number
		chance = Math.floor(Math.random() * RANDOM_SIZE);
		
		// DEBUGGING SCRIPTS - START
		jQuery("#rndNumInput").attr("value", chance);
		// DEBUGGING SCRIPTS - END
		
		// if appropriate...
		if(chance == 0)
		{
			// ...show the overlay
			jQuery("body").append("<div id='survey-overlay-bg'></div><div id='survey-overlay-vert'><div id='survey-overlay'><div id='survey-overlay-close'><a href='JavaScript:removeSurveyOverlay();'><img src='/random_survey_overlay/close.png' width='20' height='21' border='0' alt='CLOSE'></a></div><div class='survey-overlay-content'><div class='survey-overlay-main-copy'>Please take a few moments to answer a couple of questions about our site.<br><br></div><div class='survey-overlay-airmiles'><div class='survey-overlay-airmiles-copy'>Earn 10 Bonus <nobr>AIR MILES<sup>&reg;&#8224;</sup> reward miles</nobr> for completing and submitting our survey today.*</div></div><div class='survey-overlay-sub-copy'>Thank you</div></div><div id='survey-overlay-nav'><a href='#' onclick='JavaScript:window.open(\"http://survey.euro.confirmit.com/wix3/p466023754.aspx?l=9&dsrc=9\");removeSurveyOverlay();'><img src='/random_survey_overlay/btn-ok.jpg' width='110' height='39' border='0' alt='OK'></a><a href='#' onclick='JavaScript:removeSurveyOverlay();'><img src='/random_survey_overlay/btn-no.jpg' width='164' height='39' border='0' alt='NO, THANKS'></a></div></div></div>");
			
			// set the cookie that tracks if the user has been invited to take the survey
			// NOTE:  This is a session limted cookie, and it will expire when the visitor ends this browser session
			jQuery.cookie("survey_invited", "true", {expires: null});
			
			return false;
		}
	}
	
	
	// DEBUGGING SCRIPTS - START
	jQuery("#clear_cookies").click(function ()
		{
			jQuery.cookie("sip_visits", null);
			jQuery.cookie("survey_invited", null);
			
			jQuery("#sipVisitsInput").attr("value", "");
			jQuery("#surveyInvitedInput").attr("value", "");
			jQuery("#rndNumInput").attr("value", "");
		}
	);
	// DEBUGGING SCRIPTS - END

	
	// increment the sip_visits cookie
	if(sip_visits < MIN_SIP_VISITS)
	{
		sip_visits++;
		jQuery.cookie("sip_visits", sip_visits, {expires: null});
	}
};

function removeSurveyOverlay() {
	jQuery("#survey-overlay-bg").remove();
	jQuery("#survey-overlay-vert").remove();
}


