
FlashZoomer = function(){
	var index=0;
	var maskel;
	var images=[];
	
	function keyNavAction(ev) {
        var keyCode = ev.getKey();
        if(
            keyCode == 88 || // x
            keyCode == 67 || // c
            keyCode == 27
        ){        	
          closeImage(this);
        }
	}
	
	function closeImage(imgel){
		var el=Ext.get("modal-image");	
		el.child(".img-wrap").remove();
		el.hide({
			callback: function(){
				el.removeAllListeners();
				Ext.fly(document).removeAllListeners();
				maskBody(false);
				el.update("");
			}
		})
				
		Ext.fly(document).un('keydown', keyNavAction, this);		
	}
	
	function maskBody(mask){
		if(!maskel){
			// create one
			maskel = Ext.getBody().createChild({tag:"div"});
			maskel.setStyle({
				position: "absolute",
				zIndex: 100,
				opacity: .7,
				top: Ext.getBody().getScroll().top+"px",
				left: 0,
				width: Ext.lib.Dom.getViewportWidth()+"px",
				height: Ext.lib.Dom.getViewportHeight()+"px",
				backgroundColor: "black",
				overflow: "hidden"
			})
		}
		
		if(mask){
			Ext.getBody().setStyle("overflow", "hidden");
		}
		else {
			Ext.getBody().setStyle("overflow", "auto")
			maskel.remove();
			maskel=null;
			
		}
	}
	
	function showImage(url){
		var width = 500 // fallBack;
		var height = 400 //fallBack;
		
		var exp =  /.*=(\d*).*=(\d*)/gi;
		var dimensions = exp.exec(url);
		if(dimensions) {
			width = dimensions[1];
			height = dimensions[2];
		}
		// set lastIndex to 0 because the exec() method stores the result indexes in the RegExp.lastIndex property and starts searching from that point for future uses.
		exp.lastIndex = 0;
		var pwidth = Number(width) +20;
		var pheight = Number(height) +30;
		
		maskBody(true);		
		var el=Ext.get("modal-image");
		if(el==null){
			// create div to show images in:
			el = Ext.getBody().createChild({tag:"div", id:"modal-image"});
		}
		el.hide();
		el.setStyle("zIndex", 30000);
		
		el.setHeight(200)
		el.setWidth(200);
		
		//allign element:		
		el.setLeft((maskel.getWidth()-pwidth)/2);
		el.setTop(maskel.getTop() + ((maskel.getHeight()-pheight)/2));
		
		el.show(true);	
		
		// find index:
		for(var i=0; i<images.length; i++){
			if(images[i].url==url){
				index=i;
				break;
			}
		}
		
		var otemplate = new Ext.Template(				
				'<object classid="clsid:D27CDB6E-AE6D-11cf-96B8-444553540000" codebase="http://download.macromedia.com/pub/shockwave/cabs/flash/swflash.cab#version=8,0,0,0" width="{w}" height="{h}">'
			  	,'<param name="movie" value="{url}" />'
				,'<param name="allowFullScreen" value="true" /> '
				,'<param name="controller" value="true" />'
				,'<param name="wmode" value="opaque" />'
				,'<param name="allowScriptAccess" value="sameDomain" />' 
				,'<param name="quality" value="high" />'
				,'<param name="menu" value="true" /> '
				,'<param name="autoplay" value="false" />' 
				,'<param name="autoload" value="false" /> '
				,'<embed src="{url}"'
				,' quality="high" pluginspage="http://www.macromedia.com/shockwave/download/index.cgi?P1_Prod_Version=ShockwaveFlash"	'
				,'type="application/x-shockwave-flash"'
				,' width="{w}" height="{h}"'
			  	,'></embed>'
				,'</object>'
		
		)
		otemplate.append(el.dom, {url:url,w:width,h:height});
		
		
			
		var t = new Ext.Template(
			'<div class="img-wrap" style="display:none;position:abslolute">',
				'<div class="img-info"></div>',
				//'<div align="left" style="float:left" class="img-title">{title}</div>',
			    '<div style="float:right"><a href="#" class="close-img">ESC to close</a></div>',
		    '</div>'
		);
		t.append(el.dom, {title: images[index].title||'no title'});
			
		var w = pwidth;
		var h = pheight;
		
		var x = (Ext.lib.Dom.getViewportWidth()-w)/2
		var y = Ext.getBody().getScroll().top + (Ext.lib.Dom.getViewportHeight()-h)/2
					
		el.shift({
			duration: w==el.getWidth() ? .001 : .2,
			x: x,
			width: w
		}).shift({
			duration: h==el.getHeight() ? .001 : .2,
			y: y,
			height: h,
			callback: function(){
				el.child(".img-wrap").show(true);
			}
		});
	
		
		var close = el.child(".close-img");
		if(close){
			close.on("click", function(ev){
				closeImage();
				ev.stopEvent();
			})
		}
		Ext.fly(document).on('keydown', keyNavAction);
		
		
	}	

	function addImage(url, title, posturl, id){
		images.push({
			url: url,
			title: title,
			posturl: posturl,
			id: id
		})
	}
	
	return{
		
		init: function(selector, title){
			
			images=[];
			Ext.select(selector || ".rtf a").each(function(el){
				var href = el.dom.href;
				href=href.split(";")[0];	// ignore ;jsessiond-s
				var params = href.split('?'); //remove parameter
				var a = params[0].split(".");
				if(a.length>1){
					var ext = a.pop().toLowerCase();
					if(ext=="swf"){
						addImage(href, el.dom.title||title||"", null, el.dom.id);
						el.on("click", function(ev){
							ev.preventDefault();
							showImage(this.url);
						}, {url:href, title:el.dom.title})
					}
				}
			})
		},
		
		showImage: showImage,
		addImage: addImage
	}
	
}()
