

TabStripX = function(container, cssClass, clientId, copy, parent)
{
    this.Container = container;
    container.TabStrip = this;
    this.Tabs = new Array();
    this.TabsLength = 0;
    this.TabIndex = -1;
    this.CssClass = cssClass;
    this.normalScrollSpeed = 10;
    this.fastScrollSpeed = 30;
    this.scrollSpeed = this.normalScrollSpeed;
    this.rememberSelectedTab = false;
    this.initialSelection = false;
    this.clientId = clientId;
    this.cookieKey = clientId + "_" + document.location.pathname;
    this.parent = parent;
    
    if (copy) this.copy = new TabStripX(document.getElementById(container.id + "Mirror"), cssClass, clientId + "Mirror", false, this);
    
    var table = document.createElement("TABLE");
    table.className = this.CssClass;
    this.Container.appendChild(table);
    
    var tableTr = table.insertRow(0);
    
    var tableTdNav = tableTr.insertCell(0)
    tableTdNav.className = "navigationLeft";

    var tableTdTabs = tableTr.insertCell(1);
    tableTdTabs.className = "tabsList";
    
    var tableTdNav2 = tableTr.insertCell(2)
    tableTdNav2.className = "navigationRight";
    
    var divNavLeft = document.createElement("DIV"); divNavLeft.className = "left"; tableTdNav.appendChild(divNavLeft);
    var divNavRight = document.createElement("DIV"); divNavRight.className = "right"; tableTdNav2.appendChild(divNavRight);

    this.TabScrollLeftBox = tableTdNav;
    this.TabScrollRightBox = tableTdNav2;
    
    var divNavLeftA = document.createElement("A"); divNavLeft.appendChild(divNavLeftA); divNavLeftA.innerHTML = "<img src=/img/tabarrow-l.gif />";
    divNavLeftA.TabStrip = this;
    divNavLeftA.onmouseover = function() { this.TabStrip.ScrollRight(true); }
    divNavLeftA.onmouseout = function() { this.TabStrip.ScrollRight(false); }
    divNavLeftA.onmousedown = function() { this.TabStrip.setScrollSpeed(true); }
    divNavLeftA.onmouseup = function() { this.TabStrip.setScrollSpeed(false); }
    
    var divNavRightA = document.createElement("A"); divNavRight.appendChild(divNavRightA); divNavRightA.innerHTML = "<img src=/img/tabarrow-r.gif />";
    divNavRightA.TabStrip = this;
    divNavRightA.onmouseover = function() { this.TabStrip.ScrollLeft(true); }
    divNavRightA.onmouseout = function() { this.TabStrip.ScrollLeft(false); }
    divNavRightA.onmousedown = function(ignore) { this.TabStrip.setScrollSpeed(true); }
    divNavRightA.onmouseup = function(ignore) { this.TabStrip.setScrollSpeed(false); }
    
    this.TabStripClip = document.createElement("DIV");
    this.TabStripClip.className = "clip";
    tableTdTabs.appendChild(this.TabStripClip);
    
    this.TabStripContainer = document.createElement("DIV");
    this.TabStripContainer.className = "container";
    this.TabStripClip.appendChild(this.TabStripContainer);
    
    this.TabStrip = document.createElement("DIV");
    this.TabStripContainer.appendChild(this.TabStrip);
    
    this.TabScrollLeft = divNavLeftA;
    this.TabScrollRight = divNavRightA;
    this.HoverRight = false;
    this.HoverLeft = false;
    this.Initialized = false;
    var self = this;
    
    
    //Place Holder for hooking
    this.onchange = function(id) { return; }
    this.oneditbefore = function(id) { return true; }
    this.oneditcomplete = function(id, text) { return; }
    this.onmenushow = function(id) { return; }
    this.onmenuhide = function(id){ return; }
    
    this.setScrollSpeed = function(fast, ignore)
    {
        if (this.copy && !ignore) this.copy.setScrollSpeed(fast, true);
        if (this.parent && !ignore) this.parent.setScrollSpeed(fast, true);
        
        this.scrollSpeed = (fast ? this.fastScrollSpeed : this.normalScrollSpeed);
    }
    
    this.MenuShow = function(id, ignore)
    {
        if (this.copy && !ignore) this.copy.MenuShow(id, true);
        if (this.parent && !ignore) this.parent.MenuShow(id, true);
        
        onmenushow(id);
    }
    this.MenuHide = function(id, ignore)
    {
        if (this.copy && !ignore) this.copy.MenuHide(id, true);
        if (this.parent && !ignore) this.parent.MenuHide(id, true);
        
        onmenuhide(id);
    }
       
    this.ScrollLeft = function(over, ignore)
    {
        if (this.copy && !ignore) this.copy.ScrollLeft(over, true);
        if (this.parent && !ignore) this.parent.ScrollLeft(over, true);

        this.HoverLeft = over;
        this.Scroll();
    }
    
    this.ScrollRight = function(over, ignore)
    {
        if (this.copy && !ignore) this.copy.ScrollRight(over, true);
        if (this.parent && !ignore) this.parent.ScrollRight(over, true);
        
        this.HoverRight = over;
        this.Scroll();
    }
    
    this.GetTabsWidth = function()
    {    
        var node = this.TabStrip.childNodes[this.TabStrip.childNodes.length - 1];
        return node.offsetLeft + node.offsetWidth;
    }

    this.GetTabWidth = function(index)
    {
        if (index >= this.TabStrip.childNodes.length) return 0;
        if (index < 0) return 0;
        
        var node = this.TabStrip.childNodes[index];
        return node.offsetLeft + node.offsetWidth;
    }
    
    
    this.Scroll = function(ignore)
    {
        //if (this.copy && !ignore) this.copy.Scroll(true);
        //if (this.parent && !ignore) this.parent.Scroll(true);
        
    
        var value = 0;
        if (this.HoverLeft)
            value = -this.scrollSpeed;
        else if (this.HoverRight)
            value = this.scrollSpeed;
        else
            return;
            
        var x = this.TabStripContainer.offsetLeft;
        var w = this.GetTabsWidth();
        var xw = this.TabStripClip.offsetWidth;
                
        x += value;
        if (x > 0) x = 0;
        
        
        if (x + w < xw) x = xw - w;
                
        this.TabStripContainer.style.left = x + "px";
                
        setTimeout(function () { self.Scroll(); }, 50);
    }
    
    this.CheckScrolling = function(ignore)
    {

        var w = self.GetTabsWidth();
        var xw = self.TabStripClip.offsetWidth;

        var enable = (w > xw);
        
        self.TabScrollLeftBox.style.visibility = enable ? "" : "hidden";
        self.TabScrollRightBox.style.visibility = enable ? "" : "hidden";
        
        if (!enable) self.TabStripContainer.style.left = "0px";
        
        if (!self.initialSelection)
        {
            self.initialSelection = true;
            if (self.rememberSelectedTab)
            {
                var val = SimpleGetCookie(self.cookieKey);
                try
                {
                    val = parseInt(val);
                    var tabid = self.Tabs[val].ID;
                
                    self.SelectTab(tabid);
                } catch (e)
                {
                    self.SelectTab(self.Tabs[0].ID);
                }
            } else {
                self.SelectTab(self.Tabs[0].ID);
            }
        }
    }
    
    
    this.ToString = function()
    {
        return this.Container.innerHTML;
    }

    
    this.AddTab = function(text, id, cssClass, selectedCssClass, hoverCssClass)
    {
        if (this.copy) this.copy.AddTab(text, id, cssClass, selectedCssClass, hoverCssClass);
    
        var newTab = new Object();
        
        newTab.ID = id;
        newTab.TabIndex = this.TabsLength;
        newTab.Text = text;
        newTab.CssClass = cssClass;
        newTab.SelectedCssClass = selectedCssClass;
        newTab.HoverCssClass = hoverCssClass;
        
	var tabbg = document.createElement("SPAN");
        var tab = document.createElement("SPAN");
        var anchor = document.createElement("A");
        
        tabbg.className = "tabbg";
        anchor.innerHTML = newTab.Text;
        tab.className = newTab.CssClass;
        newTab.Tab = anchor;
        anchor.Tab = newTab;
        anchor.TabStrip = this;
        
        
        anchor.onmouseover = function()
        {
            if (this.TabStrip.TabIndex == this.Tab.TabIndex) return;
            this.parentNode.className = this.Tab.HoverCssClass;
        }
        anchor.onmouseout = function()
        {
            if (this.TabStrip.TabIndex == this.Tab.TabIndex) return;
            this.parentNode.className = this.Tab.CssClass;
        }
        anchor.onclick = function()
        {
            this.TabStrip.SelectTab(this.Tab.ID);
        }
        
	tab.appendChild(tabbg);
        tabbg.appendChild(anchor);
        
        this.TabStrip.appendChild(tab);
        
        this.Tabs[this.TabsLength] = newTab;
        
        this.TabsLength++;
        
        if (this.Initialized) this.CheckScrolling();
        
        return newTab;
    }
    
    this.SelectTab = function(id, ignore)
    {
        if (this.copy && !ignore) this.copy.SelectTab(id, true);
        if (this.parent && !ignore) this.parent.SelectTab(id, true);
    
        for (var i = 0; i < this.TabsLength; i++)
        {
            var tab = this.Tabs[i];
            
            if (tab.ID == id)
            {
                tab.Tab.parentNode.className = tab.SelectedCssClass;
                this.TabIndex = i;
                
                if (this.rememberSelectedTab)
                {
                    SimpleSetCookie(this.cookieKey, this.TabIndex);
                }
                
                //Move to position
                var w = this.GetTabsWidth();
                var x = this.TabStripContainer.offsetLeft;
                var xxw = this.GetTabWidth(i);
                var xw = this.TabStripClip.offsetWidth;
                
                if (x + xxw > xw)
                {
                    x = xw - xxw;
                    this.TabStripContainer.style.left = x + "px";
                }
            }
            else
                if (tab.Tab.parentNode.className != tab.CssClass) tab.Tab.parentNode.className = tab.CssClass;
        }
        
        self.onchange(id);
    }
    
    this.GetTab = function(id)
    {
        var index = this.GetTabIndex(id);
        if (index == -1) return null;
        
        return this.Tabs[index];
    }
    
    this.GetTabIndex = function(id)
    {
        for (var i = 0; i < this.TabsLength; i++)
        {
            if (this.Tabs[i].ID == id) return i;
        }
        
        return -1;
    }
    this.Initialize = function()
    {
        this.Initialized = true;
        setTimeout(this.CheckScrolling, 100);

	if (this.copy) setTimeout(this.copy.CheckScrolling, 100);
	if (this.parent) setTimeout(this.parent.CheckScrolling, 100);
    }

    if (window.addEventListener)
    {
        window.addEventListener("resize", this.CheckScrolling, false);
    }
    else
    {
        window.attachEvent("onresize", this.CheckScrolling);
    }
        
    return this;
}


















