/*	
	Original:
	---------
	sIFR 2.0.2 (http://www.mikeindustries.com/sifr/)
	Copyright 2004 - 2006 Mike Davidson, Shaun Inman, Tomas Jogin and Mark Wubben
	sIFR 2.0.2 is licensed under the CC-GNU LGPL <http://creativecommons.org/licenses/LGPL/2.1/>
	
	Adapted:
	--------
	MoosIFR v0.4 rough mootools sIFR conversion
	Copyright 2007 Chris Martin (http://www.redantdesign.com/)
	MoosIFR v0.4 is licensed under the CC-GNU LGPL <http://creativecommons.org/licenses/LGPL/2.1/>
	
	Description:
	------------
	Due to sIFR 2.0.2 having memory leaks I decided to roughly put together a mootools conversion.
	This release is incomplete and only performs the basic functionality of sIFR 2.0.2
	Please feel free to contribute to future releases and PLEASE keep memory leaks in mind.
	
	Notes:
	------
	1: The object tag appears to be the source of the memory leak in Internet Explorer.
	2: Adding flash replacement in window.onload causes memory leaks.
	
*/

var MoosIFR = new Class({
	initialize: function(elements, options) {
		this.setOptions({
			flashsrc: "",
			textcolor: "#000000",
			linkcolor: null,
			hovercolor: null,
			bgcolor: "#FFFFFF",
			paddingtop: 0,
			paddingright: 0,
			paddingbottom: 0,
			paddingleft: 0,
			flashvars: "textalign=left",
			wmode: ""
		}, options);
				
		if(this.hasFlash() && $$(elements).length > 0) {
		
			$$("html").addClass("sIFR-hasFlash");
			
			if(this.options.wmode == "transparent"){
				this.options.bgcolor = "transparent";
			}
			
			$$(elements).each(function(el){
				if(el.hasClass("sIFR-replaced") || el.hasClass("sIFR-alternate")) {
					return;
				}
				var offsets = el.getSize();
				var sWidth = offsets.size.x - this.options.paddingleft - this.options.paddingright;
				var sHeight = offsets.size.y - this.options.paddingtop - this.options.paddingbottom;
			
				var alternate = new Element("span",{"class":"sIFR-alternate"}).setHTML(el.innerHTML);
				var links = "";
				$each(el.getElements("a"), function(link, index){ 
					links += "&sifr_url_" + index + "=" + link.getProperty("href");
					link.setProperty("href","asfunction:_root.launchURL," + index);
				});
				
				this.flashvars = "txt=" + el.innerHTML.replace(/\+/g, "%2B").replace(/&/g, "%26").replace(/\"/g, "%22");
				this.flashvars += "&w=" + sWidth + "&h=" + sHeight;
				this.flashvars += links + "&" + this.options.flashvars;
				
				$each(this.options, function(v, k){
					if(v && k != "flashvars" && k != "flashsrc"){
						this.flashvars += "&" + k + "=" + v;
					}
				}.bind(this));
				
				el.empty();
				el.addClass("sIFR-replaced").setHTML(
					'<embed class="sIFR-flash" type="application/x-shockwave-flash" src="' + this.options.flashsrc + '" quality="best" wmode="' + this.options.wmode + '" bgcolor="' +  this.options.bgcolor + '" flashvars="' + this.flashvars + '" width="' + sWidth + '" height="' + sHeight + '" sifr="true"></embed>'
				);
				el.innerHTML += ""; //force IE and safari to render correctly
				
				alternate.injectInside(el);
			}, this);
		}
	
	},
	
	hasFlash: function(){
		if(window.hasFlash != null) {
			return window.hasFlash >= 6;
		};
		
		var pVersion = 0;
		
		if(navigator.plugins && navigator.mimeTypes.length) {
			var x = navigator.plugins["Shockwave Flash"];
			if(x && x.description) {
				pVersion = x.description.replace(/([a-zA-Z]|\s)+/, "").replace(/(\s+r|\s+b[0-9]+)/, ".").split(".")[0];				
			}
		} else {
			try {
				var axo = new ActiveXObject("ShockwaveFlash.ShockwaveFlash.7");
			} catch(e) {
				try {
					var axo = new ActiveXObject("ShockwaveFlash.ShockwaveFlash.6");
					pVersion = 6;
					axo.AllowScriptAccess = "always";
				} catch(e) {
					if (pVersion == 6) {
						window.hasFlash = pVersion;
						return true;
					}
				}
				try {
					axo = new ActiveXObject("ShockwaveFlash.ShockwaveFlash");
				} catch(e) {}
			}
			if (axo != null) {
				pVersion = axo.GetVariable("$version").split(" ")[1].split(",")[0];
			}
		}
		window.hasFlash = pVersion;
		return pVersion >= 6;
	}
});

MoosIFR.implement(new Options);