// JavaScript Document

var Tabs = {
		initialize: function(options){			
			this.options = Object.extend({
				startTabString:         null,
				startTabCollection:     null,
				widgetClass:           'div.tab-widget',
				ulClass:               'ul.tabs',
				isqString:             false,
				regionsClass:          'div.tab-regions',
				regionClass:           'div.region',
				cssClassActive:        'active',
				cssClassOff:           'off',
				cssClassRegionOff:     'off',
				cssClassRegionOn:      'block'
			}, options || {});	
			
			this.cookies = new Array();
			this.tabWidgets = $$(this.options.widgetClass); 
			
			
			for (i=0;i<this.tabWidgets.length;i++) {
				this.tabWidgets[i].widgetNumber    = i;   
				this.tabWidgets[i].ulDom           = $$("#" + this.tabWidgets[i].id + " " + this.options.ulClass + ":first-child");
				this.tabWidgets[i].ul              = this.tabWidgets[i].ulDom[0].id;
				this.tabWidgets[i].tabsLi          = $$("#" + this.tabWidgets[i].ul + " " + this.options.ulClass + " li");
				
				this.tabWidgets[i].tabsA           = $$("#" + this.tabWidgets[i].ul + " " + this.options.ulClass + " li " + "a");
			
				this.tabWidgets[i].tabRegionDom    = $$("#" + this.tabWidgets[i].id + " " + this.options.regionsClass);
				this.tabWidgets[i].tabRegionId     = this.tabWidgets[i].tabRegionDom[0].id;
				this.tabWidgets[i].tabRegions      = $$("#" + this.tabWidgets[i].id + " #" + this.tabWidgets[i].tabRegionId + " " + this.options.regionClass); 
				
				this.tabWidgets[i].currentNumber   = 0;
				this.tabWidgets[i].currentLi       = this.tabWidgets[i].tabsLi[0];
				this.tabWidgets[i].currentA        = this.tabWidgets[i].tabsA[0];
				this.tabWidgets[i].currentRegion   = this.tabWidgets[i].tabRegions[0];
				this.tabWidgets[i].isqString = 0;
			}
			
						
			for (i=0;i<this.tabWidgets.length;i++) {
				if(this.tabWidgets[i].id != null) {
					if(Browser.getQueryStringValue(this.tabWidgets[i].id) != null) {
						if(Browser.getQueryStringValue(this.tabWidgets[i].id).toInt()) {
							var indexTab = Browser.getQueryStringValue(this.tabWidgets[i].id);
							this.tabWidgets[i].isqString = 1;
							this.tabWidgets[i].currentNumber = indexTab;
							this.tabWidgets[i].currentLi     = this.tabWidgets[i].tabsLi[indexTab];
							this.tabWidgets[i].currentA      = this.tabWidgets[i].tabsA[indexTab];
							this.tabWidgets[i].currentRegion = this.tabWidgets[i].tabRegions[indexTab];
							Cookie.write(this.tabWidgets[i].id, indexTab); 
						} else {
							var indexTab = this.tabWidgets[i].tabsLi.indexOf($(Browser.getQueryStringValue(this.tabWidgets[i].id)))
							this.tabWidgets[i].isqString = 1;
							this.tabWidgets[i].currentNumber = indexTab;
							this.tabWidgets[i].currentLi     = this.tabWidgets[i].tabsLi[indexTab];
							this.tabWidgets[i].currentA      = this.tabWidgets[i].tabsA[indexTab];
							this.tabWidgets[i].currentRegion = this.tabWidgets[i].tabRegions[indexTab];
							Cookie.write(this.tabWidgets[i].id, indexTab); 
						}				
					}
				} 
	
			
			for (i=0;i<this.tabWidgets.length;i++) {
				if(Cookie.read(this.tabWidgets[i].id) != null && this.tabWidgets[i].isqString == 0) {
					var cookie = Cookie.read(this.tabWidgets[i].id);
					this.tabWidgets[i].currentNumber = cookie;
					this.tabWidgets[i].currentLi =     this.tabWidgets[i].tabsLi[cookie];
					this.tabWidgets[i].currentA =      this.tabWidgets[i].tabsA[cookie];
					this.tabWidgets[i].currentRegion = this.tabWidgets[i].tabRegions[cookie];		
				} 
			}
			
		}
			
			

			
			for (i=0;i<this.tabWidgets.length;i++) {
				
				this.setUI(this.tabWidgets[i].currentNumber, this.tabWidgets[i].currentLi, this.tabWidgets[i].currentRegion,		this.tabWidgets[i].currentA, this.tabWidgets[i].widgetNumber);
				j = 0;
				
				
				
				$each(this.tabWidgets[i].tabsA, function(el){
					el.widgetNumber = i;
					el.number  = j;
					el.parent  = this.tabWidgets[i].ul;
					el.onclick = this.onclick.pass(el, this);			
					j = j + 1;
					
				}, this);
				
			}		
			
			
		},
		
		
		setUI: function(currentNumber, currentLi, currentRegion, currentA, currentWidget) { 
						
			if(this.tabWidgets[currentWidget].currentRegion.className.contains('demoCaptivate')) {
				this.swf = this.tabWidgets[currentWidget].currentRegion.getElements("embed");
				if(this.swf.length == 0) {
					this.swf = this.tabWidgets[currentWidget].currentRegion.getElements("object");
				}
				if(this.swf.length > 0) {
					this.swf[0].SetVariable("cpSkinLoader_mc.rdcmndPause", 1);
					this.swf[0].SetVariable("_root.rdcmndPause", 1);
				}
			}
			
			
			this.tabWidgets[currentWidget].currentLi.removeClass(this.options.cssClassActive);
			this.tabWidgets[currentWidget].currentLi.addClass(this.options.cssClassOff);
			
			this.tabWidgets[currentWidget].currentRegion.removeClass(this.options.cssClassRegionOn);
			this.tabWidgets[currentWidget].currentRegion.addClass(this.options.cssClassRegionOff);
			
			currentLi.removeClass(this.options.cssClassOff);
			currentLi.addClass(this.options.cssClassActive);
			
			currentRegion.removeClass(this.options.cssClassRegionOff);
			currentRegion.addClass(this.options.cssClassRegionOn);
			
			this.tabWidgets[currentWidget].currentNumber = currentNumber;
			this.tabWidgets[currentWidget].currentLi     = currentLi;
			this.tabWidgets[currentWidget].currentA      = currentA;
			this.tabWidgets[currentWidget].currentRegion = currentRegion;
						
		},
		
		
		onclick: function(link) {
			Cookie.write(this.tabWidgets[link.widgetNumber].id, link.number);
			this.setUI(link.number, this.tabWidgets[link.widgetNumber].tabsLi[link.number], this.tabWidgets[link.widgetNumber].tabRegions[link.number],	this.tabWidgets[link.widgetNumber].tabsA[link.number], link.widgetNumber)
			
			return false;
		}
	}


window.addEvent('domready', Tabs.initialize.bind(Tabs,'tab-widget'));


