// Function to put 'now playing on correct thumnail on player
find_now_playing = function(){
	playing_vid_id = $('div.vimeo-currently-playing-information').attr('id');
		
	$('ul.vimeo-other-videos li').each(function(){
		curr = $(this).attr('class');
		if(playing_vid_id==curr){
			$(this).find('div.top').html('currently viewing');
			$(this).find('div.top').slideDown('fast');
			$(this).find('div.status').addClass('playing');
		}
	});
};

// Resets all thumbnail on player to 'click to play'
reset_click_to_play = function(){
	$('ul.vimeo-other-videos li').each(function(){
		$(this).find('div.top').html('click to play');
		$(this).find('div.top').slideUp('fast');
		$(this).find('div.status').removeClass('playing');
	});
};

$(document).ready(function() {
	
	if($('ul.vimeo-other-videos').length>0){
		
		// When loaded, find what is loaded by defualt
		find_now_playing();
		
		// On thumbnail hover
		$('ul.vimeo-other-videos li').hover(
			function(){		
				if(!$(this).find('div.status').hasClass('playing')){
					$(this).find('div.top').slideDown('fast');
				}
			},
			function(){		
				if(!$(this).find('div.status').hasClass('playing')){
					$(this).find('div.top').slideUp('fast');
				}
			}
		);
		
		// On thumbnail click
		$('ul.vimeo-other-videos li').click(function(){
			
			/* Ignore click if it's already loaded */
			if(!$(this).find('div.status').hasClass('playing')){
				$('.vimeo-loading-overlay').fadeIn(25).delay(2000).fadeOut(800);
				
				clicked_id = $(this).attr('class');
						
				/* Get the ID of the click and save it to the information div */
				$('div.vimeo-currently-playing-information').attr('id',clicked_id);
				
				/* Reset the 'now playing' and 'click to play' slidedowns */
				$('ul.vimeo-other-videos li').each(function(){
					if( $(this).attr('class')!=clicked_id ){
						$(this).find('div.top').html('click to play');
						$(this).find('div.top').slideUp('fast');
						$(this).find('div.status').removeClass('playing');
					}else{
						$(this).find('div.top').html('currently viewing');
						$(this).find('div.status').addClass('playing');
					}
				});
				
				/* Load the vimeo video in the iframe */
				link = "http://player.vimeo.com/video/"+$(this).attr('class')+"?js_api=1&js_swf_id=player_1&fp_version=10&title=false&byline=false&portrait=false&color=A03438"
				$('.vimeo-player iframe').attr('src',link);
				
				/* Chage the overlay information  */
				$('#current-title').html(
					$(this).find('div.bottom h3').html()
				);
				$('#current-descrip').html(
					$(this).find('div.description').html()
				);
				$('#current-url').attr('href',
					$(this).find('div.url').html()
				);
				$('#current-url').html(
					'&raquo; '+$(this).find('div.bottom h3').html() + ' Home Page'
				);
			}
		});	
	}
	
	VimeoEmbed.init();
	
	// Fade The Loading Overlay Out after a few milliseconds
	$('.vimeo-loading-overlay').delay(1200).fadeOut(800);
	if($('ul.vimeo-other-videos').length>0){
		$('ul.vimeo-other-videos li').delay(1200).fadeIn(800);
	}		
	// When the closed button is clicked
	$('.vimeo-currently-playing-information span.close').click(function(){
		$(this).parent().slideUp('fast'); 
	});

});
