/**
 * FlashTitle v1.3: Dynamic resizing of on-page display based on content volume
 *
 * FlashTitle is (c) 2007 Graphic Alliance
 *
 */
//document.domain = "gatests.co.uk";
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, align, size, lnk, highlight, pcie, bgcol, width, height, wrapper_height)
{
	// Section title
	flash_title_ids.push(id);
	var rnd = Math.random();
	var so = new SWFObject(path_to_root+"media/swf/title.swf?rnd="+rnd, "f_title_"+id, width, height, "7", bgcol, "high");

	so.addParam("wmode", "transparent");
	so.addParam("align", "middle");
	so.addParam("swfLiveConnect", "true");
	so.addParam("allowScriptAccess","sameDomain");
	so.setAttribute("style", ";");
	so.addVariable("text",txt);
	so.addVariable("colour",col);
	so.addVariable("size",size);
	so.addVariable("alignment", align);
	so.addVariable("link",lnk);
	so.addVariable("highlight",highlight);
	so.addVariable("pcie",pcie);
	so.addVariable("id",id);
	var flash = so.write("flash_title_"+id);
	fn_resize_flash_title(wrapper_height, id, flash);
}

function fn_new_flash(html_id, flash_id, swf){
	var rnd = Math.random();
	var so = new SWFObject(swf+"?rnd="+rnd, flash_id, "100%", "100%", "8", "#FFF", "high");
	so.addParam("wmode", "transparent");
	so.addParam("align", "middle");
	so.addParam("swfLiveConnect", "true");
	so.addParam("allowScriptAccess","sameDomain");
	so.setAttribute("style", ";");
	so.addVariable("webroot",path_to_root);
	var flash = so.write(html_id);
}

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)
{
	var th = $("flash_title_"+id).style.height;
	if(th != h+'px')
	{
		$("flash_title_"+id).style.height = h+'px';
	
		if(pc_IE == true && flash == true)
		{
			var flash_movie = getFlashMovieObject("f_title_"+id)
			flash_movie.height = h;
			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;
}

/**
*
* Get a HEX colour value from a RGB string
*
*/
function RGBtoHex(col) {
	// rgb(96, 174, 57)
	var rgb = col.match(/\d{1,3}/g);
	var r = parseInt(rgb[0]);
	var g = parseInt(rgb[1]);
	var b = parseInt(rgb[2]);

	var hex = (r).toColorPart() + (g).toColorPart() + (b).toColorPart();
	
	return hex;
}
/**
* 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()
{
	// ------
	// HEADER
	// ------
	//var html_id = "webpage-headerImg";
	//var flash_id = "f_header";
	//var swf = path_to_root+"media/interface/swf/homepageBanner.swf"
	//fn_new_flash(html_id, flash_id, swf)
	//showAfterLoading(html_id);
	// ---------
	// HTML TEXT
	// ---------
	var h1_array = $$('.flashTitle h1');
	h1_array.each(
		function(h1){
			var h = Element.getHeight(h1);
			var col = h1.getStyle("color");
			if(col.match(/rgb/g)){
				var col = RGBtoHex(col);
			}else{
				var col = col.replace("#", "");
			}

			var align = h1.getStyle("text-align");
			//var size = Element.getStyle(h1,"font-size");
			
			//
			//Remove image background
			h1.up('.flashTitle').setStyle({backgroundImage:'none'});					
			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 flash_txt = encodeURIComponent(txt);
			fn_new_flash_title(id, flash_txt, "0x"+col, align, '28', '0', '0', pc_IE, "#FFF", '100%', '100%', h);

		});
		
	var h1_array = $$('.homepageFlashTitle h1');
	h1_array.each(
		function(h1){
			var h = Element.getHeight(h1);
			var col = h1.getStyle("color");
			if(col.match(/rgb/g)){
				var col = RGBtoHex(col);
			}else{
				var col = col.replace("#", "");
			}
			var align = h1.getStyle("text-align");
			//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 flash_txt = encodeURIComponent(txt);
			fn_new_flash_title(id, flash_txt, "0x"+col, align, '40', '0', '0', pc_IE, "#FFF", '100%', '100%', h);
		});
		
		
	var h2_array = $$('.bannerTextHolder h2 a');
	h2_array.each(
		function(h2){
			var h = Element.getHeight(h2);
			var col = h2.getStyle("color");
			if(col.match(/rgb/g)){
				var col = RGBtoHex(col);
			}else{
				var col = col.replace("#", "");
			}
			var align = h2.getStyle("text-align");
			var bgcol = h2.getStyle("background-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 flash_txt = encodeURIComponent(txt);
			fn_new_flash_title(id, flash_txt, "0x"+col, align, '22', '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){
			/*
			*/
			// RESIZE ON INITIAL LOAD
			if(iframe.contentDocument){
				iframe.onLoad = resizeMeInit(iframe);
			} else {
				var name = iframe.name;
				document.frames[name].onLoad = resizeMeInit(iframe);
			}
			// LISTEN FOR FUTURE RELOADS AND RESIZE
			if(iframe.addEventListener){
				iframe.addEventListener("load",resizeMe,false);
			}else if(iframe.attachEvent){
				iframe.detachEvent("onload", resizeIframes);
				iframe.attachEvent("onload", resizeIframes);
			}
		});
}

function resizeMeInit(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 resizeMe(){
	/*
	*/
	element = this;
	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 resizeIframes(){
	var data_array = $$('iframe');
	data_array.each(
		function(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();
} 




