/**
 * Requires jQuery 1.3.2+ (http://jquery.com/)
 * Requires swfobject SWFObject v2.2+ (http://code.google.com/p/swfobject/)
 *
 * How to use:
 * 1) include SWFObject.js - i.e. 
 *		<script type="text/javascript" src="js/swfobject.js"></script>
 * 2) if not already added, include jQuery - i.e.
 * 		<script type="text/javascript" src="js/jquery-1.3.2.min.js"></script>
 * 3) include this Modal.js - i.e.
 * 		<script type="text/javascript" src="ja/modal.min.js"></script>
 * 4) include modal.css - i.e.
 * 		<link rel="stylesheet" type="text/css" href="css/modal.css" >
 *
 * Using in links:
 * 		<a href="[FLVPlayer complient video link - i.e. video/sample.m4v]" class="loadMovie" title="[title]">[LINK TEXT or IMAGE]</a>
 */

var movieUrl = '';
var isPlayingVideo = false;
var video_container_html = '<div id="boxes"><div id="video"><div id="swfFallback">Flash required!</div></div><div id="mask"></div></div>';
var m = '#mask';
var v = '#video';
var p = '#player';

$(document).ready(function() {	
	if(swfobject.hasFlashPlayerVersion('9')){
		
		$('body').prepend(video_container_html);
		swfobject.embedSWF("/shared/video/player.swf", "swfFallback", "525", "318", "9.0.0", "/shared/video/expressInstall.swf", {}, {wmode: 'opaque'},{id:'player',name:'player'});

		//select all the a tag with name equal to modal
		$('a.loadMovie').click(function(e) {
		
			movieUrl = $(this).attr('href'); 
			startPlayer();
			//Cancel the link behavior
			e.preventDefault();

			// disable default click action
			return false;
			
		});
	}
	//if close button is clicked
	$('#video .close').click(function (e) {
		//Cancel the link behavior
		e.preventDefault();
		unloadMovie();
	});		
	
	//if mask is clicked
	$('#mask').click(function () {  unloadMovie(); });			
	
});

function startPlayer(){
	//Get the screen height and width
	var maskHeight = $(document).height();
	var maskWidth = $(window).width();

	//Set height and width to mask to fill up the whole screen
	$(m).css({'width':maskWidth,'height':maskHeight});
	
	//transition effect		
	$(m).fadeIn(500);	
	$(m).fadeTo(200,0.8);	

	//Get the window height and width
	var winH = $(window).height();
	var winW = $(window).width();
          
	//Set the popup window to center
	$(v).css('top',  winH/2-$(v).height()/2);
	$(v).css('left', winW/2-$(v).width()/2);

	//transition effect

	$(v).fadeIn(500, function(){$(p).show();loadMovie();});		 
	
	// google tracking
	//try{pageTracker._trackPageview('/environment/'+movieUrl)} 
	//catch (err){}
}


function setMovie(movie){
	movieUrl = movie;
	startPlayer();
}



function loadMovie(){
	if($(p).is(':visiable') && !isPlayingVideo){
		try{
			$(p)[0].controlMovie(getMovieUrl());
		} catch (err) {
			setTimeout('loadMovie()', 5);
		} 
	} else {
		setTimeout('loadMovie()', 5);
	}		
}
function unloadMovie(){
	if(isPlayingVideo) {
		$(p)[0].controlMovie('stop');
		 hideModal();
	}
}
function hideModal(){
	if(!isPlayingVideo){
	
		$(m).fadeTo(200,1);	
		$(m).fadeOut(500);	
		$(v).fadeOut(500, function(){$(p).hide();})
	} else {
		setTimeout('hideModal()', 5);
	}
}
function getMovieUrl(){
	return movieUrl;
}
function closeVideo(){
	unloadMovie();	
}
function isPlaying(playing){
	isPlayingVideo = playing;
}
// add hover over effect to video images
$(function(){
	$('.loadMovie IMG, .rollOver').hover(
		function(){	var cur = $(this).attr('src');$(this).attr('src', $(this).attr('rel'));	$(this).attr('rel', cur);},
		function(){	var cur = $(this).attr('src');$(this).attr('src', $(this).attr('rel'));	$(this).attr('rel', cur);}

	);
});
