/**
 * @author Rosetta UI dan.zachary
 */
var CCHMC = CCHMC ||
{};
CCHMC.utils = CCHMC.utils ||
{};
CCHMC.props = CCHMC.props ||
{};

/***********************
 *    Configuration    *
 ***********************/
//set to false to turn off
CCHMC.props.showQ = true;
//variable for percentage of users to show survey to
CCHMC.props.percentCheck = 50;
//URL for Q
CCHMC.props.url = "http://websort.net/s/844388/";


/***********************/


CCHMC.props.date = new Date();
CCHMC.props.date.setYear(CCHMC.props.date.getFullYear() + 10);
        
//Jquery on doc ready 
$().ready(function(){
  // alert('Pages Viewed: ' + $.cookie('Pages Viewed') + ' | Saw Survey: ' + $.cookie('Saw Survey'));
    //Survey will be showed to consumers of three pages.  Check for cookie, create it if necessary, and set update if necessary
        
    if ($.cookie('Saw Survey') == "yes") {
        return;
    }
 
    var sawSurvey = $.cookie('Saw Survey');
    var pagesViewed = new Number($.cookie('Pages Viewed'));
    if (sawSurvey == null) {
        //Cookie does not exist, create it
       // alert('Saw Survey equals ' + sawSurvey + ', set it to no');
        $.cookie('Saw Survey', 'no', {
            expires: CCHMC.props.date,
            path: '/'
        });
    }
    if (pagesViewed == null) {
        //Cookie does not exist, create it
       // alert('Pages Viewed equals null, set it to 1');
        $.cookie('Pages Viewed', 1, {
            expires: CCHMC.props.date,
            path: '/'
        });
    }
    else 
        //Cookie exists, update it if necessary
        if (pagesViewed < 3 && pagesViewed != null) {
           // alert('Pages Viewed incremented from ' + pagesViewed);
            pagesViewed++;
            $.cookie('Pages Viewed', pagesViewed, {
                expires: CCHMC.props.date,            
                path: '/'
            });
       // alert('...to ' + pagesViewed);
        }
    //call show survey function
    
    CCHMC.utils.showSurvey();
});


//function to only show survey %10 of the time
CCHMC.utils.showSurvey = function(){

    if ($.cookie('Saw Survey') == "yes") {
        return;
    }
    if ($.cookie('Saw Survey') == "no") {
        if ($.cookie('first_page') == null) {
            $.cookie('first_page', 'seen', {
                expires: CCHMC.props.date,
                path: '/'
            });
            var rand = Math.floor(Math.random() * 100);
            //Create a string to represent the home page
            //var testPath = location.host + '/default.aspx';
            //get the current page path
            //var path = location.host + location.pathname;
            //50% of people on the home page only
            if (rand <= CCHMC.props.percentCheck) {
                CCHMC.utils.serveSurvey('pre');
            }
        };
        if ($.cookie('Pages Viewed') > 3) {
            return;
        }
        if ($.cookie('Pages Viewed') == 3) {
            $.cookie('Pages Viewed', 4,{
                expires: CCHMC.props.date,
                path: '/'
            })
            CCHMC.utils.serveSurvey('post');
            return;
        }
        
    }
}

CCHMC.utils.declinedSurvey = function(){
    $('#survey').dialog('close');
}

CCHMC.utils.acceptedSurvey = function(){
    var surveyq = window.open(CCHMC.props.url, "surveywindow", "location=1,status=1,scrollbars=1,width=750,height=600");
    if (!surveyq) {
        alert("We have detected a popup blocker.\nPlease disable it and try again.\nThank You.");
    }
    else {
        $('#survey').dialog('close');
        alert("Close Survey");
    }
}

CCHMC.utils.newGuid = function(){

    var g = "";

    if(useSurveyData == 'True')
    {
        for (var i = 0; i < 32; i++) 
            g += Math.floor(Math.random() * 0xF).toString(0xF) + (i == 8 || i == 12 || i == 16 || i == 20 ? "-" : "")
        return g;
    }
    else
    {
        return '000000000-0000-0000-0000-00000000000';     
    }
}

CCHMC.utils.serveSurvey = function(type){
    //alert('test path: ' + CCHMC.props.testPath + ' | path: ' + CCHMC.props.path + ' | type: ' + type);
    var surveyguid = CCHMC.utils.newGuid();
    //alert('GUID: ' + surveyguid);
    
    var surveyPath = 'http://rosetta.checkboxonline.com/Survey.aspx?s=3b27e2664fb341d396f29ebc17577f22&UID=' + surveyguid;
    if (type == 'post') {
        surveyPath = 'http://rosetta.checkboxonline.com/Survey.aspx?s=d3d5c54cae2b49cf91a2ff04baa2987a&UID=' + surveyguid;
    }
    //Make sure they don't see the survey again
    $.cookie("SurveyGuid", surveyguid, {
            expires: CCHMC.props.date,
            path: '/'
        });
    $.cookie('Saw Survey', "yes", {
        expires: CCHMC.props.date,
        path: '/'
    });
   // alert('displaying survey, Saw Survey set to ' + $.cookie('Saw Survey'));
    
    //setup the survey div
    CCHMC.props.survey = '<div id="survey"><div id="surveyContent">' +
    '<iframe width=735 height=775 scrolling=no frameborder=0 src="' +
    surveyPath +
    '"></iframe>'+
    '</div><img src="/images/branding/bg_survey.png"/></div>';
    
    //add survey to doc
    $('body').append(CCHMC.props.survey);
    /*increase the width of the blue stroke here*/
    //set up dialog
    $("#survey").dialog({
        bgiframe: true,
        modal: true,
        resizable: false,
        draggable: false,
        width: 780,
        height: 925
    });
    
    //show survey dialog
    $('#survey').dialog('open');
    
};


