/* This javascript library is developed by afaqs developers internal team.
It provides interactivity to web page and ads a layer of usability. 
It is divided in diffrent modules. DOM access, CSS Modification, Events Model, Core functions and 
Functionality. 
naming conventions - $ for internal use of functions
*/

/* DOM Access Layer provides interaction with web page HTML content */
// This function requires id parameter of HTML node and returns HTML object 
/* CSS Interaction Layer handles css property change of HTML content */
// changes css class of element provided as parameter
function addClass(e, cls)
{
	if(e)
	{
		e.className = cls;
	}
	else
		return false;
}

function addTabButtonClass(e, cls)
{
	if(e)
		e.firstChild.className = cls;
	else
		e.firstChild.className = "";
}

/* Event handler module */
// registering an event handler

EventObject = function() {
};

EventObject.prototype.registerEventHandler = function (node, event, handler)
{
	if(typeof node.addEventListener == "function")
		node.addEventListener(event, handler, false);
	else
		node.attachEvent("on"+event, handler);
}

// unregistering an event handler
EventObject.prototype.unRegisterEventHandler = function (node, event, handler)
{
	if(typeof node.removeEventListener == "function")
		node.removeEventListener(event, handler, false);
	else
		node.detachEvent("on"+event, handler);
}

// wrapper function for registering event handler
EventObject.prototype.addHandler = function(node, type, handler)
{
	wrapHandler = function (event) {
		handler(normaliseEvent(event || window.event));
	}
	this.registerEventHandler(node, type, wrapHandler);
	return {node:node, type:type, handler:wrapHandler}
}

// wrapper function for removing event handler
EventObject.prototype.removeHandler = function(object)
{
	this.unRegisterEventHandler(object.node, object.type, object.handler);
}

//normalise event and remove browser incompatibilities
normaliseEvent = function (event)
{
	if(!event.stopPropagation) 
	{
		event.stopPropagation = function () { this.cancelBubble = true; };
		event.preventDefault = function () { this.returnValue = false; };
	}
	if(!event.stop)
	{
		event.stop = function () {
			this.stopPropagation();
			this.preventDefault();
		}
	}
	if(event.srcElement && !event.target)
		event.target = event.srcElement;
	if((event.toElement || event.fromElement) && !event.relatedTarget)
		event.relatedTarget = event.toElement || event.fromElement;
	return(event);
}

/* xml http request object */

function HTTP() {
	this._factories = [
	function() { return new XMLHttpRequest(); },
	function() { return new ActiveXObject("Msxml2.XMLHTTP"); },
	function() { return new ActiveXObject("Microsoft.XMLHTTP"); }
	];
	this._factory = null;
}

HTTP.prototype = new EventObject;

HTTP.prototype._newRequest = function()
{
	if(this._factory != null) return this._factory();
	for(var i=0; i<this._factories.length; i++)
	{
		try
		{
			var factory = this._factories[i];
			var request = factory();
			if(request != null)
			{
				this._factory = factory;
				return request;
			}
		}
		catch(e)
		{
			continue;
		}
		this._factory = function()
		{
			throw new Error("XMLHttpRequest is supported");
			alert(Error);
		}
		this._factory();
	}
}

HTTP.prototype.getText = function(method, url, callback, el)
{
	req = this._newRequest();
	req.onreadystatechange = function () 
	{
		if(req.readyState == 4)
		{ 
			if(req.status == 200)
			{ 
				//setTimeout(function(){callback(el, req.responseText);}, 1000); 
				callback(el, req.responseText);
				try
				{
					//pageTracker._trackPageview(url);
					//alert(url);
				} catch(err) {alert('err'); }
			}
		}
	}
	req.open("GET", url);
	req.send(null);
}	

/* javascript core functions*/
function forEach(/*...*/)
{
	var array = arguments[0];
	var action = arguments[1];
	var param = arguments[2];
	try
	{
		if(array instanceof Array)
		{
			for(var i=0; i<array.length; i++)
				action(array[i], param);
		}
		else
			throw new Error("forEach() : arguments must be an array");
	}
	catch(Error)
	{
		alert(Error);
	}
}

function extractNodes(pnode, nodeName)
{
	var elArr = new Array();
	for(var i=0, j=0; i<pnode.childNodes.length; i++)
	{
		if(pnode.childNodes[i].nodeName == nodeName)
		{
			elArr[j++] = pnode.childNodes[i];
		}
	}
	return(elArr);
}

/*UI JavaScriptlayer */

//tabs manipulation
HandleTabs.prototype = new HTTP;

function HandleTabs(mytabs)
{
	this.dataLoadingClass = "dataloading"
	this.buttConId = mytabs.buttConId;
	this.dataUrls = mytabs.dataUrls;
	this.buttCon = extractNodes($(this.buttConId), "LI");
	this.dataConId = mytabs.dataConId;
	this.dataCon = extractNodes($(this.dataConId), "DIV");
	this.aButtClass = mytabs.aButtClass;
	this.dButtClass = mytabs.dButtClass;
	this.aDataClass = mytabs.aDataClass;
	this.dDataClass = mytabs.dDataClass;
	this.handleTabs = this.addHandler($(this.buttConId), "click", this.showTab);
	$(this.buttConId).handleTabs = this;
}

HandleTabs.prototype.activateTab = function(tab)
{
	try
	{
		for(var i=0; i<this.buttCon.length; i++)
			if(this.buttCon[i] == tab) break;
		if(this.buttCon[i] && this.dataCon[i])
		{
			forEach(this.buttCon, addTabButtonClass, this.dButtClass);
			forEach(this.dataCon, addClass, this.dDataClass);
			//this.buttCon[i].className = this.aButtClass;
			this.buttCon[i].firstChild.className = this.aButtClass;
			this.dataCon[i].className = this.aDataClass;
			if(this.dataCon[i].innerHTML == "")
			{
				this.dataCon[i].className = this.dataLoadingClass;
				this.dataCon[i].innerHTML = "<img src='/images/loading.gif' />";
				this.getText("GET", this.dataUrls[i], this.handleResponse, this.dataCon[i]);
			}
			pageTracker._trackPageview(this.dataUrls[i]);
			
		}
		else
			throw new Error("activateTab() : id does not exists!");
	}
	catch(Error)
	{
		alert(Error);
	}
};

HandleTabs.prototype.handleResponse = function (el, rText) {
	el.innerHTML = rText;
	el.className = this.aDataClass;
}

HandleTabs.prototype.showTab = function(e)
{
	try
	{
		var targ = e.target;
		while(targ.nodeName != "LI")
			targ = targ.parentNode;
		tParent = targ;
		while(tParent.nodeName != "UL")
			tParent = tParent.parentNode;
		tParent.handleTabs.activateTab(targ);
		return false;
	}
	catch(e)
	{
		//alert(e);
	}
};