﻿var mouseX, mouseY;
var showMenu = "";
var hideOnNextClick = false;
var blockFade = false;

$.fn.wait = function(time, type){
    time = time || 2500;
    type = type || "fx";
    return this.queue(type, function() {
        var self = this;
        setTimeout(function() {
            $(self).dequeue();
        }, time);
    });
};

function externalLinks(){
    if (!document.getElementsByTagName) return;
    var anchors = document.getElementsByTagName("a");
    for (var i=0; i<anchors.length; i++){
        var anchor = anchors[i];
        if (anchor.getAttribute("href") &&
            anchor.getAttribute("rel") == "external")
            anchor.target = "_blank";
    }
}

$(document).ready(function() {
    $().mousemove(function(e) {
        mouseX = e.pageX;
        mouseY = e.pageY;
    });

    externalLinks();

    if ($.browser.msie && $.browser.version < 7)
        $("div").each(function() {
            if (parseInt($(this).height()) < parseInt($(this).css("min-height").replace("px", "")))
                $(this).height(parseInt($(this).css("min-height").replace("px", "")));
        });
    $("div#photobook span.photo img.photo").each(function() {
        var prevLink, nextLink, downloadLink;

        prevLink = $("a.prevPic").attr("href");
        nextLink = $("a.nextPic").attr("href");
        downloadLink = $("a.dlPic").attr("href");

        $("a.prevPic").remove();
        $("a.nextPic").remove();
        $("a.dlPic").remove();

        if ($("#sidebar").height() > $("#main").height())
            $("#main").height($("#sidebar").height());

        if ((prevLink != null) && (prevLink != ""))
            $(this).parent().parent().parent().append("<div id=\"previousPicture\"><a href=\"" + prevLink + "#Photo\"><img src=\"" + $("img.photo").attr("src").replace("580x434_", "580x434_prev_") + "\" alt=\"Vorige\" border=\"none\" width=\"64\" height=\"64\" /></a></div>");

        if ((nextLink != null) && (nextLink != ""))
            $(this).parent().parent().parent().append("<div id=\"nextPicture\"><a href=\"" + nextLink + "#Photo\"><img src=\"" + $("img.photo").attr("src").replace("580x434_", "580x434_next_") + "\" alt=\"Volgende\" border=\"none\" width=\"64\" height=\"64\" /></a></div>");

        if ((downloadLink != null) && (downloadLink != ""))
            $(this).parent().parent().parent().append("<div id=\"downloadPicture\"><a href=\"" + downloadLink + "\" target=\"_blank\"><img src=\"" + $("img.photo").attr("src").replace("580x434_", "580x434_download_") + "\" alt=\"Download deze foto\" border=\"none\" width=\"48\" height=\"48\" /></a></div>");

        $("div#photobook span.photo img.photo").each(function() {
            var top = 0, top1, top2, top3;

            if ($.browser.msie)
                if ($.browser.version < 8)
                top = 2
            else
                top = 50
            else if ($.browser.mozilla)
                top = 49
            else if ($.browser.opera)
                top = 50
            else if ($.browser.safari)
                top = 50;

            top1 = (($("div#photobook span.photo img.photo").height() - $("#previousPicture").height()) / 2) + top;
            top2 = (($("div#photobook span.photo img.photo").height() - $("#nextPicture").height()) / 2) + top;

            if ($.browser.msie)
                if ($.browser.version < 8) {
                top1 -= 2;
                top2 -= 2;
            };

            if ($.browser.mozilla)
                top3 = top + 8
            else
                top3 = top + 7;

            if ($.browser.msie)
                if ($.browser.version < 8)
                top = top + 7;
            else
                top = top + 400;

            var left = 52;
            var right1 = $("div#photobook span.photo img.photo").width() - $("#nextPicture").width() - 8 + left;
            var right2 = $("div#photobook span.photo img.photo").width() - $("#downloadPicture").width() - 24 + left;
            $("#previousPicture").css("top", top1);
            $("#previousPicture").css("left", left);
            $("#nextPicture").css("top", top2);
            $("#nextPicture").css("left", right1);
            $("#downloadPicture").css("top", top3);
            $("#downloadPicture").css("left", right2);

            $("div#photobook").bind("mouseenter", function() {
                if (($("#previousPicture").css("display") == "none") ||
                    ($("#nextPicture").css("display") == "none") ||
                    ($("#downloadPicture").css("display") == "none")) {
                    $("#previousPicture").fadeIn("250");
                    $("#nextPicture").fadeIn("250");
                    $("#downloadPicture").fadeIn("250");
                }
            }).bind("mouseleave", function() {
                if ((($("#previousPicture").css("display") == "block") ||
                     ($("#nextPicture").css("display") == "block") ||
                     ($("#downloadPicture").css("display") == "block")) &&
                     (!blockFade)) {
                    $("#previousPicture").wait(400).fadeOut("300");
                    $("#nextPicture").wait(400).fadeOut("300");
                    $("#downloadPicture").wait(400).fadeOut("300");
                }
            });
        });
    });

    alignPage();

    $(window).resize(function() {
        alignPage();
    });

    $(".editPanel").each(function() {
        $(this).css("height", $(this).parent().height() - 14);
    });

    $(document).click(function() {
        if ((showMenu != "") && (hideOnNextClick)) {
            $("#" + showMenu).removeClass("active");
            showMenu = "";
            hideOnNextClick = false;
            $(".menuPanel").queue(function() {
                $(this).animate({ height: "2px" }, 250);
                $(this).dequeue();
            });
            $(".menuPanel").queue(function() {
                $(this).animate({ height: "0px", width: "0px", border: "0px" }, 40);
                $(this).dequeue();
            });
            $(".menuPanel").queue(function() {
                $("#menuList li").remove();
                $(this).dequeue();
            });
        }
        if ((showMenu != "") && (!hideOnNextClick))
            hideOnNextClick = true;
    });
    
    $("div#weatherMap div").click(function() {
        if ($(this).children("div.cityname").text() != "")
        {
            var tmpCityName = $(this).children("div.cityname").text();
            while (tmpCityName.indexOf(" ") > -1)
                tmpCityName = tmpCityName.replace(" ", "-");
            $("div#cityInfo").load("/Includes/Lists/CityInfo.aspx?cityname=" + tmpCityName + " div");
        }
    });
});