TabStripY = function(container, cssClass, clientId)
{
    this.Container = container;
    container.TabStrip = this;
    this.Tabs = new Array();
    this.TabsLength = 0;
    this.TabIndex = -1;
    this.CssClass = cssClass;
    this.normalScrollSpeed = 10;
    this.fastScrollSpeed = 30;
    this.scrollSpeed = this.normalScrollSpeed;
    this.rememberSelectedTab = false;
    this.initialSelection = false;
    this.clientId = clientId;
    this.cookieKey = clientId + "_" + document.location.pathname;
    
    var table = document.createElement("TABLE");
    table.className = this.CssClass;
    this.Container.appendChild(table);
    
    var tableTr = table.insertRow(0);
    
    var tableTdNav = tableTr.insertCell(0)
    tableTdNav.className = "navigationLeft";
    
    tableTr = table.insertRow(0);

    var tableTdTabs = tableTr.insertCell(0);
    tableTdTabs.className = "tabsList";
    
    tableTr = table.insertRow(0);
    
    var tableTdNav2 = tableTr.insertCell(0)
    tableTdNav2.className = "navigationRight";
    
    var divNavLeft = document.createElement("DIV"); divNavLeft.className = "left"; tableTdNav.appendChild(divNavLeft);
    var divNavRight = document.createElement("DIV"); divNavRight.className = "right"; tableTdNav2.appendChild(divNavRight);

    this.TabScrollLeftBox = tableTdNav;
    this.TabScrollRightBox = tableTdNav2;
    
    var divNavLeftA = document.createElement("A"); divNavLeft.appendChild(divNavLeftA); divNavLeftA.innerHTML = "<img src=/cms/img/tabarrow-b.gif />";
    divNavLeftA.TabStrip = this;
    divNavLeftA.onmouseover = function() { this.TabStrip.ScrollLeft(true); }
    divNavLeftA.onmouseout = function() { this.TabStrip.ScrollLeft(false); }
    divNavLeftA.onmousedown = function() { this.TabStrip.scrollSpeed = this.TabStrip.fastScrollSpeed; }
    divNavLeftA.onmouseup = function() { this.TabStrip.scrollSpeed = this.TabStrip.normalScrollSpeed; }
    
    var divNavRightA = document.createElement("A"); divNavRight.appendChild(divNavRightA); divNavRightA.innerHTML = "<img src=/cms/img/tabarrow-t.gif />";
    divNavRightA.TabStrip = this;
    divNavRightA.onmouseover = function() { this.TabStrip.ScrollRight(true); }
    divNavRightA.onmouseout = function() { this.TabStrip.ScrollRight(false); }
    divNavRightA.onmousedown = function() { this.TabStrip.scrollSpeed = this.TabStrip.fastScrollSpeed; }
    divNavRightA.onmouseup = function() { this.TabStrip.scrollSpeed = this.TabStrip.normalScrollSpeed; }
    
    this.TabStripClip = document.createElement("DIV");
    this.TabStripClip.className = "clip";
    tableTdTabs.appendChild(this.TabStripClip);
    
    this.TabStripContainer = document.createElement("DIV");
    this.TabStripContainer.className = "container";
    this.TabStripClip.appendChild(this.TabStripContainer);
    
    this.TabStrip = document.createElement("DIV");
    this.TabStripContainer.appendChild(this.TabStrip);
    
    this.TabScrollLeft = divNavLeftA;
    this.TabScrollRight = divNavRightA;
    this.HoverRight = false;
    this.HoverLeft = false;
    this.Initialized = false;
    var self = this;
    
    
    
    //Place Holder for hooking
    this.onchange = function(id) { return; }
    this.onmenushow = function(id) { return; }
    this.onmenuhide = function(id){ return; }
    
    
    this.MenuShow = function(id)
    {
        onmenushow(id);
    }
    this.MenuHide = function(id)
    {
        onmenuhide(id);
    }

    this.ScrollLeft = function(over)
    {
        this.HoverLeft = over;
        this.Scroll();
    }
    
    this.ScrollRight = function(over)
    {
        this.HoverRight = over;
        this.Scroll();
    }
    
    this.GetTabsWidth = function()
    {    
        var node = this.TabStrip.childNodes[this.TabStrip.childNodes.length - 1];
        return node.offsetTop + node.offsetHeight;
    }

    this.GetTabWidth = function(index)
    {
        if (index >= this.TabStrip.childNodes.length) return 0;
        if (index < 0) return 0;
        
        var node = this.TabStrip.childNodes[index];
        return node.offsetTop + node.offsetHeight;
    }
    
    
    this.Scroll = function()
    {
        var value = 0;
        if (this.HoverLeft)
            value = -this.scrollSpeed;
        else if (this.HoverRight)
            value = this.scrollSpeed;
        else
            return;
            
        var x = this.TabStripContainer.offsetTop;
        var w = this.GetTabsWidth();
        var xw = this.TabStripClip.offsetHeight;
                
        x += value;
        if (x > 0) x = 0;
        
        
        if (x + w < xw) x = xw - w;
                
        this.TabStripContainer.style.top = x + "px";
                
        setTimeout(function () { self.Scroll(); }, 50);
    }
    
    this.CheckScrolling = function()
    {
        var w = self.GetTabsWidth();
        var xw = self.TabStripClip.offsetHeight;

        var enable = (w > xw);
        
        self.TabScrollLeftBox.style.visibility = enable ? "" : "hidden";
        self.TabScrollRightBox.style.visibility = enable ? "" : "hidden";
        
        if (!enable) self.TabStripContainer.style.top = "0px";
        
        if (!self.initialSelection)
        {
            self.initialSelection = true;
            if (self.rememberSelectedTab)
            {
                var val = SimpleGetCookie(self.cookieKey);
                try
                {
                    val = parseInt(val);
                    var tabid = self.Tabs[val].ID;
                
                    self.SelectTab(tabid);
                } catch (e) { self.SelectTab(self.Tabs[0].ID); }
            } else {
                self.SelectTab(self.Tabs[0].ID);
            }
        }
    }
    
    
    this.ToString = function()
    {
        return this.Container.innerHTML;
    }

    
    this.AddTab = function(text, id, cssClass, selectedCssClass, hoverCssClass)
    {
        var newTab = new Object();
        
        newTab.ID = id;
        newTab.TabIndex = this.TabsLength;
        newTab.Text = text;
        newTab.CssClass = cssClass;
        newTab.SelectedCssClass = selectedCssClass;
        newTab.HoverCssClass = hoverCssClass;
        
	var tabbg = document.createElement("SPAN");
        var tab = document.createElement("SPAN");
        var anchor = document.createElement("A");
        
        tabbg.className = "tabbg";

        anchor.innerHTML = newTab.Text;
        tab.className = newTab.CssClass;
        newTab.Tab = anchor;
        anchor.Tab = newTab;
        anchor.TabStrip = this;
                        
        anchor.onmouseover = function()
        {
            if (this.TabStrip.TabIndex == this.Tab.TabIndex) return;
            this.parentNode.className = this.Tab.HoverCssClass;
        }
        anchor.onmouseout = function()
        {
            if (this.TabStrip.TabIndex == this.Tab.TabIndex) return;
            this.parentNode.className = this.Tab.CssClass;
        }
        anchor.onclick = function()
        {
            this.TabStrip.SelectTab(this.Tab.ID);
        }
        
	tab.appendChild(tabbg);
        tabbg.appendChild(anchor);
        
        this.TabStrip.appendChild(tab);
        
        this.Tabs[this.TabsLength] = newTab;
        
        this.TabsLength++;
        
        if (this.Initialized) this.CheckScrolling();
        
        return newTab;
    }
    
    this.SelectTab = function(id)
    {
        for (var i = 0; i < this.TabsLength; i++)
        {
            var tab = this.Tabs[i];
            
            if (tab.ID == id)
            {
                tab.Tab.parentNode.className = tab.SelectedCssClass;
                this.TabIndex = i;
                
                if (this.rememberSelectedTab)
                {
                    SimpleSetCookie(this.cookieKey, this.TabIndex);
                }
                
                //Move to position
                var w = this.GetTabsWidth();
                var x = this.TabStripContainer.offsetTop;
                var xxw = this.GetTabWidth(i);
                var xw = this.TabStripClip.offsetHeight;
                
                if (x + xxw > xw)
                {
                    x = xw - xxw;
                    this.TabStripContainer.style.top = x + "px";
                }
            }
            else
                if (tab.Tab.parentNode.className != tab.CssClass) tab.Tab.parentNode.className = tab.CssClass;
        }
        
        self.onchange(id);
    }
    
    this.GetTab = function(id)
    {
        var index = this.GetTabIndex(id);
        if (index == -1) return null;
        
        return this.Tabs[index];
    }
    
    this.GetTabIndex = function(id)
    {
        for (var i = 0; i < this.TabsLength; i++)
        {
            if (this.Tabs[i].ID == id) return i;
        }
        
        return -1;
    }
    this.Initialize = function()
    {
        this.Initialized = true;
        setTimeout(this.CheckScrolling, 100);
        
    }

    if (window.addEventListener)
    {
        window.addEventListener("resize", this.CheckScrolling, false);
    }
    else
    {
        window.attachEvent("onresize", this.CheckScrolling);
    }
        
    return this;
}











