﻿/// <reference path="jquery-1.6.1.min.js" />
/// <reference path="jquery.watermark.min.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.options.className = "watermark";
    $.watermark.options.useNative = false;

    //Watermark til søgeboks
    $("#searchBox input").watermark("Søg...");

    //Nyhedsbrev
    $("#newsletter-subscribe-btn").click(function (event) {
        event.preventDefault();
        $("#newsletter-form .message").remove();
        newsletterSubscribe();
    });
    $("input#newsletter-email").watermark("Skriv e-mail-adresse");

    function newsletterSubscribe() {
        var email_regex = /([a-zA-Z0-9_\.\-])+\@(([a-zA-Z0-9\-])+\.)+([a-zA-Z0-9]{2,4})+/;
        var email = $("input#newsletter-email").attr("value");
        if (email_regex.test(email)) {
            $.post("/base/Newsletter/AddEmail.aspx", { email: email }, function (data) {
                var status = $("value", data).text();
                var msg = $("<div class=\"message\">Tak. Din tilmelding er registreret.</div>").hide();
                $("#newsletter-form *:first-child").before(msg);
                msg.show("normal");
                $("#newsletter-form #newsletter-email").hide("fast");
                $("#newsletter-form #newsletter-subscribe-btn").attr("disabled", "true");
                // alert(status);
            }, "xml");
        }
        else {
            var msg = $("<div class=\"message error\">Der er en fejl i din e-mail-adresse. Prøv igen.</div>").hide();
            $("#newsletter-form *:first-child").before(msg);
            msg.show("normal");
        }

    }



    $("#link-print").click(function () {
        window.print();
    });

    if (ie6) {
        IE6Init();
    }
    addImageCaptions();
    processExternalLinks();

    $(window).unload(function () {
        //Reeanble newsletter subscribe button
        $("#newsletter-form #newsletter-subscribe-btn").removeAttr("disabled");
    });
});

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 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");
        }
    });
}
