/*
	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.

*/

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: "",
			wmode: ""
		}, options);

		if(this.hasFlash() && $$(elements).length > 0) {
			var isTransparent = (this.options.wmode == "transparent");
			var isLinux = (navigator.platform.indexOf("Linux") != -1);
			var isFirefox = window.gecko;

			if (isTransparent && !isLinux) {
				this.options.bgcolor = "transparent";
			}

			$$("html").addClass("sIFR-hasFlash");

			$$(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;

				/*
				cloning a node using the .clone() method to replace itself causes leaks,
				I chose to make the alternate content span node, then change links directly before flash replacement
				*/
				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);
				});
				//end of link replacement

				//if you haven't sucked air through your teeth yet, the next lot of code will make you do so.
				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"){
						this.flashvars += "&" + k + "=" + v;
					}
				}.bind(this));

				//change the content to flash stuff
				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>'
				);

				//add the alternate clone
				alternate.injectInside(el);
			}, this);
		}

	},

	//following function is a copy and paste
	hasFlash: function(){
		/*
		pulled from swfObject (http://blog.deconcept.com/swfobject/),
		I didn't like the sIFR way of injecting vbscript to detect flash.
		*/
		var PlayerVersion = new flashVersion([0,0,0]);

		if(navigator.plugins && navigator.mimeTypes.length){
			var x = navigator.plugins["Shockwave Flash"];
			if(x && x.description) {
				PlayerVersion = new flashVersion(x.description.replace(/([a-zA-Z]|\s)+/, "").replace(/(\s+r|\s+b[0-9]+)/, ".").split("."));
			}
		} else {
			try{
				var axo = new ActiveXObject("ShockwaveFlash.ShockwaveFlash.7");
			}catch(e){
				try {
					var axo = new ActiveXObject("ShockwaveFlash.ShockwaveFlash.6");
					PlayerVersion = new flashVersion([6,0,21]);
					axo.AllowScriptAccess = "always";
				} catch(e) {
					if (PlayerVersion.major == 6) {
						return PlayerVersion;
					}
				}
				try {
					axo = new ActiveXObject("ShockwaveFlash.ShockwaveFlash");
				} catch(e) {}
			}
			if (axo != null) {
				PlayerVersion = new flashVersion(axo.GetVariable("$version").split(" ")[1].split(","));
			}
		}
		return PlayerVersion.major >= 6;
	}
});

MoosIFR.implement(new Options);

/* also adapted from swfObject (http://blog.deconcept.com/swfobject/) */
var flashVersion = new Class({
	initialize: function(arrVersion){
		this.major = arrVersion[0] != null ? parseInt(arrVersion[0]) : 0;
		this.minor = arrVersion[1] != null ? parseInt(arrVersion[1]) : 0;
		this.rev = arrVersion[2] != null ? parseInt(arrVersion[2]) : 0;
	}
});