/**
* @description
* This function writes Flash objects into the innerHTML of a DIV
* the function also collates a FlashVars param from the innerHTML it finds in the child DIV (id=menuList)
*
* The innerHTML is held in a separate file menu.html which is included in this document using an iframe tag
* This means the user only need edit the one file to change the menu system
*
* Flash will split well formed flashVars data string myInput, into usable array of objects
* Contribute is used to add new links as simple HTML of the form  
*      <ol>
*        <li><a href="index.html">link0</a>
*          <ol>
*            <li><a href="index.html">link1_0 </a> </li>
*            <li><a href="index.html">link1_1 </a> </li>
*          </ol>
*        </li>
*        <li><a href="index.html">link1 </a> </li>
*      </ol>
* Javascript collates the HTML and passes the resulting string to the Flash object as a FlashVars
* so it can be handled as Flsh XML object
*
* NOTES
* =====
* 		N.B. Javascript is used to removes all \r, \n, \t, any double space or more and 
* 		N.B. Javascript is used to set the DIV and Flash OBJECT tag heightsUsing 
*				a second array (NOT passed to Flash) Javascript is used to replaces "<LI>" with "<li>" 
* 				and then replaces "<UL>" with "<ul>"
* 				these are replaced with "|" so we can count the menu items using array.split
* 				allowing us to set the DIV and Flash OBJECT tag heights
* 		N.B. Javascript is used to send the page name of current document into Flash as a querystring on the SRC attribute
*
* @returns void
*/

function createObjectTag(swfName,flVars,urlStr,myWidth,myHeight,targetDiv,hasRightVersion){
	if(hasRightVersion){
		//alert("called createObjectTag() : "+swfName+" , "+ flVars+" , "+urlStr+" , "+targetDiv);
		
		// makes sure query string always has value
		urlStr = "1=1"+urlStr;
		
		if( targetDiv == "fl_menu" ){/**/
			// get innerHTML from and store
			
			var dataSource = frames["menuiframe"].document.body;
			var input = String(dataSource.innerHTML); 
			//alert(input);
			
			// blank innerHTML in page
			//menuiframe.document.getElementById("menuList").innerHTML = "";
			dataSource.innerHTML = "";
			
			// lowercase as Safari doesn't like uppercase tags
			input = input.replace(/\<LI/g, "<li");
			input = input.replace(/LI\>/g, "li>");
			input = input.replace(/\<OL/g, "<ol");
			input = input.replace(/\/OL\>/g, "/ol>");
			input = input.replace(/\<A/g, "<a");
			input = input.replace(/\/A\>/g, "/a>");
			// strip out whitespace
			input = input.replace(/[\n\r\t]|   */g, ""); // remove newlines (MAC and PC), tab and double spaces
			
			// IE BUG - ie drop all end LI tags APART from last one in a list (or sub list)
			// Without white space outside tags this is a predictable fail and can be
			// corrected with regular expression replaces as follows
			if(	isIE()	){
				// remove any single spaces outside tags
				input = input.replace(/ \</g, "<");
				
				// keep beginning of sub list safe
				input = input.replace(/\<\/a\>\<ol\>/g, "BBBB");
				// keep last in list or sub list safe
				input = input.replace(/\<\/a\>\<\/li\>\<\/ol\>/g, "CCCC");
				// add missing end li tags
				input = input.replace(/\<\/a\>/g, "</a></li>");		
				// put back last in list or sub list
				input = input.replace(/CCCC/g, "</a></li></ol>");
				// put back beginning of sub list safe
				input = input.replace(/BBBB/g, "</a><ol>");
							
				// add missing end of sub list
				input = input.replace(/\<\/ol\>\<li\>/g, "</ol></li><li>");
				
				// next line just so I could cut and paste result into XML doc 
				// to test with IE XML parsing for errors
				//document.getElementById("right_content").innerHTML = input;
			}
			
			// replace apostrophies (put back in flash)
			input = input.replace(/'/g, "XApostrophy");		
			
			// create flashVars string
			var flVars = "myInput=";
			flVars += input;
			
			// get number of menus (== no of "/>")	
			// N.B. input has been stored in flVars
			input = input.replace(/\<li\>/g, "|");
			input = input.replace(/\<ol\>/g, "|");
			var inputArray = input.split("|"); // to get length
			var len = inputArray.length;
		
			// set height of containing DIV (N.B. "myHeight" also used for Flash OBJECT tag height)
			// flash will resize it later using getURL ;
			//		getURL("Javascript:void(setTimeout(\"resizeElement('menuContainer',"+ HTMLobjectHeight+")\",100))");
			// the setTimeOut is for FireFox on the Mac
			//		getURL("Javascript:void(document.getElementById('FlashIdfl_menu').style.height = "+ HTMLobjectHeight+")");
			
			// NOTE WELL the size must be BIGGER than the final size Flash assigns (for FireFox on Mac)
			myHeight = 400;
			// this is because FireFox on the PC needs embed tag resize but IE throws an error if we do
			if(isIE()){
				urlStr = urlStr+"&isIE=13";
			}
			
			//get current page filename
			arr = document.location.href.split('/');
			
			// store page name 
			var pageName = arr[arr.length-1];
		}
		
		// Flash OBJECT tag in a string
		var htmlString = "<object id='FlashId"+targetDiv+"' classid='clsid:d27cdb6e-ae6d-11cf-96b8-444553540000' codebase='http://fpdownload.macromedia.com/pub/shockwave/cabs/flash/swflash.cab#version=8,0,0,0' width='"+myWidth+"' height='"+myHeight+"' id='activateTest' align='middle'>";
			htmlString += "<param name='allowScriptAccess' value='sameDomain' />";
			htmlString += "<param name='movie' value='"+swfName+"?"+urlStr+"&pageName="+pageName+"&totalLen="+len+"' />";
			htmlString += "<param name='quality' value='high' />";
			htmlString += "<param name='bgcolor' value='#FFFFFF' />";
			htmlString += "<param name='flashvars' value='"+flVars+"' />";
			htmlString += "<embed id='FlashIdEM"+targetDiv+"'  name='FlashIdEM"+targetDiv+"' src='"+swfName+"?"+urlStr+"&pageName="+pageName+"&totalLen="+len+"' flashvars='"+flVars+"' quality='high' bgcolor='#FFFFFF' width='"+myWidth+"' height='"+myHeight+"' align='middle' allowScriptAccess='sameDomain' type='application/x-shockwave-flash' pluginspage='http://www.macromedia.com/go/getflashplayer' />";
			htmlString += "</object>";
	
		// WRITE Flash OBJECT tag in a string into page
		//var htmlString = "<p><FONT SIZE='1' FACE='Verdana'><STRONG><FONT COLOR='#FFFFFF'>Test innerHTML</FONT></STRONG></FONT></p>";
		document.getElementById(targetDiv).innerHTML = htmlString;
	}
}

/**
* @description detects IE 
* @returns true or false
*/
function isIE(){
	// convert all characters to lowercase to simplify testing
    var agt=navigator.userAgent.toLowerCase();
	
	var is_ie     = ((agt.indexOf("msie") != -1) && (agt.indexOf("opera") == -1));
	
	return is_ie;
}

/**
*
*/
function resizeElement(name,value){
	document.getElementById(name).style.height = value;
	//alert(name+' = '+document.getElementById(name).style.height);
}
	
