﻿//create a new cookie
function createCookie(name, value, days) {
    if (days) {
        var date = new Date();
        date.setTime(date.getTime() + (days * 24 * 60 * 60 * 1000));
        var expires = "; expires=" + date.toGMTString();
    }
    else var expires = "";
    document.cookie = name + "=" + value + expires + "; path=/";
}

//create a new cookie which will expire in minutes
function createCookieMin(name, value, min) {
    if (min) {
        var date = new Date();
        date.setTime(date.getTime() + (min * 1000));
        var expires = "; expires=" + date.toGMTString();
    }
    else var expires = "";
    document.cookie = name + "=" + value + expires + "; path=/";
}

//read a cookie value
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;
}

//load the survey if the visitor is the first time on the site
function LoadSurveyIfFirstTimeVisitor() {
    /* 
     * Uber nasty hack to get the survey iframe display correctly on the different browsers
     * If the survey changes this values may not be good
     */
    var fancyWidth = 718, fancyHeight = 384;
    if ( $.browser.msie && $.browser.version == 7 ) {
      fancyWidth = 722;
      fancyHeight = 424;
    } else if ( $.browser.msie && $.browser.version == 8 ) {
      fancyWidth = 722;
      fancyHeight = 424;
    } else if ( $.browser.msie && $.browser.version == 9 ) {
      fancyWidth = 730;
      fancyHeight = 410;
    } else if ( $.browser.webkit ) {
      fancyHeight = 390;
    }
    //read the cookie to see if the visitor already received the survey
    var cookieVisitorHasReceivedSurvey = readCookie("VisitorHasReceivedSurvey");
  
    //if the cookie is not yet initiated and the cookie is not true
    if (cookieVisitorHasReceivedSurvey == null || cookieVisitorHasReceivedSurvey != "1") {
        //extract date from cookie
        var cookie = readCookie("FirstVisitTime");
        var cookieDate = new Date(cookie);
        //execute the script survey
        function getSecondsDifferenceSinceCallback(diff) {
            $("a#fancybox").fancybox({
                'autoDimensions': false,
                'autoScale': false,
                'height': fancyHeight,
                'transitionIn': 'none',
                'transitionOut': 'none',
                'type': 'iframe',
                'width': fancyWidth
            });
            $("a#fancybox").trigger('click');
        }

        //calculate the time, refresh each second until the time is fitting with timeout
        function getSecondsDifferenceSince(cookieDate, timeOut) {
            var interval = setInterval(function() {
                var date = new Date(),
                nTotalDiff = cookieDate.getTime() - date.getTime(),
                diff = -(nTotalDiff);
                if (diff >= timeOut) {
                    //clear interval so it won't be executes after timeout
                    clearInterval(interval);
                    //create cookie for the visitor, it will expire 365 days
                    createCookie("VisitorHasReceivedSurvey", "1", 365);
                    //call the script
                    getSecondsDifferenceSinceCallback(diff);
                }
                //execute every second
            }, 1000);
        }

        //execute until 90 seconds 
        getSecondsDifferenceSince(cookieDate, 90000);
    }
}
