var TabTicker = {
	/**
 	 * @property lookup array that stores the current tab id for all tablists
	 */
	currentTab: new Array(),

	/**
	 * @property CONST ajax request url
	 */
	ajaxURL: "http://diablo2.ingame.de/classes/ticker/tabticker.php",

	/**
	 * @property CONST loading animation image
	 */
	loading_gif: "/layout/gfx/ticker_loading.gif",

	/**
	 * Sets the runtime options (ajaxURL)
	 */
	initialize: function(options) {
		this.ajaxURL = options.ajaxURL;
	},

	/**
	 * Returns the tablist DOM element for the given Id
	 */
	getTabList: function(tabListId) {
		return document.getElementById( "tablist" + tabListId );
	},

	/**
	 * Returns all tab-nodes for the given list
	 */
	getTabNodes: function(tabListId) {
		var node = this.getTabList(tabListId);
		var children = new Array();
		for ( var i = 0; i < node.childNodes.length; i++ ) {
			if ( "li" == node.childNodes[i].nodeName.toLowerCase() ) {
				children.push(node.childNodes[i]);
			}
		}		
		return children;
	},

	/**
	 * Returns the number of tabs in the given list
	 */
	getTabCount: function(tabListId) {
		var tabs = this.getTabNodes(tabListId);
		return tabs.length;
	},

	/**
	 * Checks which tab has the "current" class and returns his Id
	 */
	getCurrentTab: function(tabListId) {
		var tabs = this.getTabNodes(tabListId);
		var count = 0;
		for ( var i = 0; i < tabs.length; i++ ) {
			if ( "current" == tabs[i].className ) {
				return i;
			}
		}
		return null;
	},

	/**
	 * Returns the DOM node of the given tab
	 */
	getTabNode: function(tabListId, tabId) {
		var tabs = this.getTabNodes(tabListId)
		for ( var i = 0; i < tabs.length; i++ ) {
			if ( i == tabId ) {
				return tabs[i];
			}
		}
		return null;
	},

	/**
	 * Activates the given tab and hides the current one
	 */
	showTab: function(tabListId, tabId) {
		// if there is no current tab, set current tab
		if ( "undefined" == typeof this.currentTab[ tabListId ] ) {
			this.currentTab[tabListId] = this.getCurrentTab(tabListId);
		}
	
		// enable link in old tab
		this.getTabNode(tabListId, this.currentTab[tabListId]).className = "";

		// disable link in new tab
		this.getTabNode(tabListId, tabId).className = "current";
	
		var currentTicker = this.getTickerNode(tabListId, this.currentTab[tabListId]);

		// remove eventually existing loading animation
		var LoadingNode = currentTicker.childNodes[0];
		if (LoadingNode && LoadingNode.nodeName == 'SPAN') {
			currentTicker.removeChild(LoadingNode);
		}

		// hide old tab
		currentTicker .className = "invisible tab";

		// show new tab
		this.getTickerNode(tabListId, tabId).className = "visible tab";
	
		// set new tab
		this.currentTab[tabListId] = tabId;

		this.loadTicker(tabListId, tabId);
	},

	/**
	 * Returns the container DOM node for the given tabList
	 */
	getTickerContainer: function(tabListId) {
		return document.getElementById("tabs" + tabListId);
	},

	/**
	 * Returns all ticker nodes for the given tablist
	 */
	getTickerNodes: function(tabListId) {
		var container = this.getTickerContainer(tabListId);
		var tickers = new Array();
		for ( var i = 0; i < container.childNodes.length; i++ ) {
			if ( "div" == container.childNodes[i].nodeName.toLowerCase() ) {
				tickers.push(container.childNodes[i]);
			}
		}
		return tickers ;			
	},

	/**
	 * Returns the ticker DOM node for the given tab
	 */
	getTickerNode: function(tabListId, tabId) {
		var tickers = this.getTickerNodes(tabListId);
		for ( var i = 0; i < tickers.length; i++ ) {
			if ( i == tabId ) {
				return tickers[i];
			}
		}
		return null;
	},

	/**
	 * Loads the html content for the given tab from the server
	 */
	loadTicker: function(tabListId, tabId) {
		var url = this.ajaxURL + "?box=" + tabListId + "&tab=" + tabId;
		var ticker = this.getTickerNode(tabListId, tabId);
//		ticker.innerHTML = '<img src="' + this.loading_gif + '" alt="..." width="8" height="8">' + ticker.innerHTML;
		ticker.innerHTML = '<span class="loading"></span>' + ticker.innerHTML;


		new Ajax.Request(url, {
			method: 'get',
			onSuccess: this._updateTicker.bind(this),
			onFailure: function(error) {
				alert(error);
			}
		});
	},

	/**
	 * Callback function that replaces the ticker content
	 * @access private
        */
	_updateTicker: function(response) {
		var ticker = this.getTickerNode(response.responseJSON.boxId, response.responseJSON.tabId);
		ticker.innerHTML = response.responseJSON.html;
//		var IMGNode = ticker.childNodes[0];
//		if (IMGNode.nodeName == 'IMG')
//		{
//			ticker.removeChild(IMGNode);
//		}
	}
};


