

$(document).ready(function() {
	
	
	
	$('.videofocus').bind('click',function(){
		playvideo('#'+$(this).attr('id'));
	});
	
	
});
	
var playvideo=function(selector){
	
	//$('#fullSlideShow ul').cycle('pause');
	
	$('#videocontainer').html('');
	//$(selector).find('iframe:first').clone().attr('id','player_1').appendTo('#videocontainer');
	
	if ($.browser.msie ){$('#videocontainer').append('<div class="object"><object width="780" height="'+$(selector).data('video_h') +'"><param name="allowfullscreen" value="true" /><param name="allowscriptaccess" value="always" /><param name="movie" value="http://vimeo.com/moogaloop.swf?clip_id='+ $(selector).data('video_c')+ '&amp;server=vimeo.com&amp;show_title=0&amp;show_byline=0&amp;show_portrait=0&amp;color=00adef&amp;fullscreen=1&amp;autoplay=0&amp;loop=0" /><embed src="http://vimeo.com/moogaloop.swf?clip_id='+ $(selector).data('video_c')+ '&amp;server=vimeo.com&amp;show_title=0&amp;show_byline=0&amp;show_portrait=0&amp;color=00adef&amp;fullscreen=1&amp;autoplay=0&amp;loop=0" type="application/x-shockwave-flash" allowfullscreen="true" allowscriptaccess="always" width="780" height='+$(selector).data('video_h') +'></embed></object></div>');}
	else{
	$('#videocontainer').append('<iframe id="player_1" src="http://player.vimeo.com/video/'+$(selector).data('video_c') +'?api=1&amp;player_id=player_1&amp;title=0&amp;byline=0&amp;portrait=0&amp;color=C1002B&amp;" width="780" height="'+$(selector).data('video_h') +'" frameborder="0"></iframe>');}
	var subtext=$(selector).find('p.focus_subtit:first').html();
	if (subtext==null) subtext='';
	$('#videocontainer').append('<div id="videocloser" class="sez_head group"><img src="/assets/images/close.png" /><div class="sec_number" >&nbsp;</div><h6 id="labelclose">Close</h6><h6 >'+$(selector).find('h6:first').html()+'</h6><p>'+subtext+'</p>	</div>');
	
	$('#videocloser').bind('click',function(){
		closevideo();
	});
	
	
	  if (!(( $.browser.msie )&&($.browser.version<8))) 
	    {
	    	var pl = document.querySelector("#player_1");
	    	$f(pl).addEvent("ready", playerready);
	    	$('#videoplayer').stop(true,true).fadeIn(500);
	    }
		
	else{
	$('#videoplayer').css('display','block')
	}
	
	
	var player_h=$("#player_1").height();
	var section_h=$(".section:first").height();
	if($(".section:first").data('original_h'))
		section_h=$(".section:first").data('original_h');
	//console.log('sh:'+section_h+' ph:'+(player_h+175));
	if( (player_h+175)>section_h){
		if(!$(".section:first").data('original_h'))
			{
				$(".section:first").data('original_h',section_h);
				
			}
			
		//$(".section:first").stop(true,true).delay(500).animate({height: (player_h+175)+'px'}, 500);
		$(".section:first").stop(true,true).delay(500).css({height: (player_h+175)+'px'});
		//console.log( $(".section:first").height());
		
		if($(".section:first").css('position')!='relative'){
			$(".section:gt(0)").stop(true,true).delay(500).animate({top: (player_h+175)+'px'}, 500);	
		}
	}
	else{
		$(".section:first").stop(true,true).delay(500).animate({height: section_h+'px'}, 500);
		$("#player_1").css('marginTop',((section_h-54-player_h)/2)+54+'px');
		//if($(".section:first").css('position')!='relative'){
		//	$(".section:gt(0)").stop(true,true).delay(500).animate({top: section_h+175+'px'}, 500);	
		//}
	}

	$('body, html').animate({  
	        scrollTop: 0
	    }, 100 );
}

var playerready=function(){
	
	$('#player_1').stop(true,true).fadeIn(200);
	var froogaloop = $f('player_1');
	
	function setupEventListeners() {
	    froogaloop.addEvent('finish', function(data) {
	       
	       closevideo();
	    });
	    
	    froogaloop.addEvent('pause', function(data) {
	        //alert('pause event');
	        
	    });
	    
	    froogaloop.addEvent('play', function(data) {
	        //alert('play event');
	       
	    });
	
	};
	setupEventListeners();
	froogaloop.api('play');
}


var closevideo=function(){
	//$('#fullSlideShow ul').cycle('resume');
	var froogaloop = $f('player_1');
	froogaloop.api('pause');
	$('#player_1').fadeOut(200);
	
	if( ($(".section:first").data('original_h'))||($(".section:first").data('original_h')==0)){
		$(".section:first").stop(true,true).delay(200).animate({height: $(".section:first").data('original_h')+'px'}, 200);
		if($(".section:first").css('position')!='relative'){
			$(".section:gt(0)").stop(true,true).delay(200).animate({top: $(".section:first").data('original_h')+'px'}, 200);	
		}
	}
	$('#videoplayer').stop(true,true).delay(400).fadeOut(500);
};
