﻿var SlideTabItem = function(headerId, contentId)
{
    this.Header = document.getElementById(headerId);
    this.Content = document.getElementById(contentId);
    this.Content.style.display = "none";
}

var SlideTabs = function(elementId, defaultTabIndex, useClick, useCookie)
{
    this.Element = document.getElementById(elementId);
    this.DefaultTabIndex = typeof(defaultTabIndex) == 'undefined' ? 0 : parseInt(defaultTabIndex);
    this.UseClick = !!useClick;
    this.UseCookie = !!useCookie;
    
    this.CurrentTab = null;
    this.Tabs = [];
}

SlideTabs.prototype = 
{
    Add : function(item)
    {
        if(this.UseClick)
        {
            item.Header.onclick = this.CreateDelegate(this, this.HeaderEventHander);
        }
        else
        {
            item.Header.onmouseover = this.CreateDelegate(this, this.HeaderEventHander);
        }
        
        item.Index = this.Tabs.length;
        item.Header.SlideTabItem = item;
        this.Tabs.push(item);
    },

    CreateDelegate : function(obj, func)
    {
        return function() {func.apply(obj, arguments);}; 
    },

    HeaderEventHander : function(e)
    {
        if(this.CurrentTab)
        {
            this.CurrentTab.Header.className = this.CurrentTab.Header.className.replace(" Selected", "");
            this.CurrentTab.Content.className = this.CurrentTab.Content.className.replace(" Selected", "");
            this.CurrentTab.Content.style.display = "none";
        }
        
        var target = document.all ? window.event.srcElement : e.target;
        this.Show(target.SlideTabItem.Index);
    },

    Show : function(index)
    {
        if(typeof(index) == 'undefined')
        {
            if(this.UseCookie)
            {
                var cookieIndex = this.GetCookie(this.Element.id);
                if(cookieIndex)
                {
                    index = parseInt(cookieIndex);
                }
            }
        }
        
        if(typeof(index) == 'undefined')
        {
            index = this.DefaultTabIndex;
        }

        if(index < 0 || index > this.Tabs.length - 1)
        {
            index = 0;
        }
        
        var Tab = this.Tabs[index];
        
        if(Tab)
        {
            this.CurrentTab = Tab;
            
            this.CurrentTab.Header.className += " Selected";
            this.CurrentTab.Content.className += " Selected";
            this.CurrentTab.Content.style.display = "block";
            
            if(this.UseCookie)
            {
                this.SetCookie(this.Element.id, index);
            }
        }
    },

    GetCookie : function(sName) 
    {
	    var sCookie = " " + document.cookie;
	    var sSearch = " " + sName + "=";
	    var sStr = null;
	    var iOffset = 0;
	    var iEnd = 0;
	    if (sCookie.length > 0) 
	    {
		    iOffset = sCookie.indexOf(sSearch);
		    if (iOffset != -1) 
		    {
			    iOffset += sSearch.length;
			    iEnd = sCookie.indexOf(";", iOffset)
			    if (iEnd == -1) 
				    iEnd = sCookie.length;
			    sStr = unescape(sCookie.substring(iOffset, iEnd));
		    }
	    }
	    return(sStr);
    },

    SetCookie : function(sName, sVal, iDays, sPath, sDomain, bSecure) 
    {
	    var sExpires;
	    if (iDays)
	    {
		    sExpires = new Date();
		    sExpires.setTime(sExpires.getTime()+(iDays*24*60*60*1000));
	    }
	    document.cookie = sName + "=" + escape(sVal) + ((sExpires) ? "; expires=" + sExpires.toGMTString() : "") + 
		    ((sPath) ? "; path=" + sPath : "") + ((sDomain) ? "; domain=" + sDomain : "") + ((bSecure) ? "; secure" : "");
    	
	    if (document.cookie.length > 0)
		    return true;
    }
};
