globalCtab = null;
active = false;
imgRoot = "/javascript/specific/images/";
self.cAjaxTab = function (holderDivID, btnActiveClass, btnInactiveClass, contentDivId){
	this.hDivID = holderDivID;
	this.classA = btnActiveClass;
	this.classIA = btnInactiveClass;
	this.displayDivID = contentDivId;
	this.tabs = new Array();
	this.chatche = new Array();
	this.curtentBtnIndex = 0;
	this.crateAjxObj();
	this.initialize();
}
cAjaxTab.prototype.initialize = function(){
	var docFragment = document.getElementById(this.hDivID);
	if(docFragment==null)return;
	var links = docFragment.getElementsByTagName("a");
	for(var i = 0; i<links.length;i++){
		var div = links[i].parentNode.parentNode;
		var hiddenDiv = document.createElement("div");
		div.tabId = i;
		div.ctab = this;
		div.linkPutanja = links[i].href.toString();
		links[i].removeAttribute("href");
		hiddenDiv.onclick = links[i].onclick;
		div.hDiv = hiddenDiv;
		links[i].onclick = null;;
		div.onclick = cTabClick;
		div.style.cursor = "pointer";
		this.tabs.push(div)
		this.chatche.push("");
	}
}
cAjaxTab.prototype.crateAjxObj = function(){
	this.ajaxObject = GetXmlHttpObject(this.displayContent);
}
cAjaxTab.prototype.ajxObj = function(){
	return this.ajaxObject;
}
cAjaxTab.prototype.ddivID = function(){
	return this.displayDivID;
}
cAjaxTab.prototype.setChatche = function(val){
	this.chatche[this.curtentBtnIndex] = val;
}
cAjaxTab.prototype.doOnClickFunc = function(id){
	if(this.tabs[id].hDiv!=null)
		if(this.tabs[id].hDiv.onclick!=null){
			try{
				this.tabs[id].hDiv.onclick();
			}
			catch(e){
			}
		}
}

cAjaxTab.prototype.doClick = function(btnID){
	if(this.curtentBtnIndex == btnID){
		//	Izbaceno jer ako su naopako botuni ne otvara prividno zadnjeg
		//endPreload(globalCtab.ddivID());
		//return;
	}
	this.curtentBtnIndex = btnID;
	this.doOnClickFunc(btnID);
	
	if(this.chatche[btnID] == ""){
		if(this.tabs[btnID].linkPutanja!=location.href){
			this.ajaxObject.open("GET", this.tabs[btnID].linkPutanja, true);
			this.ajaxObject.send(null);
		}else{
			endPreload(globalCtab.ddivID());
		}
	}else{
		var tabcont = document.getElementById(this.displayDivID);
		endPreload(globalCtab.ddivID());
		tabcont.innerHTML = this.chatche[btnID];
	}
	for(var i=0; i<this.tabs.length; i++){
		this.tabs[i].className=i!=btnID?this.classIA:this.classA;
	}
}
self.cTabClick = function (){
	if(active)return;
	startPreload(this.ctab.ddivID())
	globalCtab = this.ctab;
	this.ctab.doClick(this.tabId);		
}
//	Prikazuje content
cAjaxTab.prototype.displayContent = function(){
	var axd = globalCtab.ajxObj();
	if (axd.readyState==4 || axd.readyState=="complete")
	{	
		var tabcont = document.getElementById(globalCtab.ddivID())
		if(tabcont!=null){
			endPreload(globalCtab.ddivID());
			tabcont.innerHTML = axd.responseText;
			globalCtab.setChatche(axd.responseText);
			globalCtab.crateAjxObj();
		}
	}
}

/*	Efekti	*/
self.startPreload = function (contentDivId){
	active=true;
	var d = document.getElementById(contentDivId);
	if(d==null)return;
	d.innerHTML = "";
	d.style.background = "url('"+ imgRoot + "ajax-loader.gif"+"') 50% 50% no-repeat #ffffff";
}
self.endPreload =function (contentDivId){
	var d = document.getElementById(contentDivId);
	if(d==null)return;
	d.innerHTML = "";
	d.style.background = "";
	active=false;
}
