﻿var mouseX, mouseY;
var imgSrc = "";
var objTemp;

$.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();
    alignPage();
    photoalbumOverlay();

    $(window).resize(function() {
        alignPage();
    });

    $("#viewstateLength").text("Length of view state: " + $("#__VIEWSTATE").attr("value").length);

    $("div.inputLeft, div.inputRight").click(function() {
        $(this).parent().children("input").focus();
    });

    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", "")));
        });
});

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 uploadImage() {
    if (imgSrc == "") {
        imgSrc = $("input#avSrc").attr("value");
        $(window).bind("focus", function() {
            $("img#userpic").attr("src", imgSrc + "?t=" + (new Date()).getTime());
        });
    }
    window.open('/Includes/Lists/Upload.aspx', 'fileupload', 'width=360,height=280,location=yes,status=no,menubar=no,directories=no,toolbar=no,resizable=no,scrollbars=no');
}

function photoalbumOverlay() {
    if ($.browser.msie){
        $("div.photoalbum").css("cursor", "pointer");
        $("div.photoalbum").each(function() {
            $(this).bind("mouseenter", function() {
                objTemp = $(this).children("div.photoalbumImage").children("img");
                objTemp.attr("src", objTemp.attr("src").replace(".jpg", "-" + $("body").attr("class") + ".jpg"));
            }).bind("mouseleave", function() {
                objTemp = $(this).children("div.photoalbumImage").children("img");
                objTemp.attr("src", objTemp.attr("src").replace("-" + $("body").attr("class") + ".jpg", ".jpg"));
            }).bind("click", function() {
                location = $("div.photoalbumName a").attr("href");
            });;
        });
    }
    else
    {
        $("div.photoalbumName a").each(function() {
            $(this).bind("mouseenter", function() {
                objTemp = $(this).parent().parent().children("div.photoalbumImage").children("img");
                objTemp.attr("src", objTemp.attr("src").replace(".jpg", "-" + $("body").attr("class") + ".jpg"));
            }).bind("mouseleave", function() {
                objTemp = $(this).parent().parent().children("div.photoalbumImage").children("img");
                objTemp.attr("src", objTemp.attr("src").replace("-" + $("body").attr("class") + ".jpg", ".jpg"));
            });
        });
    }

    var photoDescription = $("div.photoDescription div span").text();
    var photoPrev = $("div#photoPrev a").attr("title");
    var photoNext = $("div#photoNext a").attr("title");
    
    $("div#photoPrev a").removeAttr("title");
    $("div#photoNext a").removeAttr("title");
    
    $("div#photoPrev a").css("display", "none");
    $("div#photoNext a").css("display", "none");
    $("div#photoFull a").css("display", "none");
    
    if(!$.browser.msie){
        $("div#photoPrev a").css("opacity", 0.5);
        $("div#photoNext a").css("opacity", 0.5);
        $("div#photoFull a").css("opacity", 0.5);
    }
    
    $("div#photo").bind("mouseenter", function(){
        $("div#photoPrev a").css("display", "block");
        $("div#photoNext a").css("display", "block");
        $("div#photoFull a").css("display", "block");
    }).bind("mouseleave", function(){
        $("div#photoPrev a").css("display", "none");
        $("div#photoNext a").css("display", "none");
        $("div#photoFull a").css("display", "none");
    });
        
    if(!$.browser.msie){
        $("div#photoPrev a").bind("mouseenter", function(){
            $("div.photoDescription div span").text(photoPrev);
            $("div#photoPrev a").css("opacity", 1.0);
        }).bind("mouseleave", function(){
            $("div.photoDescription div span").text(photoDescription);
            $("div#photoPrev a").css("opacity", 0.5);
        });
        
        $("div#photoNext a").bind("mouseenter", function(){
            $("div.photoDescription div span").text(photoNext);
            $("div#photoNext a").css("opacity", 1.0);
        }).bind("mouseleave", function(){
            $("div.photoDescription div span").text(photoDescription);
            $("div#photoNext a").css("opacity", 0.5);
        });
        
        $("div#photoFull a").bind("mouseenter", function(){
            $("div.photoDescription div span").text('Download deze foto');
            $("div#photoFull a").css("opacity", 1.0);
        }).bind("mouseleave", function(){
            $("div.photoDescription div span").text(photoDescription);
            $("div#photoFull a").css("opacity", 0.5);
        });
    }
    else{    
        $("div#photoPrev a").bind("mouseenter", function(){
            $("div.photoDescription div span").text(photoPrev);
        }).bind("mouseleave", function(){
            $("div.photoDescription div span").text(photoDescription);
        });
        
        $("div#photoNext a").bind("mouseenter", function(){
            $("div.photoDescription div span").text(photoNext);
        }).bind("mouseleave", function(){
            $("div.photoDescription div span").text(photoDescription);
        });
        
        $("div#photoFull a").bind("mouseenter", function(){
            $("div.photoDescription div span").text('Download deze foto');
        }).bind("mouseleave", function(){
            $("div.photoDescription div span").text(photoDescription);
        });
    }
}