
//Constants
var forceUpperCase = false;
var flashPath = "/_resources/script/custom/flash/";


//Tags to replace with heading_replace.swf, and font sizes to be used in the flash movie.
var headings = new Array(["h1",'<font size="26">'],
						  ["h2",'<font size="20">'],
						  ["h3",'<font size="20">'],
						  ["h4",'<font size="16">'],
						  ["h5",'<font size="14">'],
						  ["h6",'<font size="14">']);

var sectionColor;
//Code to embed the flash file
var flash = '<div style="height: @height;" id="fdlFlashDiv_@id@" class="hide_print">';
flash += '<object id="fdlFlashObj_@id@" classid="clsid:D27CDB6E-AE6D-11cf-96B8-444553540000" codebase="http://download.macromedia.com/pub/shockwave/cabs/flash/swflash.cab#version=6,0,29,0" width="100%" height="100%">';
flash += '<param name="movie" value="@movie" />';
flash += '<param name="quality" value="high" />';
flash += '<param name="wmode" value="transparent">';
flash += '<param name=flashvars value="@flashvars" />';
flash += '<embed src="@movie" quality="high" pluginspage="http://www.macromedia.com/go/getflashplayer" type="application/x-shockwave-flash" width="100%" height="100%" wmode="transparent" flashvars="@flashvars"></embed>';
flash += '</object>';
flash += '</div>';

var fdlFlashIdCounter = 0;
										  

//Flash detection script
var MM_contentVersion = 6;
var plugin = (navigator.mimeTypes && navigator.mimeTypes["application/x-shockwave-flash"]) ? navigator.mimeTypes["application/x-shockwave-flash"].enabledPlugin : 0;
if ( plugin ) {
		var words = navigator.plugins["Shockwave Flash"].description.split(" ");
	    for (var i = 0; i < words.length; ++i)
	    {
		if (isNaN(parseInt(words[i])))
		continue;
		var MM_PluginVersion = words[i]; 
	    }
	var MM_FlashCanPlay = MM_PluginVersion >= MM_contentVersion;
}
else if (navigator.userAgent && navigator.userAgent.indexOf("MSIE")>=0 
   && (navigator.appVersion.indexOf("Win") != -1)) {
	document.write('<SCR' + 'IPT LANGUAGE=VBScript\> \n'); //FS hide this from IE4.5 Mac by splitting the tag
	document.write('on error resume next \n');
	document.write('MM_FlashCanPlay = ( IsObject(CreateObject("ShockwaveFlash.ShockwaveFlash." & MM_contentVersion)))\n');
	document.write('</SCR' + 'IPT\> \n');
}


function doReplace(){

	var els;
	var el;
	var hTagName;
	var fontTag;
	var embedCode;
	
	var height;
	var c;
	var _origText;
	var _replaceText;
	for(var j=0; j<headings.length; j++){
		hTagName = headings[j][0];
		fontTag = headings[j][1];
		els = document.getElementById("content").getElementsByTagName(hTagName);
		if(MM_FlashCanPlay){
			for(var i=0; i<els.length; i++){
				el = els[i];
				height = el.offsetHeight;
				//Pull the text out of the node
				_origText = getInnerText(el);
				_replaceText = fontTag + _origText + "</font>";
				//Write some new HTML
				
				embedCode = flash.replace(/@flashvars/g, "replacecontent=" + escape(_replaceText) + "&fdlFlashID=" + fdlFlashIdCounter);
				embedCode = embedCode.replace(/@height/g, height + "px");
				embedCode = embedCode.replace(/@movie/g, flashPath + "heading_replace.swf");
				embedCode = embedCode.replace(/@id@/g, fdlFlashIdCounter);
				
				el.innerHTML = embedCode + "<span class='hide'>" + _origText + "</span>";
				el.style.visibility = "visible";
				
				fdlFlashIdCounter ++;
				}
		}else{
			for(var i=0; i<els.length; i++){
				el = els[i];
				el.style.visibility = "visible";
			}
		}
			
	}

}

function replaceSectionHeader(){
	
	if ( !MM_FlashCanPlay )return
	
	var el;
	var movie;
	var height;
	var _html;
	var _text;
	
	el = document.getElementById("section_header");

	if(el){
		movie = "section_header.swf";
		height = "60px";
	} else {
		el = document.getElementById("home_header");
		if(el){
			
			if(!MM_FlashCanPlay)
				el.className = "home_header";
				
			movie = "home_header.swf";
			height = "200px";
		} else {
			return;
		}
	}
	
	if ( !MM_FlashCanPlay ){
		el.className = "home_header";
		return;
	}
	
	//Pull the text out of the node
	_text = getInnerText(el);
	//Write some new HTML
	
	_html = flash.replace(/@flashvars/g, "replacecontent=" + _text);
	_html = _html.replace(/@height/g, height);
	_html = _html.replace(/@movie/g, flashPath + movie);
	_html = _html.replace(/@id/g, fdlFlashIdCounter ++);

	el.innerHTML = _html + "<span class='hide'>" + _text + "</span>";
}

function replacePromo(){
		
	var el;
	var movie;
	var height;
	var _html;
	var _text;
	
	el = document.getElementById("header_promo");

	if(el && MM_FlashCanPlay){
			movie = "promo.swf";
			height = "100px";
		
			//Pull the text out of the node
			_text = getInnerText(el);
			//Write some new HTML
			
			_html = flash.replace(/@flashvars/g, "replacecontent=" + _text);
			_html = _html.replace(/@height/g, height);
			_html = _html.replace(/@movie/g, flashPath + movie);
			_html = _html.replace(/@id/g, fdlFlashIdCounter ++);
			
			el.innerHTML = _html + "<span class='hide'>" + _text + "</span>";
	}
}

function getInnerText(e){

	 var strText = "";
	 var node;
	 for(var i=0; i<e.childNodes.length; i++ )
	 {node = e.childNodes[i];
	  switch(node.nodeType)
	  {
	   case 1: // elements
		strText += getInnerText(node);
		break;
	   case 3: // text
		strText += node.nodeValue;
		break;
	   default: // comments etc
		break;
	  }
	 }
	
	//Strip leading and trailing spaces.
    var regEx = /^[ \t\r\n]+|[ \t\r\n]+$/g;
	strText = strText.replace(regEx, "");
	
	//Force upper case if neccessary
	if(forceUpperCase)strText = strText.toUpperCase();
	return strText;
}

function adjustHeadingSize(fdlId, h){
	//alert( document.getElementById("fdlFlashObj_" + fdlId).id );
	var el = document.getElementById("fdlFlashDiv_" + fdlId);
	//alert(el.tagName + " id=" + fdlId + " : " + h);
	el.style.border = "1px solid white";
	el.style.height = h + "px";
}


function addEvent(objObject, strEventName, fnHandler) { 
 // DOM-compliant way to add an event listener 
 if (objObject.addEventListener) 
   objObject.addEventListener(strEventName, fnHandler, false); 
 // IE/windows way to add an event listener 
 else if (objObject.attachEvent) 
   objObject.attachEvent("on" + strEventName, fnHandler); 
}

addEvent(window,'load',doReplace);
//addEvent(window,'load',replaceSectionHeader);
//addEvent(window,'load',replacePromo);

