
function tabClicked(tabId,selId)
{
    if (TabStrip.TabMap[tabId])
    {
        var p = parseInt(selId.split('$')[1],10);
        TabStrip.TabMap[tabId].selectTab(p);
    }
    else
		alert("The DHtml Page Content has become corrupt, unable to locate tab frame. Please Re-Load Page");
	return false;
}

function TabStrip(id)
{
    TabStrip.TabMap[id] = this;
    this.id = id;
    this.div = getElement(id);
    this.activeTab = -1;
    this.tabList = null;
    this.tabCount = 0;
   	this.titles = new Array();
   	this.actions = new Array();
   	this.loaded = new Array();
   	this.reload = new Array();
   	this.saveState = null;
   	this.expandHeight = -1;
   	if (arguments[1]!=undefined && eval(arguments[2])==true && parseInt(arguments[1]) >= 0)
   	    this.loaded[parseInt(arguments[1])] = arguments[2];
    if (this.div)
        this.initStrip(arguments[1]?arguments[1]:0);
}

TabStrip.TabMap = new Array();

TabStrip.Print = function(tabId)
{
    if (TabStrip.TabMap[tabId])
    {
        TabStrip.TabMap[tabId]._print();
    }
    return false;
}

TabStrip.Refresh = function(tabId,sel)
{
    if (TabStrip.TabMap[tabId])
    {
        TabStrip.TabMap[tabId].selectTab(sel,true);
    }
    return false;
}

TabStrip.ExpandCollapse = function(tabId,expand)
{
    if (TabStrip.TabMap[tabId])
    {
        if (expand)
            TabStrip.TabMap[tabId].expand();
        else
            TabStrip.TabMap[tabId].collapse();
    }
    return false;
}

TabStrip.prototype.showPrint = function()
{
    var prt = document.createElement('DIV');
    prt.className = 'tabPrint';
    prt.innerHTML = '<img class="hotButton" src="/gfx/print.gif" onclick="return TabStrip.Print(\''+this.id+'\')">';
    this.div.insertBefore(prt,this.div.firstChild);
    return this;
}

TabStrip.prototype._print = function()
{
    var div = getElement(this.id+'$'+this.activeTab);
    if (!div)
        return;
        
    var divs = document.getElementsByTagName('DIV');
    var pDiv = document.createElement('DIV');
    pDiv.innerHTML = div.innerHTML;
    pDiv.id = "$TABPRN$"+this.id+'$'+this.activeTab;
    pDiv.onafterprint = printCompleted(pDiv.id);
    for (d = 0; d < divs.length; d++)
    {
        addClassName(divs[d],'noprn');
    }
    
    addClassName(pDiv,'prn');
    document.body.insertBefore(pDiv,document.body.firstChild);
    
    window.print();
    // Let the onafterprint tidy up
}

TabStrip.prototype.setSaveState = function(uri)
{
    this.saveState = uri;
    return this;
}

TabStrip.prototype.setHeight = function(height)
{
	var size = parseInt(height);
	for (loop = 0; loop < this.tabCount; loop++)
	{
	    var div = getElement(this.id+'$'+loop);
		if (div)
		{
			div.style.overflow = 'auto';
			div.style.height = size+'px';
		}
	}
    return this;
}
TabStrip.prototype.collapse = function()
{
    this.expandHeight = -1;
	for (loop = 0; loop < this.tabCount; loop++)
	{
	    var div = getElement(this.id+'$'+loop);
		if (div && getElementHeight(div) > this.expandHeight)
		{
		    div.style.display='none';
		    this.expandHeight = getElementHeight(div);
		}
	}
    return this;
}

TabStrip.prototype.expand = function()
{
    if (this.expandHeight == -1)
        return;
	for (loop = 0; loop < this.tabCount; loop++)
	{
	    var div = getElement(this.id+'$'+loop);
		if (div && getElementHeight(div) > this.expandHeight)
		{
		    div.style.display='block';
		}
	}
	this.expandHeight = -1;
}