function panelMenu(controlParent, menuName, pageID){
    var className = $("#" + controlParent).attr("id").replace("ctl00_", "");
    className = className.substring(0, className.indexOf('_'));
    
    if (showMenu == ""){
        showMenu = controlParent;
        $("#" + controlParent).addClass("active");
        $("#menuList").load("/Includes/Lists/" + menuName + ".aspx?classname=" + className + "&pageid=" + pageID + " #menuItemList li", {},function(){
            $(".menuPanel").css("visibility","visible");
            $(".menuPanel").css("left",(mouseX + 3));
            $(".menuPanel").css("top",mouseY);
            $(".menuPanel").queue(function(){
                $(this).css("height","2px");
                $(this).css("border","solid 1px #000000");
                if (($(document).width()) < mouseX + 183)
                    $(this).animate({left:(mouseX - 183) + "px",height:"4px",width:"180px"}, 50)
                else
                    $(this).animate({height:"4",width:"180px"}, 50);
                
                $(this).dequeue();
            });
            $(".menuPanel").queue(function(){
                var i = $("#menuList li").length;
                if (i > 0)
                    if (($(window).height()) < mouseY + (i * 18))
                        $(this).animate({top:(mouseY - (i * 18)) + "px",height:(i * 18) + "px"}, 250)
                    else
                        $(this).animate({height:(i * 18) + "px"}, 250);
                $(this).dequeue();
            });
        });
    }
}

function alignPage(){
    var topSize, bottomSize, heightType, windowHeight;
    
    topSize = $("#title").height() + $("#navbar").height() + $(".topbar").height();
    bottomSize = $("#footer").height();
    
    if ($.browser.opera && $.browser.version > "9.5")
        windowHeight = document.documentElement["clientHeight"]
    else
        windowHeight = $(window).height();
    
    if ($.browser.msie && $.browser.version < 7)
        heightType = "height"
    else
        heightType = "min-height";
    
    $("#main, #vrule, #sidebar").css(heightType, windowHeight - topSize - bottomSize);
        
    if ($("#main").height() > $("#sidebar").height())
        $("#sidebar").height($("#main").height());
    if ($("#sidebar").height() > $("#main").height())
        $("#main").height($("#sidebar").height());
        
    $("#vrule").height($("#main").height());
    
    if ($.browser.msie && $.browser.version < 7){
        $("#website").css("margin-left", (($(window).width() - $("#website").width()) / 2));
    }
}

function showCities(size){
    if (size == "small")
    {
        $("div#weatherMap div.small").css("display", "none");
        $("div#weatherMap div.medium").css("display", "none");
    }
    if (size == "medium")
    {
        $("div#weatherMap div.small").css("display", "none");
        $("div#weatherMap div.medium").css("display", "block");
    }
    if (size == "big")
    {
        $("div#weatherMap div.small").css("display", "block");
        $("div#weatherMap div.medium").css("display", "block");
    }
}

function linkFadeIn(sender){
    //TODO: Menu code
}

function editParagraph(sender){
    var cnt = $("#" + sender).parent().parent().children(".editContent");
    cnt.css("background-color", "#dddddd");
    cnt.find("h1").css("display", "none");
    cnt.append($("div#articleEditorForm"));
    $("#articleEditorForm").css("display", "inline-block");
}