function TabbedControl_Rebase(clientId, baseurl, ret, index)
{
	//re-base effective urls
	var i = 0;
	var si = 0;
	var ii = 0;
	var buffer = "";

	while (true)
	{
		si = ret.indexOf(baseurl, i);
		if (si == -1)
		{
			buffer += ret.substring(i, ret.length);
			break;
		} else {
			buffer += ret.substring(i, si);
			buffer += "javascript:" + clientId + "_SetActiveTab(" + index + ",'";
			ii = ret.indexOf("\"", si);
			if (ii == -1) ii = ret.length;
			buffer += ret.substring(si + baseurl.length, ii) + "');";
			i = ii;
		}

	}
	return buffer;
}








function TabbedControl_SetActiveTab(clientid, hash, url, index, added)
{
	document.getElementById(clientid + "_tabContentLoading").style.display = "block";
	document.getElementById(clientid + "_TabStripContainer").className = "tabLoading";

    var par = new Array("tabid", index);
    var ret = BryceWebGroup_CMS_AJAX_CreateRequestDump("get", par, hash);
    
    if (added)
    {
        if (added.substring(0, 1) == "?")
        {
            var i = ret.url.indexOf("?");
        
            ret.url = ret.url.substring(0, i) + added;
        } else {
            ret.url += added;
        }
    }
    var calls = new Object();
    calls.index = index;
    calls.clientid = clientid;
    calls.url = url;
    
    var http = BryceWebGroup_CMS_AJAX_CreateRequestObject(ret.url, "get", null, null, null)
    http.onreadystatechange = function() { TabbedControl_SetActiveTab_return(http, calls); }
    http.send(""); 
}

function TabbedControl_SetActiveTab_return(ret, calls)
{
    if (!ret || ret.readyState != 4) return;
    
    if (ret.status != 200)
    {
        document.write(ret.responseText);
        return;
    }
    ret = ret.responseText;
    
	document.getElementById(calls.clientid + "_tabContentLoading").style.display = "none";
	document.getElementById(calls.clientid + "_TabStripContainer").className = "";

	//remove disruptive encoding
	ret = ret.replace(/&amp;/g, "&");
	var buffer = TabbedControl_Rebase(calls.clientid, calls.url, ret, calls.index);

	document.getElementById(calls.clientid + "_TabContent").innerHTML = buffer;
	
	BryceWebGroup_CMS_AJAX_ExecJS(document.getElementById(calls.clientid + "_TabContent"));
}


