function createCookie(name, value, days) {
    if (days) {
        $.cookie(name, value, {expires: days, path: '/'});
    }else{
        $.cookie(name, value, { path: '/' });
    }
}

function readCookie(name) {
    return $.cookie(name);
}

function eraseCookie(name) {
    createCookie(name, "", -1);
}

function schrijfUitnodiging(uitnodiging, uitnodigingurl, uitnodigingimg, imgwidth, imgheight, noWidth, noHeight, noLeft, noTop, yesWidth, yesHeight, yesTop, yesLeft, closeWidth, closeHeight, closeLeft, closeTop) {



    var popupDiv = $('<div id="enquetePopup">&nbsp;</div>');
    popupDiv.css('position', 'absolute');
    popupDiv.css('z-index', '9999');
    popupDiv.css('padding', '0');
    popupDiv.css('margin', '0');
    popupDiv.offset({ top: $(window).scrollTop() + ($(window).height() / 2 - imgheight / 2), left: $(document).width() / 2 - imgwidth / 2 });
    popupDiv.width(imgwidth);
    popupDiv.height(imgheight);

    var popupImage = $('<img alt="uitnodiging" src="/vpt/images/' + uitnodigingimg + '" />');
    popupImage.css('margin', 0);
    popupImage.css('padding', 0);
    popupImage.css('position', 'absolute');
    popupImage.css('z-index', 0);
    popupImage.offset({ left: 0, top: 0 });

    popupDiv.append(popupImage);
    popupDiv.hide();

    var noButton = $('<img src="/vpt/images/blank.gif"></img>');
    noButton.width(noWidth);
    noButton.height(noHeight);
    noButton.css('cursor', 'pointer');
    noButton.css('position', 'absolute');
    noButton.css('z-index', 1);
    noButton.offset({ left: noLeft, top: noTop });
    noButton.click(function () {
        createCookie(uitnodiging, 'nee', 365);
        closeUitnodiging(uitnodiging);
    });
    popupDiv.append(noButton);

    var yesButton = $('<img src="/vpt/images/blank.gif"></img>');
    yesButton.width(yesWidth);
    yesButton.height(yesHeight);
    yesButton.css('cursor', 'pointer');
    yesButton.css('position', 'absolute');
    yesButton.css('z-index', 1);
    yesButton.offset({ left: yesLeft, top: yesTop });
    yesButton.click(function () {
        closeUitnodiging(uitnodiging, uitnodigingurl);
    });
    popupDiv.append(yesButton);

    var closeButton = $('<img src="/vpt/images/blank.gif"></img>');
    closeButton.width(closeWidth);
    closeButton.height(closeHeight);
    closeButton.css('cursor', 'pointer');
    closeButton.css('position', 'absolute');
    closeButton.offset({ left: closeLeft, top: closeTop });
    closeButton.click(function () {
        createCookie(uitnodiging, 'nee', 365);
        closeUitnodiging(uitnodiging);
    });
    popupDiv.append(closeButton);

    var popupBackground = $('<div id="enqueteBackground">&nbsp;</div>');
    popupBackground.css('background-color', 'black');
    popupBackground.css('position', 'absolute');
    popupBackground.css('z-index', '9998');
    popupBackground.width($(document).width());
    popupBackground.height($(document).height());

    if ($.browser.msie) {
        popupBackground.offset({ top: -2, left: -2 });
    } else {
        popupBackground.offset({ top: 0, left: 0 });
    }
    popupBackground.click(function () {
        createCookie(uitnodiging, 0, 365);
        closeUitnodiging(uitnodiging);
    });
    popupBackground.hide();



    $('body').append(popupBackground);
    $('body').append(popupDiv);

    popupBackground.fadeTo(500, 0.7, function () {
        popupDiv.fadeIn(500);
    });

    setTimeout("makeResizeFunction("+imgwidth+","+imgheight+")",1000);
    _gaq.push(['_trackEvent', 'survey klik', 'openen']);
}

function closeUitnodiging(uitnodiging, url) {
    $('#enquetePopup').fadeOut(function () {
        $('#enqueteBackground').fadeOut();
    });
    if (url != null) {
        window.open(url, "Uitnodiging", "resizable=1,width=806,height=689");
        _gaq.push(['_trackEvent', 'survey klik', 'ja']);
    } else {
        _gaq.push(['_trackEvent', 'survey klik', 'nee']);
    }
}

function sizePopup(imgwidth, imgheight) {

    var popupBackground = $('#enqueteBackground');

    if (popupBackground) {
        popupBackground.width(0);
        popupBackground.height(0);
        popupBackground.width($(document).width());
        popupBackground.height($(document).height());
    }
    var popupDiv = $('#enquetePopup');
    if (popupDiv) {
        popupDiv.offset({ top: $(window).scrollTop() + ($(window).height() / 2 - imgheight / 2), left: $(document).width() / 2 - imgwidth / 2 });
    }
}

$(document).ready(function () {

    //****ENQUETE IS IN MAART 2011 OP VERZOEK VAN KLANT UITGEZET
    return;


    /* invullen van de variabele voor de offerte ? */

    var uitnodiging = 'helpdeskSurvey-2010-11';   // naam van de cookie
    var uitnodigingurl = 'http://questions.netq.nl/nq.cfm?q=662c7914-ec86-418e-9e2e-bba7c81bfd17';   // waar heen na het ja klikken op de popup
    var uitnodigingimg = 'uitnodiging.jpg'; //moet in de images map geplaatst worden
    var imgwidth = 785;
    var imgheight = 498;
    var noWidth = 101;
    var noHeight = 31;
    var noLeft = 70;
    var noTop = 354;
    var yesWidth = 150;
    var yesHeight = 31;
    var yesLeft = 209;
    var yesTop = 354;
    var closeWidth = 33;
    var closeHeight = 33;
    var closeLeft = 747;
    var closeTop = 3;

    currentCookieValue = readCookie(uitnodiging);
    if (currentCookieValue) {

        if (currentCookieValue == "nee") {
            //Gebruiker wil niet meedoen
        } else {
            //Incrementeer het aantal bezochte pagina's en toon de enquete bij 1
            aantal = parseInt(currentCookieValue);
            aantal++;
            createCookie(uitnodiging, aantal, 365);
            if (aantal == 2) {
                schrijfUitnodiging(uitnodiging, uitnodigingurl, uitnodigingimg, imgwidth, imgheight, noWidth, noHeight, noLeft, noTop, yesWidth, yesHeight, yesTop, yesLeft, closeWidth, closeHeight, closeLeft, closeTop);
            }
        }
    } else {
        createCookie(uitnodiging, 0, 365);
    }
    //schrijfUitnodiging(uitnodiging, uitnodigingurl, uitnodigingimg, imgwidth, imgheight, noWidth, noHeight, noLeft, noTop, yesWidth, yesHeight, yesTop, yesLeft, closeWidth, closeHeight, closeLeft, closeTop);

});

function makeResizeFunction(imgwidth, imgheight) {
    $(window).resize(function () {
        sizePopup(imgwidth, imgheight);
    });
    sizePopup(imgwidth, imgheight);
}
