Event.DOMReady(prepareFlash);
Event.DOMReady(prepareFontReplacement);
/*Event.addEvent(window, "load", prepareFlash);
Event.addEvent(window, "load", prepareFontReplacement);*/

var JSFontDirectory = "fonts/";
var JSGetFlashIcon = "js_images/ico_get_flash.gif";

// search html for all font replacements
function prepareFontReplacement(){
	var fontReplacements = Document.getElementsByClassMatch("JS:Font");
	fontReplacements.each(FontReplacement);
}
// search html for all flash content
function prepareFlash(){
	var flashElements = Document.getElementsByClassMatch("JS:Flash");
	flashElements.each(Flash);
}

/*************** Flash CLASS ***************
* inserts flash content whilst detecting the users current flash player
*******************************************/
function Flash(element){
	var parameters = Document.getArguments(element,"JS:Flash");
	var swfPath = parameters[0];
	var width = parameters[1];
	var height = parameters[2];
	var version = parameters[3];
	var playerRequired = (parameters[4] == undefined) ? false : parameters[4];
	var queryString = (parameters[5] == undefined) ? "" : parameters[5];
	var swf = new FlashObject(swfPath,width,height);
	var playerInstalled = swf.flashPlayerRequired(element, playerRequired);
	if(playerInstalled){
		var versionInstalled = swf.flashVersionRequired(element, version, playerRequired);
		if(versionInstalled){
			swf.addQueryString(queryString);
			swf.embedSWF(element);
		}
	}	
}
/*************** FONT REPLACEMENT CLASS ***************
* replaces text with complex fonts using flash
*******************************************/
// font replacement constructor
function FontReplacement(element, wmode){
	var flashVersion = Browser.getFlashVersion();
	if(flashVersion >= 9){
		var parameters = Document.getArguments(element,"JS:Font");
		var font = parameters[0];
		var overideSize = parameters[1];
		overideSize = (overideSize == undefined) ? 0 : overideSize;
		var fontColour = Document.getStyle(element, "color");
		fontColour = Document.rgb2HEX(fontColour);
		var fontSize = FontReplacement.getFontSize(element);
		// calculate width
		var leftPadding = Document.getStyle(element, "padding-left").replace("px","");
		var rightPadding = Document.getStyle(element, "padding-right").replace("px","");
		var textWidth = element.offsetWidth - leftPadding - rightPadding;
		// calculate height
		var topPadding = Document.getStyle(element, "padding-top").replace("px","");
		var bottomPadding = Document.getStyle(element, "padding-bottom").replace("px","");
		var textHeight = element.offsetHeight - topPadding - bottomPadding;
		var textHeight = (overideSize == 0) ? textHeight : element.parentNode.offsetHeight;
		// fix accessibility issues
		var elementHTML = element.innerHTML.replace("&amp;","%26");
		element.innerHTML = "";
		var span = document.createElement("span");
		element.appendChild(span);
		var span2 = document.createElement("span");
		span2.style.marginLeft = "-9999px";
		span2.style.height = "0px";
		span2.style.float = "left";
		span2.style.fontSize = "0.1em";
		span2.innerHTML = elementHTML;
		element.appendChild(span2);
		// add flash replacement
		var swf = new FlashObject(JSFontDirectory + font, textWidth, textHeight);
		if(wmode != undefined){
			swf.setWMode("window");
		}
		swf.addVariable("text",elementHTML);
		swf.addVariable("size",fontSize);
		swf.addVariable("colour",fontColour);
		swf.addVariable("overide",overideSize);
		swf.addVariable("id",swf.id);
		swf.embedSWF(span);
	}
}
// returns the pixel size of an element font
FontReplacement.getFontSize = function(element){
	var tempDiv = document.createElement("div");
	tempDiv.style.margin = "0";
	tempDiv.style.padding = "0";
	tempDiv.style.clear = "both";
	document.getElementsByTagName("body")[0].appendChild(tempDiv);
	var newElement = document.createElement(element.tagName);
	newElement.style.padding = "0";
	newElement.style.margin = "0";
	newElement.style.lineHeight = "1";
	newElement.style.visibility = "hidden";
	newElement.innerHTML = "M";
	tempDiv.appendChild(newElement);
	var pixels = tempDiv.offsetHeight;
	tempDiv.removeChild(newElement);
	tempDiv.parentNode.removeChild(tempDiv);
	return pixels;
}
// callback used by flash to retrieve height of as3 textfield
FontReplacement.getTextFieldHeight = function(id, value){
	var flashElement = document.getElementById(id);
	flashElement.setAttribute("height",value);
}

