﻿/***************************/
//@Author: Adrian "yEnS" Mato Gondelle
//@website: www.yensdesign.com
//@email: yensamg@gmail.com
//@license: Feel free to use it, but keep this credits please!					
/***************************/

//SETTING UP OUR POPUP
//0 means disabled; 1 means enabled;
var popupStatus = 0;
var mouseX = 0;
var mouseY = 0;
var bOffPage = false;





//loading popup with jQuery magic!
function loadPopupExitQuestion() {
    //loads popup only if it is disabled
    if (popupStatus == 0) {
        $("#backgroundPopup").css({
            "opacity": "0.7"
        });

        $("#backgroundPopup").fadeIn("slow");
        $("#exitQuestion").fadeIn("slow");
        popupStatus = 1;
    }
}




function loadPopupNotUk() {
    //loads popup only if it is disabled
    if (popupStatus == 0) {
        $("#backgroundPopup").css({
            "opacity": "0.7"
        });
        $("#backgroundPopup").fadeIn("slow");
        $("#notUk").fadeIn("slow");
        popupStatus = 1;
    }
}


// exit question popup

function loadPopupExperience() {
    //loads popup only if it is disabled
    if (popupStatus == 0) {
        $("#backgroundPopupExperience").css({
            "opacity": "0.7"
        });
        $("#backgroundPopupExperience").fadeIn("slow");
        $("#exitQuestion").fadeIn("slow");
        popupStatus = 1;
    }
}


function loadPopupCountryDetector() {
    //loads popup only if it is disabled
    if (popupStatus == 0) {
        $("#countryDetectorbg").css({
            "opacity": "0.7"
        });
        $("#countryDetectorbg").fadeIn("slow");
        $("#countryDetector").fadeIn("slow");
        popupStatus = 1;
    }
}





























//loading popup with jQuery magic!
function loadPopup(){
	//loads popup only if it is disabled
	if(popupStatus==0){
		$("#backgroundPopup").css({
			"opacity": "0.7"
});

if ($("#popupContact").html().length > 0) {

    $("#backgroundPopup").fadeIn("slow");
    $("#popupContact").fadeIn("slow");
    popupStatus = 1;
}
	}
}

//disabling popup with jQuery magic!
function disablePopup(){
	//disables popup only if it is enabled
	if(popupStatus==1){
		$("#backgroundPopup").fadeOut("slow");
		$("#popupContact").fadeOut("slow");
		popupStatus = 0;
	}
}


//disabling popup with jQuery magic!
function disablePopupExperience() {
    //disables popup only if it is enabled
    if (popupStatus == 1) {
        $("#backgroundPopup").fadeOut("slow");
        $("#exitQuestion").fadeOut("slow");
        popupStatus = 0;
    }
}






//centering popup
function centerPopupExperience(){
	//request data for centering
	var windowWidth = document.documentElement.clientWidth;
	var windowHeight = document.documentElement.clientHeight;
	var popupHeight = $("#exitQuestion").height();
	var popupWidth = $("#exitQuestion").width();
	//centering
	$("#exitQuestion").css({
		"position": "absolute",
		"top": windowHeight/2-popupHeight/2,
		"left": windowWidth/2-popupWidth/2
	});
	//only need force for IE6
	
	$("#backgroundPopup").css({
		"height": windowHeight
	});

}


//centering popup
function centerPopupExitQuestion() {
    //request data for centering
    var windowWidth = document.documentElement.clientWidth;
    var windowHeight = document.documentElement.clientHeight;
    var popupHeight = $("#exitQuestion").height();
    var popupWidth = $("#exitQuestion").width();
    //centering
    $("#exitQuestion").css({
        "position": "absolute",
        "top": windowHeight / 2 - popupHeight / 2,
        "left": windowWidth / 2 - popupWidth / 2
    });
    //only need force for IE6

    $("#backgroundPopupExit").css({
        "height": windowHeight
    });

}











//centering popup
function centerPopup() {
    //request data for centering
    var windowWidth = document.documentElement.clientWidth;
    var windowHeight = document.documentElement.clientHeight;
    var popupHeight = $("#popupContact").height();
    var popupWidth = $("#popupContact").width();
    //centering
    $("#popupContact").css({
        "position": "absolute",
        "top": windowHeight / 2 - popupHeight / 2,
        "left": windowWidth / 2 - popupWidth / 2
    });
    //only need force for IE6

    $("#backgroundPopup").css({
        "height": windowHeight
    });

}












