var calendarPopupOpen = false;

function createoverlay(){
    if(typeof document.body.style.maxHeight === "undefined") { // IE6
        $("#overlay").show;
    } else {
        var docHeight = $(document).height();
        $("#overlay")
          .height(docHeight)
          .css({
             'opacity' : 0.4,
             'position': 'fixed',
             'top': 0,
             'left': 0,
             'background-color': 'black',
             'width': '100%',
             'z-index': 100
          });
        $("#overlay").fadeIn(200);
    }
}

function deleteoverlay(){
    if(typeof document.body.style.maxHeight === "undefined") { // IE6
        $("#overlay").hide;
    } else {
        if ( $("#overlay").length > 0 ) {
           $("#overlay").fadeOut(200);
        }
    }
}

function calendarSlider(sKey, sDirection) {
    if (calendarPopupOpen == true) {

        if (sDirection == 'left') {
            $("#sitepopup div.popupContent").animate({marginLeft: '600px'}, 300, "easeOutQuad", function() {
                newcalendarSlide(sKey, sDirection);
            } );
        } else {
            $("#sitepopup div.popupContent").animate({marginLeft: '-600px'}, 300, "easeOutQuad", function() {
                newcalendarSlide(sKey, sDirection);
            } );
        }
    } else {
        newcalendarSlide(sKey);
    }
    return false;
}

function newcalendarSlide(sKey, sDirection) { 
    createoverlay();
    $.getJSON("swis_website/modules/kalender/models/getcalendar.php", { id: sKey }, function(json) {   
        var data = eval(json);
        var text = ""; 

        //vul teksten in
        var text = '<h2>'+ data.datum +'</h2>';
        if (data.titel) text = text + '<h2>'+ data.titel +'</h2>';
        if (data.tekst) text = text + data.tekst;

        $("#sitepopup .tekst").html(text);

        //pas de vorige/volgende links aan
        if (data.sPrevKey!==false) {
            $("a#left").css({'display': 'block'});      
            $("#sitepopup a#left").attr('href', '');
            $("#sitepopup a#left").unbind('click');
            $("#sitepopup a#left").click( function() {
                calendarSlider(data.sPrevKey, 'left');
                return false;
            });
        } else {
            $("a#left").css({'display': 'none'});            
        }

        if (data.sNextKey!==false) {
            $("a#right").css({'display': 'block'});   
            $("#sitepopup a#right").attr('href', '');
            $("#sitepopup a#right").unbind('click');
            $("#sitepopup a#right").click( function() {
                calendarSlider(data.sNextKey, 'right');
                return false;
            });
        } else {
            $("a#right").css({'display': 'none'});
        }

        $("#overlay").click( function() {
            closecalendarSlider();
            return false;
        });        
        
        //open popup als nodig
        if (calendarPopupOpen == false) {
            mouseY = mouseY-220;
            if (mouseY<0) mouseY=0;
            $("#sitepopup").show('normal');
            $("#sitepopup").css({
                'top': (mouseY-120),
                'left': 100
            });               
            calendarPopupOpen = true;
        }

        //animeer alles in en uit beeld
        if (sDirection == 'left') {
            $("#sitepopup div.popupContent").css('margin-left', '-600px');
            $("#sitepopup div.popupContent").animate({marginLeft: '0px'}, 300, "easeOutQuad");
        } else {
            $("#sitepopup div.popupContent").css('margin-left', '600px');
            $("#sitepopup div.popupContent").animate({marginLeft: '0px'}, 300, "easeOutQuad");
        }

        return false;

    } );

}

function closecalendarSlider() {
    deleteoverlay();
    $("#sitepopup").hide('normal');
    $("#sitepopup").removeClass("display");
    calendarPopupOpen = false;
    return false;
}

$(document).ready(function() {
    $("div#SelectcalendarMonthBox").mouseenter(function(){
        $('#calendarMonthBox').slideDown("fast");
    });

    $('#calendarMonthBox').mouseleave(function(){
        $('#calendarMonthBox').slideUp("fast");      
    });
});

var mouseX = 0;
var mouseY = 0;

$().mousemove( function(e) {
   mouseX = e.pageX; 
   mouseY = e.pageY;
 });
