﻿var popupStatus = 0;
var allvars;


//loading popup with jQuery magic!
function loadPopup() {
    //loads popup only if it is disabled
    if (popupStatus == 0) {
        $("#popupContact").fadeIn("slow");
        popupStatus = 1;
    }
}

//disabling popup with jQuery magic!
function disablePopup() {
    //disables popup only if it is enabled
    if (popupStatus == 1) {
        $("#popupContact").fadeOut("slow");
        popupStatus = 0;
    }
}

function getY(oElement) {
    var iReturnValue = 0;
    while (oElement != null) {
        iReturnValue += oElement.offsetTop;
        oElement = oElement.offsetParent;
    }
    return iReturnValue;
}

function getX(oElement) {
    var iReturnValue = 0;
    while (oElement != null) {
        iReturnValue += oElement.offsetLeft;
        oElement = oElement.offsetParent;
    }
    return iReturnValue;
}

//positioning popup
function positionPopup() {
    //request data for centering
    var windowWidth = document.documentElement.clientWidth;
    var windowHeight = document.documentElement.clientHeight;
    var popupHeight = $("#popupContact").height();
    var popupWidth = $("#popupContact").width();

    var o = document.getElementById("btnimg");
    var posY = getY(o);
    var posX = getX(o);
    //positioning
    $("#popupContact").css({
        "position": "absolute",
        "top": posY - popupHeight - 25,
        "left": posX - popupWidth + 97
    });
    //only need force for IE6


}

//CONTROLLING EVENTS IN jQuery
$(document).ready(function() {
    //init vars
    allvars = new AllVars();
    allvars.initVars();

    //LOADING POPUP
    //Click the button event!
    $("#popupbutton").click(function() {
        //centering with css
        positionPopup();
        //load popup
        loadPopup();
    });

    //CLOSING POPUP
    //Click the x event!
    $("#popupContactClose").click(function() {
        disablePopup();
    });
    //Press Escape event!
    $(document).keypress(function(e) {
        if (e.keyCode == 27 && popupStatus == 1) {
            disablePopup();
        }
    });

    //type selectors events
    $("#exhibit_typ").click(function() {
        var classname = $(this).attr("class");
        swapTypeClass($(this), classname);
        allvars.toggleTypeValue("1");
        centralProc();
    });

    $("#workSem_typ").click(function() {
        var classname = $(this).attr("class");
        swapTypeClass($(this), classname);
        allvars.toggleTypeValue("2");
        centralProc();
    });

    $("#conference_typ").click(function() {
        var classname = $(this).attr("class");
        swapTypeClass($(this), classname);
        allvars.toggleTypeValue("3");
        centralProc();
    });

    $("#others_typ").click(function() {
        var classname = $(this).attr("class");
        swapTypeClass($(this), classname);
        allvars.toggleTypeValue("4");
        centralProc();
    });

    for (var row = 0; row < 5; row++) {
        for (var col = 0; col < 7; col++) {
            var strid = "#c" + col + "r" + row + "DateID";

            $(strid).click(function() {
                var classname = $(this).attr("class");
                var idstr = $(this).attr("id");
                var c = idstr.substring(1, 2);
                var r = idstr.substring(3, 4);
                swapDateClass($(this), classname);
                allvars.toggleDateArr(r, c);
                centralProc();
            });
        }
    }

});

function centralProc() {
    var markers = markerSet.getMarkers();
    //remove trails
    if (currTrail != null)currTrail.removeTrail();


    for (var i = 0; i < markers.length; i++) {
        var marker = markers[i];
        marker.removeMarker();
    }
    var drr = allvars.getDateArr();
    var tagarr = allvars.getTypeValues();
    
    for (var i = 0; i < markers.length; i++) {
        var marker = markers[i];
        marker.toggleMarker(drr,tagarr);
    }

    thismap.map.setCenter(new GLatLng(1.28953, 103.84503), 13);


    
}



function swapTypeClass(elem, str) {
    if (str == "typeSelectorOn") {
        elem.removeClass("typeSelectorOn");
        elem.addClass("typeSelectorOff");
    } else {
        elem.removeClass("typeSelectorOff");
        elem.addClass("typeSelectorOn");

    }
}

function swapDateClass(elem, str) {
    if (str == "dateSelectorOn") {
        elem.removeClass("dateSelectorOn");
        elem.addClass("dateSelectorOff");
    } else {
    elem.removeClass("dateSelectorOff");
        elem.addClass("dateSelectorOn");

    }
}
