(function($) {

 $.fn.sliderVideo = function(options){
	var defaults = {
		videoID : 'asvideo' // id prefix
	};

	return this.each(function(){
		var self = this;
		var video, tmp, service;
		video = {};
		video.options = $.extend({}, defaults, options);
		video.services = $.fn.sliderVideo.services;
		video.len = 0; // used to add a unique ID to videos "asvideo#"
		video.hasYoutube = 0;
		video.hasVimeo = 0;
		video.list = {};

		// find and save all known videos
		for (service in video.services) {
			if (typeof(service) === 'string') {
				selFunc = video.services[service].selector;
				var videoList = selFunc(jQuery(self));
				jQuery(videoList).each(function(){
					tmp = $(this);
					// save panel and video selector in the list
					var panel = tmp.closest('.media')[0];
					var list = jQuery(panel).data('videoList');
					if(typeof list == 'undefined'){
						jQuery(panel).data('videoList',video.len);
					}else{
						jQuery(panel).data('videoList',list+','+video.len);
					}
					tmp.attr('id', video.options.videoID + video.len);
					video.list[video.len] = {
						id       : video.options.videoID + video.len++,
						panel    : panel,
						service  : service
					};
				});
			}
		}

		// Initialize each video, as needed
		jQuery.each(video.list, function(i,s){
			var service = s.service;
			if(service == 'youtube')
				video.hasYoutube++;
			if(service == 'vimeo')
				video.hasVimeo++;
			video.services[service].init(s.id,video);
		});
		jQuery(this).data('video',video);
					 
	});
};

$.fn.sliderVideo.services = {
	// *** HTML5 video ***
	video : {
		selector : function(el){
			return jQuery(el).find('video');
		},
		init : function(id,video){
			var mediaElement = new MediaElementPlayer(jQuery('#'+id), 
													  {
														success:function(mediaElement,domObject){
															mediaElement.addEventListener('play',function(e){
																jQuery('#'+id).trigger('videoPlay');
															});
														}
													});
			jQuery('#'+id).data('video',mediaElement);
		},
		pause : function(id){
			jQuery('#'+id).data('video').pause();
		}
	},
	flash:{
		selector : function(el){
			return jQuery(el).find('.flash');
		},
		init: function(id,video){
			var $el = jQuery('#'+id);
			var $wrap = $el.wrap(function() {
				return '<div class="flashScreen"/>';
			}).parent();
			var poster = $el.find('img');
			var src = video.options.poster;

			if(poster.length != 0){
				imgsrc = $el.find('img').attr('src');
				if(typeof imgsrc != 'undefined')
					src= imgsrc;
			}
			$wrap.data('poster',src);

			var width = $el.attr('width');
			width = width ? width : video.options.width;
			$wrap.data('width',width);

			var height = $el.attr('height');
			height = height ? height : video.options.height;
			$wrap.data('height',height);

			$wrap.data('src',$el.attr('data'));
			var paramList = {};
			$el.find('param').each(function(){
				paramList[jQuery(this).attr('name')]=jQuery(this).attr('value');				   
			});
			$wrap.data('paramList',paramList);
			$wrap.html('');
			$wrap.attr('id',id);
			jQuery('<img/>').attr('width',$el.attr('width')).attr('height',$el.attr('height')).attr('src',src).appendTo($wrap);
			$wrap.bind('click',function(){
				if($wrap.find('object').length==0){
					var width = jQuery(this).data('width');
					var height = jQuery(this).data('height');
					var src = jQuery(this).data('src');
					$wrap.html('');
					jQuery('<div/>').attr('id','flash_'+id).appendTo($wrap);
					var params =  { allowScriptAccess: "always",allowFullScreen:'true',expressInstaller:video.options.expressInstall, play:true};
					var params1 = $wrap.data('paramList');
					params = jQuery.extend(params,params1);
					var atts = { id: 'flash_'+id };
					swfobject.embedSWF(src, 'flash_'+id, width, height, "8", null, null, params,atts );
				}
				
			});
			jQuery('#'+id).bind('click',function(){
				jQuery(this).trigger('videoPlay');					 
			});
		},
		pause : function(id){
			var $el = jQuery('#'+id);
			$el.html('');
			jQuery('<img/>').attr('width',$el.data('width')).data('height',$el.data('height')).attr('src',$el.data('poster')).appendTo($el);
		}
	},
	youtube:{
		selector : function(el){
			return jQuery(el).find('.youtube');
		},
		init : function(id,video){
			if(video.hasYoutube==1){
				window.onYouTubePlayerReady = function(playerId){
					if (playerId && playerId != 'undefined') {
						player=document.getElementById(playerId);
						player.pauseVideo();
						player.addEventListener("onStateChange",'window.youTubeStat'+playerId);
					}
				};
			}
			var $el = jQuery('#'+id);
			var $wrap = $el.wrap(function() {
				return '<div class="youtubeScreen"/>';
			}).parent();
			var vidsrc = '';
			var objParams = {};
			if($el.is('iframe')){
				vidsrc = $el.attr('src').replace('embed','v');
			}else if($el.is('embed')){
				vidsrc = $el.attr('src');
			}else if($el.is('object')){
				vidsrc = $el.attr('data');
				$el.find('param').each(function(){
					objParams[jQuery(this).attr('name')]=jQuery(this).attr('value');				   
				});
			}
			if(typeof vidsrc =='undefined' || vidsrc=='')
				return false;
			var tmp = (vidsrc.match(/\?/g) ? '' : '?') + '&wmode=transparent'; 
			var api = '&iv_load_policy=3&enablejsapi=1&version=3&playerapiid='+id;
			var vidsrc = vidsrc + tmp + api;
			var width = $el.attr('width');
			width = width ? width:video.options.width;
			var height = $el.attr('height');
			height= height ? height:video.options.height;
			$wrap.attr('id',id);
			$wrap.data('width',width);
			$wrap.data('height',height);
			$wrap.html('');
			var params = { allowScriptAccess: "always",allowFullScreen:'true' };
			params = jQuery.extend(params,objParams);
			var atts = { id: id };
			swfobject.embedSWF(vidsrc, id, width, height, "8", null, null, params, atts);

			if(typeof window['youTubeStat'+id] == 'undefined'){
				window['youTubeStat'+id] = function(stat){
					if(stat ==1 || stat==3)
						jQuery('#'+id).trigger('videoPlay');
				};
			}
		},	
		pause : function(id){
			var player = document.getElementById(id);
			if(typeof player != 'undefined' && typeof player.pauseVideo !='undefined')
				player.pauseVideo();
		}
	},
	vimeo:{
		selector : function(el){
			return 	jQuery(el).find('.vimeo');
		},
		init : function(id,video){
			if(video.hasVimeo==1){
				window.vimeo_player_loaded = function(playerId){
					if (playerId && playerId != 'undefined') {
						player=document.getElementById(playerId);
						player.api_addEventListener('play', 'onVimeoPlay');
					}
				};
				window.onVimeoPlay = function(id){
					jQuery('#'+id).trigger('videoPlay');
				}
			}
			var $el = jQuery('#'+id);
			var $wrap = $el.wrap(function() {
				return '<div class="vimeoScreen"/>';
			}).parent();
			var objParams = {};
			if($el.is('iframe')){
				vidsrc = $el.attr('src');
				srcArray = vidsrc.replace('http://player.vimeo.com/video/','').split('?');
				vidsrc = 'http://vimeo.com/moogaloop.swf?clip_id='+srcArray[0];
				if(srcArray.length >1)
					vidsrc +='&'+srcArray[1];
			}else if($el.is('embed')){
				vidsrc = $el.attr('src');
			}else if($el.is('object')){
				vidsrc = $el.attr('data');
				$el.find('param').each(function(){
					objParams[jQuery(this).attr('name')]=jQuery(this).attr('value');				   
				});
			}
			if(typeof vidsrc =='undefined' || vidsrc=='')
				return false;
			var tmp = (vidsrc.match(/\?/g) ? '' : '?') + '&wmode=transparent'; 
			var api = '&api=1&player_id='+id;
			var vidsrc = vidsrc + tmp + api;
			var width = $el.attr('width');
			width = width ? width:video.options.width;
			var height = $el.attr('height');
			height= height ? height:video.options.height;
			$wrap.attr('id',id);
			$wrap.data('width',width);
			$wrap.data('height',height);
			$wrap.html('');
			var params = { allowScriptAccess: "always",allowFullScreen:'true'};
			params = jQuery.extend(params,objParams);
			var atts = { id: id };
			swfobject.embedSWF(vidsrc, id, width, height, "8", null, {api:1}, params, atts);

		},
		pause : function(id){
			var player = document.getElementById(id);
			if(typeof player != 'undefined' && typeof player.api_pause !='undefined')
				player.api_pause();
		}

	}
};

})(jQuery);

