(function(){
	var path = 'http://arizala74.com/flowplayer/';
	$.fn.extend({
		video: function(options){
			var o = $.extend({
				autoplay: true,
				hideEndPlayButton: false,
				replayLabel: $('html').attr('lang') == 'es' ? 'Ver de nuevo' : 'Play again',
				scaling: "scale",
				autoHide: 'fullscreen',
				hideDelay: 1500,
				bottom: 0,
				controls: {},
				canvas: {},
				onLoad: function(){},
				onUnload: function(){},
				wmode: 'window',
				//autoBuffering: true,
				//bufferLength: 3,
				streaming: 'pseudo',	//Opciones => 'rtmp', 'pseudo', false
				plugins: {},
				autoResize: true,
				maxWidth: this.width(),
				maxHeight: this.height()
			}, options);
			this.each(function(){
				if(o.pseudo !== undefined)
					o.streaming = o.pseudo ? 'pseudo' : false;
				var clip = {
					autoPlay: o.autoplay,
					scaling: o.scaling,
					//autoBuffering: true,
					//bufferLength: 3,
					url: o.url || $(this).attr('rel'),
					onStart: function(clip) {
						if(pageTracker){
							pageTracker._trackEvent("Videos", "Play", clip.url);
						}
						if(o.hideEndPlayButton){
							this.getPlugin("play").css({opacity: 0.8});
						}
					},
					onPause: function(clip) {
						if(pageTracker){
							pageTracker._trackEvent("Videos", "Pause", clip.url, parseInt(this.getTime(), 10));
						}
					},
					onFinish: function(clip) { 
						if(pageTracker){
							pageTracker._trackEvent("Videos", "Finish", clip.url); 
						}
						if(o.hideEndPlayButton){
							this.getPlugin("play").css({opacity: 0});
						}
					}
				};
				$.extend(o.controls, {
					autoHide: o.autoHide,
					hideDelay: o.hideDelay,
					bottom: o.bottom
				});
				o.plugins.controls = o.controls;
				if(o.autoResize){
					clip.onMetaData = function(clip) {
						var m = clip.metaData;
						if(m.width && m.height){
							var width = parseInt(m.width, 10);
							var height = parseInt(m.height, 10);
							if(m.width > o.maxWidth || m.height > o.maxHeight){
								var s = Math.min(o.maxWidth / width, o.maxHeight / height);
								width = s * width;
								height = s * height;
							}
							$(this.getParent()).width(width).height(height + 24);
						}
					};
				}
				if(o.streaming == 'pseudo'){
					clip.provider = "lighttpd";
					o.plugins.lighttpd = {url: path + 'flowplayer.pseudostreaming-3.2.5.swf'};
					if(o.queryString)
						o.plugins.lighttpd.queryString = o.queryString;
				} else if(o.streaming == 'rtmp'){
					clip.provider = "rtmp";
					o.plugins.rtmp = {url: path + 'flowplayer.rtmp-3.2.3.swf'};
					if(o.netConnectionUrl)
						o.plugins.rtmp.netConnectionUrl = o.netConnectionUrl;
				}
				var param1 = {
					src: path + "flowplayer.commercial-3.2.6.swf",
					wmode: o.wmode
				},
				param2 = {
					key: o.key,
					clip: clip,
					play: {replayLabel: o.replayLabel},
					plugins: o.plugins,
					canvas: o.canvas,
					onLoad: o.onLoad,
					onUnload: o.onUnload,
					onFail: function(){
						//console.log(this)
					}
				};
				if($.fn.flowplayer)
					$(this).flowplayer(param1, param2);
				else {
					var me = this;
					$.getScript(path + 'flowplayer-3.2.4.min.js', function(){$(me).flowplayer(param1, param2);});
				}
			});
			return this;
		}
	});
})();
