/**
 * FlashTitle v1.3: Dynamic resizing of on-page display based on content volume
 *
 * FlashTitle is (c) 2007 Graphic Alliance
 *
 */
var flash_title_ids = Array(); // PC USES TO PASS ON LOAD VARS
var flash_id = 0;
//var pc_IE = true;

/**
* fn_new_flash_title
*
* primary function:
* Swaps DIV content for flash title
*
*/
function fn_new_flash_title(id, txt, col, size, lnk, highlight, pcie, bgcol, width, height, wrapper_height)
{
	// Section title
	flash_title_ids.push(id);
	var rnd = Math.random();
	var flashvars = {
	text: txt,
	colour: col,
	size: size,
	link: lnk,
	highlight: highlight,
	pcie: pcie,
	id: id
	};
	var params = {
	bgcolor: bgcol,
	quality: "high",
	wmode: "transparent",
	align: "middle",
	swliveconnect: "true",
	allowscriptaccess: "sameDomain"
	};
	var attributes = {};
	swfobject.embedSWF(path_to_root+"media/interface/swf/title.swf?rnd="+rnd, "flash_title_"+id, width, height, "7.0.0", "", flashvars, params, attributes);
	fn_resize_flash_title(wrapper_height, id);
}

function fn_new_flash(html_id, flash_id, swf){
	var rnd = Math.random();
  var flashvars = {};
  var params = {
  bgcolor: "#FFFFFF",
  quality: "high",
  wmode: "transparent",
  align: "middle",
  swliveconnect: "true",
  allowscriptaccess: "sameDomain"
  };
  var attributes = {};
  swfobject.embedSWF(swf+"?rnd="+rnd, html_id, "100%", "100%", "8.0.0", "", flashvars, params, attributes);
}

function getFlashMovieObject(movieName)
{
  if (window.document[movieName])
  {
      return window.document[movieName];
  }
  if (navigator.appName.indexOf("Microsoft Internet")==-1)
  {
    if (document.embeds && document.embeds[movieName])
      return document.embeds[movieName];
  }
  else // if (navigator.appName.indexOf("Microsoft Internet")!=-1)
  {
    return document.getElementById(movieName);
  }
}

/**
* fn_resize_flash_title
*
* primary function:
* Resizes divs around flash titles allowing them
* to expand as required by their own text box
*
* further function: if var pc_IE
* directly resizes flash object as this does not respond to its parent div size correctly
*
*/
function fn_resize_flash_title(h, id, flash)
{
	//alert(h);
	if($("flash_title_"+id) != null)
	{
		var th = $("flash_title_"+id).style.height;
		if(th != h+'px')
		{
			$("flash_title_"+id).style.height = h+'px';
	
			if(pc_IE == true)
			{
				var flash_movie = getFlashMovieObject("flash_title_"+id);
				//alert("flash_title_"+id+": "+flash_movie);
				flash_movie.height = h+"px";
				flash_movie.SetVariable("_root.pc_resize","1");/**/
			}
		}
	}
		
		
}

function debug(msg)
{
	var err = $('template_2_col_3').innerHTML+"<p>" + msg + "</p>";
	$('template_2_col_3').innerHTML = err;
}

/**
* auto_insert_flash
*
* primary function:
* Automatically replaces specified html entities with flash
* Typically expect this to be H1 tag
* Can be extended to H2, H3, IMG etc.
*
*/

function auto_insert_flash()
{

	// ---------
	// HTML TEXT
	// ---------
	var h1_array = $$('.module_html h1');
	h1_array.each(
		function(h1){
			//
			var h = Element.getHeight(h1);
			//var col = Element.getStyle(h1,"color");
			//var size = Element.getStyle(h1,"font-size");
			var txt = h1.innerHTML;
			var id = flash_title_ids.length;
			Element.replace(h1,"<div class=\"h1\" id=\"flash_title_"+id+"\"><h1>"+txt+"</h1></div>");
			var txt = txt.replace(/&amp;/g, "&");
			var flash_txt = encodeURIComponent(txt);
			fn_new_flash_title(id, flash_txt, '0x333333', '28', '0', '0', pc_IE, "#FFF", '100%', '100%', h);
		});
	
	var h2_array = $$('.module_html h2');
	h2_array.each(
		function(h2){
			//alert("h2");
			var h = Element.getHeight(h2);
			//var col = Element.getStyle(h1,"color");
			//var size = Element.getStyle(h1,"font-size");
			var txt = h2.innerHTML;
			var id = flash_title_ids.length;
			Element.replace(h2,"<div class=\"h2\" id=\"flash_title_"+id+"\"><h2>"+txt+"</h2></div>");
			var txt = txt.replace(/&amp;/g, "&");
			var flash_txt = encodeURIComponent(txt);
			fn_new_flash_title(id, flash_txt, '0x72BF44', '20', '0', '0', pc_IE, "#FFF", '100%', '100%', h);
		});
	/**/
}

function StripLink(txt){
	var reLinkURL=/(<a)(.*)(href\=\")(.*)(\")(.*)(>)(.*)(<\/a>)/
	var lnk = txt.match(reLinkURL);
	if(lnk!=null){
		var lnk = lnk[4];
	}else{
		//catch uppercase
		var reLinkURL=/(<A)(.*)(href\=\")(.*)(\")(.*)(>)(.*)(<\/A>)/
		var lnk = txt.match(reLinkURL);
		if(lnk!=null){
			var lnk = lnk[4];
		}
	}
	return lnk;
}

function StripHtml(txt){
	var reDisplayText=/(<)(.*)(>)(.*)(<\/)(.*)(>)/
	var txt = txt.replace(reDisplayText,"$4");
	return txt;
}

function StripComments(txt){
	var reComment=/(<!--).*(-->)/
	var clean = txt.replace(reComment,"");
	return clean;
}

function confirm_flash_load(){
	flash_title_ids.each(
		function(f){
			var movie = getFlashMovieObject("f_title_"+f);
			movie.SetVariable("_root.pc_load","1");
			debug(movie.id+":"+movie.GetVariable("/:mystatus"));
			if(pc_IE == true){
//				debug(movie);
			}
		});
}

function auto_size_iframe(){
	var data_array = $$('iframe');
	data_array.each(
		function(iframe){
			if(iframe.contentDocument){
				iframe.onLoad = resizeMe(iframe);
			} else {
				var name = iframe.name;
				document.frames[name].onLoad = resizeMe(iframe);
			}
		});
}

function resizeMe(element){
	/*
	*/
	if(element.contentDocument)
	{
		// Compliant browsers
		if(element.height!=element.contentDocument.body.parentNode.offsetHeight){
			//alert(element.contentDocument.body.parentNode.offsetHeight);
			element.style.height = (element.contentDocument.body.parentNode.offsetHeight)+"px";
		}
	} else
	{
		// IE
		var name = element.name;
		if(element.height!=document.frames[name].document.body.offsetHeight){
			element.style.height = (document.frames[name].document.body.offsetHeight)+"px";
		}
	}
}

function hideWhileLoading(id){
	var element = $(id);
	element.parentNode.style.visibility = "hidden";
}

function showAfterLoading(id){
	var element = $(id);
	element.parentNode.style.visibility = "visible";
}

/**
* window.onload
*
* primary function:
* Called on page load to tell flash titles that they can call the js functions
*
* reason: pc ie cannot deal with css backgrounds and js calls before the page has loaded
*
*/
window.onload = function(){
	//auto_size_iframe();
	auto_insert_flash();
	//confirm_flash_load();
}



