//
// Class TabSwitcher
//
var ParillaTabSwitcher = Class.create();
ParillaTabSwitcher.prototype = {
	initialize: function (tabLis, tabSelClass, tabDiv, loadingHTML, initialDiv, actualDiv) {
		this.tabSelClass = tabSelClass;
		this.tabDiv = tabDiv;
		this.loadingHTML = loadingHTML;
		this.prevSelDiv = initialDiv;
		this.actualDiv = actualDiv;
	
		$A(tabLis).each(function (tabLi) {
			var aElm;
			if (tabLi.tagName.toLowerCase() == 'a') {
				aElm = tabLi;
			} else {
				aElm = tabLi.getElementsBySelector('a')[0];
			}
			Event.observe(aElm, 'click', this.tabSwitch.bind(this, tabLi, actualDiv));
		}.bind(this));
		
		this.selLi = $A(tabLis).find(function (tabLi) {
			return tabLi.hasClassName(tabSelClass);
		});
	},
	
	tabSwitch: function (tabLi,actualDiv) {
		if (this.selLi != undefined) {
			this.selLi.removeClassName(this.tabSelClass);
		}
		
		tabLi.addClassName(this.tabSelClass);
		this.selLi = tabLi;
		
		var aElm;
		if (tabLi.tagName.toLowerCase() == 'a') {
			aElm = tabLi;
		} else {
			aElm = tabLi.getElementsBySelector('a')[0];
		}
		
		if (aElm.rel.substring(0,4) == 'tab:') {
			var tabName = aElm.rel.substring(4);
			if (this.prevSelDiv != null && this.prevSelDiv != '') {
				$(this.prevSelDiv).hide();
			}
			else{
				if(tabName == 'FDF_Telecinco_today' || tabName == 'La_Siete_today' || tabName == 'Boing_today'){
					this.prevSelDiv = 'Telecinco_today';
				}
				else if (tabName == 'FDF_Telecinco_tomorrow' || tabName == 'La_Siete_tomorrow' || tabName == 'Boing_tomorrow'){
					this.prevSelDiv = 'Telecinco_tomorrow';
				}
				else {
					this.prevSelDiv = 'Telecinco_after_tomorrow';
				}
				$(this.prevSelDiv).hide();
			}
			$(tabName).show();

                        if(typeof(actualDiv) !== 'undefined' && actualDiv != null) {
			if (tabName != 'Telecinco_Shop_today') {
				$(actualDiv + '_content').style.top= '0px';
				$(actualDiv + '_theScroll').style.top= '0px';
				initActualProgram(tabName,actualDiv); // presente in div_scroller.js
			} else {
				$(actualDiv + '_content').style.top = '0px';
				$(actualDiv + '_theScroll').style.top = '0px';
			}
                        }
			this.prevSelDiv = tabName;
		
		} else {
			$(this.tabDiv).innerHTML = this.loadingHTML;
										
			new Ajax.Updater(this.tabDiv, aElm.rel, {
				method: 'get',
				evalScripts: true
			});
			
			if (this.prevSelDiv != null && this.prevSelDiv != this.tabDiv) {
				$(this.prevSelDiv).hide();
			}
			
			
			$(this.tabDiv).show();
			
			if (this.tabDiv != 'Telecinco_Shop_today') {
				initActualProgram(this.tabDiv, actualDiv);
			} else {
				 $(actualDiv + '_content').style.top = '0px';
				 $(actualDiv + '_theScroll').style.top = '0px';
			}
			
			this.prevSelDiv = this.tabDiv;
		}
		/*if (this.actualDiv!=undefined) {
			resizeDiv(this.actualDiv);
			initActualProgram(this.actualDiv); // presente in div_scroller.js
		}*/
		return false;
	}
};

