
function startSlideshow(){
	
	
	//variable to track slideshow change count
	var Site = {
		counter: 0
	};
	
	//function to update slideshow
	var addCount = function(){
	
		this.counter++;
		
		if (this.counter < 3) {
			
			
			var fadeOut = new Fx.Morph($("preview" + this.counter), {duration: 1000} )
			
			fadeOut.addEvent('onStart',function(item){
				var anchor = item.getElements('a');
				var itemId = item.getProperty("id");
				anchor.addEvent('click',function(){
					
					fadeOut.cancel();
					fadeIn.cancel();
					item.setStyle('z-index','3');
					item.setStyle('opacity',1);
				})
			});
			fadeOut.addEvent('onComplete',function(item){item.setStyle('z-index','1');});
			fadeOut.start({'opacity':0});


			var fadeIn = new Fx.Morph($("preview" + (this.counter + 1)), {duration: 1000} ).addEvent('onComplete',function(item){item.setStyle('z-index','3')})
			fadeIn.start({'opacity':1});

		}
		else {
		
			var fadeOut = new Fx.Morph($("preview3"), {duration: 1000} )
			
			fadeOut.addEvent('onStart',function(item){
				var anchor = item.getElements('a');
				
				anchor.addEvent('click',function(){
				
					fadeOut.cancel();
					fadeIn.cancel();
					item.setStyle('z-index','3');
					item.setStyle('opacity',1);
				})
			});
			fadeOut.addEvent('onComplete',function(item){item.setStyle('z-index','1');});
			fadeOut.start({'opacity':0});


			var fadeIn = new Fx.Morph($("preview1"), {duration: 1000} ).addEvent('onComplete',function(item){item.setStyle('z-index','3')})
			fadeIn.start({'opacity':1});
			
		
			this.counter = 0;
			
		}
		
	};
	
	//fire slideshow change every designated amount of time
	var slideTimer = addCount.periodical(6000, Site)
	
	
	
	// when posters are clicked, stop the slideshow before starting video playback
	var cancelSlideShow = function(){
		$clear(slideTimer);
		
		var id = this.getProperty("id");
		for(var x=1;x<=3;x++){
			var preview = $("preview" + x);
			
			if (preview.getProperty("id") != id) {
				$("preview" + x).setStyle('display', 'none');
			}else{
				$("preview" + x).setStyle('display', 'block');
			}
		}
		//var vidplayer = this.getElement('.videoplayer');
		//var newVidplayer = vidplayer.clone(true,true);
		//newVidplayer.replaces($('previewlist'));
		//alert(vidplayer);
		//this.setStyle('height','232px');
		//this.parent('ul'setStyle('height','232px');
	}
	var anchorList = $$(".videoplayer").getElements('a');
	$each(anchorList, function(item){
		
		var parentLI = item.getParent('li');
		item.addEvent("click",cancelSlideShow.bind(parentLI));
		
	});
	
	
}

//play a different preview
function playClip(num){

	previewNum = num;
	
	for(var x=1;x<=3;x++){
		
		if (x == previewNum) {
			var videoToPlay = $('preview'+x);
			$('preview'+ x).setStyle('display', 'block');
			$('preview'+ x).setStyle('z-index', '3');
			var video = videoToPlay.getElement('.videoplayer');
		}else{
			$('preview'+ x).setStyle('display', 'none');
		}
	}

	$(video).getElement('.pausePlayToggle').fireEvent('click');

}



//start slideshow on page
window.addEvent('domready', startSlideshow);
