var DISPLAY_TYPE_BLOCK = 'block';
var DISPLAY_TYPE_NONE = 'none';
var DEFAULT_SHOW_TIMEOUT = 200;
var TAB_CONTENT_ID_SUFFIX = 'c';

var UltimateTabstrips;	// UltimateTabstrip object collection

// Return DOM Element
function GetDOMElement(elementId) {
	if (document.all) {
		return document.all[elementId];
	}
	else if (document.getElementById) {
		return document.getElementById(elementId);
	}
}

// Add event handler to given object
function AddEventHandler(obj, eventName, functionNotify) {
	if (obj.attachEvent) {
		obj.attachEvent('on' + eventName, functionNotify);
	}
	else if (obj.addEventListener) {
		obj.addEventListener(eventName, functionNotify, true);
	}
}

// BEGIN - UltimateTabstrip
// UltimateTabstrip object
function UltimateTabstrip(ultimateTabstripControlId, ultimateCallbackClientID, contenContainerId, tabOnId, tabOnCssClass, tabOffCssClass, tabOverCssClass, tabs, showTabOnInitially, openOnMouseOver, showTimeout) {
	this.ultimateTabstripControlId = ultimateTabstripControlId;
	this.ultimateCallbackClientID = ultimateCallbackClientID;
	this.contenContainerId = contenContainerId;
	this.tabOnId = tabOnId;
	this.tabOnCssClass = tabOnCssClass;
	this.tabOffCssClass = tabOffCssClass;
	this.tabOverCssClass = tabOverCssClass;
	this.tabs = tabs;
	this.showTabOnInitially = showTabOnInitially;
	this.openOnMouseOver = openOnMouseOver;
	this.showTimeout = (typeof(showTimeout) != 'undefined') ? showTimeout : DEFAULT_SHOW_TIMEOUT;

	if (tabs) {
		for (var i = 0, loopCnt = tabs.length; i < loopCnt; i++) {
			if (showTabOnInitially && tabs[i].tabId == tabOnId) {
			    if (ultimateCallbackClientID) {
				    DoUltimateCallback(ultimateCallbackClientID, tabs[i].tabFileName);
				}
				else {
                    GetDOMElement(tabs[i].tabId + TAB_CONTENT_ID_SUFFIX).style.display = DISPLAY_TYPE_BLOCK;
				}
			}
			tabs[i].tabstripObj = this;
			tabs[i].tabArrIdx = i;
		}
	}

	this.InsertTabstrip();
}

// Insert tab strip object into tab strip object collection
UltimateTabstrip.prototype.InsertTabstrip = function() {
	if (!UltimateTabstrips) {
		UltimateTabstrips = new Object;
	}
	UltimateTabstrips[this.ultimateTabstripControlId] = this;
};
// END - UltimateTabstrip

// BEGIN - UltimateTab
// UltimateTab object
function UltimateTab(tabId, tabFileName) {
	this.tabId = tabId;
	this.tabFileName = tabFileName;

	var thisPrivate = this;

	var tabElem = GetDOMElement(tabId);
	AddEventHandler(tabElem, 'click', function() { thisPrivate.HandleClick(); });
	AddEventHandler(tabElem, 'mouseover', function() { thisPrivate.HandleMouseOver(); });
	AddEventHandler(tabElem, 'mouseout', function() { thisPrivate.HandleMouseOut(); });
}

// Handle tab click
UltimateTab.prototype.HandleClick = function() {
    var tabstripObj = this.tabstripObj;
	if (!tabstripObj.openOnMouseOver && !this.IsTabOn()) {
		GetDOMElement(tabstripObj.tabOnId).className = tabstripObj.tabOffCssClass;
		GetDOMElement(this.tabId).className = tabstripObj.tabOnCssClass;
        this.ShowTabContent();
	}
};

// Handle tab mouse over
UltimateTab.prototype.HandleMouseOver = function() {
    var tabstripObj = this.tabstripObj;
    if (tabstripObj.openOnMouseOver) {
        this.showTimer = window.setTimeout('UltimateTabstrips["' + tabstripObj.ultimateTabstripControlId + '"].tabs[' + this.tabArrIdx + '].ShowTabContent()', tabstripObj.showTimeout);
    }
	else if (!this.IsTabOn()) {
		GetDOMElement(this.tabId).className = tabstripObj.tabOverCssClass;
	}
};

// Handle tab mouse out
UltimateTab.prototype.HandleMouseOut = function() {
    var tabstripObj = this.tabstripObj;
    if (tabstripObj.openOnMouseOver) {
        window.clearTimeout(this.showTimer);
    }
	else if (!this.IsTabOn()) {
		GetDOMElement(this.tabId).className = tabstripObj.tabOffCssClass;
	}
};

// Show tab content
UltimateTab.prototype.ShowTabContent = function() {
    var tabstripObj = this.tabstripObj;

	if (tabstripObj.openOnMouseOver && !this.IsTabOn()) {
		GetDOMElement(tabstripObj.tabOnId).className = tabstripObj.tabOffCssClass;
        var tabContentElem = GetDOMElement(tabstripObj.tabOnId + TAB_CONTENT_ID_SUFFIX);
        if (tabContentElem) {
            tabContentElem.style.display = DISPLAY_TYPE_NONE;
        }
		GetDOMElement(this.tabId).className = tabstripObj.tabOnCssClass;
    }

    tabstripObj.tabOnId = this.tabId;

    var contentContainerElem;
    if (tabstripObj.ultimateCallbackClientID) {
        DoUltimateCallback(tabstripObj.ultimateCallbackClientID, this.tabFileName);
        contentContainerElem = GetDOMElement(tabstripObj.ultimateCallbackClientID);
    }
    else {
        var tabContentElem = GetDOMElement(this.tabId + TAB_CONTENT_ID_SUFFIX);
        if (tabContentElem) {
            tabContentElem.style.display = DISPLAY_TYPE_BLOCK;
        }
        contentContainerElem = GetDOMElement(tabstripObj.contenContainerId);
    }

    if (contentContainerElem) {
        if (contentContainerElem.scrollLeft) {
	        contentContainerElem.scrollLeft = 0;
        }
        if (contentContainerElem.scrollTop) {
	        contentContainerElem.scrollTop = 0;
        }
    }
};

// Return whether the tab is on
UltimateTab.prototype.IsTabOn = function() {
	return (this.tabId == this.tabstripObj.tabOnId);
};
// END - UltimateTab
