var debug = false;
var tabArr;
var randNum;
var activeTab;
var nextTab;
var homepage_feature_rotator;
var homepage_feature_timer = 7000;

// IndexOf fix for IE
if (!Array.prototype.indexOf) {
    Array.prototype.indexOf = function (searchElement /*, fromIndex */ ) {
        "use strict";
        if (this === void 0 || this === null) {
            throw new TypeError();
        }
        var t = Object(this);
        var len = t.length >>> 0;
        if (len === 0) {
            return -1;
        }
        var n = 0;
        if (arguments.length > 0) {
            n = Number(arguments[1]);
            if (n !== n) { // shortcut for verifying if it's NaN
                n = 0;
            } else if (n !== 0 && n !== Infinity && n !== -Infinity) {
                n = (n > 0 || -1) * Math.floor(Math.abs(n));
            }
        }
        if (n >= len) {
            return -1;
        }
        var k = n >= 0 ? n : Math.max(len - Math.abs(n), 0);
        for (; k < len; k++) {
            if (k in t && t[k] === searchElement) {
                return k;
            }
        }
        return -1;
    }
}

$(document).ready(function () {
    var pipesMain = $("ul#mainItems li:last-child span.pipe");
    pipesMain.css({display: "none" });
    var pipesSecondary = $("ul#secondaryItems li:last-child span.pipe");
    pipesSecondary.css({display: "none" 
    });                    
    //News lists
    $('ul#news li ul.moreNewsLinks li:last-child').addClass('last');
    $('ul.news li ul.moreNewsLinks li:last-child').addClass('last');
    
    //Careers listing
    $('.careerListings div:last-child').addClass('last');
    
    //Case Studies layout
    $('.caseStudiesLayout ul li:last-child').addClass('last');    
    
    //Footer
    $('#footer ul#contactInfo li:last').addClass('last');
    $('#footer ul#mainItems li:last').addClass('last');
    $('#footer ul#secondaryItems li:last').addClass('last');
    
    $('ul#menu').superfish({
        delay:       200,             
        animation:   {height:'show'}, 
        speed:       'fast',          
        autoArrows:  false,           
        dropShadows: false                    
    });    
    
    // Random tab on load
    //randNum = Math.floor(Math.random()*3);
    if (debug)
        console.log("randNum: " + randNum);
    tabArr = ["green","blue","yellow"];
    
    // Rearrange array
    //tabArr.unshift(tabArr[randNum]);
    //tabArr.splice(randNum+1, 1);
    
    if (debug)
        console.log("tabArr: " + tabArr);        
    activeTab = $("#" + tabArr[0]);
    nextTab = 1;
    // Display tabs after determining active tab
    activeTab.addClass("active");
    $(".hompage_feature_tab_list").prepend(activeTab.parent());
    $(".hompage_feature_tab_list").show();
    
    // Display active content and duck
    $(".homepage_feature_content." + tabArr[0]).show();
    $(".homepage_feature_duck." + tabArr[0]).addClass("active");    
    
    // Tab click function    
    $(".homepage_feature_tab_link").click(function() {
        showTabContent($(this), true);
        var thisTab = tabArr.indexOf($(this).attr("id"));
        nextTab = thisTab == 2 ? 0 : thisTab+1;
        if (debug)
            console.log(tabArr.indexOf($(this).attr("id")));
        return false;
    });
    
    // Rotator
    homepageFeatureRotator();    
    /*$(".homepage_feature").mouseover(function() {
        clearInterval(homepage_feature_rotator);
    }).mouseout(function() {
        homepageFeatureRotator();
    });*/
    $(".homepage_feature_tab_link").click(function() {
        clearInterval(homepage_feature_rotator);
        return false;
    });
    
    // Hide expertise overview
    $("#expertise_overview_item").hide();
    $("#expertise").removeAttr("href");
});
function showTabContent(tab, clicked) {
    if (debug)
        console.log("Called!");
    if(jQuery.browser.msie) {
        $(".homepage_feature_tab_link").removeClass("active");
        tab.addClass("active");
        $(".homepage_feature_content").hide();
        $(".homepage_feature_content." + tab.attr("id")).show();
    } else {        
        if (clicked) {
            $(".homepage_feature_tab_link").removeClass("active");
            tab.addClass("active");
        } else {
            $(".homepage_feature_tab_link").removeClass("active", 200);
            tab.addClass("active", 200);
        }
        $(".homepage_feature_content").hide("slow");
        $(".homepage_feature_content." + tab.attr("id")).show("slow");
    }
    $(".homepage_feature_duck").removeClass("active");
    $(".homepage_feature_duck." + tab.attr("id")).addClass("active");    
}
function homepageFeatureRotator() {
    homepage_feature_rotator = setInterval(function(){
        showTabContent($(".homepage_feature_tab_link." + tabArr[nextTab]), false);
        nextTab = nextTab == 2 ? 0 : nextTab+1;
        if (debug)
            console.log("nextTab: " + nextTab);
    }, homepage_feature_timer);
}
