﻿$(document).ready(function() {
    $(".accordion").accordion();
    $(".tabs").tabs();
});

function showSubnav(sender, eventArgs, subnavId) {
    var subnavElement = $('#' + subnavId);
    if (subnavElement.length == 0) return;

    var subnavSource = $('#' + sender.id + '_subnav');
    if (subnavSource.length == 0) return;
    if (subnavSource.html().length == 0) return;

    subnavElement.stop(true, false);
    if (subnavElement.css("display") != "none") subnavElement.hide();

    subnavElement.html(subnavSource.html());

    var position = getPosition(sender);
    position.y += sender.offsetHeight;

    subnavElement.css("display", "none");
    subnavElement.css("left", position.x);
    subnavElement.css("top", position.y);
    subnavElement.css("height", "");

    subnavElement.slideDown("slow");
}

function hideSubnav(sender, eventArgs, subnavId) {
    var subnavElement = $('#' + subnavId);
    if (subnavElement.length == 0) return;

    subnavElement.stop(true, false);
    subnavElement.slideUp("slow");
}

function killHide(sender, eventArgs) {
    $(sender).stop(true, false);
    $(sender).css("height", "");
}

function getPosition(element) {
    var position = {};
    position.x = 0;
    position.y = 0;

    while (element != null) {
        position.x += element.offsetLeft;
        position.y += element.offsetTop;
        element = element.offsetParent;
    }

    return position;
}

function translate(language) {
    var urlRegex = /[&\?]u=(.*?)(?:$|&)/;
    var urlMatches = urlRegex.exec(document.location);

    var url = document.location;

    if (urlMatches && urlMatches[1])
        url = urlMatches[1];

    if (language != 'en')
        url = "http://translate.google.com/translate?u=" + url + "&sl=en&tl=" + language + "&hl=en&ie=UTF-8";

    return url;
}

function setLanguage() {
    var langRegex = /[&\?]tl=(.*?)(?:$|&)/;
    var langMatches = langRegex.exec(document.location);
    var lang = 'en';

    if (langMatches && langMatches[1])
        lang = langMatches[1];

    var langsel = document.getElementById("languageselector");
    if (langsel)
        selectValue(langsel, lang);
}

function selectValue(target, value) {
    for (var i = 0; i < target.options.length; i++) {
        if (target.options[i].value == value) {
            target.selectedIndex = i;
            return;
        }
    }
}

function page_load(sender) {
    if (top.location != location)
        top.location.href = location;

    setLanguage();

    if (typeof(rotatingImageList) !== "undefined") {
        rotatingImageList.NextImage = 0;

        if (rotatingImageList.length > 0)
            setTimeout(fadeImage, 4000);
    }
}

function swapImage() {
    var graphic = $("#graphic");
    graphic.attr("src", rotatingImageList[rotatingImageList.NextImage++]);
    $("#graphiccontainer").animate({ height: graphic.height() }, "fast");
    graphic.fadeIn(1500);

    rotatingImageList.NextImage %= rotatingImageList.length;

    setTimeout(fadeImage, 4000);
}

function fadeImage() {
    var graphic = $("#graphic");
    $("#graphiccontainer").css("height", graphic.height());
    graphic.fadeOut(1500, swapImage);
} 
      

