var headingBasePath = "headings/";

// Tags to replace with heading_replace.swf, and font sizes to be used in the flash movie.
var headings = new Array(
{
 	selector: "#site_header_logo h1",
	skip: true // Use to override flash replace
	}
,
{
 	selector: "#mini_login h3",
	skip: true // Use to override flash replace
	}
,
{
 	selector: "#welcome h2",
	css: "div {font-family: __standard; font-size: 24px; color: #999999; letter-spacing: 0px; leading: -10px;} strong {color: #1c1e1c}",
	fontFile: "myriad_italic.swf",
	textThickness: 50,
	textThickness: 0,
	skip: false,
	defaultInteraction: true
	}
,	
{
 	selector: "#sub_menu_container h3",
	css: "div {font-family: __standard; font-size: 14px; color: #1c1e1c; letter-spacing: 0px; leading: 0px;}",
	fontFile: "myriad_italic.swf",
	textThickness: 0}		
,
{
 	selector: ".homepage_panel h3",
	css: "div {font-family: __standard; font-size: 14px; color: #1c1e1c; letter-spacing: 0px; leading: 0px;}",
	fontFile: "myriad_italic.swf",
	textThickness: 0}		
,
{
 	selector: "#hp_search h3",
	css: "div {font-family: __standard; font-size: 16px; color: #ffffff; leading: -2px; vertical-align: center; shadowSize: 1; shadowStrength: 1; shadowAlpha: 1; shadowColor: #000000}",
	fontFile: "myriad_italic.swf",
	textThickness: 0}
,
{
 	selector: "h1",
	css: "div {font-family: __standard; font-size: 24px; color: #1c1e1c; letter-spacing: 0px; leading: -5px;} strong {color: #1c1e1c}",
	fontFile: "myriad_italic.swf",
	textThickness: 50,
	textThickness: 0,
	skip: false,
	defaultInteraction: true
	}	
,
{
 	selector: "h2",
	css: "div {font-family: __standard; font-size: 18px; color: #1c1e1c; letter-spacing: 0px; leading: 0px;}",
	fontFile: "myriad_italic.swf",
	textThickness: 0}
,
{
 	selector: "h3",
	css: "div {font-family: __standard; font-size: 14px; color: #1c1e1c; letter-spacing: 0px; leading: 0px;}",
	fontFile: "myriad_italic.swf",
	textThickness: 0}


/*,	
{
 	selector: "h3",
	css: "div {font-family: __standard; font-size: 12px; color: #000000; letter-spacing: 0px; leading: 0px;}",
	fontFile: "myriad.swf",
	textThickness: 50}
	*/
/*,	
{
 	selector: "#menu a strong",
	css: "div {font-family: __standard; font-size: 10px; color: #000000; letter-spacing: 0px; leading: 0px; shadowSize: 1; shadowStrength: 1; shadowAlpha: 0; vertical-align: center; text-transform: uppercase;}",
	cssOver: "div {font-family: __standard; font-size: 10px; color: #999999; letter-spacing: 0px; leading: 0px; shadowSize: 1; shadowStrength: 1; shadowAlpha: 0; vertical-align: center;  text-transform: uppercase;}",	
	fontFile: "myriad.swf",
	textThickness: 50,
	defaultInteraction: false
	}	*/
);

var fontFlashObjs;
var fontCheckFlashBlockInterval;

if(swfobject.getFlashPlayerVersion().major >= flashVersion){
	document.write('<style type="text/css">');
	for(var g=0; g<headings.length; g++)
		document.write(headings[g].selector + " {visibility: hidden;}");
	document.write('</style>');
}

function addFontObject(fontObj){
	if(swfobject.getFlashPlayerVersion().major < flashVersion)return;
	headings.unshift(fontObj);
	document.write('<style type="text/css">');
	document.write(fontObj.selector + " {visibility: hidden;}");
	document.write('</style>');
}	

function fontFlashObjLoaded(flashId){
	
	clearInterval(fontCheckFlashBlockInterval);
	var flashObj = document.getElementById(flashId);

	//Debug.addLine("heading_replace.js", "fontFlashObjLoaded", flashId);
	
	if(flashObj.parentNode.isPreloader){
		renderFontObjects(fontFlashObjs[flashObj.parentNode.id].fontObjs);
		flashObj.parentNode.parentNode.removeChild(flashObj.parentNode);		
		return;
	}

	flashObj.init();
	
	if(flashObj.defaultInteraction){
		flashObj.onmouseover = function(){this.mouseover();}
		flashObj.onmouseout = function(){this.mouseout();}
	}
}


function fontCheckFlashBlock(){
	
	//Debug.addLine("heading_replace.js", "fontCheckFlashBlock");
	
	for(var i in fontFlashObjs){
		
		var preloaderEl = document.getElementById(i);

		if(preloaderEl.firstChild.tagName == "DIV"){
			preloaderEl.parentNode.removeChild(preloaderEl);
			restoreFontHtml();
			clearInterval(fontCheckFlashBlockInterval);
		}
	}
}

function restoreFontHtml(){
	for(var i=0; i<headings.length; i++){
		els = getElementsBySelector(headings[i].selector);
		for(var j=0; j<els.length; j++){
			els[j].style.visibility = "visible";
		}
	}
}

