/*CLASS tabControl
____________________________________________________
JSON:

	JSON object:
		{key:value}
		value - if string surrounded by quote marks


	JSON array:
		[{}, {}, {} ...] 
____________________________________________________
TAB JSON OBJECT:
	{header:"tab 1", content:"content"}
		header - tab title
		content - content inside tab's div
		source - for async tabs(web service url)
		params - for async tabs(params for web service)
____________________________________________________
INCLUDES REQUIRED:
	YUI:
		tabview-min.js
		element-beta-min.js
		yahoo-dom-event.js
	jquery:
		jquery-1.2.2.js
____________________________________________________
METHODS:
	tabControl
		Constructor - creates instance of tabControl class
		param - html, jquery or id of div that surrounds YUI tab
	
	setTabContent(for internal usage)
		set content for the tab
		params: 
			TAB JSON objects
			YUI tab
			
	setActiveTabContent
		set content for the active tab
		param - TAB JSON objects
	
	addTabs
		add new tab to the end of current tabs array
		param - TAB JSON objects
	
	
	addTabs
		adds new tabs to the end of current tabs array
		param - array of tabs(JSON objects)
	
	
	removeTabs
		removes tabs
		param - array of tab indexes to delete
		
	tabsCount
		returns number of existing tabs
		
	change content
		updates content of tabs, insert new tabs if necessary
		param - array of tabs(JSON objects)

	setAsyncContent
		for async tabs sets content
		params
			yuiTab - tab object in terms of YUI
			objTab - tab JSON Object(use source and param attributes)
_____________________________________________________________________________________________________________________________________
*/


/* now you could make just $(div_id).load(some_url_of_web_service)  and it will work
but by default - it is get and load prevents get request - so it will not work , because web service doesn't support get requests
*/
jQuery.ajaxSetup({
  type: "POST"
});

/*tabControl
	Constructor - creates instance of tabControl class
	param - html, jquery or id of div that surrounds YUI tab
*/
function tabControl(htmlControl, events) {
	this._control = htmlControl;
	this._yuiTabs = new YAHOO.widget.TabView(jQuery(htmlControl).attr("id"));
	this._events = events;
	if(this._events.tabChanged){
		this._yuiTabs.addListener("activeTabChange", this._events.tabChanged);
	}
}

/*setTabContent(for internal usage)
	set content for the tab
	params: 
		TAB JSON objects
		YUI tab
*/
tabControl.prototype.setTabContent = function (tab, tabContent){
	if(tabContent.content){
		tab.set('content', tabContent.content);			
	}
	if(tabContent.source){
		this.setAsyncContent(tab, tabContent);
	}
	if(tabContent.header)
	{
		tab.set('label', tabContent.header);		
		this.beautifyTab(tab);
	}
}


tabControl.prototype.clear = function (){
	for(var i=0; i < this.tabsCount(); i++){
		var tab = this._yuiTabs.getTab(i);
		this._yuiTabs.removeTab(tab);
	}
}

tabControl.prototype.getActiveTabIndex = function (){
	return this._yuiTabs.get("activeIndex");
}

tabControl.prototype.addEmptyTab = function(){
		var newTab =  new YAHOO.widget.Tab({ 
	        label: "Information Center", 
	        content: "",
			active: true
	    });
		this.beautifyTab(newTab);
		this._yuiTabs.addTab(newTab);
		return newTab;
}


/*setActiveTabContent
	set content for the active tab
	param - TAB JSON objects
*/
tabControl.prototype.setActiveTabContent = function (tabContent){
	var activeIndex = this._yuiTabs.get("activeIndex");
	var activeTab = this._yuiTabs.getTab(activeIndex);
	if(this.tabsCount() == 0){
		activeTab = this.addEmptyTab();
		this._yuiTabs.set("activeIndex", 0);
		this.decideVisible();
	}
	this.setTabContent(activeTab, tabContent);
}

/*setAsyncContent
	for async tabs sets content
	params
		yuiTab - tab object in terms of YUI
		objTab - tab JSON Object(use source and param attributes)
*/
tabControl.prototype.setAsyncContent = function(yuiTab, objTab){
	jQuery.post(objTab.source, objTab.params, 
			function(data){
				if(data.documentElement.childNodes.length > 0)
					jQuery(yuiTab.get("contentEl")).html(getFullContent(data));					
			}, "xml");
}

/*addTabs
	add new tab to the end of current tabs array
	param - TAB JSON objects
*/
tabControl.prototype.addTab = function (objTab){
		var newTab =  new YAHOO.widget.Tab({ 
	        label: objTab.header, 
	        content: objTab.content 
	    });
		this.beautifyTab(newTab);
		this._yuiTabs.addTab(newTab);				
		if(objTab.source){
			this.setAsyncContent(newTab, objTab);
		}		
}


/*addTabs
	adds new tabs to the end of current tabs array
	param - array of tabs(JSON objects)
*/
tabControl.prototype.addTabs = function (arr){
	for(var i = 0; i < arr.length; i++){
		this.addTab(arr[i]);
	}    
}


/*removeTabs
	removes tabs
	param - array of tab indexes to delete
*/
tabControl.prototype.removeTabs = function (arr){
	for(var i = 0; i < arr.length; i++){
		var tab = this._yuiTabs.getTab(arr[i]);
		this._yuiTabs.removeTab(tab);
	}    
}

/*tabsCount
	returns number of existing tabs
*/
tabControl.prototype.tabsCount = function(){
	return this._yuiTabs.get("tabs").length;
}

/*show
	shows tab control
*/
tabControl.prototype.show = function(){
	jQuery(this._control).show();
	jQuery('.widgetSetRelatedElements').show();
}

/*hide
	hides tab control
*/
tabControl.prototype.hide = function(){
	jQuery(this._control).hide();
}

/*decideVisible
	shows tab control if there are tabs, and hides otherwise
*/
tabControl.prototype.decideVisible = function(){
	if(this.tabsCount() == 0){
		this.hide();
	}
	else{
		this.show();
	}
}


/*change content
	updates content of tabs, insert new tabs if necessary
	param - array of tabs(JSON objects)
*/
tabControl.prototype.changeContent = function(arr){
	for(var i = 0; i < arr.length; i++){
		var tab = this._yuiTabs.getTab(i);
		if(tab){
			this.setTabContent(tab, arr[i]);
		}
		else{
			this.addTab(arr[i]);
		}
		this._yuiTabs.set('activeIndex', 0);
	}
}


tabControl.prototype.beautifyTab = function(tab){
	if(jQuery(tab.get("labelEl")).parents(".roundedHeader").length > 0) return;
	var template = '<table class="roundedHeader">'+
						'<tr>'+
	                        '<td class="leftCorner"></td>'+
		                    '<td><em class="headerEm">#TEXT#</em></td>'+
							'<td class="rightCorner"></td>'+
						'</tr>'+
					'</table>';
	var label = tab.get("label");
	tab.set("label", template.replace("#TEXT#", label));		
}

