$(document).ready(function(){
	
	function initoverlay(){
		var adiv = $('<div class="overlay">&nbsp;</div>');
		$(adiv).height(  )
		$('body').append(adiv);
		
		
	}	
	
	
	
	function closeoverlay(){
		$('.overlay').css( {display : 'none' } );
		$f(0).stop();
		$('.videlem').remove();
		$('.videowarpper').remove();
		$('body').unbind('click' ,closeoverlay);
		currentPlay = false;
	}
	
	function displayoverlay(){
		$('body').css( {overflow:'hidden'} );
		console.log($('body').height() );
		console.log(screen.availHeight );
		var h = ( $('body').height() >screen.availHeight ) ? $('body').height()  : screen.availHeight;
		$('.overlay').css( {display : 'block' , width : screen.availWidth, height : h , opacity: '0.5', overflow : 'hidden' } ) ;
		$('body').bind('click' ,closeoverlay);
	}
	
	initoverlay();
	
	
	
	var currentPlay = false;
	$('a[href$=".flv"]').click(viewVideo);
	$('a[href$=".f4v"]').click(viewVideo);
	$('a[href$=".mp4"]').click(viewVideo);
	
	 $('a[href$=".flv"]').each(function(){
	 	if( $(this).children('img').length >0 ){
	 		addplaybutton(this);
	 	}
	 		
	 	
	 });
	 $('a[href$=".f4v"]').each(function(){
	 	if( $(this).children('img').length >0 ){
	 		addplaybutton(this);
	 	}
	 		
	 	
	 });
	 $('a[href$=".mp4"]').each(function(){
	 	if( $(this).children('img').length >0 ){
	 		addplaybutton(this);
	 	}
	 		
	 	
	 });
	 function addplaybutton( elem ){
	 	$(elem).css({
	 		display:'block',
				
				position: 'relative'
	 	});
	 	var playimg = $('<img src="/resources/application/layouts/public/images/player.png">');
		$(playimg).css({
				position:'absolute' ,
				top: '50px' ,
				left :'91px'
		});
		$(elem).append(playimg);
	 }
	 passage = 1;
	function viewVideo(eventObject){
		var href = '';
		console.log( passage );
		passage ++ ;
		if(eventObject.currentTarget.src){
			var p = $(this).parent('a');
			
		}
		else{
			href = $(this).attr('href');
		}
		displayoverlay();
		
		//display video
		var adiv= $('<div>').addClass('videowarpper');

		centerThis(adiv);
		var closeBtn = $('<a>').addClass('close');
		$(adiv).append(closeBtn);
		$(closeBtn).bind('click' , closeoverlay );
		
		var lnk = $('<a>').attr('href', href ).attr('rel', href ).attr('id', 'player').addClass('videlem').html( href );
		lnk.css({
				display:'block',
				width: '640px',
				height: '480px',
				margin: '10px' ,
				position: 'relative'
		});
		
		
		
		
		$(adiv).append(lnk);
		$('body').append(adiv);
		console.log( href);
			
		var player =	$(lnk).flowplayer( "/resources/application/modules/content/flowplayer/flowplayer-3.2.7.swf" , {
					clip :{
						url : href , 
						autobuffering : true ,
						autoplay : true
					} ,
					plugins: {
			            controls: {
			                autoHide: "never"    // we don't want the controlbar to hide with an overlay
			            }
			        }
				}
			);
				
			$f(0).play(0);	
			
			
		return false;
	}
	function centerThis(element) {
	var windowHeight = $(window).height();
	var windowWidth = $(window).width();
	var elementHeight = $(element).height();
	var elementWidth = $(element).width();
	 
	var elementTop, elementLeft;
	 
	if (windowHeight <= elementHeight) {
	elementTop = $(window).scrollTop();
	} else {
	elementTop = ((windowHeight - elementHeight) / 2) + $(window).scrollTop();
	}
	 
	if (windowWidth <= elementWidth) {
	elementLeft = $(window).scrollLeft();
	} else {
	elementLeft = ((windowWidth - elementWidth) / 2) + $(window).scrollLeft();
	}
	 
	$(element).css({
	'top': elementTop,
	'left': elementLeft
	});
	}
});

