/**
 * jQuery.nexVideo
 * Copyright (c) 2009 Christof Haemmerle - Nex9 - http://www.nex9.com
 * Dual licensed under MIT and GPL.
 *
 * @author Christof Haemmerle
 * @version 0.1
 *
**/


jQuery.fn.nexVideo = function (options) {

	var defaults = {

		width: 852,
		height: 480,
		autoplay: true,
		player: "/player-licensed.swf",
		targetContainer: "#video-player"
	};
	
	var iphone = false;
	if((navigator.userAgent.match(/iPhone/i)) || (navigator.userAgent.match(/iPod/i)) || (navigator.userAgent.match(/iPad/i)) ) {
		iphone = true;
	}
	
	// alert(iphone); // works
	
	var settings = $.extend(true, {}, defaults, options);
	
	// set default jquery.media properies
	$.fn.media.mapFormat('mp4', 'flash');
	$.fn.media.defaults.flvPlayer = '/player-licensed.swf';
	$.fn.media.defaults.flashvars = { controlbar: 'over', autostart: true, skin: '/resources/flash/modieus/modieus.swf' };
	$.fn.media.defaults.params = { allowFullScreen: 'true', allowScriptAccess: 'always' };
	$.fn.media.defaults.attrs = { id: 'player1', name: 'player1' };
	$.fn.media.defaults.bgColor = '#f3f7f7';
	
	
	$(settings.targetContainer).hide(); // hide video container initially
	
	
	return this.each(function () {
		
		// change href if for the iphone
		if(iphone) {
			$(this).attr('href', $(this).attr('href').replace('video-480p-900.mp4','iphone.mov').replace('http://jeckersoncom.appspot.com','') );
		} else {
			$(this).click(function() {
				$('#video-gallery').hide();
				$(settings.targetContainer)
					.fadeIn("slow")
					.find("#video-container").media({
						width: 852,
						height: 480,
						autoplay: true,
						src: settings.player,
						flashvars: { file: $(this).attr("href") }
				});

				$('#video-close').click(function() { // bind click to body to hide the player and empty the div, also remove the click event.
					$(settings.targetContainer).fadeOut("normal", function() { 
						$(this).find('#video-container').empty().hide();
						$('#video-gallery').fadeIn("fast");
					});

				});
				return false;
			});
		}
	
	});
};