/**
 * Survey Callout
 * Version 3.2
*/
var ROSETTA = ROSETTA ? ROSETTA : new Object();
ROSETTA.prepost = function(){
    this.popCallout = goSurveyCallout;
    this.showPostCallout;
    this.showPreCallout;
    // Declare and Initialize Global Variables
    var randPre, randPost
    var preCalloutData = new Array();
    var postCalloutData = new Array();

    // PRE SURVEY CONTENT
    var preHeadline = new Array();
    var preCopy = new Array();
    var preURL = new Array();
    preHeadline[0] = "Your input counts.";
    preCopy[0] = "Help us make this Web site even better by completing our 3-minute survey.";
    preURL[0] = "http://rosetta.checkboxonline.com/Survey.aspx?s=63d5b5396710439298cb0d527efa11a9";

    // POST SURVEY CONTENT
    var postHeadline = new Array();
    var postCopy = new Array();
    var postURL = new Array();
    postHeadline[0] = "Your input counts.";
    postCopy[0] = "Help us make this Web site even better by completing our 3-minute survey.";
    postURL[0] = "http://rosetta.checkboxonline.com/Survey.aspx?s=259ab74ae27b446785e38e6e70830707";

    // Survey information and statistics
    var widthOfPopUp = 815; // Popup Survey Width
    var heightOfPopUp = 600; // Popup Survey Height
    var sessionIdneeded = true; // Is a session ID needed for this survey?  IF set to true will look for the id in the url query string AND cookies, and append to survey URL.
    var sessionIdName = "SessionID"; // Name of the server session ID variable where session ID is stored (either cookie or query string - looks for both)
    var currentDate = new Date(); // Current Date and time for time stamping 

    // Set page excludes
    var preExclude = new Array();
    var postExclude = new Array();
    postExclude[0] = "post_exclude.html"; // page will be excluded from the post survey

    // Callout float config
    var floatSurvey = true; // Will the survey callout scroll when the user scrolls?

    // Callout config
    var timerMax = 10; // Time in whole seconds the callout will remain on the page
    var timerCalloutMax = 10; // Time in whole seconds the callout will remain on the page after mousing over it.
    var surveyYOffset = 250; // How far from the right the callout will go
    var surveyXOffset = 0; // How far from the top the callout wil go
    var imagePath = "/Style%20Library/images/survey/";
    var calloutTargetDiv = "surveyTarget"; // Id of the DIV on the page that will contain the callout
    var formName = "CalloutSurveyForm"; // name and Id of the survey form

    var logo = "hp_logo_top.png";
    var logoWidth = 197;
    var logoHeight = 82;
    var logoAlt = "Natrelle";
    var confirmButton = "yes_btn.gif";
    var confirmButtonAlt = "Yes";
    var confirmButtonWidth = 53;
    var confirmButtonHeight = 20;
    var cancelButton = "btn_noThanks.gif";
    var cancelButtonAlt = "No, Thanks";
    var cancelButtonWidth = 79;
    var cancelButtonHeight = 21;

    // Cookie Info
    var pageCountCookie = "pageCount";
    var trackedStatusCookie = "tracked";
    var surveyCookie = "survey";
    var surveyDateTime = "callOutDateTime";
    var calloutResponse = "callOutResponse";
    var cookieExpires = 1000000;

    if (sessionIdneeded) {
	    var clientSessionId = null;
	    if (readCookie(sessionIdName)) {
		    clientSessionId = readCookie(sessionIdName);
	    } else if (getUrlParam(sessionIdName)) {
		    clientSessionId = getUrlParam(sessionIdName);
	    }
    }
    this.openSurvey = openSurvey;
    this.closeSurvey = closeSurvey;
    function openSurvey() {
        var targetUrl = this.showPostCallout ? postURL[0] : preURL[0];
        window.open(targetUrl, "_blank", "width=" + widthOfPopUp + ", height=" + heightOfPopUp + ", scrollbars=yes");
        closeSurvey();
    }

    function closeSurvey() {
        floatCallout(true);
	    $('#survey_callout').animate({
	    	opacity: 0
	    }, 1000, function(){
	    	$('#surveyTarget').remove();
	    });
    }
    function fadeSurvey() {
    	floatCallout();
	    $('#survey_callout').animate({
	    	opacity: 1,
	    	left: '-=200'
	    }, 1000);
    }

    function buildSurveyCallout(tempUrl, tmpHtext, tmpCtext) {
        var url = tempUrl;
        var HText = tmpHtext;
        var CText = tmpCtext;
        if (sessionIdneeded && clientSessionId) {
	        url += "&" + sessionIdName + "=" + clientSessionId;
        }
        // Build the form
        formCallout = document.createElement('form');
        formCallout.setAttribute('name', formName);
        formCallout.setAttribute('id', formName);
        formCallout.setAttribute('target', '_new');
        formCallout.setAttribute('method', 'post');
        formCallout.setAttribute('action', url);

        // Overall container

        surveyCallout = document.createElement('div');
        surveyCallout.setAttribute('id', 'survey_callout');

        // Outside Box

        surveyCalloutBox = document.createElement('div');
        surveyCalloutBox.setAttribute('id', 'survey_callout_box');

        // Inside Box

        surveyInnerBox = document.createElement('div');
        surveyInnerBox.setAttribute('id', 'inner_box');

        // Logo container and Logo

        surveyLogoDiv = document.createElement('div');
        surveyLogoDiv.setAttribute('id', 'survey_logo');
        clientLogo = document.createElement('img');
        clientLogo.setAttribute('src', imagePath + logo);
        clientLogo.setAttribute('width', logoWidth);
        clientLogo.setAttribute('height', logoHeight);
        clientLogo.setAttribute('alt', logoAlt);
        surveyLogoDiv.appendChild(clientLogo);

        // Callout Body

        calloutHeader = document.createElement('h3');
        calloutHeaderCopy = document.createTextNode(HText);
        calloutHeader.appendChild(calloutHeaderCopy);
        calloutBody = document.createElement('p');
        // Sets text
        calloutBody.innerHTML = CText;
        surveyLinkP = document.createElement('p');
        surveyLinkP.setAttribute('class', 'survey_nav');
        surveyLinkConfirm = document.createElement('a');
        surveyLinkConfirm.setAttribute('href', 'javascript: natrelle.ppc.callout.openSurvey();');
        surveyLinkConfirmImage = document.createElement('img');
        surveyLinkConfirmImage.setAttribute('src', imagePath + confirmButton);
        surveyLinkConfirmImage.setAttribute('alt', confirmButtonAlt);
        surveyLinkConfirmImage.setAttribute('width', confirmButtonWidth);
        surveyLinkConfirmImage.setAttribute('height', confirmButtonHeight);
        surveyLinkConfirmImage.setAttribute('border', '0');
        surveyLinkConfirm.appendChild(surveyLinkConfirmImage);
        surveyLinkSpacer = document.createTextNode(' ');
        surveyLinkCancel = document.createElement('a');
        surveyLinkCancel.setAttribute('href', 'javascript: natrelle.ppc.callout.closeSurvey();');
        surveyLinkCancelImage = document.createElement('img');
        surveyLinkCancelImage.setAttribute('src', imagePath + cancelButton);
        surveyLinkCancelImage.setAttribute('alt', cancelButtonAlt);
        surveyLinkCancelImage.setAttribute('width', cancelButtonWidth);
        surveyLinkCancelImage.setAttribute('height', cancelButtonHeight);
        surveyLinkCancelImage.setAttribute('border', '0');
        surveyLinkCancel.appendChild(surveyLinkCancelImage);
        surveyLinkP.appendChild(surveyLinkConfirm);
        surveyLinkP.appendChild(surveyLinkSpacer);
        surveyLinkP.appendChild(surveyLinkCancel);

        // Background shadows

        surveyLinkShadowRight = document.createElement('div');
        surveyLinkShadowRight.setAttribute('id', 'survey_callout_box_right');
        surveyLinkShadowBottom = document.createElement('div');
        surveyLinkShadowBottom.setAttribute('id', 'survey_callout_box_btm');

        //Build Everything

        surveyInnerBox.appendChild(surveyLogoDiv);
        surveyInnerBox.appendChild(calloutHeader);
        surveyInnerBox.appendChild(calloutBody);
        surveyInnerBox.appendChild(surveyLinkP);
        surveyCalloutBox.appendChild(surveyInnerBox);
        surveyCallout.appendChild(surveyCalloutBox);
        surveyCallout.appendChild(surveyLinkShadowRight);
        surveyCallout.appendChild(surveyLinkShadowBottom);
        formCallout.appendChild(surveyCallout);

        // Attach to page
        var tmpDiv = document.createElement("div");
        tmpDiv.id = calloutTargetDiv;
        tmpDiv.appendChild(formCallout);
        $(tmpDiv).prependTo(document.body);
        // fade and move it in
        fadeSurvey();
    }

    function goSurveyCallout() {
        initContent();
        
        if(this.showPostCallout){
            //setTimeout("buildPostCallout()",5000);
            buildSurveyCallout(postCalloutData[randPost][2], postCalloutData[randPost][0], postCalloutData[randPost][1]);
        } else if (this.showPreCallout) {
	        buildSurveyCallout(preCalloutData[randPre][2], preCalloutData[randPre][0], preCalloutData[randPre][1]);
        }
    }
    
    // build function needed for the timer obj to function correctly
    function buildPostCallout(){
        buildSurveyCallout(postCalloutData[randPost][2], postCalloutData[randPost][0], postCalloutData[randPost][1]);
    }
    function readCookie(name) {
	    var nameEQ = name + "=";
	    var ca = document.cookie.split(';');
	    for(var i=0;i < ca.length;i++)
	    {
		    var c = ca[i];
		    while (c.charAt(0)==' ') c = c.substring(1,c.length);
		    if (c.indexOf(nameEQ) == 0) return c.substring(nameEQ.length,c.length);
	    }
	    return null;
    }

    function getUrlParam(name) {
	    var q = unescape(location.search.substring(1)).split(/[=&]/);
	    for (var j=0; j<q.length; j++) {
		    if (q[j] == name) {
			    return q[j+1];
		    }
	    }
	    return null;
    }

    function initContent() {
	    for (ab=0;ab<preHeadline.length;ab++) {
		    var tmpContent = new Array();
		    tmpContent[0] = preHeadline[ab];
		    tmpContent[1] = preCopy[ab];
		    tmpContent[2] = preURL[ab];
		    preCalloutData.push(tmpContent);
	    }
	    for (ac=0;ac<postHeadline.length;ac++) {
		    var tmpContent = new Array();
		    tmpContent[0] = postHeadline[ac];
		    tmpContent[1] = postCopy[ac];
		    tmpContent[2] = postURL[ac];
		    postCalloutData.push(tmpContent);
	    }
    	
	    randPre = Math.floor(Math.random()*preCalloutData.length);
	    randPost = Math.floor(Math.random()*postCalloutData.length);	
    }
    // functions below this line are used to float the callout as the page scrolls.
    function iecomptest(){
    return (document.compatMode && document.compatMode!="BackCompat")? document.documentElement : document.body
    }

    function floatCallout(stopFloating){
	    var ns = (navigator.appName.indexOf("Netscape") != -1) || window.opera;
	    var d = document;
	    var tp = 25; //top padding of callout from top as specified in the stylesheet
    	
	    function ml(id){
		    var el=d.getElementById(id);
		    if(d.layers)el.style=el;
		    el.sP=function(x,y){null;this.style.top=(y+tp)+"px";};
		    el.x = 0;
		    el.y = 0;
		    return el;
	    }
    	
	    window.stickyWindow=function(){
		    var pY = ns ? pageYOffset : iecomptest().scrollTop;
		    srvObj.y += (pY - srvObj.y)/8;
		    srvObj.sP(srvObj.x, srvObj.y);
		    if (stopFloating == false || typeof stopFloating == "undefined" || !stopFloating || stopFloating == "complete") {
			    setTimeout("stickyWindow()", 10);
		    }
	    }
    	
	    srvObj = ml('survey_callout');
	    stickyWindow();
    }
}

var showPostCallout;
var showPreCallout;

/* ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
 * Prepost Callout
============================================ */
var natrelle = natrelle || {};
natrelle.prepost = function(){
	this.callout;
};
natrelle.prepost.prototype.init = function(){
	var obj = this;
	obj.callout = new ROSETTA.prepost();
	obj.callout.showPostCallout = showPostCallout;
	obj.callout.showPreCallout = showPreCallout;
	if(obj.callout.popCallout) obj.callout.popCallout();
}  
$(document).ready(function() {
	natrelle.ppc = new natrelle.prepost();
	natrelle.ppc.init(); 
});