/*************** FLASH OBJECT CLASS ***************
* inserts flash easily with ability to send variables to movie
*******************************************/
function FlashObject(swfPath, width, height){
	this.id = this.createId();
	this.swfPath = swfPath;
	this.width = width;
	this.height = height;
	this.queryString = "";
	this.transparent = "transparent";
}
FlashObject.prototype.createId = function(){
	var idTaken = true;
	var count = 0;
	while(idTaken){
		if(document.getElementById("swf" + count) == null){
			return "swf" + count;
			idTaken = false;
			break;
		}
		count ++;
	}
}
FlashObject.prototype.getId = function(){
	return this.id;	
}
FlashObject.prototype.setWMode = function(wmode){
	this.transparent = wmode; //window or transparent
}
FlashObject.prototype.embedSWF = function(element){
	var objectHTML = '<embed src="'+ this.swfPath +'.swf'+ this.queryString +'" ';
	objectHTML += 'loop="false" menu="false" quality="high" width="'+ this.width +'" height="'+ this.height +'" wmode="'+this.transparent+'" ';
	objectHTML += 'name="'+ this.id +'" id="'+ this.id +'" align="middle" allowScriptAccess="sameDomain" scale="noscale" salign="left" ';
	objectHTML += 'type="application/x-shockwave-flash" pluginspage="https://www.macromedia.com/go/getflashplayer" />';
	
	element.innerHTML = objectHTML;
}
FlashObject.prototype.addVariable = function(variable, value){
	if(this.queryString.length <= 0){
		this.queryString = "?" + variable + "=" + value;
	}
	else{
		this.queryString += "&" + variable + "=" + value;
	}
}
FlashObject.prototype.setId = function(value){
	this.id = value;	
}
FlashObject.prototype.addQueryString = function(query){
	this.queryString = query.replace("&amp;","&");
}
FlashObject.prototype.flashPlayerRequired = function(element, required){
	var isInstalled = Browser.isFlashInstalled();
	if(required && !isInstalled){
		element.innerHTML = '<p>The following content requires the Adobe Flash Player. We highly recommend that you download the plugin to enjoy the latest web technologies, please use the link below to proceed with the install (this will open a new window)</p><p><a href="http://get.adobe.com/flashplayer/" title="Get Flash" target="_blank"><img src="'+ JSGetFlashIcon +'" alt="Get Flash"></a></p><p class="clearer clear">Once the installation has completed, please refresh the web page to load the Flash content, or if your browser needs to be restarted, please copy the current web address, and paste it back once your browser has restarted.</p>';
	}
	if(isInstalled){
		return true;
	}
	return false;
}
FlashObject.prototype.flashVersionRequired = function(element, version, required){
	var currentVersion = Browser.getFlashVersion();
	if(required && currentVersion < version){
		element.innerHTML = '<p>The following content requires the Latest version of the Adobe Flash Player. We highly recommend that you upgrade to the latest version to enjoy the latest web technologies, please use the link below to proceed with the install (this will open a new window)</p><p><a href="http://get.adobe.com/flashplayer/" title="Get Flash" target="_blank"><img src="'+ JSGetFlashIcon +'" alt="Get Flash"></a></p><p class="clearer clear">Once the installation has completed, please refresh the web page to load the Flash content, or if your browser needs to be restarted, please copy the current web address, and paste it back once your browser has restarted.</p>';
	}
	if(currentVersion >= version){
		return true;
	}
	return false;
}
