/**
 * @author Nathan Derksen
 * Copyright 2007 IBM Corp
 * Copyright 2007 Tiffany Corp
 */

var flashHeader_numHeaders = 0;
var pageLanguage = "en";
var headerIntervalId = 0;
var startingHeader = 0;
var startingHeader2 = 0;
var startingHeader3 = 0;
var startingHeader4 = 0;
var flashPath = "/Shared/flash/";

if (locale != null && locale != "" && typeof(locale) != "undefined")
{
	var splitLocale = locale.split("-");
	if (splitLocale.length > 0)
	{
		if (splitLocale[0].length == 2)
		{
			pageLanguage = splitLocale[0];
		}
	}
}

// Major version of Flash required
var requiredMajorVersion = 8;
// Minor version of Flash required
var requiredMinorVersion = 0;
// Minor version of Flash required
var requiredRevision = 0;

var h1Properties = {
	size:24, 
	asianSize:19,
	color:"#000000",
	horizontalShift:2,
	baselineShift:-1,
	multiLine:true,
	leading:6,
	tracking:0,
	backgroundColor:"#FFFFFF",
	maxLines:0,
	align:"left",
	sharpness:0,
	thickness:0,
	advancedAntiAliasing:true
};

var h2Properties = {
	size:18, 
	asianSize:15,
	color:"#808080",
	horizontalShift:0,
	baselineShift:-1,
	multiLine:true,
	leading:6,
	tracking:0,
	backgroundColor:"#FFFFFF",
	maxLines:0,
	align:"left",
	sharpness:0,
	thickness:0,
	advancedAntiAliasing:true
};

var h3Properties = {
	size:18, 
	asianSize:15,
	color:"#000000",
	horizontalShift:2,
	baselineShift:-1,
	multiLine:true,
	leading:6,
	tracking:0,
	backgroundColor:"#FFFFFF",
	maxLines:0,
	align:"left",
	sharpness:0,
	thickness:0,
	advancedAntiAliasing:true
};

var h4Properties = {
	size:18, 
	asianSize:15,
	color:"#808080",
	horizontalShift:0,
	baselineShift:-1,
	multiLine:true,
	leading:6,
	tracking:0,
	backgroundColor:"#FFFFFF",
	maxLines:0,
	align:"left",
	sharpness:0,
	thickness:0,
	advancedAntiAliasing:true
};

if (FlashAPI.isFlashAvailable(requiredMajorVersion, requiredMinorVersion, requiredRevision) == true)
{
	// Make sure the non-flash text does not flicker on at page start
	document.write('<style>h1.flashTitle {visibility:hidden}</style>');
	document.write('<style>h2.flashTitle {visibility:hidden}</style>');
	document.write('<style>h3.flashTitle {visibility:hidden}</style>');
	document.write('<style>h4.flashTitle {visibility:hidden}</style>');
}

// -------------------------------------------
// Function: 	stopParse
// Description:	Stops the loop which starts on beginning of page load
// 				to look for <h1> tags.
// inputs: 		<none>
// returns: 	<null>
// -------------------------------------------
function stopParse()
{
	clearInterval(headerIntervalId);
	replaceHeaders();
}


// -------------------------------------------
// Function: 	replaceHeaders
// Description:	Loops through all <h1> tags, replacing contents with
//				Flash equivalent.
// inputs: 		<none>
// returns: 	<null>
// -------------------------------------------
function replaceHeaders()
{
	var headerList = document.getElementsByTagName("h1");
	var header2List = document.getElementsByTagName("h2");
	var header3List = document.getElementsByTagName("h3");
	var header4List = document.getElementsByTagName("h4");
	var start = startingHeader;
	var start2 = startingHeader2;
	var blockFlash = false;
	
	if (typeof(blockTIFR) != "undefined")
	{
		blockFlash = blockTIFR;
	}

	if (FlashAPI.isFlashAvailable(requiredMajorVersion, requiredMinorVersion, requiredRevision) == true && blockFlash == false)
	{
		for (var i=start; i < headerList.length; i++)
		{
			headerList[i].style.visibility = "visible";
			createHeader(headerList[i], h1Properties);
			startingHeader = i+1;
		}
		
		for (i=start; i < header2List.length; i++)
		{
			header2List[i].style.visibility = "visible";
			createHeader(header2List[i], h2Properties);
			startingHeader2 = i+1;
		}

		for (var i=start; i < header3List.length; i++)
		{
			header3List[i].style.visibility = "visible";
			createHeader(header3List[i], h3Properties);
			startingHeader3 = i+1;
		}
		
		for (i=start; i < header4List.length; i++)
		{
			header4List[i].style.visibility = "visible";
			createHeader(header4List[i], h4Properties);
			startingHeader4 = i+1;
		}
	}
}

