/**
 * FlashTitle v1.3: Dynamic resizing of on-page display based on content volume
 *
 * FlashTitle is (c) 2007 Graphic Alliance
 *
 */
//document.domain = "fundchoice.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, 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 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){
	var Opera = window.opera ? true : false;
	var browse = navigator.userAgent;
	var mob_browse = ['Opera Mini', 'Minimo', 'IEMobile','Blazer', 'Sprint', 'BlackBerry'];
	var found = false;

	for (var i=0; i<mob_browse.length; i++)
	{
		if(browse.match(mob_browse[i]) != null)
		{
			found = true;
		}
	}
	if(found == true)
	{
		return;
	}
	if(Opera)
	{
		return;
	}
	else if(element.contentDocument)
	{
		element.style.overflow = "hidden"
		// Compliant browsers
		if(element.height!=element.contentDocument.body.parentNode.offsetHeight){
			var bodyheight = element.contentDocument.body.offsetHeight;
			var parentheight = element.contentDocument.body.parentNode.offsetHeight;

			if(bodyheight > parentheight){
				element.style.height = (element.contentDocument.body.offsetHeight) + "px"
			}
			else{
				element.style.height = (element.contentDocument.body.parentNode.offsetHeight) + "px";
			}
		}
	}
	else
	{
		element.style.overflow = "hidden"
		// 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 getcookie(cookieName)
{
	var cookie = " " + document.cookie;
	var search = " " + escape(cookieName) + "=";
	var setStr = null;
	var offset = 0;
	var end = 0;

	if (cookie.length > 0) {
		offset = cookie.indexOf(search);
			if (offset!= -1) {
				offset += search.length;
				end = cookie.indexOf(";", offset)
					if (end == -1) {
						end = cookie.length;
					}
				setStr = unescape(cookie.substring(offset, end));
			}
	}

	return(setStr);
}

function resizeMe(){
	/*
	*/
var element = $$('iframe');
element.each(
function(iframe){
var Opera = window.opera ? true : false;
	var browse = navigator.userAgent;
	var mob_browse = ['Opera Mini', 'Minimo', 'IEMobile','Blazer', 'Sprint', 'BlackBerry'];
	var found = false;

	for (var i=0; i<mob_browse.length; i++)
	{
		if(browse.match(mob_browse[i]) != null)
		{
			found = true;
		}
	}
	if(found == true)
	{
		return;
	}
	if(Opera)
	{
		return;
	}
	else if(iframe.contentDocument)
	{
		iframe.style.overflow = "hidden"
		// Compliant browsers
		if(iframe.height!=iframe.contentDocument.body.parentNode.offsetHeight){
			var bodyheight = iframe.contentDocument.body.offsetHeight;
			var parentheight = iframe.contentDocument.body.parentNode.offsetHeight;

			if(bodyheight > parentheight){
				iframe.style.height = (iframe.contentDocument.body.offsetHeight) + "px"
			}
			else{
				iframe.style.height = (iframe.contentDocument.body.parentNode.offsetHeight) + "px";
			}
		}
	}
	else
	{
		iframe.style.overflow = "hidden"
		// IE
		var name = element.name;
		if(iframe.height!=document.frames[name].document.body.offsetHeight){
			iframe.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(){


	//confirm_flash_load();
}