//tab effects
var timer;
var target;

var TabbedContent = {
    cookiename: '',
    init: function (cookiename) {
        this.cookiename = cookiename;
        $(".tab_item").mouseover(function (event) {
            target = $(this);
            timer = setTimeout("TabbedContent.runAnim(300)", 500);
        });
        $(".tab_item").click(function () {
            target = $(this);
            clearTimeout(timer);
            TabbedContent.runAnim(300);
        });
        $(".tab_item").mouseout(function () {
            clearTimeout(timer);
        });

        var hash = $.cookie(cookiename) ? $.cookie(this.cookiename).substr(1).toLowerCase() : '';

        //if(window.location.hash.substr(1))
        //    hash = window.location.hash.substr(1).toLowerCase();

        if (hash) {
            var obj = $('#' + this.cookiename);
            var tabs = obj.find('.tab_item').each(function (i) {
                if (hash == $(this).text().toLowerCase()) {
                    // we got a winner, pass it on
                    target = $(this);
                    TabbedContent.runAnim(0);
                }
            });
        }
    },

    runAnim: function (duration) {
        // set the cookie for this id
        var obj = $('#' + this.cookiename);
        //window.location.hash = '#' + target.text();
        $.cookie(this.cookiename, '#' + target.text());

        var margin = obj.find(".slide_content").width();
        margin = 0 - (margin * (target.prevAll().size() - 1));

        obj.find(".moving_bg").stop().animate({ left: target.position()['left'] }, { duration: duration });
        obj.find(".tabslider").stop().animate({ marginLeft: margin + "px" }, { duration: duration });
    }
}

$(document).ready(function () {
    TabbedContent.init('tabNirvana1');
});

