/*	sIFR (Scalable Inman Flash Replacement) Version 2.0b
	Copyright 2004 Mike Davidson, Shaun Inman, Tomas Jogin and Mark Wubben

	This software is licensed under the CC-GNU LGPL <http://creativecommons.org/licenses/LGPL/2.1/>
*/

var hasFlash = function(){
	var nRequiredVersion = 6;	
	
	if(navigator.appVersion.indexOf("MSIE") != -1 && navigator.appVersion.indexOf("Windows") > -1){
		document.write('<script language="VBScript"\> \n');
		document.write('on error resume next \n');
		document.write('hasFlash = (IsObject(CreateObject("ShockwaveFlash.ShockwaveFlash." & ' + nRequiredVersion + '))) \n');  
		document.write('<'+'/script\> \n');
		/*	If executed, the VBScript above checks for Flash and sets the hasFlash variable. 
			If VBScript is not supported it's value will still be undefined, so we'll run it though another test
			This will make sure even Opera identified as IE will be tested */
		if(window.hasFlash != null){
			return window.hasFlash;
		};
	};
	
	if(navigator.mimeTypes && navigator.mimeTypes["application/x-shockwave-flash"] && navigator.mimeTypes["application/x-shockwave-flash"].enabledPlugin){
		var flashDescription = (navigator.plugins["Shockwave Flash 2.0"] || navigator.plugins["Shockwave Flash"]).description;
		var flashVersion = parseInt(flashDescription.charAt(flashDescription.indexOf(".") - 1));
		return flashVersion >= nRequiredVersion;
	};
	
	/* Fallback */
	return false;
}();

String.prototype.normalize = function(){
	return this.replace(/\s+/gi, " ");
};

/* IE 5.0 does not support the push method, so here goes */
if(Array.prototype.push == null){
	Array.prototype.push = function(item){
		this[this.length] = item;
		return this.length;
	};
};

/*	The following code parses CSS selectors.
	This script however is not the right place to explain it,
	please visit the documentation for more information. */
