/**
 * @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 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, 
	color:"#000000",
	horizontalShift:5,
	baselineShift:-1,
	leading:30,
	tracking:0,
	backgroundColor:"#FFFFFF",
	align:"left",
	sharpness:0,
	thickness:0,
	spaceWidth:3,
	useTransparentCopy:"true",

	sizeCJK:19, 
	colorCJK:"#000000",
	horizontalShiftCJK:5,
	baselineShiftCJK:-1,
	leadingCJK:30,
	trackingCJK:0,
	sharpnessCJK:0,
	thicknessCJK:0,
	spaceWidthCJK:3
};

var h2Properties = {
	size:18, 
	color:"#808080",
	horizontalShift:0,
	baselineShift:-1,
	leading:28,
	tracking:0,
	backgroundColor:"#FFFFFF",
	align:"left",
	sharpness:0,
	thickness:0,
	spaceWidth:0,
	useTransparentCopy:"false",
	
	sizeCJK:15, 
	colorCJK:"#808080",
	horizontalShiftCJK:0,
	baselineShiftCJK:-1,
	leadingCJK:22,
	trackingCJK:0,
	sharpnessCJK:0,
	thicknessCJK:0,
	spaceWidthCJK:0
};

if (pageLanguage == "zh")
{
	h1Properties.baselineShiftCJK = 2;
	h1Properties.sizeCJK = 21;
	h1Properties.trackingCJK = -2;
}

if (locale.toLowerCase() == "en-us-ird")
{
	h1Properties.backgroundColor = "#e5e5e5";
	h2Properties.backgroundColor = "#e5e5e5";
}

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>');
}

// -------------------------------------------
// 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 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;
		}
	}
}

function resetHeaders()
{
	startingHeader = 0;
	startingHeader2 = 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);

	if (typeof(propObj.color) != "undefined" && propObj.color != null && propObj.color != "")
	{
		propObj.color = propObj.color.split("#").join("0x");
	}
	if (typeof(propObj.color) != "undefined" && propObj.color != null && propObj.color != "")
	{
		propObj.colorCJK = propObj.colorCJK.split("#").join("0x");
	}

	var flashVars = "header=" + hContents
	+ "&color=" + propObj.color
	+ "&bgColor=" + propObj.backgroundColor
	+ "&fontSize=" + propObj.size
	+ "&horizontalShift=" + propObj.horizontalShift.toString()
	+ "&baselineShift=" + propObj.baselineShift.toString()
	+ "&leading=" + propObj.leading.toString()
	+ "&letterSpacing=" + propObj.tracking.toString()
//	+ "&backgroundColor=" + propObj.backgroundColor.toString()
	+ "&align=" + propObj.align
	+ "&sharpness=" + propObj.sharpness
	+ "&thickness=" + propObj.thickness
	+ "&spaceWidth=" + propObj.spaceWidth
	+ "&useTransparentCopy=" + propObj.useTransparentCopy

	+ "&colorCJK=" + propObj.colorCJK
	+ "&fontSizeCJK=" + propObj.sizeCJK
	+ "&horizontalShiftCJK=" + propObj.horizontalShiftCJK.toString()
	+ "&baselineShiftCJK=" + propObj.baselineShiftCJK.toString()
	+ "&leadingCJK=" + propObj.leadingCJK.toString()
	+ "&letterSpacingCJK=" + propObj.trackingCJK.toString()
//	+ "&backgroundColorCJK=" + propObj.backgroundColor.toString()
	+ "&sharpnessCJK=" + propObj.sharpnessCJK
	+ "&thicknessCJK=" + propObj.thicknessCJK
	+ "&spaceWidthCJK=" + propObj.spaceWidthCJK
	+ "&fontPath=/shared/flash/font-" + pageLanguage + "/";

	var itemFlash = new FlashAPI(hItem);
	itemFlash.setAttribute("src", flashPath + 'flashHeader.swf?v=3');
	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 double quotes with curly ones
	var quote = /"([^"]*)"/g;
	cleanText = cleanText.replace(quote, "\u201C$1\u201D"); 
	// 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 = trimString(cleanText);
	return cleanText;
}

startParse();

//window.onload = stopParse;
BrowserUtils.addOnLoadHandler(stopParse); // ND: Added to allow multiple onload handlers
