/**
 * 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 so = new SWFObject(path_to_root+"media/interface/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("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;
}

/**
* 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 = $$('.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 flash_txt = encodeURIComponent(txt);
			fn_new_flash_title(id, flash_txt, '0x003B4D', '21', '0', '0', pc_IE, "#FFF", '100%', '100%', h);
		});
	var h2_array = $$('.module_html h2');
	h2_array.each(
		function(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 flash_txt = encodeURIComponent(txt);
			fn_new_flash_title(id, flash_txt, '0x7E7367', '21', '0', '0', pc_IE, "#FFF", '100%', '100%', h);
		});
	// ----------------
	// HOME PAGE PANELS
	// ----------------
	var h2_array = $$('.module-panel h2');
	h2_array.each(
		function(h2){
			var h = Element.getHeight(h2);
			//var col = Element.getStyle(h1,"color");
			//var size = Element.getStyle(h1,"font-size");
			var txt = h2.innerHTML;
			var flash_txt = encodeURIComponent(txt);
			var id = flash_title_ids.length;
			//Element.replace(h2,"<div class=\"h2\" id=\"flash_title_"+id+"\"><h2>"+txt+"</h2></div>");
			Element.replace(h2,"<div class=\"h2\" id=\"flash_title_"+id+"\"><h2>"+txt+"</h2></div>");
			fn_new_flash_title(id, flash_txt, '0x7E7367', '16', '0', '0', pc_IE, "#FFF", '100%', '100%', '20');
		});
	// --------------
	// CAPTION MODULE
	// --------------
	var caption_p_array = $$('.quote_content p');
	//debug("cpa:"+caption_p_array[1].innerHTML);
	caption_p_array.each(
		function(p){
			//debug(p.innerHTML);
			var h = Element.getHeight(p)*1.1;
			var txt = StripComments(p.innerHTML);
			var lnk = StripLink(txt);
			if(lnk==null)
			{
				lnk = 0;
				var col = '0xFFFFFF';
			} else {
				var txt = StripHtml(txt);
				var col = '0xFF9E2E';
			}
			var id = flash_title_ids.length;
			if(lnk==0){
				Element.replace(p,"<div class=\"p\" id=\"flash_title_"+id+"\"><p>"+txt+"</p></div>");
			} else {
				Element.replace(p,"<div class=\"p\" id=\"flash_title_"+id+"\"><p><a href=\""+lnk+"\">"+txt+"</a></p></div>");
			}
			var flashtxt = encodeURIComponent(txt);
			fn_new_flash_title(id, flashtxt, col, '16', lnk, '0', pc_IE, "#7E7367", '96%', '100%', h);
		});
	// --------------
	// QUOTE MODULE
	// --------------
	var quote_p_array = $$('.caption_content p');
	//debug("cpa:"+caption_p_array[1].innerHTML);
	quote_p_array.each(
		function(p){
			//debug(p.innerHTML);
			var h = Element.getHeight(p)*1.1;
			var txt = StripComments(p.innerHTML);
			lnk = 0;
			var col = '0xA39C97';
			var flash_txt = encodeURIComponent(txt);
			var id = flash_title_ids.length;
			Element.replace(p,"<div class=\"p\" id=\"flash_title_"+id+"\"><p>"+txt+"</p></div>");
			fn_new_flash_title(id, flash_txt, col, '16', lnk, '0', pc_IE, "#A39C97", '90%', '100%', h);
		});
	// ------------
	// LINKS MODULE
	// ------------
	var h2_array = $$('.module_dyn_links h2');
	h2_array.each(
		function(h2){
			var h = Element.getHeight(h2)*1.5;
			//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 flashtxt = encodeURIComponent(txt);
			fn_new_flash_title(id, flashtxt, '0xFF9E2E', '21', '0', '0', pc_IE, "#FFF", '100%', '100%', h);
		});
	var li_array = $$('.module_dyn_links li');
	li_array.each(
		function(li){
			var txt = li.innerHTML;
			Element.replace(li,"<li><img class=\"links-left\" src=\""+path_to_root+"media/interface/png/links-left.png\"/>"+txt+"<img class=\"links-right\" src=\""+path_to_root+"media/interface/png/links-right.png\"/></li>");
		});
	// --------------
	// LOZENGE MODULE
	// --------------
	//
	/*
	// icon - info
	if($$('.module_dyn_lozenge #info') != ''){
		var html_id = "info";
		var flash_id = "f_info";
		var swf = path_to_root+"media/interface/swf/icon_i-02.swf";
		fn_new_flash(html_id, flash_id, swf);
		showAfterLoading(html_id);
	}
	//
	// icon - pie chart
	if($$('.module_dyn_lozenge #piechart') != ''){
		var html_id = "piechart";
		var flash_id = "f_piechart";
		var swf = path_to_root+"media/interface/swf/icon_pie_chart.swf";
		fn_new_flash(html_id, flash_id, swf);
		showAfterLoading(html_id);
	}
	//
	// icon - cart
	if($$('.module_dyn_lozenge #cart') != ''){
		var html_id = "cart";
		var flash_id = "f_cart";
		var swf = path_to_root+"media/interface/swf/icon_cart.swf";
		fn_new_flash(html_id, flash_id, swf);
		showAfterLoading(html_id);
	}
	//
	// icon - barchart
	if($$('.module_dyn_lozenge #barchart') != ''){
		var html_id = "barchart";
		var flash_id = "f_barchart";
		var swf = path_to_root+"media/interface/swf/icon_bar_chart.swf";
		fn_new_flash(html_id, flash_id, swf);
		showAfterLoading(html_id);
	}
	//
	// icon - linechart
	if($$('.module_dyn_lozenge #linechart') != ''){
		var html_id = "linechart";
		var flash_id = "f_linechart";
		var swf = path_to_root+"media/interface/swf/icon_linechart.swf";
		fn_new_flash(html_id, flash_id, swf);
		showAfterLoading(html_id);
	}

	var h2_array = $$('.lozenge_content h2');
	h2_array.each(
		function(h2){
			var h = Element.getHeight(h2)*0.75;
			//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 flashtxt = encodeURIComponent(txt);
			fn_new_flash_title(id, flashtxt, '0xFF9E2E', '17', '0', '0', pc_IE, "#7E7367", '96%', '100%', '20');
		});

	var caption_p_array = $$('.lozenge_content p');
	//debug("cpa:"+caption_p_array[1].innerHTML);
	caption_p_array.each(
		function(p){
			//debug(p.innerHTML);
			var h = Element.getHeight(p)*0.75;
			var txt = StripComments(p.innerHTML);
			var lnk = StripLink(txt);
			if(lnk==null)
			{
				lnk = 0;
				var col = '0xFFFFFF';
			} else {
				var txt = StripHtml(txt);
				var col = '0xFF9E2E';
			}
			var id = flash_title_ids.length;
			if(lnk==0){
				Element.replace(p,"<div class=\"p\" id=\"flash_title_"+id+"\"><p>"+txt+"</p></div>");
			} else {
				Element.replace(p,"<div class=\"p\" id=\"flash_title_"+id+"\"><p><a href=\""+lnk+"\">"+txt+"</a></p></div>");
			}
			var flashtxt = encodeURIComponent(txt);
			fn_new_flash_title(id, flashtxt, col, '11', lnk, '0', pc_IE, "#7E7367", '96%', '100%', '10');
		});
	*/
	//
	// extract LOZENGE modules
	var lozenge_array = $$('.module_dyn_lozenge');
	// loop through lozenges
	lozenge_array.each(
		function(lozenge){
			var id = lozenge.id;
			var elements = lozenge.select("div.lozenge_graphic","h2","p","a");
			var lozenge_object = {graphic:'', title:'', text:[], mylink:''};
			elements.each(
				function(me){
					if(me.className == "lozenge_graphic"){
						lozenge_object.graphic = me.id;
					} else if(me.inspect() == "<h2>"){
						lozenge_object.title = me.firstChild.nodeValue;
					} else if(me.inspect() == "<p>"){
						if(me.firstChild.nodeValue != null){
							var node = "";
							var num_nodes = me.childNodes.length;
							for(var i=0; i<num_nodes; i++){
								if(me.childNodes[i].nodeValue != null){
									if(node == ""){
										node = me.childNodes[i].nodeValue;
									} else {
										node += "<br/>"+me.childNodes[i].nodeValue;
									}
								}
							}
							lozenge_object.text.push(node);
						}
					} else if(me.inspect() == "<a>"){
						lozenge_object.mylink = me.readAttribute("href");
						lozenge_object.text.push(me.firstChild.nodeValue);
					} else {
						alert(me.inspect());
					}
					showAfterLoading(me);
				}
			);
			//alert("graphic:"+lozenge_object.graphic+" title:"+lozenge_object.title+" text:"+lozenge_object.text[0]+" link:"+lozenge_object.mylink);
			fn_new_flash_lozenge(id, lozenge_object);
		}
	);
}

function fn_new_flash_lozenge(html_id, data){
	var rnd = Math.random();
	var id = "f_"+html_id;
	var so = new SWFObject(path_to_root+"media/interface/swf/lozenge.swf?rnd="+rnd, 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);
	so.addVariable("graphic",data.graphic);
	so.addVariable("title",data.title);
	so.addVariable("link",data.mylink);
	var n=0;
	data.text.each(
		function(text){
			so.addVariable("text_"+n,data.text[n]);
			n++;
		}
	);
	so.addVariable("num_text",n);
	var flash = so.write(html_id);
}

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();
} 