//Get Survey

function getDefaultSurvey(key) {

    $.post(
        "/ajax/ajax_GetSurvey.aspx?" + rnd(),
        { param_key: key },
        function(data) {
            //alert(data);
            //getQuestion(data, key);
            $("#popupContact").html(data);
            centerPopup();
            //load popup
            loadPopup();

            setAnswerClicks();
        });

    }

    function getQuestion(id, key) {
        //alert(id + " : " + key);
        $.post(
        "/ajax/ajax_GetSurvey.aspx?"+rnd(),
        { param_key: key,
            param_id: id
        },
        function(data) {
            //alert(data)
            $("#popupContact").html(data);

            centerPopup();
            //load popup
            loadPopup();
            
            setAnswerClicks();

        });
    }


    function rnd() { return String((new Date()).getTime()).replace(/\D/gi, '') }

//Send Answers by Ajax
function sendAnswer(answer) {

    $.post(
        "/ajax/ajax_AnswerSurvey.aspx?" + rnd(),
        { param_answer: answer },
        function(data) {
        var pathname = window.location.pathname;
            if (pathname == "/" || pathname== "/default.aspx") {
                if (data.indexOf(".aspx") > 0) {

                    if (data.indexOf("-1") > 0) {
                        url = data.replace("-1", "");
                        window.location = url;
                    } else {
                        url = data.replace("0", "");
                        window.location = url;
                    }

                }
            }
            if (data == "0" || data == "-1") {
                disablePopup();
            }
            else {

                /*var answerData = answer.split("_");

                var id = answerData[0].replace("answer", "");
                var key = answerData[1];


                setUnload(key, id);*/
                //alert("setUnload(" + key + ", " + id + ")");
                /*$("#popupContact").fadeOut("slow");
                getQuestion(id, key)
                $("#popupContact").fadeIn("slow");*/

                disablePopup();

            }



        }
        );


    }
    
    

    function setAnswerClicks() {
        $("a.answer").click(function() {

            //alert('send');
            sendAnswer($(this).attr('id'));

        });
    }

    function setUnload(key, id) {

        //alert('setting unload. key: ' + key + ' - id: ' + id);
        window.onbeforeunload = function() {
        //alert('hey again');
            if (bOffPage) {

                //hopefully this  means the user is navigating away from the site

                getQuestion(id, key);

                return ("Before you go we have one more question to ask you. Please press cancel to answer our question.");
            }

        }
    }


    function doUnload() {

        var evtSource;

        if (document.all) // If IE
        {

            if (window.event.clientX < 0 && window.event.clientY < 0) {
                alert("Window is closed.");
            }

        }
        else // For Firefox
        {

            if (e.clientX < 0 && e.clientY < 0) {
                alert("Window is closed.");
            }

        }
}

//CONTROLLING EVENTS IN jQuery
    $(document).ready(function() {

        /*window.onunload = function() {
        doUnload();
        };*/

        /*$(window).bind('beforeunload', function(e) {
        getQuestion(5, "test");
        return false;
        });*/

        $(document).bind("mouseleave", function(e) {
            //console.log(e.pageY);
            if (e.pageY - $(window).scrollTop() <= 1) {

                bOffPage = true;

            }
        });

        $(document).bind("mouseenter", function(e) {
            //console.log(e.pageY);
            if (e.pageY - $(window).scrollTop() > 1) {

                bOffPage = false;

            }
        });



        /* $("html").mouseout(function(e) {
        alert('hi');
        });*/

        /*$(document).mousemove(function(e) {
        mouseX = e.pageX;
        mouseY = e.pageY;
        });*/


        //LOADING POPUP
        //Click the button event!
        $("#button").click(function() {
            //centering with css
            centerPopup();
            //load popup
            loadPopup();
        });

        //CLOSING POPUP
        //Click the x event!
        $("#popupContactClose").click(function() {
            /*disablePopup();*/
        });
        //Click out event!
        $("#backgroundPopup").click(function() {
            /*disablePopup();*/
        });


        //Get Set All Answers Click Events



        //Answer Yes
        $("#answerYes").click(function() {
            sendAnswer('yes');
            disablePopup();
        });
        //Answer No
        $("#answerNo").click(function() {
            sendAnswer('no');
            disablePopup();
        });


        //Press Escape event!
        $(document).keypress(function(e) {
            if (e.keyCode == 27 && popupStatus == 1) {
                disablePopup();
            }
        });

       

    });
