var popupStatus = 0;

$(document).ready(function() {
 	var cookieValue = $.cookie('POLLCOOKIE_'+currentPoll);
    //Er is geen cookie of hij is niet voor de huidige poll?
    if(cookieValue == null) {
        buildPopup();
        centerPopup();
        loadPopup();
        $("#popupPollClose").click(disablePopup);
        $("#btnStart").click(doPoll);
    }
});

function buildPopup() {
    $('<div id="popupPoll"><a id="popupPollClose">x</a><h1>Wat vindt u van de website? Kunt u gemakkelijk de informatie vinden, die u zoekt?</h1><div id="pollContentContainer"><div id="pollContent"><p>Wij zijn erg benieuwd wat u van onze website vindt. Wilt u ons helpen door drie korte vragen te beantwoorden? Dan maakt u kans op één van de digitale fotolijstjes, die wij verloten.</p><form id="pollForm"><ul><li><input type="radio" name="action" id="action1" value="ja" /><label for="action1">Ja</label></li><li><input type="radio" name="action" id="action2" value="nee" /><label for="action2">Nee</label></li><li><input type="radio" name="action" id="action3" value="misschien" /><label for="action3">Misschien later</label></li></ul><button id="btnStart">doorgaan</button></form></div></div></div>').prependTo('#pageContainer');
}

function centerPopup(){
    var windowWidth = document.documentElement.clientWidth;
    var windowHeight = document.documentElement.clientHeight;
    var popupHeight = $("#popupPoll").height();
    var popupWidth = $("#popupPoll").width();
    //centering
    $("#popupPoll").css({"position": "relative","margin":"10px auto 0px auto"});
    //only need force for IE6
    $("#backgroundPopup").css({"height": windowHeight});
}


function loadPopup(){
    if(popupStatus==0){
        $("#backgroundPopup").css({"opacity": "0.7"});
        $("#backgroundPopup").fadeIn("slow");
        $("#popupPoll").fadeIn("slow");
        popupStatus = 1;
    }
}

function disablePopup(){
    //disables popup only if it is enabled
    if(popupStatus==1){
        $("#backgroundPopup").fadeOut("slow");
        $("#popupPoll").fadeOut("slow");
        popupStatus = 0;
    }
}

function doPoll(e) {
    e.preventDefault();
    var action = $("input[name='action']:checked").val();
    if(action == "nee") { 
        $.cookie('POLLCOOKIE_'+currentPoll,'false',{expires: 365, path: '/'});
        disablePopup();
    } else if(action == "misschien") {
        $.cookie('POLLCOOKIE_'+currentPoll,'false',{expires: 1, path: '/'});
        disablePopup();
    }
    else {
        getPoll();
    }
}

function getPoll() {
    $("#popupPoll h1").remove();
    var pars = {compactDisplay:true,objectId:currentPoll};
    $("#pollContent").html("<img src='/"+siteid+"/images/lightbox/loading.gif' />").load("/poll/",pars);
}

function sendPoll() {
     $.cookie('POLLCOOKIE_'+currentPoll,'true',{expires: 365, path: '/'});
     $.post("/poll/",$("#frmSurveyPoll").serialize(),function(html) { $("#pollContent").html(html); });
     $("#pollContent").html("<img src='/"+siteid+"/images/lightbox/loading.gif' />");
}