function resetHeaders()
{
	startingHeader = 0;
	startingHeader2 = 0;
	startingHeader3 = 0;
	startingHeader4 = 0;
	replaceHeaders();
}

// -------------------------------------------
// Function: 	startParse
// Description:	Starts the loop which looks for new <h1> tags.
// inputs: 		<none>
// returns: 	<null>
// -------------------------------------------
function startParse()
{
	replaceHeaders();
	headerIntervalId = setInterval("replaceHeaders()", 100);
}

// -------------------------------------------
// Function: 	createHeader
// Description:	Creates the Flash equivalent of a particular header
// inputs: 		hItem <element>: The DOM handle to the tag element who's
//				text contents are to be replaced.
//				propObject <object>: An object of text properties to pass to Flash.
// returns: 	<null>
// -------------------------------------------
function createHeader(hItem, propObj)
{
	var hWidth = hItem.offsetWidth;
	var hHeight = hItem.offsetHeight;
	var hContents = hItem.innerHTML;
	var hContentsLower = hContents.toLowerCase();

	if (hContentsLower.indexOf("<object") == -1 && hContentsLower.indexOf("<embed") == -1)
	{
		// Safari 3 beta has some problems with a global var that causes a duplicate call. 
		// Get around this by looking whether there is already an <object> or <embed> tag in the HTML
		createFlashMovie(hItem, hWidth, hHeight, hContents, propObj, true);
	}
}

// -------------------------------------------
// Function: 	createFlashMovie
// Description:	Creates the HTML for the object/embed tags that put the
//				Flash movie on screen.
// inputs: 		hWidth <integer>: Pixel width of content area
//				hHeight <integer>: Pixel height of content area
//				hContents <string>: Text to display
//				propObj <object>: Object of text properties to pass to Flash movie
//				isShim <Boolean>: If isShim true, load the wrapper Flash movie
//				instead of the primary Flash movie. The wrapper will indirectly load 
//				the primary Flash movie. Used for IE display issue.
// returns: 	<null>
// -------------------------------------------
function createFlashMovie(hItem, hWidth, hHeight, hContents, propObj, isShim)
{
	var isShimStr = "";
	if (isShim == true)
	{
		isShimStr = "Shim";
	}

	hContents = cleanHeaderText(hContents);

	var flashVars = "header=" + hContents
	+ "&color=" + propObj.color
	+ "&fontSize=" + propObj.size
	+ "&asianFontSize=" + propObj.asianSize
	+ "&horizontalShift=" + propObj.horizontalShift.toString()
	+ "&baselineShift=" + propObj.baselineShift.toString()
	+ "&multiLine=" + propObj.multiLine.toString()
	+ "&leading=" + propObj.leading.toString()
	+ "&letterSpacing=" + propObj.tracking.toString()
//	+ "&backgroundColor=" + propObj.backgroundColor.toString()
	+ "&maxLines=" + propObj.maxLines.toString()
	+ "&align=" + propObj.align
	+ "&sharpness=" + propObj.sharpness
	+ "&thickness=" + propObj.thickness
	+ "&advancedAntiAliasing=" + propObj.advancedAntiAliasing;

	var itemFlash = new FlashAPI(hItem);
	itemFlash.setAttribute("src", flashPath + 'flashHeader' + isShimStr + '_' + pageLanguage + '.swf?v=5');
	itemFlash.setAttribute("width", hWidth);
	itemFlash.setAttribute("height", hHeight);
//	itemFlash.setAttribute("bgcolor", propObj.backgroundColor);
	itemFlash.setAttribute("flashVars", flashVars);
	itemFlash.setAttribute("name", "itemZoom");
	itemFlash.setAttribute("wmode", "transparent");
	itemFlash.setAttribute("scale", "noscale");
	itemFlash.setAlternateHTML(hContents);
	itemFlash.setFlashVersion(requiredMajorVersion, requiredMinorVersion, requiredRevision);
	itemFlash.generateFlash();
}

function cleanHeaderText(headerText)
{
	var cleanText = headerText;
	// Replace special characters with escaped equivalents.
	cleanText = cleanText.split("\t").join("");
	cleanText = cleanText.split("\n").join("");
	cleanText = cleanText.split("  ").join(" ");

	cleanText = cleanText.split("%").join("%25");
	cleanText = cleanText.split("&amp;").join("%26");
	cleanText = cleanText.split("&").join("%26");
	cleanText = cleanText.split("+").join("%2B");
	cleanText = cleanText.split("\"").join("%22");
	return cleanText;
}

startParse();

//window.onload = stopParse;
BrowserUtils.addOnLoadHandler(stopParse); // ND: Added to allow multiple onload handlers
