﻿/// <reference path="jquery-1.4.1-vsdoc.js" />
/// <reference path="cufon-yui.js" />


function refreshCufon() {
    Cufon.replace(".DIN", { fontFamily: "DIN", hover: true });
    Cufon.now();
}

function initSearchBox() {
    // setup handlers for text fields, init fields
    $(".jToggleInput")
        .each(function() {
            if ($(this).val() == $(this).attr("title"))
                $(this).addClass("Blur");
            else if ($(this).val() == "") {
                $(this).addClass("Blur");
                $(this).val($(this).attr("title"));
            }
            else
                $(this).removeClass("Blur");
        })
        .focus(function() {
            if ($(this).val() == $(this).attr("title")) {
                $(this).val("");
                $(this).removeClass("Blur");
            }
        })
        .blur(function() {
            if ($(this).val() == "") {
                $(this).val($(this).attr("title"));
                $(this).addClass("Blur");
            }
        });
    // setup dropdown handler too
    $(".jToggleSelect")
        .each(function() {
            if ($(this).val() == "")
                $(this).addClass("Blur");
            else
                $(this).removeClass("Blur");
        })
        .change(function() {
            if ($(this).val() == "")
                $(this).addClass("Blur");
            else
                $(this).removeClass("Blur");
        });
}

function extractUrl(input) {
    // remove quotes and wrapping url()
    return input.replace(/"/g, "").replace(/url\(|\)$/ig, "");
}

var loadedImages = []; // this must be outside any function to remain in scope all the time
function addImageToCache(image) {
    var cacheImage = document.createElement('img');
    cacheImage.src = image;
    loadedImages.push(cacheImage);
}

function initRollOvers() {
    $(".jRollOver img, img.jRollOver, input[type='image'].jRollOver").each(function() {
        // don't do rollovers for IE6 if image is PNG
        if ($.browser.msie && $.browser.version.substr(0, 1) < 7 && $(this).hasClass("jRolloverPng"))
            return;

        var normalSrc = $(this).attr("src");
        var hoverSrc = normalSrc.replace("_Normal", "_Over");

        // preload rollovers
        addImageToCache(hoverSrc);

        // setup rollover
        $(this).hover(function() {
            $(this).attr("src", hoverSrc);
        },
        function() {
            // don't un-hover 'Acativated' items
            if ($(this).hasClass("Activated"))
                return;
            $(this).attr("src", normalSrc);
        });
    });
    // preload CSS images
    $(".jRollOverBackground").each(function() {
        var normalSrc = extractUrl($(this).css("background-image"));
        var hoverSrc = normalSrc.replace("_Normal", "_Over");
        if (normalSrc != hoverSrc)
            addImageToCache(hoverSrc);
    });
}

function initMenu() {
    $("#Menu .Item").each(function() {
        var menu = $(this);
        var submenu = $(".DropDownMenu", menu);
        var overMenu = false;
        // animation on button
        $(".Base", menu).hover(function() {
            menu.addClass("Hover");
            // hide other menus
            $("#Menu .DropDownMenu").stop(false, true).hide();
            // show this
            overMenu = true;
            submenu.stop(false, true).show();
        },
        function() {
            menu.removeClass("Hover");
            overMenu = false;
            // hide menu after a moment
            submenu.stop(false, true).animate({ top: 139 }, 700, function() {
                if (!overMenu)
                    submenu.hide();
            });
        });
        // animation on menu
        submenu.hover(function() {
            overMenu = true;
            submenu.stop(false, true).show();
        },
        function() {
            overMenu = false;
            submenu.stop(false, true).animate({ top: 139 }, 400, function() {
                if (!overMenu)
                    submenu.hide();
            });
        });
    });
}

$(function() {
    refreshCufon();
    initSearchBox();
    initRollOvers();
    initMenu();

    // hack external links,
    // we cannot have target="_blank" in xhtml strict, so ensure all external links here open in a new window
    $('a[rel=external]').click(function() {
        window.open(this.href);
        return false;
    });
});