$(document).ready(function(){
	var videoPlayer = $('body').attr('class');
	$('.input_clear').click(function(){
									 
		var original_val = $(this).attr('data-original');
		if($(this).val() == original_val){
			$(this).val('');
		}
		$(this).bind('blur',function(){
			if($(this).val() == ''){
				$(this).val(original_val);	
			}
		});
	});

						   
/****************************
		UPPER ROTATION AREA
*****************************/
	//GLOBAL VARIABLES, DOH!
	var rotationID;
	var restartID;
	var theCount = $('.rotation_block_item.rotation').size();
	var whereAt = $('.rotation_block_item').index('.selected');
	var rotationSpeed = 850;
	//THE AUTO ROTATION FUNCTION
	function auto_rotation(theCount){
		rotationID = setInterval(function (){
			if(whereAt < theCount-1){	
				whereAt++;	
			}else{
				whereAt = 0;
			}
			$('.rotation_block_item').removeClass('selected').fadeOut(rotationSpeed);
			$('.rotation_block_item').eq(whereAt).addClass('selected').fadeIn(rotationSpeed);
		},8000);
	};
	//SHOW EVENT BLOCKS, THESE DON'T AUTO ROTATE
	function show_event_block(elem){
		var showThisBlock = elem.attr('rel');
		$('.rotation_block_item').fadeOut(rotationSpeed);
		$(showThisBlock).fadeIn(rotationSpeed);
	}
	//PAUSE ROTATION WHEN EVENT BUTTONS ARE PRESSED
	function pause_rotation(){
		clearInterval(rotationID);
		restartID = setTimeout(function(){
			$('.rotation_block_item').fadeOut(rotationSpeed);
			$('.rotation_block_item.selected').fadeIn(rotationSpeed);
			auto_rotation(theCount);
		},8000);
	}
	
//EVENTS
	
	//TEASER TRAILER BUTTON
	$('#teaser_trailer_button').click(function(){
		clearInterval(rotationID);
		$('.rotation_block_item').fadeOut(rotationSpeed);
		$('#quicktime_video').fadeIn(rotationSpeed,function(){
			setTimeout(function(){		
				if(videoPlayer == 'reg'){						
					var movie = document.theVideo;


					if (document.addEventListener ){
						setTimeout(function(){
							movie.Play();
						},1000);
						movie.addEventListener('qt_canplay',function(){
							movie.Play();
						},false);
						movie.addEventListener('qt_ended',videoDone,false);
					}else{
						// IE
						movie.Play();
						movie.attachEvent('onqt_ended', videoDone);
						//alert(theVid);
					}
					
				}else{
					$('#gtv_box').empty();
					var vidfile = $('#gtv_box').attr('vidfile');
					$('#gtv_box').html('<video controls preload="auto" style="width:643px;height:375px;background:black" id="vidPlayer" src="'+vidfile+'"></video>');
					
					var video = document.getElementById('vidPlayer');			
					video.play();
					video.addEventListener('ended', function () {  
					   this.pause();  
					   videoDone();
					}, false); 
				}
				function videoDone(){
					auto_rotation(theCount);
					$('#quicktime_video').fadeOut(800);
				}
				function testerson(){alert('yeser');}
			},900);
		});
		return false;
	});
	
	//CLOSE VIDEO BUTTON
	$('#quicktime_close a').click(function(){
		
		auto_rotation(theCount);
		if(videoPlayer == 'reg'){
			document.theVideo.Stop();
		}else{
			var video = document.getElementById('vidPlayer');			
			video.pause();
		}
		$('#quicktime_video').fadeOut(300);
		$('.rotation_block_item:first').fadeIn()							   
	});
	
	
	//ROTATION BUTTON CLICKED
	$('.rotation_button').click(function(){
		if($(this).attr('rel') != 'link'){								 
			clearInterval(restartID);
			pause_rotation();
			show_event_block($(this));
			if(videoPlayer == 'reg'){
				$('#quicktime_video').fadeOut(rotationSpeed);
			}else{
				var video = document.getElementById('vidPlayer');			
				video.pause();
				$('#quicktime_video').fadeOut(rotationSpeed);
			}
			
			return false;
		}
	});	    
	$('.rotation_button').hover(
		function(){
			$('.rotation_btn_overlay', this).show();	
		},
		function(){
			$('.rotation_btn_overlay', this).hide();	
		}
	);
	//ROTATION BLOCK MOUSE ENTER
	$('.rotation_block_item').mouseenter(function(){
		clearInterval(rotationID);										  
	});
	//ROTATION BLOCK MOUSE LEAVE
	$('.rotation_block_item').mouseleave(function(){
		auto_rotation(theCount);											  
	});
	//START AUTOROTATION
	(function(){
		auto_rotation(theCount);	
	})();
	
	/******************************
	SCHEDULE TABS
	*******************************/
		$('.date_tab').click(function(){
			var whichTable = $(this).attr('rel');
			$('.date_tab').removeClass('selected');							  
			$('#schedule_table').hide();
			$(whichTable).show();
			$(this).addClass('selected');
			return false;
		});
	
	//QTVIDEO STUFF
	function videoDone(){
		auto_rotation(theCount);
		$('#quicktime_video').fadeOut(800);
		
	}


	
	
});