function renderFontObjects(fontObjs){

	for(var i=0; i<fontObjs.length;i++){
		var fontFlashObj = fontObjs[i];
		var el = fontFlashObj.el;
		var fontFile = headingBasePath + fontFlashObj.fontFile;
		var width = fontFlashObj.width;
		var height = fontFlashObj.height;
		var pars = fontFlashObj.pars;
		var flashObj = writeFlash(el, fontFile, width, height, pars);
		fontFlashObj.id = flashObj.id;
		flashObj.defaultInteraction = fontFlashObj.defaultInteraction;
	}
	
}

function preloadFlash(){
	for(var i in fontFlashObjs){
		var fontFile = headingBasePath + fontFlashObjs[i].fontFile;
		var el = document.createElement("div");
		el.id = i;
		with(el.style){
			position = "fixed";
			top = "1px";
			left = "1px";
		}
		document.getElementsByTagName("body")[0].appendChild(el);
		var pars = { wmode: "window" };
		var preLoadObj = writeFlash(el, fontFile, 1, 1, pars, {isPreloader: true});

	}
}

	
function initHeadingReplace(){
	
	if(swfobject.getFlashPlayerVersion().major < flashVersion)
		return;

	//Debug.addLine("heading_replace.js", "initHeadingReplace", navigator.userAgent);
	//Debug.addLine("heading_replace.js", "initHeadingReplace", navigator.platform);
	
	fontFlashObjs = new Object();
	
	for(var j=0; j<headings.length; j++){
		
		heading = headings[j];

		heading.cssOver = 				heading.cssOver == undefined ? heading.css : heading.cssOver;
		heading.textThickness = 		heading.textThickness == undefined ? 0 : heading.textThickness;
		heading.textSharpness = 		heading.textSharpness == undefined ? 0 : heading.textSharpness;
		heading.skip = 					heading.skip == undefined ? false : heading.skip;
		heading.defaultInteraction = 	heading.defaultInteraction == undefined ? true : heading.defaultInteraction;
		heading.wmode = 				heading.wmode == undefined ? "transparent" : heading.wmode;
		
		var els = getElementsBySelector(heading.selector);			

		for(var i=0; i<els.length; i++){
			var el = els[i];
			processFontObj(el, heading);
		}
	}

	preloadFlash();
	//renderFontFlash();
	
	if(navigator.userAgent.toLowerCase().indexOf("firefox") > -1){
		fontCheckFlashBlockInterval = setInterval(fontCheckFlashBlock, 100);
	}
}

function processFontObj(el, heading){
	
	
	
	if(el.skip)return;
	if(heading.skip) {
		el.skip = "skip";
		el.style.visibility = "visible";
		return
	}
	
	el.skip = "skip"; 
	if(el.tagName == "A"){
		link = el.href;
	}else if(el.parentNode.tagName == "A") {
		link = el.parentNode.href;
	}else{
		link = "";
	}
	
	//Default Offset widths, these include padding.
	var height = el.offsetHeight;
	var width = el.offsetWidth;
	
	//Allow for padding
	if(window.getComputedStyle){ // Firefox etc
		width = window.getComputedStyle(el, null).getPropertyValue("width");
		height = window.getComputedStyle(el, null).getPropertyValue("height");
		
		width = width != "auto" ? width.substr(0, width.length - 2) : width;
		height = height != "auto" ? height.substr(0, height.length - 2) : height;
	
	}else if(el.currentStyle){ // IE
		var pl;
		var pr;
		var pt;
		var pb;
		pl = el.currentStyle.paddingLeft;
		pr = el.currentStyle.paddingRight;
		pt = el.currentStyle.paddingTop;
		pb = el.currentStyle.paddingBottom;
		
		pl = pl.substr(0, pl.length - 2);
		pr = pr.substr(0, pr.length - 2);
		pt = pt.substr(0, pt.length - 2);
		pb = pb.substr(0, pb.length - 2);
		
		height = height - pt - pb ;
		width = width - pl - pr;
	}
		  
	//Fix Firefox rendering bug, holds the element open
	var h = height.toString().indexOf("%")>-1 || height.toString() == "auto" ?height:height+"px";
	el.style.height = h;

	//Pull the text out of the node
	var content = "<div>" + getInnerHtml(el); + "</div>";
	
	var flashVars = "content=" + encodeURIComponent(content) +
		"&css=" +			escape(heading.css) +
		"&link=" + 			encodeURIComponent(link) +
		"&css_over=" + 		escape(heading.cssOver) +
		"&aa_thickness=" + 	heading.textThickness +
		"&aa_sharpness=" + 	heading.textSharpness;
	
	var pars = {
		flashvars: 			flashVars,
		wmode: 				heading.wmode
		};
	
	var fontFlashObj = {
		el:					el,
		width:				width,
		height:				height,
		fontFile:			heading.fontFile,
		defaultInteraction:	heading.defaultInteraction,
		pars:				pars
		};
		
	
	//Debug.addLine("heading_replace.js", "processFontObj", flashVars);
			
	//fontFlashObjs.push(fontFlashObj);
	var fontFileId = heading.fontFile.replace(".", "_");
	
	if(fontFlashObjs[fontFileId]==undefined)fontFlashObjs[fontFileId] = new Object({fontFile: heading.fontFile, fontObjs: []});
	fontFlashObjs[fontFileId].fontObjs.push(fontFlashObj);
	
}

EventUtils.addEventListener(window,'load', initHeadingReplace);