TabStrip.prototype.initStrip = function(initialActive)
{
	var divs = this.div.getElementsByTagName('DIV');
	
  	this.activeTab=initialActive==-1?0:(initialActive?initialActive:0);
   	var tabCount = -1;
   	var initialLoad = true;
    for (loop = 0; loop < divs.length; loop++)
    {
        if (divs[loop].tagName=='DIV' && ((' '+divs[loop].className+' ').indexOf(' tabContent ') != -1 || (' '+divs[loop].className+' ').indexOf(' tabContentOpen ') != -1))
        {
            tabCount++;
            if (tabCount == this.activeTab)
                divs[loop].style.display='block';
            else	            
                divs[loop].style.display='none';
            divs[loop].id = this.id+'$'+tabCount;
            this.titles[tabCount] = divs[loop].title;
            divs[loop].title = "";
            if (divs[loop].getAttribute("refresh") && divs[loop].getAttribute("refresh") == 'always')
                this.reload[tabCount] = true;
            else
                this.reload[tabCount] = false;
            if (divs[loop].getAttribute("href"))
            {
                if (divs[loop].getAttribute("type") == "content")
                    this.actions[tabCount] = new Function("asyncSend('#','"+divs[loop].getAttribute("href")+"','"+this.id+'$'+tabCount+"');")
                else if (divs[loop].getAttribute("type") == "click")
                {
                    this.actions[tabCount] = new Function("top.location='"+divs[loop].getAttribute("href")+"';");
                    if (tabCount == this.activeTab)
                        initialLoad = false;
                }
                else
                    this.actions[tabCount] = new Function("top.location='"+divs[loop].getAttribute("href")+"';");
            }
	    }
	}
	this.tabCount = tabCount+1;
    this.buildTabs(this.activeTab);
    if (initialLoad)
        this.selectTab(this.activeTab);
}

TabStrip.prototype.buildTabs = function(initialActive)
{
    this.ul = document.createElement("UL");
    this.ul.className='fileTabs';
    this.ul.zIndex=1;
    for (loop = 0; loop < this.titles.length; loop++)
    {
        this.ul.appendChild(this.makeTabNode(loop,initialActive==loop));
    }
    this.div.insertBefore(this.ul,this.div.firstChild);
    if (getElementTop(this.ul.firstChild) != getElementTop(this.ul.lastChild))
    {
        // Bodge for FF cause the float:left li elements occupy no space
        x = parseInt(getElementTop(this.ul.lastChild)) - parseInt(getElementTop(this.ul.firstChild));
        x = parseInt(getElementHeight(this.ul))+x;
        if (!isIE())
            this.ul.style.height = x+'px';
        this.ul.style.backgroundPosition='left '+(getElementHeight(this.ul)-1)+'px';
    }
}

TabStrip.prototype.makeTabNode = function(sel,active)
{
    var li = document.createElement("LI");
    var ac = null;
    if (active)
    {
        li.className="fileTabActive";
        ac = li;
    }
    else
    {
        ac = document.createElement("A");
        ac.href="#";
        ac.onclick = new Function("return tabClicked('"+this.id+"','"+this.id+"$"+sel+"')");
        li.appendChild(ac);
    }
    ac.innerHTML="<div><span>"+this.titles[sel]+"</span></div>";
    return li;
}

TabStrip.prototype.selectTab = function (sel,forceLoad)
{
    this.deSelectTab(this.activeTab);
    var div = getElement(this.id+'$'+sel);
    if (div)
    {
        if (this.saveState)
        {
            if (div.getAttribute("sel"))
            {
                if (this.saveState.indexOf('?') == -1) 
        			sendAndForget(this.saveState+"?sel="+div.getAttribute("sel"));
        		else
        			sendAndForget(this.saveState+"&sel="+div.getAttribute("sel"));
            }
            else
            {
                if (this.saveState.indexOf('?') == -1) 
        			sendAndForget(this.saveState+"?sel="+sel,"debug");
        		else
        			sendAndForget(this.saveState+"&sel="+sel,"debug");
   			}
		}
        div.style.display='block';
        if (this.actions[sel] && !this.loaded[sel] || forceLoad)
        {
//            alert(this.actions[sel]);
            eval(this.actions[sel]());
            if (this.reload[sel])
                this.loaded[sel] = false;
            else
                this.loaded[sel] = true;
        }
    }
    var li = this.makeTabNode(sel,true);
    this.ul.replaceChild(li,this.ul.childNodes[sel]);
    this.activeTab = sel;
}

TabStrip.prototype.deSelectTab = function (sel)
{
    var div = getElement(this.id+'$'+sel);
    if (div)
    {
        div.style.display='none';
    }
    var li = this.makeTabNode(sel,false);
    try {
    this.ul.replaceChild(li,this.ul.childNodes[sel]);
    } catch (e)
    {
        alert(e);
    }
}

