BF_VIDEO_TYPES_STRING='YouTube, Vimeo, Hulu, Google Video, LiveLeak, MetaCafe, DailyMotion, or MySpace Video';

bf2_terminal_Video = function() {
	
	this.type = null;
	this.id = null;
	this.embed_link = null;
	this.detected = false;
	this.src_link = null;
	this.getting_thumbnail = false;

	this.detect_type = function(link) {
		this.detected = false;
		
		if(link) {

			// is youtube?
			var matches = link.match( /(http:\/\/)*(\w+\.)?youtube\.com\/(watch[\?#!]{1,2}v=|v\/)([\w-]+)/ );
			// http://www.youtube.com/watch?v=ErMWX--UJZ4&otherstuff
			// http://www.youtube.com/watch?v=ErMWX--UJZ4
			// http://www.youtube.com/watch#v=ErMWX--UJZ4
			// http://www.youtube.com/watch#!v=ErMWX--UJZ4
			// http://www.youtube.com/v/ErMWX--UJZ4
			
			if(matches) {
				this.type = 'youtube';
				this.id = matches[4];
				this.embed_link = 'http://youtube.com/watch?v=' + this.id;
				this.detected = ( new Date() ).valueOf();
				this.src_link = 'http://www.youtube.com/v/' + this.id;
				this.img0 = 'http://img.youtube.com/vi/' + this.id + '/0.jpg'
				this.img1 = 'http://img.youtube.com/vi/' + this.id + '/1.jpg'
				this.img2 = 'http://img.youtube.com/vi/' + this.id + '/2.jpg'
				this.img3 = 'http://img.youtube.com/vi/' + this.id + '/3.jpg'
			
				// Eventually replace static img? vars
				this.thumbs = [ this.img1, this.img0, this.img2, this.img3 ];
				
				// Thumbnail supported on server
				this.thumbnail_available = true;
			} 

			// is vimeo?
			if(! this.detected) {
				matches = link.match( /(http:\/\/)*(\w+\.)?vimeo\.com\/(moogaloop\.swf\?clip_id=)*(\d+)/ );
				// http://vimeo.com/8335194
				// http://www.vimeo.com/moogaloop.swf?clip_id=8335194	
				if(matches) {
					this.type = 'vimeo';
					this.id = matches[4];
					this.embed_link = 'http://vimeo.com/' + this.id;
					this.detected = true;
					this.src_link = 'http://www.vimeo.com/moogaloop.swf?clip_id=' + this.id;

					// Thumbnail retrieval supported on server
					this.thumbnail_available = true;
				} 
			}

			// is google video?
			if(! this.detected) {
				matches = link.match(/^http:\/\/video\.google\.com\/videoplay?(.*)?docid=((\-)?(\d+))/);
				if(matches) {
					this.type = 'google video';
					this.id = matches[2];
					this.embed_link = 'http://video.google.com/videoplay?docid=' + this.id;
					this.detected = true;
					this.src_link = 'http://video.google.com/googleplayer.swf?docid='+this.id
				}
			} 
		
			//<embed id="VideoPlayback" src="http://video.google.com/googleplayer.swf?docid=8361110035270212838&hl=en&fs=true" style="width:400px;height:326px" allowFullScreen="true" allowScriptAccess="always" type="application/x-shockwave-flash"> </embed>
			// is google video embed?
			if(! this.detected) {
				matches = link.match(/http:\/\/video\.google\.com\/googleplayer.swf\?docid=((\-)?(\d+))/);
				if(matches) {
					//alert(link + ' ' + matches[1]);
					this.type = 'google video';
					this.id = matches[1];
					this.embed_link = 'http://video.google.com/videoplay?docid=' + this.id;
					this.detected = true;
					this.src_link = 'http://video.google.com/googleplayer.swf?docid='+this.id;					
				}
			} 


			// is live leak?	
			if(! this.detected) {
				matches = link.match(/^http:\/\/(www\.)?liveleak\.com\/.*i=(\w{3}_(\d+)?)/);
				if(matches) {
					//console.dir({matches: matches});	
					this.type = 'live leak';
					this.id = matches[2];
					this.embed_link = 'http://www.liveleak.com/view?i=' + this.id;
					this.detected = true;
					this.src_link = 'http://www.liveleak.com/e/'+this.id
				}
			} 
			
			//<object width="450" height="370"><param name="movie" value="http://www.liveleak.com/e/0bd_1222963986"></param><param name="wmode" value="transparent"></param><embed src="http://www.liveleak.com/e/0bd_1222963986" type="application/x-shockwave-flash" wmode="transparent" width="450" height="370"></embed></object>
			if(! this.detected) {
				matches = link.match(/http:\/\/(www\.)?liveleak\.com\/e\/(\w{3}_(\d+))/);
				if(matches) {
					this.type = 'live leak';
					this.id = matches[2];
					this.embed_link = 'http://www.liveleak.com/view?i=' + this.id;
					this.detected = true;
					this.src_link = 'http://www.liveleak.com/e/'+this.id
				}
			} 


			// is myspace?
			if(! this.detected) {
				matches = link.match(/^http:\/\/vids\.myspace\.com\/.*ideoID=(\d+)/i);
				if(matches) {				
					this.type = 'myspace';
					this.id = matches[1];
					this.embed_link = 'http://vids.myspace.com/index.cfm?fuseaction=vids.individual&VideoID=' + this.id;
					this.detected = true;
					this.src_link = 'http://mediaservices.myspace.com/services/media/embed.aspx/m=' + this.id + ',t=1,mt=video';										
				}
			} 		

			// is myspace embed?
			if(! this.detected) {
				matches = link.match(/http:\/\/vids\.myspace\.com\/.*ideoID=(\d+)/i);
				if(matches) {				
					this.type = 'myspace';
					this.id = matches[1];
					this.embed_link = 'http://vids.myspace.com/index.cfm?fuseaction=vids.individual&VideoID=' + this.id;
					this.detected = true;
					this.src_link = 'http://mediaservices.myspace.com/services/media/embed.aspx/m=' + this.id + ',t=1,mt=video';										
				}
			} 		

			// is hulu embed?
			if(! this.detected) {				
				matches = link.match(/src="(http:\/\/(www\.)?hulu\.com\/embed\/(.*?))"/i);
				if(matches) {			
					this.type = 'hulu';
					this.id = matches[3];
					this.embed_link = 'http://www.hulu.com/embed/' + this.id;
					this.detected = true;
					this.src_link = matches[1];										
				}
			} 

			// is hulu embed src?
			if(! this.detected) {				
				matches = link.match(/^http:\/\/(www\.)?hulu\.com\/embed\/(.*)$/i);
				if(matches) {					
					this.type = 'hulu';
					this.id = matches[2];
					this.embed_link = 'http://www.hulu.com/embed/' + this.id;
					this.detected = true;
					this.src_link = this.embed_link;										
				}
			} 


			// is daily motion embed?
			if(! this.detected) {				
				matches = link.match(/(http:\/\/(www\.)?dailymotion\.com\/swf\/(\w+))/i);
				if(matches) {
					this.type = 'daily motion';
					this.id = matches[3];
					this.embed_link = 'http://www.dailymotion.com/swf/' + this.id;
					this.detected = true;
					this.src_link = matches[1];										
				}
			}


			// is daily motion?
			if(! this.detected) {				
				matches = link.match(/^http:\/\/(www\.)?dailymotion\.com\/.*(related|video)\/([\w-]+)/i);
				if(matches) {
					this.type = 'daily motion';
					this.id = matches[3];
					this.embed_link = 'http://www.dailymotion.com/swf/' + this.id;
					this.detected = true;
					this.src_link = this.embed_link;										
				}
			}


			//meta cafe?			
			if(! this.detected) {				
				matches = link.match(/^http:\/\/(www\.)?metacafe\.com\/watch\/(.*?)\/(\w+)/i);
				if(matches) {
					//console.dir({matches: matches});
					this.type = 'metacafe';
					this.id = matches[2];
					this.embed_link = 'http://www.metacafe.com/fplayer/' + this.id + '/' + matches[3] + '.swf';
					this.detected = true;
					this.src_link = this.embed_link;										
				}
			}

			//meta cafe embed?			
			if(! this.detected) {				
				matches = link.match(/http:\/\/(www\.)?metacafe\.com\/fplayer\/(.*?)\/(\w+)/i);
				if(matches) {
					this.type = 'metacafe';
					this.id = matches[2];
					this.embed_link = 'http://www.metacafe.com/fplayer/' + this.id + '/' + matches[3] + '.swf';
					this.detected = true;
					this.src_link = this.embed_link;										
				}
			}
		 	
		}
	}

	/**
	 * Retrieves thumbnail image url from buzzfeed server and assigns to object
	 *
	 * @param 	video_url 	Video URL of respective video service
	 * @return 					Thumbnail image URL string
	 */
	this.detect_thumbnail_url = function( video_url, callback ) {
		
		// Thumbnail retrieval already in process	
		if( this.getting_thumbnail ) return; else this.getting_thumbnail = true;
		
		// callback is first argument
		if( typeof video_url === "function" ) {
			callback = video_url;
			video_url = this.embed_link;
		}
	
		if( ! video_url ) return false;

		var url = "/buzzfeed/_rservice/?uri=" + encodeURIComponent( video_url ) + "&thumbnail_url";

		// ajax callback needs access to "this"
		var video = this;

		// Thumb urls already exist
		if( this.thumbs && this.thumbs.length )
			if( typeof callback === "function" ) {
				callback.call( video );
				return false;
			}
		
		// Ugh.  Why does prototype still exist?  It's like the Windows of javascript
		new Ajax.Request( url, {
			method: "get", 
			onComplete: function( transport ) {

				video.getting_thumbnail = false;
				video.thumbs = [ transport.responseText ];
				
				if( typeof callback === "function" ) {
					callback.call( video );
				}
			}
		} );
	};
}


BF_initSwfObject = function(url, args) {
	var so = null;
	var width = 425;
	var height = 355;

	var id = (new Date()).getTime();	
	if( args && args.width ) width = args.width;
	if( args && args.height ) height = args.height;
	if( args && args.id ) id = args.id;

	var vid =  new bf2_terminal_Video();
	vid.detect_type(url);
	if(vid.detected) url = vid.src_link;
	if(url.match(/(hulu|myspace|liveleak|flickr)\.com/)) {
		so = new SWFObject(url, id, width, height, "6", "#000");
	} else if (url.match(/video\.google\.com/)) {
		so = new SWFObject(url, id, width, height, "6", "#000");
	} else {
		so = new SWFObject(url+'&amp;autostart=false&amp;autoplay=0&amp;color1=0x3a3a3a&amp;color2=0x999999&amp;border=0', id, width, height, "6", "#000");
	}
	return so;			
}

function bf_initSwfObject2(url, args) {
	var width = 425;
	var height = 355;
	var id = (new Date()).getTime();	
	if( args && args.width ) width = args.width;
	if( args && args.height ) height = args.height;
	if( args && args.id ) id = args.id;
	
	if(url.match(/(hulu|myspace|liveleak|flickr|google)\.com/)) {
		//so = new SWFObject(url, id, width, height, "6", "#000");
		swfobject.embedSWF(url, id, width, height, "9.0.0");		
	} else {
		//so = new SWFObject(, id, width, height, "6", "#000");
		swfobject.embedSWF(url+'&amp;autostart=false&amp;autoplay=0&amp;color1=0x3a3a3a&amp;color2=0x999999&amp;border=0', id, width, height, "9.0.0");		
	}
}
