﻿google.load("jquery", "1.3.2");
google.load("jqueryui", "1.7.2");
google.load("swfobject", "2.2");

google.setOnLoadCallback(function() {
    $(document).ready(function() {
        init();
    });
});

var currentScrollLoc;

function init() {
    //$("#curtain").fadeOut(300);
    killAnchors();
    rigNavigation();
    rigTables();
    rigGalleryOverlay();
    rigTabs();
    findCountry();
}

function findCountry() {
    var countryCode = "GB";
    var cl = google.loader.ClientLocation;
    if (cl) {
        countryCode = cl.ClientLocation.address.country_code;
    }
}

function killAnchors() {
    $("a[href='#']").click(function() { return false });
}

function rigTables() {
    $("tr:nth-child(odd)").not(".break").addClass("odd");
}

function rigTable(what) {
    $(what).find("tr:nth-child(odd)").not(".break").addClass("odd");
}

function rigGalleryOverlay() {
    $(".galleryOverlay").click(function() { alert(this.attr("href")); return null; });
}

function rigTabs() {
    var tabGroups = $(".tabGroup");
    for (var i = 0; i < tabGroups.length; i++) {
        $(tabGroups[i]).find(".tabs > li:first").addClass("current");
        $(tabGroups[i]).children(".tabContent").hide();
        $(tabGroups[i]).children(".tabContent:first").show();
        $(tabGroups[i]).find(".tabs li a").click(function() { showTab($(this)); });
    }

}

function showTab(what) {
    $(what).closest(".tabs").find("li").removeClass("current");
    $(what).closest("li").addClass("current");
    var index = $(what).closest(".tabs").children("li").index($(what).closest("li"));
    var contentBlocks = $(what).closest(".tabGroup").children(".tabContent").hide();
    $(contentBlocks[index]).show();
}

function showOverlay() {
    currentScrollLoc = $("body").scrollTop();

    $("body").removeClass("printScope");
    $("html, body").animate({ scrollTop: 0 }, 100);

    $("#overlay .buttons").wrapInner('<div class="innerButtons"></div>');
    $("#overlay .buttons").wrapInner('<div class="wrapper"></div>');

    $("#overlay").show(500, function() { fitOverlay() }).addClass("printScope");
    $("#overlay .closeButton").click(function() { hideOverlay(); });
}

function hideOverlay() {
    $("body").addClass("printScope");
    $("#overlay").hide(500).removeClass("printScope").empty();

    $("html, body").animate({ scrollTop: currentScrollLoc }, 100);
}

function fitOverlay() {
    $("#overlay").css("min-height", $(document).height());
}

function rigNavigation() {
    $(".navigationBar li").hover(function() { showChildren(this) }, function() { hideChildren(this) });
}

function showChildren(what) {

    var timer = 250;
    if ($.browser.msie && $.browser.version.substr(0, 1) < 7) {
        $(" > ul", what).height($(" > ul", what).height());
        $(" > li", what).css({ 'margin-top': -6 });
        $(" > ul", what).show();
        $(" > ul", what).width($(" > ul", what).width());
    }
    else {
        if ($(" > ul", what).parents('ul').size() >= 2) {
            $(" > ul", what).show('slide', { direction: 'left', easing: 'swing' }, timer);
        }
        else {
            //$(" > ul", what).parents('ul > li').addClass('current');
            $(" > ul", what).show('slide', { direction: 'up', easing: 'swing' }, timer);
        }

    }
}

function hideChildren(what) {
    if ($(what).parents('ul').size() == 1) {
        $(".navigationBar li").find('ul').hide();
    }
    else {
        $(what).find('ul:visible').hide();
    }
}