﻿/* Tab scripts
--------------------------  */
var intialtab = true;
function initialisetabs() {
  
    // Get alll list items and process those marked as tabs
    var ularr = document.getElementsByTagName("UL");
    // Tab cookie
    var tabcookie = readCookie("sniffatabs");
    var jsontabs = tabcookie ? JSON.parse(tabcookie) : {};

    for (var i = 0; i < ularr.length; i++) {
        if (ularr[i].className == "sniffatabs") {
            //Go through each li and set styles, set visible content and apply scripts
            var liarr = ularr[i].getElementsByTagName("LI");
            
            for (var j = 0; j < liarr.length; j++) {
                // hide the h1 tags
                document.getElementById(liarr[j].id.replace("li", "")).getElementsByTagName("H1")[0].className = "hidden";
                // If not the first item then hide the content
                if (j > 0) {
                    document.getElementById(liarr[j].id.replace("li", "")).className = "hidden";
                }
                else {
                    // Set first item as selected
                    liarr[j].className = "selected";
                    ularr[i].setAttribute("current", liarr[j].id);
                    document.getElementById(liarr[j].id.replace("li", "")).className = "";
                }
                // Add the onclick scripts
                if (isIE) {
                    liarr[j].attachEvent("onclick", selecttab);
                }
                else {
                    liarr[j].setAttribute("onclick", "selecttab(this);");
                }
                // hide the anchor links (ie 8 has a problem with the positioning)
                document.getElementById(liarr[j].id.replace("li", "a")).className = "hidden";
                liarr[j].firstChild.setAttribute("href", "javascript:;");
            }
            
            // Set selected tab
            for(key in jsontabs){ 
                if(key == ularr[i].id) {
                    selecttab(document.getElementById(jsontabs[key]), true);
                }
            } 

        }
    }
    // Check for latest videos link
    if (window.location.href.indexOf("#latestvideos") > -1) {
        selecttab(document.getElementById("lilatestvideos"));
    }
    /*
    var hasharr = window.location.href.split("#");
    if (hasharr.length == 2) {
        if (document.getElementById("li" + hasharr[1])) {
            selecttab(document.getElementById("li" + hasharr[1]));
        }
    }
    */
}

function selecttab(o, fromcookie) {

    if (o.srcElement) {
        o = o.srcElement;
    }
    
    while (o.tagName != "LI") {
        o = o.parentNode;
    }
    
    var ul = o;
    while (ul.tagName != "UL") {
        ul = ul.parentNode;
    }
    
    // Don't process the current tab
    if (ul.getAttribute("current") == o.id) {
        return false;
    }
    
    // Hide currently selected
    document.getElementById(ul.getAttribute("current")).className = "";
    document.getElementById(ul.getAttribute("current").replace("li", "")).className = "hidden";

    // Show newly selected
    o.className = "selected";
    document.getElementById(o.id.replace("li", "")).className = "";

    // Set Selected
    ul.setAttribute("current", o.id);
    
    // Set selected tab
    if (!fromcookie) {
        // Tab cookie
        var tabcookie = readCookie("sniffatabs");
        var jsontabs = tabcookie ? JSON.parse(tabcookie) : {};
        var found = false;
        var key;
        for(key in jsontabs){
            if(key == ul.id) {
                jsontabs[key] = o.id;
                found = true;
                break;
            }
        }
        var jsonstring;
        if (!found) {
            if (key != null) {
                jsonstring = JSON.stringify(jsontabs).replace("}", ",\"" + ul.id + "\":\""+ o.id + "\"}");
            }
            else {
                jsonstring = "{\"" + ul.id + "\":\""+ o.id + "\"}";
            }
        }
        else {
            jsonstring = JSON.stringify(jsontabs);
        }
        // Set the cookie value
        createCookie("sniffatabs", jsonstring, 0);
    }

    // Analytics
    if (!intialtab) {
        if (window.pageTracker != undefined) {
            pageTracker._trackPageview("Tab Page " + o.firstChild.innerHTML); 
        }
    }
    intialtab = false;
}

//onloadAdd('initialisetabs()');
