//***********Functions for tabs****************************
function Tabs(tab_id, parent_id, tab_nums, tab_buttons, tab_body, tab_body_id, active_tab) {
	this.tab_id = tab_id;
	this.tabs = new Array();
	this.tab_buttons_ = new Array();
	this.tab_bodys_ = new Array();
	this.tab_nums_ = tab_nums;
	this.obj = "";
	
	var tab = "";
	var main = this;
	
	function Element(element, id) {
		return "<" + element + " id=\"" + id + "\"></" + element + ">";
	}
	
	this.SetActiveTab = function(tab) {
		if (tab_body_id.length == 0) {
			//Set Active class to needded div else hide it.
			for (i=0; i <= this.tab_nums_ - 1; i++) {
				$(this.tab_id + "-tab-button-" + i).removeClassName("active-tab-button");
				$(this.tab_id + "-tab-" + i).hide();
			}
			
			$(this.tab_id+"-tab-button-"+tab).addClassName("active-tab-button");
			$(this.tab_id+"-tab-button-"+tab).removeClassName("tab-button");
			$(this.tab_id+"-tab-"+tab).show();
		}else{
			for (i=0; i <= this.tab_nums_ - 1; i++) {
				$(this.tab_id + "-tab-button-" + i).removeClassName("active-tab-button");
				$(this.tab_id + "-tab-button-" + i).addClassName("tab-button");
				$(tab_body_id[i]).hide();
			}

			// alert(this.tab_id+"-tab-button-"+tab);

			$(this.tab_id+"-tab-button-"+tab).addClassName("active-tab-button");
			$(this.tab_id+"-tab-button-"+tab).removeClassName("tab-button");
			$(tab_body_id[tab]).show();
		}
		
		return 0;
	}
	
	for (i=0; i<tab_nums; i++) {
		caption = tab_buttons[i];
		class_ = "tab-button ";
//		tab = "<div id=\""+this.tab_id+"-tab-button-"+i+"\" class=\""+class_+"\">"+caption+"</div>";
		tab = "<td id=\""+this.tab_id+"-tab-button-"+i+"\" class=\""+class_+"\" nowrap>"+caption+"</td>";
		this.tab_buttons_.push(tab);
	}
	//this.tab_buttons_.push("<div id=\""+this.tab_id+"-empty-tab-button\" class=\"tab-"+this.tab_id+"-empty-tab-button\">&nbsp;&nbsp;</div>");
	
	if (tab_body_id.length == 0) {
		for (i=0; i<tab_nums; i++) {
			caption = tab_body[i];
			tab = "<div id=\""+this.tab_id+"-tab-"+i+"\" class=\"tab "+this.tab_id+"-tab-"+i+"\">"+caption+"</div>";
			this.tab_bodys_.push(tab);
		}
	}else{
		for (i=0; i<tab_nums; i++) {
			$(tab_body_id[i]).hide();
		}
	}
	
	body = "";
	buttons = "";
	for (i=0; i < this.tab_buttons_.length; i++) {
		buttons += this.tab_buttons_[i];
	}
	
	for (i=0; i < this.tab_bodys_.length; i++) {
		body += this.tab_bodys_[i];
	}
	
	new Insertion.Top(parent_id, Element("div", "tab_" + this.tab_id));
	$("tab_" + this.tab_id).addClassName("body-" + this.tab_id);
	new Insertion.Top("tab_" + this.tab_id, Element("div", "tab-" + this.tab_id + "-buttons"));
//	new Insertion.Top("tab_" + this.tab_id, Element("table", "tab-" + this.tab_id + "-buttons"));
	new Insertion.Top("tab-" + this.tab_id + "-buttons", "<table width=100% cellpadding=\"0\" cellspacing=\"0\"><tr><td class=\"tab-button\"></td>" + buttons + "<td width=100% class=\"tab-button\"> </td></tr></table>");
//	new Insertion.Bottom("tab_" + this.tab_id, "</table>");
//	new Insertion.Top("tab-" + this.tab_id + "-buttons", line);
	new Insertion.Bottom("tab_" + this.tab_id, body);

	for (i=0; i<tab_nums; i++) {
		// Added OnClick event.
		$(this.tab_id + "-tab-button-" + i).onclick = OnClick;
		if ($(this.tab_id + "-tab-button-" + i).captureEvents) 
			$(this.tab_id + "-tab-button-" + i).captureEvents(Event.CLICK);
		
		// Added OnMouseOver, OnMouseOut events.
		$(this.tab_id + "-tab-button-" + i).onmouseover = OnMouseOver;
		if ($(this.tab_id + "-tab-button-" + i).captureEvents) 
			$(this.tab_id + "-tab-button-" + i).captureEvents(Event.CLICK);
		$(this.tab_id + "-tab-button-" + i).onmouseout = OnMouseOut;
		if ($(this.tab_id + "-tab-button-" + i).captureEvents) 
			$(this.tab_id + "-tab-button-" + i).captureEvents(Event.CLICK);
	}
	
	function OnMouseOver(e) {
		if (!e) {
			e = event;
		}
		
		element = e.target || e.srcElement;
		element.addClassName("tab-button-over");
	}
	
	function OnMouseOut(e) {
		if (!e) {
			e = event;
		}
		
		element = e.target || e.srcElement;
		element.removeClassName("tab-button-over");
	}
	
	function OnClick(e) {
		if (!e) {
			e = event;
		}
		
		element = e.target || e.srcElement;
		if(element.id) {
			a = element.id.split("-");
		} else {
			a = element.parentNode.id.split("-");
		}
		main.SetActiveTab(a[a.length - 1]);
	}
	
	this.SetActiveTab(active_tab);
}
//*********************************************************
