/******************
* 
*
*		SVEP - Smart Video Embed Plugin with jQuery
*		Copyright (c) 2010 Orkun GURSEL, orkungrsl@gmail.com
*		Supported services: youtube.com, video.google.com, vimeo.com(with album), dailymotion.com, metacafe.com, blip.tv, screenr.com, viddler.com
*
*	   
******************/   



	   $.fn.SVEP = function(options) {
			
			
			// defaut options
		
			var defaults = {
					
					default_video_player_url : "http://releases.flowplayer.org/swf/flowplayer-3.2.3.swf",
					width: 600,
					height: 400,
					svep_loading_class: "svep-loading",
					svep_thumbnail_class: "svep-thumbnail",
							"vimeo_album_background": "000000",
							"vimeo_album_color": "00adef",
							"vimeo_album_slideshow": 0
					
				};
			
			var options = $.extend(defaults, options);
			
							options.vimeo_album_background = options.vimeo_album_background.replace('#','');
							options.vimeo_album_color = options.vimeo_album_color.replace('#','');
							if (options.vimeo_album_slideshow){options.vimeo_album_slideshow = 1;}else{options.vimeo_album_slideshow = 0;};
							
							
							
							
							
					create_video = function(el,id,thumb){
						
							if (!el.html().match(/([a-zA-Z0-9\-_]+).(png|jpg|gif)/)){
							
							el.after('<div class="svep"><div id="v-'+ id +'"><div class="'+ options.svep_loading_class +'" id="loading-'+id+'"></div></div></div>');					
							
							
							}else{
							el.after('<div class="svep"><div id="thumb-'+id+'"><a href="#" onclick="javascript:$(\'#thumb-'+id+'\').remove(); $(\'#v-'+id+'\').fadeIn(); return false;" class="'+ options.svep_thumbnail_class +'"><img src="'+el.html()+'"/></a></div><div id="v-'+ id +'" style="display:none;"><div class="'+ options.svep_loading_class +'" id="loading-'+id+'"></div></div></div>');
							
							}
							
							el.remove();
							
					};
							
							
							
			
						this.each(function() {
						
						
						var url =  $(this).attr('href');
						var rel =  $(this).attr('rel');
						
						if (rel=="noembed") return true;
						
						
						if (rel) {
							
							rel  = rel.split(',');
							width = rel[0];
							height = rel[1];
							
						if (!!isNaN(width)) width= options.width;
						if (!!isNaN(height)) height= options.height;
							
							}else{
								
							width = options.width;
							height = options.height;
								
								};
								
							
														
				if (url.indexOf("youtube.com") > 0) // if in eached links have "youtube.com" query, run these codes
			
             {			
			 
				 				 
						var regExp = /http:\/\/(?:www\.)?youtube.*watch\?v=([a-zA-Z0-9\-_]+)/; //we get unique id from page's link
					 
						var match = url.match(regExp);
			 
						if (match){ // if the link is valid

						var video_id = match[1]+"_"+Math.floor(Math.random()*5000);

						create_video($(this),video_id,$(this).html());
						$('#loading-'+video_id).remove();
																
													// create flash object with swfobject plugin
													
													$('#v-'+ video_id +'').flash(
													{
														swf: 'http://www.youtube.com/v/'+match[1]+'&hl=en_US&fs=1',
														width: width,
														height: height,
														allowFullScreen: true,
														allowscriptaccess: "always"
													});
							
									}//END of "if (match)"
			 
					} // END of is it "youtube.com" link?
			
			
			else if (url.indexOf("vimeo.com/album/") > 0) // if in eached links have "vimeo.com/album/" query, run these codes
			
             {
				 				 
						var regExp = /http:\/\/(www\.)?vimeo.com\/album\/(\d+)($|\/)/; //we get unique id from page's link
					 
						var match = url.match(regExp);
			 
						if (match){ // if the link is valid

						var video_id = match[2]+"_"+Math.floor(Math.random()*5000);
				
						create_video($(this),video_id,$(this).html());
						$('#loading-'+video_id).remove();
										
													// create flash object with swfobject plugin
													
													$('#v-'+ video_id +'').flash(
													{
														swf: 'http://vimeo.com/hubnut/?color='+options.vimeo_album_color+'&amp;background='+options.vimeo_album_background+'&amp;fullscreen=1&amp;slideshow='+options.vimeo_album_slideshow+'&amp;stream=album&amp;id='+match[2]+'&amp;server=vimeo.com',
														width: width,
														height: height,
														allowfullscreen: true,
														quality: 'best'
													});
							
									}//END of "if (match)"
			 
					} // END of is it "vimeo.com/album/" link?
			
			
			
			
			
			
				else if (url.indexOf("vimeo.com") > 0) // if in eached links have "vimeo.com" query, run these codes
			
             {
				 				 
						var regExp = /http:\/\/(www\.)?vimeo.com\/(\d+)($|\/)/; //we get unique id from page's link
					 
						var match = url.match(regExp);
			 
						if (match){ // if the link is valid

						var video_id = match[2]+"_"+Math.floor(Math.random()*5000);
				
						create_video($(this),video_id,$(this).html());
						$('#loading-'+video_id).remove();
						
						
													// create flash object with swfobject plugin
													
													$('#v-'+ video_id +'').html('<iframe src="http://player.vimeo.com/video/'+match[2]+'" width="'+width+'" height="'+height+'" frameborder="0"></iframe>');
							
									}//END of "if (match)"
			 
					} // END of is it "vimeo.com" link?
			
				else if (url.indexOf("metacafe.com") > 0) // if in eached links have "metacafe.com" query, run these codes
			
             {
				 				 
						var regExp = /http:\/\/(www\.)?metacafe.com\/watch\/(\d+)($|\/)/; //we get unique id from page's link
					 
						var match = url.match(regExp);
			 
						if (match){ // if the link is valid

						var video_id = match[2];
				
						create_video($(this),video_id,$(this).html());
						$('#loading-'+video_id).remove();
					
													
													// create flash object with swfobject plugin
													
													$('#v-'+ video_id +'').flash(
													{
														swf: 'http://www.metacafe.com/fplayer/'+match[2]+'/video.swf',
														width: width,
														height: height,
														allowfullscreen: true
													});
							
									}//END of "if (match)"
			 
					} // END of is it "metacafe.com" link?
					
				else if (url.indexOf("dailymotion.com") > 0) // if in eached links have "dailymotion.com" query, run these codes
			
             {
				 				 
						var regExp = /http:\/\/(www\.)?dailymotion.com\/video\/([a-zA-Z0-9\-_]+)/; //we get unique id from page's link
					 
						var match = url.match(regExp);
			 
						if (match){ // if the link is valid

						var video_id = match[2]+"_"+Math.floor(Math.random()*5000);
						
						create_video($(this),video_id,$(this).html());
						$('#loading-'+video_id).remove();
						
										
													// create flash object with swfobject plugin
													
													$('#v-'+ video_id +'').flash(
													{
														swf: 'http://www.dailymotion.com/swf/video/'+match[2]+'?additionalInfos=0',
														width: width,
														height: height,
														allowFullScreen: true,
														allowscriptaccess: "always"
													});
							
									}//END of "if (match)"
			 
					} // END of is it "dailymotion.com" link?
					
					
					
				else if (url.indexOf("video.google.") > 0) // if in eached links have "video.google.com" query, run these codes
			
             {			
				 				 
						var regExp = /http:\/\/(www\.)?video.google.*\/videoplay\?docid=([a-zA-Z0-9\-_]+)/; //we get unique id from page's link
					 
						var match = url.match(regExp);
			 
						if (match){ // if the link is valid

						var video_id = match[2]+"_"+Math.floor(Math.random()*5000);
				
						create_video($(this),video_id,$(this).html());
						$('#loading-'+video_id).remove();
										
													// create flash object with swfobject plugin
													
													$('#v-'+ video_id +'').flash(
													{
														swf: 'http://video.google.com/googleplayer.swf?docid='+match[2]+'',
														width: width,
														height: height,
														allowFullScreen: true,
														allowscriptaccess: "always"
													});
							
									}//END of "if (match)"
			 
					} // END of is it "video.google.com" link?
					
					
		
			else if (url.indexOf("blip.tv") > 0) // if in eached links have "blip.tv" query, run these codes
			
			
          		   {
				 				 
						var regExp = /http:\/\/(www\.)?blip.tv\/file\/([a-zA-Z0-9\-_]+)/; //we get unique id from page's link
				 
						var match = url.match(regExp);
				 
						if (match){ // if the link is valid
		
						var video_id = match[2]+"_"+Math.floor(Math.random()*5000);
						
						create_video($(this),video_id,$(this).html());
						var nwidth= width; var nheight= height;
										
										$.ajax({
										dataType: 'json',
										url: 'http://oohembed.com/oohembed/?url='+url+'&callback=?',
										success: function(data) {

										var newregExp = /<embed src="(.*?)"/;

										var blip_data = data.html.match(newregExp);
																				
										$('#loading-'+video_id).remove(); // remove loading link if loading is over
												
															if (!blip_data) { $('#v-'+ video_id +'').remove(); return false;};
											
																					// create flash object with swfobject plugin
																					$('#v-'+ video_id +'').flash(
																					{
																						swf: ""+blip_data[1]+"",
																						width: nwidth,
																						height: nheight,
																						allowFullScreen: true,
																						allowscriptaccess: "always"
																					});
																																
														} //  END of success function
										
											}); // END of "ajax"
		
								}	//END of "if (match)"
			 
					} // END of is it "blip.tv" link?
					
					
					
		
			else if (url.indexOf("screenr.com") > 0) // if in eached links have "screenr.com" query, run these codes
			
			
          		   {
				 				 
						var regExp = /http:\/\/(www\.)?screenr.com\/([a-zA-Z0-9\-_]+)/; //we get unique id from page's link
				 
						var match = url.match(regExp);
				 
						if (match){ // if the link is valid
		
						var video_id = match[2]+"_"+Math.floor(Math.random()*5000);
						
						create_video($(this),video_id,$(this).html());		
						var nwidth= width; var nheight= height;
										
										$.ajax({
										dataType: 'json',
										url: 'http://screenr.com/api/oembed.json?url='+url+'&callback=?',
										success: function(data) {

										
										var newregExp = /http:\/\/screenr.com\/Content\/assets\/(.*?).swf/;
										var newregExp_flashvar = /(flashvars="i=(.*?)")/;
														
										var screenr_data = data.html.match(newregExp);
										var screenr_data_flashvar = data.html.match(newregExp_flashvar);
										
										$('#loading-'+video_id).remove(); // remove loading link if loading is over
												
															if (!screenr_data) { $('#v-'+ video_id +'').remove(); return false;};
											
																					// create flash object with swfobject plugin
																					$('#v-'+ video_id +'').flash(
																					{
																						swf: screenr_data[0],
																						width: nwidth,
																						height: nheight,
																						allowFullScreen: true,
																						allowscriptaccess: "always",
																						flashvars: {
																							i		: screenr_data_flashvar[2]}
																					});
															
															
															} //  END of success function
										
										}); // END of "ajax"
		
							}	//END of "if (match)"
			 
					} // END of is it "screenr.com" link?
					
						else if (url.indexOf("viddler.com") > 0) // if in eached links have "viddler.com" query, run these codes
			
			
          		   { 
						var regExp = /http:\/\/(www\.)?viddler.com\/explore\/([a-zA-Z0-9\-_]+)\/([a-zA-Z0-9\-_]+)\/([a-zA-Z0-9\-_]+)/; //we get unique id from page's link
				 
						var match = url.match(regExp);
				 
						if (match){ // if the link is valid
						
						var video_id = match[4]+"_"+Math.floor(Math.random()*5000);
						
						create_video($(this),video_id,$(this).html());		
						var nwidth= width; var nheight= height;
										
										$.ajax({
										dataType: 'json',
										url: 'http://oohembed.com/oohembed/?url='+url+'&callback=?',
										success: function(data) {
										
										var newregExp = /<embed src="(.*?)"/;
														
										var viddler_data = data.html.match(newregExp);
										
										$('#loading-'+video_id).remove(); // remove loading link if loading is over
												
															if (!viddler_data) { $('#v-'+ video_id +'').remove(); return false;};
											
																					// create flash object with swfobject plugin
																					$('#v-'+ video_id +'').flash(
																					{
																						swf: ""+viddler_data[1]+"",
																						width: nwidth,
																						height: nheight,
																						allowFullScreen: true,
																						allowscriptaccess: "always"
																					});
															

															} //  END of success function
										
										}); // END of "ajax"
		
							}	//END of "if (match)"
			 
					} // END of is it "viddler.com" link?
					
					else {
					
					
					 
						var match = url.match(/([a-zA-Z0-9\-_]+).(flv|mp4)/);
						var match2 = url.match(/([a-zA-Z0-9\-_]+).(swf)/);
			 
						if (match){ // if the link is valid
						
							var video_id = Math.floor(Math.random()*5000);
							
							
							var myloc = window.location.href;
							var locarray = myloc.split("/");
							delete locarray[(locarray.length-1)];
							var currentdir = locarray.join("/");
							
							
							
								if (url.indexOf("http://")) {
										url = currentdir+""+url;
									};
									
								
							
						create_video($(this),video_id,$(this).html());		

							
							$('#v-'+ video_id +'').flash(
							{
								swf: options.default_video_player_url,
								width: width,
								height: height,
								allowFullScreen: true,
								allowscriptaccess: "always",
								cachebusting:	true,
								flashvars: { 
											config: "{'clip': {'url': '"+ url +"', 'autoPlay': false, 'autoBuffering': true} }}"
											
											}

								
							});
						
						}else if (match2) {
							
							var video_id = Math.floor(Math.random()*5000);
							
							
							var myloc = window.location.href;
							var locarray = myloc.split("/");
							delete locarray[(locarray.length-1)];
							var currentdir = locarray.join("/");
							
							
							
								if (url.indexOf("http://")) {
										url = currentdir+""+url;
									};
									
							
						create_video($(this),video_id,$(this).html());
						
						$('#v-'+ video_id +'').flash(
							{
								swf: url,
								width: width,
								height: height,
								allowFullScreen: true,
								allowscriptaccess: "always"
								
							});
							
							
							
							};

					};
				
		 }); // END of Each
		
					
					
				
							
					
						}; // END of function