var parseSelector = function(){
	var reParseSelector = /^([^#\.>\`]*)(#|\.|\>|\`)(.+)$/;
	function parseSelector(sSelector, oParentNode, sMode){
		sSelector = sSelector.replace(" ", "`");
		var selector = sSelector.match(reParseSelector);
		var node, listNodes, listSubNodes, subselector;
		var listReturn = [];

		if(selector == null){ selector = [sSelector, sSelector]	};
		if(selector[1] == ""){ selector[1] = "*" };
		if(sMode == null){ sMode = "`" };
		
		switch(selector[2]){
			case "#":
				subselector = selector[3].match(reParseSelector);
				if(subselector == null){ subselector = [null, selector[3]] };
				node = 	document.getElementById(subselector[1]);
				if((selector[1] != "*" && node.nodeName.toLowerCase() != selector[1].toLowerCase())){
					return false;
				};
				if(subselector.length == 2){
					listReturn.push(node);
					return listReturn;	
				};
				return parseSelector(subselector[3], node, "#");
			case ".":
				if(sMode == "`"){
					listNodes = getElementsByTagName(oParentNode, selector[1]);
				} else {
					listNodes = oParentNode.childNodes;
				};
				
				for(var i = 0; i < listNodes.length; i++){
					node = listNodes[i];
					if(node.nodeType != 1){
						continue;	
					};

					subselector = selector[3].match(reParseSelector);
					if(subselector != null){
						if(node.className.match("\\b" + subselector[1] + "\\b") == null){
							continue;
						};
						listSubNodes = parseSelector(subselector[3], node, subselector[2]);
						if(listSubNodes != false){
							listReturn = listReturn.concat(listSubNodes);	
						};
					} else if(node.className.match("\\b" + selector[3] + "\\b") != null){
						listReturn.push(node);
					};
				};
				return listReturn;
			case ">":
				if(sMode == "`"){
					listNodes = getElementsByTagName(oParentNode, selector[1]);
				} else {
					listNodes = oParentNode.childNodes;
				};
				for(var i = 0; i < listNodes.length; i++){
					node = listNodes[i];
					if(node.nodeType != 1){
						continue;	
					};
					if(node.nodeName.toLowerCase() != selector[1].toLowerCase()){
						continue;
					};
					listSubNodes = parseSelector(selector[3], node, ">");
					if(listSubNodes != false){
						listReturn = listReturn.concat(listSubNodes);	
					};
				};
				return listReturn;
			case "`":
				listNodes = getElementsByTagName(oParentNode, selector[1]);
				for(var i = 0; i < listNodes.length; i++){
					node = listNodes[i];
					listSubNodes = parseSelector(selector[3], node, "`");
					if(listSubNodes != false){
						listReturn = listReturn.concat(listSubNodes);	
					};
				};
				return listReturn;
			default:
				listNodes = getElementsByTagName(oParentNode, selector[0]);
				for(var i = 0; i < listNodes.length; i++){
					listReturn.push(listNodes[i]);
				};
				return listReturn;
		};
	};
	
	function getElementsByTagName(oParentNode, sTagName){
		/*	IE5.x does not support document.getElementsByTagName("*")
			therefore we're resorting to element.all */
		if(sTagName == "*" && oParentNode.all != null){
			return oParentNode.all;
		};
		return oParentNode.getElementsByTagName(sTagName);
	};
	
	return parseSelector;
}();

/*	Executes an anonymous function which returns the function sIFR (defined inside the function).
	You can replace elements using sIFR.replaceElement()
	All other variables and methods you see are private. If you want to understand how this works you should
	learn more about the variable-scope in JavaScript. */
var sIFR = function(){

	if(window.hasFlash == false || !document.createElement || !document.getElementById){ return function(){return false} };
	
	/* Providing a hook for you to hide certain elements if Flash has been detected */
	if(document.documentElement){
		document.documentElement.className += " sIFR-hasFlash";
	};

	/* Opera and Mozilla require a namespace when creating elements in an XML page */
	var sNameSpaceURI = "http://www.w3.org/1999/xhtml";
	var UA = { bIsKHTML: navigator.userAgent.indexOf('KTHML') != -1 || navigator.userAgent.indexOf('AppleWebKit') != -1, bIsOpera : navigator.userAgent.indexOf("Opera") > -1 };
	var bIsInitialized = false;
	
	function fetchContent(oNode, oNewNode){
		var sContent = "";
		var oSearch = oNode.firstChild;
		var oRemove, oRemovedNode, oTarget;

		while(oSearch){
			if(oSearch.nodeType == 3){
				sContent += oSearch.nodeValue;
			} else if(oSearch.nodeType == 1){
				if(oSearch.nodeName.toLowerCase() == "a"){
				if(oSearch.getAttribute("target")){
					oTarget = oSearch.getAttribute("target");
				} else {
					oTarget = "";
				};
					sContent += '<a href="' + oSearch.getAttribute("href") + '" target="' + oTarget + '">';
				};				
				if(oSearch.hasChildNodes){
					sContent += fetchContent(oSearch);
				};
				if(oSearch.nodeName.toLowerCase() == "a"){
					sContent += "</a>";
				};
			};
			oRemove = oSearch;
			oSearch = oSearch.nextSibling;
			if(oNewNode != null){
				oRemovedNode = oRemove.parentNode.removeChild(oRemove);
				oNewNode.appendChild(oRemovedNode);	
			};
		};
		return sContent;
	};
	
	function createElement(sTagName){
		if(document.createElementNS){
			return document.createElementNS(sNameSpaceURI, sTagName);	
		} else {
			return document.createElement(sTagName);
		};
	};






	
	function createObject(sWidth, sHeight, sFlashSrc, sWmode, sBgColor, sVars){
		var nodeFlash = createElement("object");
		nodeFlash.className = "sIFR-flash";
		nodeFlash.setAttribute("classid", "clsid:D27CDB6E-AE6D-11cf-96B8-444553540000");
		nodeFlash.setAttribute("codebase", "http://download.macromedia.com/pub/shockwave/cabs/flash/swflash.cab#version=6,0,0,0");
		nodeFlash.setAttribute("width", sWidth);
		nodeFlash.setAttribute("height", sHeight);
		nodeFlash.setAttribute("data", sFlashSrc);
		nodeFlash.style.width = sWidth + "px";
		nodeFlash.style.height = sHeight + "px";

		createObjectParameter(nodeFlash, "movie", sFlashSrc);
		createObjectParameter(nodeFlash, "wmode", sWmode);
		createObjectParameter(nodeFlash, "bgcolor", sBgColor);
		createObjectParameter(nodeFlash, "flashvars", sVars);
		
		return nodeFlash;
	};
	
	function createObjectParameter(nodeObject, sName, sValue){
		var node = createElement("param");
		node.setAttribute("name", sName);	
		node.setAttribute("value", sValue);
		nodeObject.appendChild(node);
	};
	
	function createEmbed(sWidth, sHeight, sFlashSrc, sWmode, sBgColor, sVars){
		var nodeFlashEmbed = createElement("embed");
		nodeFlashEmbed.className = "sIFR-flash";
		nodeFlashEmbed.setAttribute("src", sFlashSrc);
		nodeFlashEmbed.setAttribute("flashvars", sVars);
		nodeFlashEmbed.setAttribute("width", sWidth);
		nodeFlashEmbed.setAttribute("height", sHeight);

		nodeFlashEmbed.setAttribute("type", "application/x-shockwave-flash");
		nodeFlashEmbed.setAttribute("pluginspage", "http://www.macromedia.com/go/getflashplayer");
		nodeFlashEmbed.setAttribute("wmode", sWmode);
		nodeFlashEmbed.setAttribute("bgcolor", sBgColor);
		nodeFlashEmbed.style.width = sWidth + "px";
		nodeFlashEmbed.style.height = sHeight + "px";
		return nodeFlashEmbed;
	};
	
	function forceRedraw() {
		/*	Corrects a margin-bottom sum bug in Mozilla
			In case you're wondering why I didn't use document.body,
			it's because that throws an error if used in an XML page. */
		var nodeBody = document.getElementsByTagName("body")[0];
		nodeBody.style.height = "1px";
		nodeBody.style.height = "auto";
	};
	
	function replaceElement(sSelector, sFlashSrc, sColor, sLinkColor, sHoverColor, sBgColor, nPaddingTop, nPaddingRight, nPaddingBottom, nPaddingLeft, sFlashVars){
		if(sFlashVars != null){
			sFlashVars = "&" + sFlashVars.normalize();
		} else {
			sFlashVars = "";	
		};
		
		var sWmode = (sBgColor == "transparent") ? "transparent" : "opaque";
		var node, sWidth, sHeight, sMargin, sPadding, sText, sVars, nodeAlternate, nodeFlash;
		var listNodes = parseSelector(sSelector, document);
		if(listNodes.length == 0){ return false };
		
		for(var i = 0; i < listNodes.length; i++){
			node = listNodes[i];
			/* Prevents elements from being replaced multiple times. */
			if(node.className.match(/\bsIFR\-replaced\b/) != null){ continue; };

			sWidth = node.offsetWidth - nPaddingLeft - nPaddingRight;
			sHeight = node.offsetHeight - nPaddingTop - nPaddingBottom;
			
			nodeAlternate = createElement("span");
			nodeAlternate.className = "sIFR-alternate";

			sText = fetchContent(node, nodeAlternate);
			sText = sText.replace(/&amp;/g, '%26');
			sText = sText.replace(/&/g, '%26');
			sText = sText.replace(/\"/g, '\'');
						
			sVars = "txt=" + sText + sFlashVars + "&w=" + sWidth + "&h=" + sHeight;
			if (sColor != null){sVars += "&textcolor=" + sColor};
			if (sLinkColor != null){sVars += "&linkcolor=" + sLinkColor}; 
			if (sHoverColor != null){sVars += "&hovercolor=" + sHoverColor};
			
			node.className += " sIFR-replaced";
			/*	First of all, KHTML (Apple Web Kit) based browsers do not really support object elements,
				appending them with an embed element as child will not work. Mozilla doesn't really support
				object elements either, but embed will work.
				IE throws erros when appending the object element, if you catch these and append the embed
				element instead it won't load.
				Frankly, Opera only supports the object element. It's a mess, ain't it? */
			if(UA.bIsOpera){
				nodeFlash = createObject(sWidth, sHeight, sFlashSrc, sWmode, sBgColor, sVars);
				node.appendChild(nodeFlash);
			} else {
				node.appendChild(createEmbed(sWidth, sHeight, sFlashSrc, sWmode, sBgColor, sVars));
			};
			node.appendChild(nodeAlternate);
			
			/*	Workaround to force KHTML-browsers to repaint the document. Additionally, IE for both Mac and PC need 				this, however, Opera will choke on it
				See: http://neo.dzygn.com/archive/2004/09/forcing-safari-to-repaint */

			if(UA.bIsKHTML || (navigator.appName == "Microsoft Internet Explorer" && navigator.appVersion.indexOf("Mac") > -1)){
				node.innerHTML += " ";
			} else if (navigator.userAgent.indexOf("Opera") == -1) {
				node.innerHTML += "";
			};
		};
		forceRedraw();	
	};
	
	function sIFR(){
		if(bIsInitialized == true){
			return;
		} else {
			bIsInitialized = true;
		};

		 replaceElement("h2.main-title", "flash/header_flash.swf", "#000099", null, null, null, 0, 0, 0, 0);
		 replaceElement("span.main-title", "flash/header_flash.swf", "#000099", null, null, null, 0, 0, 0, 0);
		 replaceElement("div.right-text", "flash/p_flash.swf", "#000099", null, null, null, 0, 0, -15, 0, "textalign=center");
		

	/* You can place your replace statement(s) right above here, or in the preferred place, at the end of the HTML document: 
	replaceElement(sSelector, sFlashSrc, sColor, sLinkColor, sHoverColor, sBgColor, nPaddingTop, nPaddingRight, nPaddingBottom, nPaddingLeft, sFlashVars);
	
	Here is what goes in your statements:
	
	sSelector = CSS selector you'd like to replace
	sColor = text color (e.g. '#000000')
	sLinkColor = link color (e.g. '#0000CC')
	sHoverColor = link color on hover (e.g. '#00CC00')
	sBgColor = color of background (e.g. '#CCCCCC', or 'transparent'... transparent not recommended)
	
	padding parameters = enter 0 in here unless you specifically have padding assigned to the element in CSS... padding numbers must match up exactly in order to properly control for box models)
	
	sFlashVars = this is a special parameter which lets you pass other things into Flash. The only other hooks I've added are textalign, and horizontal/vertical offset.  So to center your text horizontally and push it down 2 pixels and right 4 pixels, you'd enter 'textalign=center&offsetTop=2&offsetLeft=4'
	
	*/

	};

	sIFR.replaceElement = replaceElement;
	
	if(window.attachEvent){
		window.attachEvent("onload", sIFR);
	} else {
		if(document.addEventListener){
			document.addEventListener("load", sIFR, false);	
		};
		if(window.addEventListener){
			window.addEventListener("load", sIFR, false);	
		};
	};

	return sIFR;
}();