Slider = Class.create();

Slider.prototype = {
	
	initialize: function(container, topContainer, options) {
		this.topContainer = $(topContainer);
		this.container = $(container);
		this.lastExpandedTab = null;
		this.currExpandedTab = null;
		this.sliderTabs = new Array();
		this.setOptions(options);
		this._attachBehaviors();
		
		if (this.options.onLoadShowTab >= this.sliderTabs.length)
			this.options.onLoadShowTab = 0;
		
		this.lastExpandedTab=this.sliderTabs[this.options.onLoadShowTab];
		this.currExpandedTab=this.sliderTabs[this.options.onLoadShowTab];
		// init allapot
		for (var i=0;i<this.sliderTabs.length;i++) {
			if (i != this.options.onLoadShowTab) {
				var top=this.options.tabHeight - this.options.tabHeadHeight;
				if (this.sliderTabs[i].count > this.currExpandedTab.count) {
					top+=this.options.tabHeadHeight * this.sliderTabs[i].count;
				}
				else if (this.sliderTabs[i].count < this.currExpandedTab.count) {
					top=this.options.tabHeadHeight * sliderTabs[i].count;
				}
				this.sliderTabs[i].tab.style.top=top+'px';
			}
			
			if (this.sliderTabs[i] == this.currExpandedTab) {
				this.currExpandedTab.showExpanded();
			}
			else {
				this.sliderTabs[i].showCollapsed();
			}
		}
		this.topContainer.style.display='';
	},

	setOptions: function (options) {
		this.options = {
			collapsedBg : '#333333',
			tabHeadHeight : 25,
			tabHeight : 200,
			onLoadShowTab : 0
		}
		Object.extend(this.options, options || {});
	},

	_attachBehaviors: function () {
		
		var count=0;
		var sliders = this._getDirectChildrenByTag(this.container, 'DIV');
		for (var i=0;i<sliders.length;i++) {
			var tabChild = this._getDirectChildrenByTag(sliders[i], 'DIV');
			//alert(tabChild[0].firstChild);
			var btn = tabChild[0].firstChild.firstChild;
			//var btn = Class.create();
			this.sliderTabs.push(new Slider.Tab(this, sliders[i], btn, count++));
		}
		
	},
	_getDirectChildrenByTag: function(e, tagName) {
		var kids = new Array();
		var allKids = e.childNodes;
		for (var i=0;i<allKids.length;i++) {
			if (allKids[i] && allKids[i].tagName && allKids[i].tagName == tagName)
				kids.push(allKids[i]);
		} 
		return kids;
	},

	showTab: function (sliderTab, animate) {
		//alert(sliderTab);
		if (this.lastExpandedTab == sliderTab)
			return ;
		this.lastExpandedTab.showCollapsed();
		//if (this.options.onHideTab) {
		//	this.options.onHideTab(this.lastExpandedTab);
		//}
		this.currExpandedTab = sliderTab;
		for (var i=0;i<this.sliderTabs.length;i++) {
			if (this.sliderTabs[i].count == sliderTab.count) {
				var top = this.options.tabHeadHeight * this.sliderTabs[i].count;
			}
			else if (this.sliderTabs[i].count > sliderTab.count) {
				var top = (this.options.tabHeadHeight *  (this.sliderTabs[i].count-1)) + this.options.tabHeight;
			}
			else if (this.sliderTabs[i].count < sliderTab.count) {
				var top = this.options.tabHeadHeight * this.sliderTabs[i].count;
			}
			this.sliderTabs[i].tab.style.top=top+'px';
		}
		this.lastExpandedTab = sliderTab;
		sliderTab.showExpanded();
	}

}

Slider.Tab = Class.create();
Slider.Tab.prototype = {
	
	initialize: function(slider, tab, btn, count) {
		this.count = count;
		this.slider = slider;
		this.tab = tab;
		this.btn = btn;
		this._attachBehaviors();
	},

	_attachBehaviors: function () {
		this.tab.style.top=this.slider.options.tabHeadHeight * this.count;
		this.btn.onclick = this.btnClicked.bindAsEventListener(this);
	},

	collapse: function() {
		/*
		var top=this.slider.options.tabHeight - this.slider.options.tabHeadHeight;
		if (this.count > this.slider.currExpandedTab.count) {
			top+=this.slider.options.tabHeadHeight * this.count;
		}
		else if (this.count < this.slider.currExpandedTab.count) {
			top=this.slider.options.tabHeadHeight * this.count;
		}
		this.tab.style.top=top+'px';
		*/
	},
	btnClicked: function (e) {
		//alert("click" +e);
		this.slider.showTab(this, 0);
	},
	
	showCollapsed: function () {
		this.expanded = false;
		this.btn.src="http://static.fidesz.hu/images/plus.gif";
		this.tab.style.height=this.slider.options.tabHeadHeight+'px';
		this.btn.style.cursor='pointer';
		//alert("collapsed:" +this.count);
	},

	showExpanded: function () {
		this.expanded = true;
		this.btn.src="http://static.fidesz.hu/images/minus.gif";
		this.tab.style.height=this.slider.options.tabHeight+'px';
		this.btn.style.cursor='default';
	}

}
