// JavaScript Document
window.addEvent('domready', function() {	
	/*Adds events to the now showing and full listing tabs*/
	$('nowShowingLink').addEvent('click', function(event) {
		event.stop();
		this.set('class','active');
		$('nowShowing').set('class','tabcontent active');
		$('fullListingLink').set('class','inactive');
		$('fullListing').set('class','tabcontent inactive');
		if($('scrollbar')!=null) {
			$('scrollbar').setStyle('left', '-10000px');
		}
	});
	
	$('fullListingLink').addEvent('click', function(event) {
		event.stop();
		this.set('class','active');
		$('fullListing').set('class','tabcontent active');
		$('nowShowingLink').set('class','inactive');
		$('nowShowing').set('class','tabcontent inactive');
		if($('scrollbar')!=null) {
			$('scrollbar').setStyle('left', '379px');
		}
	});
	
	/* Following setsup the scroll bar on the fulllisting section of the homepage!*/
	if (showcount>=5) {
		var listingScrollBar = new Element('div', {
			'id':'scrollbar',
			'class':'scrollbar-vert'
		});
		
		var listingScrollHandle = new Element('div', {
			'id':'handle',
			'class':'handle-vert'
		});
		
		if(window.location.search=='?full') {
			listingScrollBar.setStyle('left', '379px');
		}
		
		listingScrollHandle.inject(listingScrollBar);
		listingScrollBar.inject($('leftColumn'),'bottom');
		makeScrollbar( $('fullListing'), $('scrollbar'), $('handle') );
	}
	
	StartCarousel();
});

function makeScrollbar(content,scrollbar,handle,horizontal,ignoreMouse){
	var steps = (horizontal?(content.getScrollSize().x - content.getSize().x):(content.getScrollSize().y - content.getSize().y))
	var slider = new Slider(scrollbar, handle, {	
	steps: steps,
	mode: (horizontal?'horizontal':'vertical'),
	onChange: function(step){
		// Scrolls the content element in x or y direction.
		var x = (horizontal?step:0);
		var y = (horizontal?0:step);
		content.scrollTo(x,y);
	}
	}).set(0);
	if( !(ignoreMouse) ){
	// Scroll the content element when the mousewheel is used within the 
	// content or the scrollbar element.
	$$(content, scrollbar).addEvent('mousewheel', function(e){	
		e = new Event(e).stop();
		var step = slider.step - e.wheel * 30;	
		slider.set(step);					
	});
	}
	// Stops the handle dragging process when the mouse leaves the document body.
	$(document.body).addEvent('mouseleave',function(){slider.drag.stop()});
};

var hide = new Array();
var CurrentSlide = 0;

function StartCarousel() {
	var list = $$('#alsoShowing div.show-collection');
	if (list.length>1) {
		for (x=0; x<list.length; x++) {
			hide[x] = new Fx.Morph(list[x], {
				duration: 750, 
				transition: Fx.Transitions.Sine.easeOut
			});
			
			hide[x].addEvent('complete', function(listItem) {
	
				var list = $$('#alsoShowing div.show-collection');
				for (i=0; i<list.length; i++) {					
					//sets the z-index's of the li's so we can continually loop them hiding them as we go				
					if (listItem==list[i]) {
						list[i].setStyle('z-index',1);
					} else {
						list[i].setStyle('z-index', list[i].getStyle('z-index').toInt()+1 );
					}
				}
				
				this.set({
					'opacity': [1]
				});
							
				(function() { SwapCarousel() }).delay(5000); //inits the next swap
			});
		}
		
		(function() { SwapCarousel() }).delay(5000);
	}
};

function SwapCarousel() {
	var PreviousSlide = null;
	var listlength = $$('#alsoShowing div.show-collection').length;
	hide[CurrentSlide].start({
		'opacity': [1, 0]
	});
	
	if (CurrentSlide<(listlength-1)) {
		CurrentSlide++;
	} else {
		CurrentSlide = 0;
	}
};
