// JavaScript Document

var total = null;
window.addEvent('domready', function() {
	//$('videos').setStyle('margin-left','208px');
	//delays slide in of the videos for 2 seconds
	
//	(function() { 
//		var videoSlideIn = new Fx.Morph('videos', {duration: 500, transition: Fx.Transitions.Sine.easeOut});
//		videoSlideIn.start({ 'margin-left': ['208px','0px'] });
//			
//		if (total>208) {
//			$('videoOuterWrapper').addEvent('mouseenter', function() {
//				var Slide = new Fx.Morph('videoNavBg', {duration:250, transition:Fx.Transitions.Sine.easeOut, link:'ignore'});
//				Slide.start({ 
//					'margin-top': ['30px'],
//					'height': ['0px']
//				});	
//			});
//			
//			$('videoOuterWrapper').addEvent('mouseleave', function() {
//				var Slide = new Fx.Morph('videoNavBg', {duration:250, transition:Fx.Transitions.Sine.easeOut, link:'ignore'});
//				Slide.start({ 
//					'margin-top': ['0px'],
//					'height': ['30px']
//				});	
//			});
//			
//			$('previous').setStyle('display','block');
//			$('next').setStyle('display','none');
//			
//		} else {
//			$('previous').setStyle('display','none');
//			$('next').setStyle('display','none');
//		}
//		
//	}).delay(2000);
	
	
//	//Gets the ul with the highest number of videos within it so we know how much sliding we can do!
//	var ul = $$('#videos ul');
//	var margin = 0;
//	total = ul[0].getChildren('li').length;
//	if (total < ul[1].getChildren('li').length) {
//		total = ul[1].getChildren('li').length;
//	}
//	total = total*104; //sets total to the width of the highest ul
//
//	//sets up the sliding of the video when clicking on the previous button
//	$('previous').addEvent('click', function(event) {
//		event.stop();									 
//		var tmp = (total-208)-((total-208)*2);
//		if (tmp<margin) {
//			var Slide = new Fx.Morph('videos', {duration:500, transition:Fx.Transitions.Sine.easeOut, link:'ignore'});
//			margin=margin-208;
//			
//			Slide.addEvent('complete', function() { 
//				if (tmp>=margin) {
//					$('previous').setStyle('display','none');
//					$('next').setStyle('display','block');
//				} else {
//					$('previous').setStyle('display','block');
//					$('next').setStyle('display','block');
//				}
//			});
//			
//			//margin=margin-208;
//			Slide.start({ 'margin-left': [margin + 'px'] });	
//		}
//	});
//	
//	//sets up sliding when click on next button
//	$('next').addEvent('click', function(event) {
//		event.stop();
//		if (margin<0) {
//			var Slide = new Fx.Morph('videos', {duration:500, transition:Fx.Transitions.Sine.easeOut, link:'ignore'});
//			//Slide.addEvent('complete', function() { margin=margin+208; })
//			margin=margin+208;
//			
//			Slide.addEvent('complete', function() { 
//				if (margin>=0) {
//					$('previous').setStyle('display','block');
//					$('next').setStyle('display','none');
//				} else {
//					$('previous').setStyle('display','block');
//					$('next').setStyle('display','block');
//				}
//			});
//			
//			Slide.start({ 'margin-left': [margin + 'px'] });	
//		}
//	});
	
	var a = $(document.body).getElements('a[rel=video]');
	for (x=0; x<a.length; x++) {
		a[x].addEvent('click', function(event) {
			event.stop();
			
			DestroyVideo();
			var url = this.toString();
			var width = url.toLowerCase().substring(url.indexOf('?w=')+3, url.indexOf('h=')-1);
			var height = url.toLowerCase().substring(url.indexOf('h=')+2);
			var win = window.getScrollSize();
			var overlay = new Element ('div', {
				'id':'videoOverlay',
				'styles': {
					'height':win.y + 'px',
					'width':win.x + 'px'
				},
				'events': {
					'click' : function() {
						DestroyVideo();	
					}
				}
			});
			var flashContainer = new Element('div', {
				'id' : 'videoContainer',
				'styles' : {
					'margin-left' : '-' + ((width/2)+10) + 'px',
					'margin-top' : '-' + ((height/2)+10) + 'px',
					'height' : '0px',
					'width' : width + 'px'
				},
				'events': {
					'click': function() {
						DestroyVideo();		
					}
				}
			});
			$(document.body).appendChild(overlay);
			$(document.body).appendChild(flashContainer);
			var showFlash = new Fx.Morph('videoContainer', {duration: 500, transition: Fx.Transitions.Sine.easeOut});
			showFlash.addEvent('complete', function() {
				var flash = new SWFObject(url, "flash-video", width, height, "8", "#ffffff");
				flash.write('videoContainer');					
			});
			showFlash.start({
				'height': [0, height]
			});

		}); //Ends Click of Video Thumbnail
	} //Ends for loop
}); //Ends dom ready

function DestroyVideo() {
	if ($('videoOverlay')!=null) {
		$('videoOverlay').destroy();
		$('videoContainer').destroy();
	}
};

