var overlayApi; // store video api object globally
var nextVideo; // temproarily stores the next video's path

function playVideo( url ) {
	nextVideo = url;
	
	// load overlay, wait for "onload" to issue video play command
	overlayApi.load();
}

$(function () {
	overlayApi = $('#vidplayer_holder').overlay({
		target: '#vidplayer_holder',
		top: 'center',
		left: 'center',
		closeOnClick: true,
		close: '#video_close',
		expose: {
			color: '#012f53',
			opacity: 0.9,
			loadSpeed: 'fast',
			closeOnClick: true

		},        
		onLoad: function() {
			
			// embed video player swf
			var flashVars = {
			};
			
			var params = {
				wmode: 'transparent',
				menu: 'false',
				allowScriptAccess: 'always',
				allowFullScreen: 'true'
			};

			swfobject.embedSWF("/swf/video_player.swf", "vidplayer", "640", "360", "9", null, flashVars, params);
			
			//$('#vidplayer').appendTo( '#vidplayer_holder' );
			setTimeout( function() {
				$('#vidplayer').get(0).loadVideo( nextVideo, 'true' );
			}, 1000 );
		},
		onBeforeClose: function() {
			$('#vidplayer').get(0).stopVideo();
			//location.reload();
			//$('#vidplayer').appendTo( 'body' );
			//setTimeout( function() {
				$('#vidplayer').remove();
				var newdiv = $(document.createElement('div'));
				newdiv.attr('id', 'vidplayer');
				$('#vidplayer_holder').append( newdiv );
			//}, 1000 );
		},
		api: true
	});
	
	// make each video link play the associated flv (the "rel" attribute of the link)
	$('a.video_link').click( function() {
		playVideo( $(this).attr('rel') );
	});
});

