if (typeof jQuery !== 'undefined') {
 $ = jQuery.noConflict();
}

 // define PNC if it does not exist
if (!PNC) var PNC = {};

PNC.Video = {
	open: function (element)
	{

		// remove current video if another is selected
		jQuery('#overlay, #flash_container').remove();
		var link = false;

		if (typeof element == 'string')
			var parameters = { media_path: element};//file_path };
		else
		{
			var parameters = { media_path: element[0].href };
			$link.addClass('remote-processing'); // used to not make multiple calls
			link = true;
		}

		jQuery.get(PNC.Video.url, parameters, function (response){
			// build containers
			var overlay = jQuery('<div id="overlay">');

			jQuery('body').prepend(overlay);
			jQuery('body').prepend('<div id="flash_container"><div id="flash_player" /></div>');

			jQuery('#overlay').fadeTo('slow', 0.7, function (){
				jQuery('#flash_player').html(response);

				if (link)
					$link.removeClass('remote-processing');
			});
		}, 'html');
	},
	close: function ()
	{
		jQuery('#flash_container').remove();
		jQuery('#overlay').fadeTo('slow',0,function (){ jQuery(this).remove() });
		jQuery('.remote-processing').removeClass('remote-processing');
	}
};

// redefine path as needed
PNC.Video.url = '/themes/gb/gb_video_player.php';

jQuery(function (){
	// define supported extensions
	var selectors = ['mov','flv','mp4', 'f4v'];

	for (var i in selectors)
		selectors[i] = 'a[href$=.'+selectors[i]+']';

	jQuery(selectors.join(',')).click(function (){
		$link = jQuery(this);

		if (!$link.hasClass('remote-processing')){
			PNC.Video.open($link);

		}

		return false;
	});

	jQuery('#player .close_player, #overlay').live('click',function (){

	//jQuery('#player .close_player, #overlay, #flash_container').live('click',function (){
		PNC.Video.close();
		return false;
	});

	// remove video on esc
	jQuery(document).keyup(function (event){
		if (event.keyCode == 27)
			PNC.Video.close();
	});
});

