﻿String.prototype.endsWith = function(str)
{return (this.match(str+"$")==str)}

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(){
    var schoolSelected = false;
    
    externalLinks();
    
    $(window).bind("resize", function(){
        alignPage();
    });
    
    /* replace HTML elements */
    if (!$.browser.msie){
        $("div#nav ul li").each(function(){
            var currClass = $(this).attr("class");
            var activePage = (location.href.endsWith($(this).find("a").attr("href")));
            
            if (activePage){
                $("div#nav").append("<a href=\"" + $(this).find("a").attr("href") + "\"><img class=\"" + currClass + "\" alt=\"" + $(this).find("a").text() + "\" src=\"/art/" + currClass + "o.jpg\" /></a>\n");
            }
            else{
                $("div#nav").append("<a href=\"" + $(this).find("a").attr("href") + "\"><img class=\"" + currClass + "\" alt=\"" + $(this).find("a").text() + "\" src=\"/art/" + currClass + ".jpg\" /></a>\n");
                $("div#nav img." + currClass).bind("mouseenter", function(){
                    $(this).attr("src", "/art/" + currClass + "o.jpg");
                }).bind("mouseleave", function(){
                    $(this).attr("src", "/art/" + currClass + ".jpg");
                });
            }
        });
        
        $("div#nav ul").remove();
        
        $("div#content h1").each(function(){
            var headerImage = "/GeneratedImages/header_" + $(this).text().toLowerCase();
            
            while (headerImage.indexOf(" ", 0) > 0){
                headerImage = headerImage.replace(" ", "-");
            }
            while (headerImage.indexOf(":", 0) > 0){
                headerImage = headerImage.replace(":", "-");
            }
            
            $(this).after("<img class=\"header\" alt=\"" + $(this).text() + "\" src=\"" + headerImage + ".jpg\" />");
            $(this).remove();
        });
        
        $("div#info h1").each(function(){
            var infoImage = "/GeneratedImages/info_" + $(this).text().toLowerCase();
            
            while (infoImage.indexOf(" ", 0) > 0){
                infoImage = infoImage.replace(" ", "-");
            }
            
            $(this).after("<img class=\"header\" alt=\"" + $(this).text() + "\" src=\"" + infoImage + ".jpg\" />");
            $(this).remove();
        });
    }
    else
    {        
        $("div#nav a img").each(function(){
            var activePage = (location.href.endsWith($(this).parent().attr("href")));
            
            if (activePage){
                $(this).attr("src", "/art/" + $(this).attr("class") + "o.jpg");
            }
            else{            
                $(this).bind("mouseenter", function(){
                    $(this).attr("src", "/art/" + $(this).attr("class") + "o.jpg");
                }).bind("mouseleave", function(){            
                    $(this).attr("src", "/art/" + $(this).attr("class") + ".jpg");
                });
            }
        });        
    }
    
    $("div#map img.scholen").bind("mouseenter", function(){
        $(this).attr("src", "/art/scholeno.jpg");
    }).bind("mouseleave", function(){
        if (!schoolSelected)
        {
            $(this).attr("src", "/art/scholen.jpg");
        }
    });
    
    $("img#overlayClose").bind("mouseenter", function(){
        $(this).attr("src", "/art/overlay-closeo.jpg");
    }).bind("mouseleave", function(){
        $(this).attr("src", "/art/overlay-close.jpg");
    });
    
    $("div#map img.scholen, #overlayClose").bind("click", function(){
        if(schoolSelected){
            $("div#info").fadeOut("200");
        }
        else{
            $("div#info").fadeIn("200");
        }
        schoolSelected = !schoolSelected;
        
        if ($(this).attr("id") == "overlayClose"){
            $("div#map img.scholen").attr("src", "/art/scholen.jpg");
        }
    });
})

function alignPage(){
    if ($.browser.msie){
        $("div#top, div#main, div#bottom").each(function(){
            $(this).css("left", (($(window).width() - parseInt($("form").css("width").replace("px",""))) / 2));
        });
    }
    
    var topSize = $("#top").height();
    var bottomSize = $("#bottom").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").css(heightType, windowHeight - topSize - bottomSize - 72);
}