/*	
	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.
 
*/

// This code was not written by Kristofer Baxter. If you'd like to see examples of my Javascript, send me an email. http://www.kristoferbaxter.com/contact/
 
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,
			lettercase: "",
			flashvars: "",
			wmode: ""
		}, options);
 
		if(this.hasFlash() && $$(elements).length > 0) {
			if(this.options.wmode == "transparent"){
				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
				*/
				if (this.options.lettercase == "upper") { el.setHTML(el.innerHTML.toUpperCase()); }
				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
				if (window.ie) { 
					el.addClass("sIFR-replaced").setHTML('<object class="sIFR-flash" classid="clsid:D27CDB6E-AE6D-11cf-96B8-444553540000" width="' + sWidth + '" height="' + sHeight + '"><param name="movie" value="' + this.options.flashsrc + '" /><param name="bgcolor" value="'+ this.options.bgcolor +'" /><param name="quality" value="best"></param><param name="flashvars" value="' + this.flashvars + '" />');
				}
				else {			
					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;
	}
});

/* Kristofer Baxter's website sifr commands */
window.addEvent('domready', function() {
	new MoosIFR("#sidebar h3", {
		"flashsrc": "/media/myriadpro.swf",
		"flashvars": "offsetTop=3",
		"bgcolor": "#ffffff",
		"textcolor": "#313131",
		"lettercase": "upper"
	});
	new MoosIFR("#main dl dt.title span", {
		"flashsrc": "/media/myriadpro.swf",
		"flashvars": "offsetTop=3",
		"bgcolor": "#ffffff",
		"textcolor": "#313131",
		"lettercase": "upper",
		"paddingbottom": "6"
	});
	var mainh3s = "";
	if ($$('body')[0].hasClass('ramblings') == false) { 
		mainh3s = "#main h3"; 
		new MoosIFR("#comments h3.title", {
			"flashsrc": "/media/myriadpro.swf",
			"flashvars": "offsetTop=3",
			"bgcolor": "#ffffff",
			"textcolor": "#313131",
			"lettercase": "upper",
			"paddingbottom": "6"
		});
	}
	else { mainh3s = "#main h3 span"; }
	new MoosIFR(mainh3s, {
		"flashsrc": "/media/myriadpro.swf",
		"flashvars": "offsetTop=3",
		"bgcolor": "#ffffff",
		"textcolor": "#313131",
		"lettercase": "upper",
		"paddingbottom": "6"
	});
	new MoosIFR("div.jobhistoryitem div.note h5", {
		"flashsrc": "/media/myriadpro.swf",
		"flashvars": "offsetTop=3",
		"bgcolor": "#ced9df",
		"textcolor": "#313131",
		"lettercase": "upper",
		"paddingbottom": "6"
	});
	var fontTestMovieSource = "/media/fontdetection.swf";
	if ($('fonttest')) {
		if (window.ie) { $('fonttest').setHTML('<object classid="clsid:D27CDB6E-AE6D-11cf-96B8-444553540000" width="1" height="1"><param name="movie" value="' + fontTestMovieSource + '" /><param name="bgcolor" value="#FFFAF5" /><param name="quality" value="best"></param>'); }
		else { $('fonttest').setHTML('<embed type="application/x-shockwave-flash" src="' + fontTestMovieSource + '" quality="best" wmode="transparent" bgcolor="#FFFAF5" width="1" height="1"></embed>'); }
	}
});

sendFontCookie = function(value) {
	Cookie.set('sifrNeeded', ( value === true ) ? 'true' : 'false' ,{duration:100, path: '/'});
}
