﻿/// <reference path="jquery-1.3.2.min.js" />
/// <reference path="jquery.watermark.js" />
/// <reference path="library.js" />
var ie6 = false;
var safari = false;



$(function() {
    if (jQuery.browser.msie) {
        if (jQuery.browser.version == 6) {
            ie6 = true;
        }


    }
    //alert(navigator.userAgent);
    if (navigator.userAgent.indexOf("Safari") != -1) {
        //Not for Chrome
        if (navigator.userAgent.indexOf("Chrome") == -1) {
            $("body").addClass("browser-safari");

            //Only Windows
            if (navigator.userAgent.indexOf("Windows") != -1) {
                safari = true;
                $("body").addClass("browser-safari-win");
            }
        }
    }

    //Juster layout
    var sidebarMenuHeight = $("#sidebar .menu").height();
    var contentHeight = $("#content").height();
    if (sidebarMenuHeight > contentHeight) {
        //Menuen til venstre er højere end indholdet til højre. Det går ikke
        setMinHeight("#content", sidebarMenuHeight + 50);

    }
    var twoColLeftHeight = $(".two-col-container .left").height();
    var twoColRightHeight = $(".two-col-container .right").height();
    if (twoColRightHeight > twoColLeftHeight) {
        setMinHeight(".two-col-container", twoColRightHeight + 50);

    }

    //Watermark til søgeboks
    $("#searchBox input").watermark({
        watermarkCssClass: "watermark",
        watermarkText: "Søg..."
    });

    $("#link-print").click(function() {
        window.print();
    });

    menuExtras();

    if (ie6) {
        IE6Init();
    }
    addImageCaptions();
    processExternalLinks();
});

function IE6Init() {
    //<summary>Specielle ting, får at få IE6 til at virke</summary>
    $("#menu ul li:not(.current)").hover(function() {
        $(this).addClass("hover")
    },
        function() {
        $(this).removeClass("hover")
        });
    }

function addImageCaptions()
{
    var images = $(".float-left, .float-right");
    images.each(function() {

        var image = $(this);

        var imageClass = image.attr("class");
        //Billedet kan være viklet ind i span eller lignende.
        //find selve billedet
        if (image.context.nodeName.toUpperCase() != "IMG") {
            image = image.find("img");
        }
        var parentParagraph = image.closest("p");
        //Læg div udenom
        image.wrap("<div class=\"image\"></div>");
        //tilføj class til div og fjern den fra billedet
        image.parent().addClass(imageClass);
        image.removeAttr("class");
        //juster bredde på container
        var parentWidth = image.attr("width");

        if (parentWidth == 0) {
            parentWidth = image.context.clientWidth;
            var test = "test";
        }
        //usandsynlig lav bredde. vent på endelig bredde (kun for IE)
        if (parentWidth > 50) {


            image.parent().css("width", parentWidth);
        }
        else {
            //billedet er ikke indlæst endnu, så vi kender ikke bredden.
            //vent til vi kender den

            image.bind("load", imageResize);
        }
        //Find eksisterende caption - p med class=caption
        var removeParagraph = false;
        var caption = parentParagraph.next(".caption");
        if (caption.length == 0) {
            //ingen caption fundet søg efter næste afsnit
            //caption i samme afsnit
            var spanCaption = parentParagraph.find(".caption");
            caption = spanCaption;
            if (spanCaption.length == 0) {
                var npSpanCaption = parentParagraph.next().find("span.caption:first");
                caption = npSpanCaption;
            }
        }
        image.after("<div class=\"caption\">" + caption.text() + "</div>");
        caption.remove();

        parentParagraph.before(image.parent());

        parentParagraph.remove();
    });
}

function imageResize() 
{
 
    $(this).parent().width($(this).width());
}

function menuExtras() {
    $("#menu ul li.diverse:first").addClass("first").before("<li class=\"seperator\"></li>");
}
function setMinHeight (selector, height)
{
    $(selector).css("min-height", height);
    if(ie6)
    {
        $(selector).css("height", height);
    }
}

function processExternalLinks() {
    //alle links med attributten href, hvor værdien starter med http://
    //dette vil sige alle eksterne links
    $("a[href^=http://]").each(function() {
        var link = $(this);
        var href = link.attr("href");
        //links til den gamle side skal ikke i nyt vindue
        if (!(href.beginsWith("http://www.holocaust.nu") || href.beginsWith("http://holocaust.nu"))) {
            link.attr("target", "_blank");
        }
    });
}