﻿/// <reference path="jquery-1.3.1-vsdoc.js" />

$(document).ready(function() {

    // Mouse-over effect for the top menu except menu items that does not contain text
    var wasBlack = false;
    $("#TopMenu ul li").hover(function() {
        if (!$(this).hasClass("selected")) {
            var link = $(this).children("a");
            if (link.text() != "")
                $(this).css("background-image", "url(/Images/MenuItemSelected.jpg)");
        }
    }, function() {
        if (!$(this).hasClass("selected")) {
            var link = $(this).children("a");
            if (link.text() != "")
                $(this).css("background-image", "url(/Images/MenuItem.jpg)");
        }
    });

    // move the language menu item on spot to the left in the case where there is a top menu item with no text
    var lastLiLink = $("#TopMenu ul li.last a");
    var prevLiLink = lastLiLink.parent("li").prev("li").children("a");
    if (prevLiLink.text() == "" || prevLiLink.text() == " ") {
        lastLiLink.insertBefore(prevLiLink);
        prevLiLink.remove();
    }    

    // We need to set the position of the box with jQuery, since it would be hidden in IE6 if we did it through the stylesheet for some weird reason.
    // Furthermore we we make the box visible to avoid flickering when the page is loading
    var box = $(".FrontpageImageBox")
    box.css("position", "absolute");
    box.css("visibility", "visible");

    /* Stuff to make the frontpage image box transparent in a smooth way.
    The basic idea is that we create a lot of div elements that each has a certain width 
    and an opcacity value to create a flowing transparent effect */
    var boxInner = $(".FrontpageImageBoxInner");
    var boxContent = $(".FrontpageImageBoxContent");
    var boxLink = $(".FrontpageImageBoxLink");

    boxInner.height((boxContent.innerHeight() + boxLink.innerHeight()) + "px");

    var divFirst = document.createElement("div");
    $(divFirst).attr("class", "firstOpacity");
    $(divFirst).css("height", boxContent.innerHeight() + "px");
    $(divFirst).css("width", boxLink.innerWidth() + "px");

    boxInner.prepend(divFirst);

    var totalWidth = $(divFirst).innerWidth();
    var counter = 2;
    var multiplier = 5;
    var i = 98;
    while (i > 80) {
        var div = document.createElement("div");
        $(div).attr("class", "opacity");
        $(div).css("left", totalWidth);
        $(div).css("height", boxContent.innerHeight() + "px");
        $(div).css("width", (counter + multiplier) + "px");
        $(div).css("opacity", "0." + i);
        $(div).css("filter", "alpha(opacity = " + i + ")");
        totalWidth = totalWidth + (counter + multiplier);
        i = i - counter;
        boxContent.before(div);
    }

    var divLast = document.createElement("div");
    $(divLast).attr("class", "lastOpacity");
    $(divLast).css("height", boxContent.innerHeight() + "px");
    $(divLast).css("width", (boxInner.innerWidth() - totalWidth) + "px");

    boxContent.before(divLast);

    // Replace all occurences of CO2 with CO2    
    function doCO2Replace(elem) {
        var element = $(elem);
        var replaceString = "CO<sub>2</sub>";
        if (element.html() != null) {
            var html = $(element).html();
            var replacedHtml = html.replace(/CO2/gi, replaceString);
            $(element).html(replacedHtml);
        }
    }

    //doCO2Replace($("#Content"));
    //doCO2Replace($(".FrontpageInfoBox"));

    // The search function
    // the prefix variable is declared in the masterpage to be able to use server side tags
    function doSearch(value) {
        if (value != '') {
            if (prefix == '/en/')
                window.location = prefix + 'search.aspx?q=' + value;
            else
                window.location = prefix + 'søgning.aspx?q=' + value;
        }
    }

    // Search button actions - remember to place the search field immediately before the search button!
    $(".SearchButton").click(function() {
        var searchField = $(this).prev();
        doSearch(searchField.val());
    });

    // Check for keypress in the search field
    $(".SearchField").keypress(function(e) {
        var keyCode = null;

        if (e.which)
            keyCode = e.which;
        else if (e.keyCode)
            keyCode = e.keyCode;
        if (13 == keyCode) {
            doSearch($(this).val());
            return false;
        }
    });

    // Set up anti-spam on email links
    var links = document.getElementsByTagName("a");
    for (var i = 0; i < links.length; i++) {
        var element = links[i];
        if (element.href.search('mailto') != -1) {
            element.href = "javascript:em('" + element.innerHTML + "')";
            element.innerHTML = element.innerHTML.replace(/---/, '@');

        }
    }
});

// Function to call on email link click to replace the fake email with a real one
// It must be called outside the scope of jQuery's document ready function
function em(str) {
    location.href = "mailto:" + str.replace(/---/ig, "@");
}