/******************************************************************************************

	JQUERY YOUTUBE PLUGIN 
	Author: Raphael Landas, Front End Developer, BMF

******************************************************************************************/
;(function($){
		
		
		$.fn.youtube = function(options) {
			options = $.extend({}, $.fn.youtube.defaults, options);
			var $gal = $(this);
			
			return $(this).each(function(i){
				var $this = $(this);
				var url = $.trim($this.attr('href'));
				var ytId = url.match("[\\?&]v=([^&#]*)");
				
				if (url != null && ytId != null) {
					ytId = ytId[1];
					
					var thmb;
					var w = $this.width();
					if (w <= 120) {
						thmb = "http://img.youtube.com/vi/" + ytId + "/2.jpg";
					} else {
						thmb = "http://img.youtube.com/vi/" + ytId + "/0.jpg";
					}
					
					$this.bind('thmb', function(evt){
						$this.css({background:'transparent url(' + thmb + ') 50% 50% scroll no-repeat', 'text-indent':'-99999px'});
					}).trigger('thmb');
					
					$this.bind('play', function(evt){
						var $imgplay = 'images/' + ytId + '_play.png';
						$this.css({background:'transparent url(' + $imgplay + ') 50% 50% scroll no-repeat', 'text-indent':'-99999px'});
					});
					
					$this.bind('over', function(evt){
						var $imgover = 'images/' + ytId + '_over.png';
						$this.css({background:'transparent url(' + $imgover + ') 50% 50% scroll no-repeat', 'text-indent':'-99999px'});
					});
					
					$this.bind('mouseenter', function(evt){
						$this.trigger('over');
					}).bind('mouseleave', function(evt){
						if(!$this.hasClass("playing")){
							$this.trigger('thmb');
						}
					}).bind('click', function(evt){
						$gal.trigger('thmb').removeClass('playing');						
						$this.addClass('playing');
						$this.trigger('play');
						return false;
					});
					
					/*$('<div class="videohover"></div>').appendTo($this);
					$('<div class="videoplaying"></div>').appendTo($this);
					if($.browser.msie){ 
						$(".videohover, .videoplaying").css({'behavior':'url(css/iepngfix.htc)'});
					}
					$this.bind('mouseenter', function(evt){
						$(".videohover").hide();
						$(".videohover", $this).show();
					}).bind('mouseleave click', function(evt){
						$(".videohover").hide();
					});*/
					
					$this.bind('click', function(evt){
						var plyr = $(options.player);
						options.maxWidth = plyr.width();
						options.maxHeight = plyr.height();
						plyr.embed(url, options);
						
						/*$(".videoplaying").hide();
						$(".videoplaying", $this).show();*/
						return false;
					});
				}
			});
		};
		
		$.fn.youtube.defaults = {embed:false,player:'#videoplayer',maxWidth:400,maxHeight:400};
		
		$.fn.embed = function(url, options){
			options = $.extend({}, $.fn.youtube.defaults, options);
			
			var $this = $(this);
			var ytId = url.match("[\\?&]v=([^&#]*)");
			if (ytId != null) {
				ytId = ytId[1];

				var html = '';
				html +='<object width="' + options.maxWidth + '" height="' + options.maxHeight + '">';
				html += '<param name="movie" value="http://www.youtube.com/v/' + ytId + '&hl=en&fs=1&"></param>';
				html += '<param name="allowFullScreen" value="true"></param>';
				html += '<param name="allowscriptaccess" value="always"></param>';
				html += '<param name="wmode" value="transparent"/>';
				html += '<embed src="http://www.youtube.com/v/' + ytId + '&h1=en&fs=1&" type="application/x-shockwave-flash" allowscriptaccess="always" wmode="transparent" allowfullscreen="true" width="' + options.maxWidth + '" height="' + options.maxHeight + '"></embed>';
				html += '</object>';
				
				$this.html(html);
				if ($this.is(":hidden")) {
					$this.siblings().hide();
					$this.show();
				}
			}
		}

})(jQuery);