// SliderSwap
var SliderSwap = Class.create();
Object.extend(Object.extend(SliderSwap.prototype, AC.ContentSwap.prototype), {

	initialize: function(selectorClass, contentClass) {
		// get hash (default tab)
		if (document.location.hash != '' || document.location.hash != null || document.location.hash) {
			var first = document.location.hash.substring(1, document.location.hash.length);
		}

		// get lists of selectors and content in order
		this.selectorList = document.getElementsByClassName(selectorClass);
		this.contentList = document.getElementsByClassName(contentClass);
		this.arrowList = [];

		// set default state
		for(var i=0; i<this.selectorList.length; i++) {
			this.arrowList[i] = (this.selectorList[i].getElementsByClassName('arrow').length>0) ? this.selectorList[i].getElementsByClassName('arrow')[0] : null;
			if (Element.hasClassName(this.selectorList[i], 'active')) {
				this.lastSelectorIndex = i;
				this.arrowList[i].style.top = this.selectorList[i].offsetHeight/2+'px';
			} else {
				this.contentList[i].style.display = 'none';
				this.arrowList[i].style.display = 'none';
				this.arrowList[i].style.top = '0';
			}
		}

		// set default state (from hash)
		
		//if (selectorClass == 'slider') this.swapContent(document.initialSelection);
			
		/*	
		if (first) {
			for (var i=0; i<this.selectorList.length; i++) {
				var link = this.selectorList[i].getElementsByTagName('a')[0];
				var href = link.href.substring(link.href.indexOf('#')+1, link.href.length);
				if (first.indexOf(href)>-1) this.swapContent(i);
			}
		}
		*/

		// set selector events
		this.setEvents();
	},
	
	setEvents: function() {
		for(var i=this.selectorList.length-1, selector; selector=this.selectorList[i]; i--) {
			Event.observe(selector, 'mouseover', function() { Element.addClassName(this, 'hover'); }.bind(selector), false);
			Event.observe(selector, 'mouseout', function() { Element.removeClassName(this, 'hover'); }.bind(selector), false);
			Event.observe(selector, 'click', this.swapContent.bind(this, i), false);
		}
		Event.observe('hometrigger', 'click', this.swapContent.bind(this, 0), false);
	},
	
	swapContent: function(selectorIndex) {
		// set variables for selectorIndex
		var selector = this.selectorList[selectorIndex];
		var content = this.contentList[selectorIndex];
		var contentArrow = this.arrowList[selectorIndex];
	
		// swap the selector state
		for(var i=0; i<this.selectorList.length; i++) {
			if(i != selectorIndex) {
				if(Element.hasClassName(this.selectorList[i], 'active')) Element.removeClassName(this.selectorList[i], 'active');
			} else {
				if(!Element.hasClassName(selector, 'active')) Element.addClassName(selector, 'active');
			}
		}
	
		// if we're doing a new one, swap the content
		if(selectorIndex != this.lastSelectorIndex) {
			var lastContent = this.contentList[this.lastSelectorIndex];
			var lastContentArrow = this.arrowList[this.lastSelectorIndex];
	
	        if (lastContent && lastContentArrow) {
 	          new Effect.Parallel([
				new Effect.Fade(lastContentArrow, { sync:true,
					afterFinish: function(obj) {
						obj.element.style.top = '0';
					}
				}),
				new Effect.BlindUp(lastContent, { sync:true }),
				new Effect.BlindDown(content, { sync:true }) ],
				{ duration:.4 }
			  );
			} 
			else
			{
			    new Effect.Parallel([new Effect.BlindDown(content, { sync:true }) ], { duration:.4 });
			}
			
			new Effect.Appear(contentArrow, { duration:.5,
				beforeUpdate: function(obj) {
					obj.element.style.top = obj.element.parentNode.offsetHeight/2+'px';
				}
			});

			// set the last selector index to the current one
			this.lastSelectorIndex = selectorIndex;
		}
		
		return false;
	}
});

// set slider swap (sidebar nav)
Event.observe(window, 'load', function() {
  new SliderSwap('slider', 'slidercontent'); 
  new SliderSwap('sliderMovies', 'slidercontentMovies'); 
  }, false);
