//Tab.js

/// <summary>
/// Tab class object
/// </summary>
/// <param name="instance">The instance name of the Tab class-object.</param>
/// <param name="parent">The id of the parent object container.</param>
/// <param name="menu">The id of menu object container.</param>
/// <param name="target">The id of the target object container.</param>
function Tab(instance,parent,menu,target) {

	this.instance = instance;
	this.parent = parent;
	this.menu = menu;
	this.target = target;
	this._tab = [];
	this._tabUri = [];
	this._tabFunc = [];
	this._duration = null;

	_xml = null, _data = "";
	_container = document.getElementById(this.target);
	_t = 0;
	_rotating = false;

	if(typeof Tab._init == "undefined") {

		Tab.prototype.random = function(objUri,objTitle,objParent,objTarget) {
			try {
				tabUriArr = objUri;
				tabArr = objTitle;
				rNum = Math.floor(Math.random() * tabArr.length) + 1;
				Tab.prototype.getTabUri(tabUriArr[rNum-1],tabArr[rNum-1],objParent,objTarget);
			}
			catch(e) {
				_errMsg("Tab._random " + e.message);
			}
		};

		Tab.prototype.addTab = function(data,uri,custFunc) {
			try {
				if(data) {
					this._tab.push(data);
					this._tabUri.push(uri);
					this._tabFunc.push(custFunc);
				}
			}
			catch(e) {
				_errMsg("Tab.prototype.addTab " + e.message);
			}
		};

		Tab.prototype.load = function(starter,duration) {
			try {
				_div = document.getElementById(this.menu);
				_ul = _createElement("ul");
				_ul.id = "headlines-tab-ul";

				for(i=0; i<this._tab.length; i++) {
					_a = _createElement("a");
					_a.innerHTML = this._tab[i];
					_a.href = "#";
					_a.title = this._tab[i];
					_a.id = this._tab[i];

					if(document.all) {
						if(!this._tabFunc[i]) _a.onclick = this.instance + ".getTabUri('" + this._tabUri[i] + "','" + this._tab[i] +"');";
						else _a.onclick =  this.instance + ".getTabUri('" + this._tabUri[i] + "','" + this._tab[i] +"'); return _doThat();";

						_data += "<li id='tab-" + this._tab[i] + "'>" + _a.outerHTML + "</li>";
						delete _a;
					}
					else {
						if(!this._tabFunc[i]) _a.setAttribute("onclick",this.instance + ".getTabUri('" + this._tabUri[i] + "','" + this._tab[i] +"');");
						else _a.setAttribute("onclick","document.getElementById('headlines-tab').className = 'scoreboard'; _doThat();");
	
						_li = _createElement("li");
						_li.id = "tab-" + this._tab[i];
						_li.appendChild(_a);
						delete _a;
						_ul.appendChild(_li);
						delete _li;
					}
				}

				if(document.all) _div.innerHTML = "<ul>" + _data + "</ul>";
				else {
					_div.innerHTML = "";
					_div.appendChild(_ul);
				}
			
				if((starter) && (starter != "random") && (starter != "rotate")) {
					for(j=0; j<this._tab.length; j++) {
						if(starter == this._tab[j]) Tab.prototype.getTabUri(this._tabUri[j],this._tab[j],this.parent,this.target);
					}
				}
				else if(starter == null) {
					_doThis(document.getElementById(this._tab[0]).onclick());
					_doThis(document.getElementById(this._tab[0]).onclick());
				}
				else if(starter == "random") Tab.prototype.random(this._tabUri,this._tab,this.parent,this.target);
				else if(starter == "rotate") {
					_doThis(document.getElementById(this._tab[0]).onclick());
					_timedTab = this._tab;
					this._duration = duration;
					_rotating = true;
					_setTimer(this._duration);
				}
			}
			catch(e) {
				_errMsg("Tab.prototype.load " + e.message);
			}			
		};

		Tab.prototype.getTabUri = function(source,styleName,tabParent,tabTarget) {
			try {
				_httpReq = null;
				if(!this.parent) {
					_httpReq = new HTTPRequest(tabTarget);
					_httpReq.load(source);
					document.getElementById(tabParent).className = styleName;
				}
				else {
					_httpReq = new HTTPRequest(this.target);
					_httpReq.load(source);
					document.getElementById(this.parent).className = styleName;
				}
				delete _httpReq;
				_rotating = false;
			}
			catch(e) {
				_errMsg("Tab.prototype.getTabUri " + e.message);
			}
		};

		Tab.prototype.timer = function() {
			if(_rotating) {		
				if(_t < _timedTab.length) {
					_doThis(document.getElementById(_timedTab[_t++]).onclick());
					_rotating = true;
				}
				else {
					_t = 0;
					_setTimer(this._duration);
				}
			}
		};

		Tab._init = true;
	}

	function _inc() {
		try {
			return _t++;
		}
		catch(e) {
			_errMsg("Tab._inc " + e.message);
		}
	}

	function _dec() {
		try {
			return _t--;
		}
		catch(e) {
			_errMsg("Tab._dec " + e.message);
		}
	}

	function _setTimer(duration) {
		try {
			_timer = setInterval("Tab.prototype.timer();",duration);
		}
		catch(e) {
			_errMsg("Tab._setTimer " + e.message);
		}
	}

	function _doThis(something) {
		try {
			return something;
		}
		catch(e) {
			_errMsg("Tab._doThis " + e.message);
		}
	}

	function _createElement(obj) {
		try {
			return document.createElement(obj);
		}
		catch(e) {
			_errMsg("Tab._createElement() " + e.message);
		}
	}
	
	function _errMsg(msg) {
		alert(msg);
	}
}

/// <summary>
/// HTTPRequest class object
/// </summary>
/// <param name="requester">The object recipient of the HTTP request data.</param>
function HTTPRequest(requester) {
	_requester = document.getElementById(requester);
	this.requester = requester;
	if(typeof HTTPRequest._init == "undefined") {

		function _doThis(something) {
			try {
				return _errMsg(something);
			}
			catch(e) {
				_httpErrMsg("HTTPRequest._doThis " + e.message);
			}
		}

		function _stateChange() {
			try {
				if(xmlhttp.readyState == 4) {
			  		if(xmlhttp.status == 200) {
						_requester.innerHTML = xmlhttp.responseText;
						_doThis();
			  		}
					else _errMsg(xmlhttp.statusText);
				}
			}
			catch(e) {
				_httpErrMsg("HTTPRequest._stateChange " + e.message);
			}	
		}
		
		function _httpErrMsg(msg) {
			return;
		}

		HTTPRequest.prototype.load = function(uri) {
			try {
				this.uri = uri,	xmlhttp = null;
				if(window.XMLHttpRequest) xmlhttp = new XMLHttpRequest();
	   	  		else if (window.ActiveXObject) xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");
			  
				if(xmlhttp != null) {
			  		xmlhttp.onreadystatechange = _stateChange;
			  		xmlhttp.open("GET",this.uri,true);
			  		xmlhttp.send(null);
					xmlhttp.close;
			  	}
				else _requester.innerText = "XMLHTTP not supported";
			}
			catch(e) {
				_httpErrMsg("HTTPRequest.prototype.load " + e.message);
			}
		};	

		HTTPRequest._init = true;
	}
}

