// GupShup Tabview
(function(){
GupShup.widget.TabView = function(tabHolder){
this.tabConfList = new Array();
this.yahooTabView = new YAHOO.widget.TabView(tabHolder);
}

GupShup.widget.TabView.prototype.addTab = function(yahooConf,gpshpConf) {
	var pos = this.tabConfList.length;
	var tab = null;
	
	if(typeof gpshpConf == "undefined" || !gpshpConf)
		tab = {label:yahooConf.label,isActive:yahooConf.isActive,content:yahooConf.content};
	else
		tab = {label:yahooConf.label,isActive:yahooConf.isActive,content:yahooConf.content,gupshupConf:gpshpConf};
		
	this.tabConfList[pos] = tab;
}

GupShup.widget.TabView.prototype.createTabView = function() {
	var addListner = false;
	for( var i = 0 ; i < this.tabConfList.length ; i++)
	{
		var tabConf = this.tabConfList[i];
		var tab = null;
		tab = new GupShup.widget.Tab({
		label: tabConf.label,
		active: tabConf.isActive,
		content:tabConf.content,
		gupshupConf: tabConf.gupshupConf
		})
		this.yahooTabView.addTab(tab);
		//Settting data src later coz yahoo makes request while rendering tab :( its hack :)
		if(typeof tabConf.gupshupConf == 'object' && typeof tabConf.gupshupConf.url =='string')
		{
			tab.set('dataSrc', tabConf.gupshupConf.url);
			tab.set('cacheData',false);
			addListner = true;
		}
			tab.addListener('afterContentChange',function(e){GupShup.html.util.hideLoader();});
	}
	if(addListner)
		this.yahooTabView.addListener('activeTabChange',function(e){GupShup.html.util.showLoader();});
}

GupShup.widget.TabView.prototype.set = function(index,content){
	this.yahooTabView.getTab(index).set('content', content);
}
GupShup.widget.TabView.prototype.setActive = function(index){
	this.yahooTabView.set('activeIndex', index);
}

GupShup.widget.TabView.prototype.get = function(attr){
	return this.yahooTabView.get(attr);
}
})();

// GupShup Tab
(function(){
GupShup.widget.Tab = function(ele) {
		this.constructor.superclass.constructor.call(this, ele);
		if(typeof ele.gupshupConf != "undefined" && typeof ele.gupshupConf == "object")
		{
			this.gupshupConf = ele.gupshupConf;
			if(this.gupshupConf.reqType == 'AJAX')
			{
				this.loadHandler =  {
					success: function(o){ 
											if(typeof o.argument == 'undefined')
												o.argument = {};
											o.argument.tab = this;
											o.argument.other = this.gupshupConf.success.arguments;
											this.gupshupConf.success.callBack(o);
											GupShup.util.callUrchinTracker(o);
										},
		            failure: function(o){
											if(typeof o.argument == 'undefined')
												o.argument = {};
											o.argument.tab = this;
											o.argument.other = this.gupshupConf.failure.arguments;
											this.gupshupConf.failure.callBack(o);
											GupShup.util.callUrchinTracker(o);
										}
		        };	
			}
			else if(this.gupshupConf.reqType == 'RELOAD')
			{
				this.addListener("activeChange", function(e){if((e.prevValue == false) &&  (e.newValue == true))window.location = this.gupshupConf.url;return false;});
			}
		}
    };
	YAHOO.lang.extend(GupShup.widget.Tab, YAHOO.widget.Tab);
})();

(function(){
GupShup.util.createTabView = function (tabs,holder) {
	var tabView = new GupShup.widget.TabView(holder);
	for (var i = 0; i < tabs.length; ++i)
	{
		tabView.addTab(tabs[i].yahooConf, tabs[i].gupshupConf);
	}
	tabView.createTabView();
	return tabView;
}
})();
