﻿/**
* Version: 1.0 Alpha-1 
* Build Date: 14-Oct-2009
* Copyright (c) 2009, VML. (http://www.vml.com/). All rights reserved.
*/
//Generic tab visibility through javascript
//tabs (anchor tags) containing the string "_tab_" within their id
//are matched to a content pane with a similar id
//of "_tab_content" in place of "_tab_"
//i.e. id=Products_tab_0 would control the div with id=Products_tab_content_0
//When # is seen in the tab's href, enable bookmarking (#"this tab" seen in address bar when clicked)

$(document).ready(function() {

    setTabByHash();

    $("a[id*='_tab_']").each(function(i) {
        //selectedTab
        var prefix = this.id.substring(0, this.id.lastIndexOf('_'));
        var selectedTab = $("a[id^='" + prefix + "'][class=on]");
        //If no tab within this group is selected, select the first one
        if (!selectedTab.html()) {
            $("a[id^='" + prefix + "']:eq(0)").addClass("on");
        }

        //When # is seen in the href, enable bookmarking (#"this tab" seen in address bar when clicked)
        if ($(this).attr("href").indexOf("#") >= 0) {
            var anchorName = "tab=" + this.id;
            $(this).attr("href", "#" + anchorName);
        }

        $(this).click(function() {
            $(this).siblings().removeClass("on");
            $(this).addClass("on");
            refreshTabs();
        });
    });
    refreshTabs();

    window.setInterval(checkTabByHash, 200);

    $("a[id*='spotlight_']").each(function(i) {
        $(this).click(function() {
            $("li[id*='li_spotlight_']").each(function(i) {
                $(this).removeClass("ui-tabs-selected");
            });
            $(this).parent().addClass("ui-tabs-selected");
            $("a[id*='spotlight_']").each(function(i) {
                $(this).removeClass("ui-tabs-selected");
            });
            $(this).addClass("ui-tabs-selected");
            refreshSpotlight();
        });
    });
    refreshSpotlight();
});

function refreshSpotlight() {
    $("a[id*='spotlight_']").each(function() {
        var tabContent = $("#" + this.id.replace("spotlight_", "spotlight_content2_"));
        if (tabContent) {
            if ($(this).hasClass("ui-tabs-selected")) {
                tabContent.show();
            }
            else {
                tabContent.hide();
            }
        }
    });
}

var lastTabHash = null;

function checkTabByHash() {
    if (lastTabHash != window.location.hash) {
        setTabByHash();
        refreshTabs();
    }
    lastTabHash = window.location.hash;
}

function setTabByHash() {
    //alert(window.location.hash);

    // Select the tab in the url #tab=id of tab
    var anchoredTabMatch = window.location.hash.match(/#tab=(.*$)/);
    if (anchoredTabMatch && anchoredTabMatch.length > 1) {
        var anchoredTab = $("#" + anchoredTabMatch[1]);
        if (anchoredTab) {
            anchoredTab.addClass("on");
            anchoredTab.siblings().removeClass("on");
        }
    }
}

function refreshTabs() {
    $("a[id*='_tab_']").each(function() {
        var tabContent = $("#" + this.id.replace("_tab_", "_tab_content_"));
        if (tabContent) {
            if ($(this).hasClass("on")) {
                tabContent.show();
            }
            else {
                tabContent.hide();
            }
        }
    });
}