Flowplayer = function(){

	return {

		init: function(selector, swf_url){
			Ext.select(selector||".rtf a").each(function(el){
				if(el.parent(".WGA-Item-Value-Unencoded"))
					return;		// ignore this images
				var href = el.dom.href||el.dom.src;
				if(!href)
					return;
				href=href.split(";")[0];	// ignore ;jsessiond-s
				var a = href.split(".");
				if(a.length>1){
					var ext = a.pop().toLowerCase();
					if(ext=="mov" || ext=="mp4" || ext=="m4v"){
						var img = el.child("img");
						if(img){
							el.setStyle({
								display: "block",
								textDecoration: "none",
								height: img.getHeight()+"px",
								width: img.getWidth()+"px"
							})

							/*	This for some strange reasons the following code does not work in IE, so we do it by hand :-(
							var imgWrapper = el.createChild({
								tag: "div",
								cls: "video-img-wrapper",
								children: [{
									tag: "div",
									cls: "video-title",
									html: el.dom.title||img.dom.title
								}]
							}, img)
							*/
							
							var iw = document.createElement("div");
							iw.className = "video-img-wrapper";
							var iw_title = document.createElement("div");
							iw_title.className = "video-title";
							iw_title.innerHTML = el.dom.title||img.dom.title;
							iw.appendChild(iw_title);
							
							el.dom.insertBefore(iw, img.dom);
										
							var imgWrapper = Ext.fly(iw);
							imgWrapper.dom.title = "Click to play Video ...";
							imgWrapper.setStyle({
								position: "absolute",
								opacity: 0,
								height: img.getHeight()+"px",
								width: img.getWidth()+"px"
							})
							if(Ext.isWebKit){
								// use transitions
								imgWrapper.setStyle("opacity", .6)
							}
							else {
								imgWrapper.pause(1).fadeIn({
									endOpacity: .6,
									duration: 1,
									afterStyle: "visibility:visible"
								});
							}
						}
						else el.update("");
						$f(el.dom, swf_url||"/plugin-flowplayer/file/distribution/flowplayer-3.2.5.swf", {
							clip: {
								onFinish: function () {
								    this.unload()										
								}
							}
						}).ipad();
					}
				}
			})
		}
		
		,initOverlay: function(config){
			var config = config||{};
			var overlay = $('<div id="flowplayer-overlay"/>')
			var player = $('<div id="flowplayer-player"/>');
			if(config.width)
				player.width(config.width);
			if(config.height)
				player.height(config.height);
			$("body").append(overlay);
			$(overlay).append(player);
			var player = $f("flowplayer-player", config.swf||"/plugin-flowplayer/file/distribution/flowplayer-3.2.5.swf").ipad();
			
			$("a[href$='.mov'], a[href$='.mp4'], a[href$='.m4v']").not(".WGA-Item-Value-Unencoded a")
				.attr("rel", "#flowplayer-overlay")
				.overlay({
					mask: config.mask||"#666666",
					//effect: 'apple',
					onLoad: function() {			
						player.play(this.getTrigger()[0].href);
					},
					onClose: function() {
						player.unload();
					}
				});
	
		}
		
	}
	
}()
