/*############################################################################################
##									Generic UI Functions									##
############################################################################################*/

var currentHighlight = null;

function AddCssClass (o,cn)
{
	if(o.className.indexOf(cn)==-1) o.className=o.className.length==0?cn:o.className+" "+cn;
}

function RemoveCssClass (o,cn)
{
	a = o.className.split(" ");
	for(i=0;i<a.length;i++)
	{
		if(a[i].toLowerCase()==cn.toLowerCase())
		{
			a.splice(i,1);
			break;
		}
	}
	o.className = a.join(" ");
}

function Outline(o)
{
	if(currentHighlight!=null) RemoveCssClass(currentHighlight,"Outline");
	AddCssClass(o,"Outline");
	currentHighlight = o;
}

function ReadCookie (name)
{
	var nameEQ = name + "=";
	var ca = document.cookie.split(';');
	for(var i=0;i < ca.length;i++)
	{
		var c = ca[i];
		while (c.charAt(0)==' ') c = c.substring(1,c.length);
		if (c.indexOf(nameEQ) == 0) return c.substring(nameEQ.length,c.length);
	}
	return "";
}

function WriteCookie (name,value,date)
{
	document.cookie = name+"="+value+"; expires="+date+"; path=/";
}

//Change the class of the item
var ActiveObject = null;

function SelectFirstItem(panel,activeObjectID)
{
	ActiveObject = document.getElementById(activeObjectID);
	o = document.getElementById(panel);
	if(o.childNodes[0].childNodes[0]!=null)
	{
		if(o.childNodes[0].childNodes[0].click)o.childNodes[0].childNodes[0].click();
	}
}


function ChangeClass(o,classActive,classInactive)
{
	if(ActiveObject!=null) classChangeObj(ActiveObject,classInactive);
	classChangeObj(o,classActive);
	ActiveObject = o;
}


function ChangeClassOver(o,className)
{
	if(o!=ActiveObject) classChangeObj(o,className);
}


//Function to change a class
function classChangeVal(Val,className)
{
	o = document.getElementById(Val);
	classChangeObj(o,className)
}


//Function to change a class
function classChangeObj(obj,className)
{
	obj.className = className;
}


//Image loaded to workround the IE bug
var imgIndex = 0;
function LoadImages ()
{
	if(imgIndex<Thumbs.length)
	{
		var img = document.getElementById(Elements[imgIndex]).firstChild.firstChild;
		if(img&&!img.complete)
		{
			img.onload = function(){this.onload=null;LoadImages();};
			img.src=Thumbs[imgIndex];
			++imgIndex;
		} else {
			LoadImages(++imgIndex);
		}
	}	
}

function GetFirstChildElement (o,element)
{
	for(var i=0;i<o.childNodes.length;i++)
	{
		if(o.childNodes[i].nodeName.toLowerCase()==element.toLowerCase()) return o.childNodes[i];
	}
	return null;
}

function GotoTop()
{
	if(document.body.scrollTop)
	{
		document.body.scrollTop=0;
	}else{
		document.body.pageYOffset=0;
	}
}


/*############################################################################################
##									InfoItem Functions										##
############################################################################################*/

function InfoItem (){};

//InfoItem.HTTPRequest	= null;
InfoItem.Initialised	= false;
InfoItem.Layer			= null;
InfoItem.Content		= null;
InfoItem.DropShadow		= null;
InfoItem.ContainerID	= null;
InfoItem.ContentID		= null;

InfoItem.Init = function ()
{
	if(InfoItem.Initialised) return;
	InfoItem.Initialised				= true;
	InfoItem.Layer						= document.getElementById(InfoItem.ContainerID);
	InfoItem.Layer.className			= "InfoItem";
	InfoItem.Layer.style.display		= "none";
	InfoItem.Content					= document.getElementById(InfoItem.ContentID);
	InfoItem.Content.className			= "InfoContent";
	InfoItem.Content.style.display		= "none";
}

InfoItem.Show = function(o)
{
	InfoItem.Content.src					= o.url;
	InfoItem.Layer.style.display			= "block";
	InfoItem.Content.style.display			= "block";
	InfoItem.Content.style.visibility		= "visible";
	
	var y = document.body.clientHeight? document.body.scrollTop : pageYOffset;
	document.getElementById("InfoUnit_InfoUnitContainer").style.top = (y+60)+"px";
}

InfoItem.Close = function()
{
	InfoItem.Layer.style.display		= "none";
	InfoItem.Content.style.display		= "none";
	InfoItem.Content.style.visibility	= "hidden";
	InfoItem.Content.src				= "about:blank";
}

InfoItem.Create = function (o,url)
{
	InfoItem.Init();
	o.onmousedown 	= null;
	o.url 			= url;
	o.onmouseup 	= function(e){InfoItem.Show(o);InfoItem.ClearEvent(e)};
	o.onclick		= function(e){InfoItem.ClearEvent(e)};
	o.target 		= "_self";
	o.href			= "javascript:void(0);";
	o.onmouseover	= function(){window.status = o.innerText};
	o.onmouseout	= function(){window.status = ""};
}

InfoItem.ClearEvent = function(e)
{
	if(!e)e=window.event;
	e.cancelBubble = true;
	if(e.stopPropagation) e.stopPropagation();
}

/*############################################################################################
##									Image Rotate Functions									##
############################################################################################*/
function RotateImage ()
{
	/*
	var corpHomepageImage = ReadCookie("corpHomepageImage");
	if(corpHomepageImage!="")
	{
		corpHomepageImage = parseInt(corpHomepageImage);
	} else {
		corpHomepageImage = 0;
	}
	var chpi = corpHomepageImage;
	
	if(++corpHomepageImage>=HPImages.length) corpHomepageImage = 0;
	var d = new Date();
	d.setTime(d.getTime()+(365*24*60*60*1000));
	WriteCookie("corpHomepageImage",corpHomepageImage,d.toGMTString());
	
	//RMUInstance.SetMediaSrc(HPImages[chpi],'','');
	document.getElementById(MainMenuUnitID).src = HPImages[chpi];
	*/
}

/*############################################################################################
##									Filter Elements Functions								##
############################################################################################*/
function FilterElements (dd,doClick)
{
	dd.disabled = false;
	var o;
	for(var i=0;i<Elements.length;i++)
	{
		o = document.getElementById(Elements[i]);
		o.style.display = o.className.indexOf(dd.value)==-1 ? "none" : "block";
		if(doClick&&o.style.display=="block")
		{
			doClick=false;
			var aTag = GetFirstChildElement(o,"a");
			if(aTag!=null)if(aTag.onclick)aTag.onclick();
		}
	}
	if(SetHeight)SetHeight();
}

//Change the specs heading if available
function ChangeHeading(dd,id) {		
	try
	{
		oSpan = document.getElementById(id);
		if(oSpan!=null) {
			oSpan.innerText = dd.options[dd.selectedIndex].text;
		}
	}catch(e){} //do nothing
}

/*############################################################################################
##									Rich Media Unit Functions								##
############################################################################################*/
function RichMediaUnit()
{
	this.m_Index = RichMediaUnit.m_Instances.length;
	RichMediaUnit.m_Instances[this.m_Index] = this;
}
RichMediaUnit.m_Instances				= new Array();
RichMediaUnit.prototype.RMUID			= null;
RichMediaUnit.prototype.RMU				= null;
RichMediaUnit.prototype.m_CurrentSrc	= "";
RichMediaUnit.prototype.m_RetryCount	= 0;
RichMediaUnit.prototype.m_RetryMax		= 50;
RichMediaUnit.prototype.m_RetryItvl		= null;

RichMediaUnit.CallInstance = function (i,method,p0,p1,p2)
{
	RichMediaUnit.m_Instances[i][method](p0,p1,p2);
}

RichMediaUnit.prototype.GetSrcUrl = function(root,src)
{
	if(root.indexOf("?")==-1)
	{
		return root+"?mediaSrc="+src;
	} else {
		return root.indexOf("mediaSrc")==-1 ? root+"&mediaSrc="+src : root.split("mediaSrc=")[0]+"mediaSrc="+src;
	}
}

RichMediaUnit.prototype.SetMediaSrc=function(src,captionLeft,captionRight)
{
	var tmp = src+captionLeft+captionRight;
	if(tmp==this.m_CurrentSrc) return;
	
	if(window[this.RMUID])
	{
		this.RMU = window.document[this.RMUID] ? window.document[this.RMUID] : window[this.RMUID];
	} else if(document[this.RMUID]) {
		this.RMU = document[this.RMUID];
	} else {
		this.RMU = document.getElementById(this.RMUID);
	}
	
	var s = src.toLowerCase();
	if(!RichMediaUnit.FlashEnabled&&(s.indexOf(".flv")!=-1||s.indexOf(".swf"))!=-1)
	{
		self.location.href = this.GetSrcUrl(self.location.href,src);
		return;
	}
	if(RichMediaUnit.FlashEnabled)
	{
		try
		{
			if(++this.m_RetryCount<this.m_RetryMax) this.m_RetryItvl = setTimeout("RichMediaUnit.CallInstance("+this.m_Index+",'SetMediaSrc','"+src+"','"+captionLeft+"','"+captionRight+"')",100);
			this.RMU.SetVariable("captionLeft",captionLeft);
			this.RMU.SetVariable("captionRight",captionRight);
			this.RMU.SetVariable("mediaSrc",src);
			clearTimeout(this.m_RetryItvl);
		}
		catch (e)
		{
			if(RichMediaUnit.FlashEnabled)
			{
				var s = this.GetSrcUrl(RichMediaUnit.StubFileSource,src)+"&captionLeft="+captionLeft+"&captionRight="+captionRight;
				if(this.RMU.movie)
				{
					this.RMU.movie = s;
				} else {
					// trap browsers that can't even update the src attribute correctly :- safari
					if(navigator.userAgent.toLowerCase().indexOf("safari"))
					{
						var fvars = "mediaSrc="+src+"&amp;captionLeft="+captionLeft+"&amp;captionRight="+captionRight;
						var t = this.RMU.outerHTML;
						var fvi = t.indexOf("flashvars=");
						t = t.substring(0,fvi)+"flashvars=\"" +fvars+"\""+ t.substring(t.indexOf("\"",fvi+11)+1,t.length);
						this.RMU.outerHTML = t;
					} else {
						this.RMU.src = s;
					}
				}
			} else {
				this.RMU.src = src;
			}
		}
	} else {
		this.RMU.src = src;
		if(captionLeft!="")
		{
			var capDiv = document.getElementById(this.RMUID+"_Caption");
			if(capDiv!=null) capDiv.innerHTML = captionLeft;
		}
	}
	
	if(document.body.scrollTop)
	{
		document.body.scrollTop = 0;
	} else {
		document.body.pageYOffset = 0;
	}
	
	this.m_CurrentSrc = tmp;
}


/*############################################################################################
##									Glossary Item Functions									##
############################################################################################*/
function GlossaryItem (){};

GlossaryItem.HTTPRequest	= null;
GlossaryItem.Initialised	= false;
GlossaryItem.Layer			= null;
GlossaryItem.Content		= null;
GlossaryItem.MouseLoc		= {x:0,y:0};
GlossaryItem.DropShadow		= null;

GlossaryItem.Init = function ()
{
	if(GlossaryItem.Initialised) return;
	
	GlossaryItem.Initialised				= true;
	GlossaryItem.HTTPRequest				= new HTTPRequest();
	GlossaryItem.Layer						= CreateLayer(document.body,"GlossaryItem");
	GlossaryItem.Layer.className			= "GlossaryLayer";
	GlossaryItem.Layer.style.display		= "none";
	GlossaryItem.Layer.style.position		= "absolute";
	GlossaryItem.Layer.style.zIndex			= "100";
	GlossaryItem.DropShadow					= document.getElementById("GlossaryItem_shadow");
	GlossaryItem.Content					= document.getElementById("GlossaryItem_content");
	GlossaryItem.Content.className			= "GlossaryContent";
	
	document.onmouseup = function ()
	{
		GlossaryItem.Layer.style.display		= "none";
	}

	if (document.captureEvents)document.captureEvents(Event.MOUSEMOVE)
	document.onmousemove = GlossaryItem.OnMouseMove;
}

GlossaryItem.Show = function(o)
{
	this.HTTPRequest.Send(o.url,null,GlossaryItem,"OnLoad");
}

GlossaryItem.OnMouseMove = function (e)
{
	if(e)
	{
		GlossaryItem.MouseLoc.x = e.PageX;
		GlossaryItem.MouseLoc.y = e.PageY;
	} else {
		GlossaryItem.MouseLoc.x = event.clientX+document.body.scrollLeft;;
		GlossaryItem.MouseLoc.y = event.clientY+document.body.scrollTop;
	}
}

GlossaryItem.OnLoad = function (r)
{
	GlossaryItem.Content.innerHTML			= r;
	GlossaryItem.Layer.style.display		= "block";
	var tmpx = Math.max(10,GlossaryItem.MouseLoc.x-30);
	var tmpy = Math.max(10,GlossaryItem.MouseLoc.y-30);
	GlossaryItem.Layer.style.left			= tmpx+"px";
	GlossaryItem.Layer.style.top			= tmpy+"px";
}

GlossaryItem.Create = function (o,url)
{
	GlossaryItem.Init();
	o.onmousedown 	= null;
	o.url 			= url;
	o.onmouseup 	= function(e){GlossaryItem.OnMouseMove(e);GlossaryItem.Show(o);GlossaryItem.ClearEvent(e)};
	o.onclick		= function(e){GlossaryItem.ClearEvent(e)};
	o.target 		= "_self";
	o.href			= "#";
}

GlossaryItem.ClearEvent = function(e)
{
	if(!e)e=window.event;
	e.cancelBubble = true;
	if(e.stopPropagation) e.stopPropagation();
}


/*############################################################################################
##									Write Flash Functions									##
############################################################################################*/
var swfQueryString = "";
function WriteFlash (tagid)
{
	
	var nsTag = document.getElementById(tagid);
	var fstring = nsTag.innerHTML;
	var id=nsTag.firstChild.id;
	var flashvars="";
	
	for(var i=0;i<nsTag.firstChild.childNodes.length;i++)
	{
		if(nsTag.firstChild.childNodes[i].name=="flashvars")
		{
			flashvars = nsTag.firstChild.childNodes[i].value;
			break;
		}
	}
	if(swfQueryString!="")
	{
		if(fstring.indexOf(".swf?")==-1)
		{
			fstring = fstring.replace(/.swf/,".swf?"+swfQueryString);
		} else {
			fstring = fstring.replace(/.swf\?/,".swf&");
			fstring = fstring.replace(/.swf/,".swf?"+swfQueryString);
		}
	}
	nsTag.innerHTML = "";
	nsTag.style.display = "none";
	fstring = fstring.replace("FlashVars\" VALUE=\"\"","FlashVars\" VALUE=\""+flashvars+"\"");
	document.write(fstring.replace("<OBJECT","<OBJECT id=\""+id+"\""));
}


/*############################################################################################
##							     	Drop Shadow / Chrome functions							##
############################################################################################*/
function WriteDropShadowPanel (id,width,height,panelColour,shadowImgUrl,zIndexBase)
{
	document.write("<div id=\""+id+"\" >");
	var ua = navigator.userAgent.toLowerCase();
	if(ua.indexOf("msie")>0)
	{
		document.write("<div id=\""+id+"_Shadow\" style=\"left:13px;top:13px;position:absolute;width:"+width+"px;height:"+height+
		"px;background-color:#666666;filter:progid:DXImageTransform.Microsoft.Blur(pixelradius=10);z-index:"+(zIndexBase+1)+"\"></div>");
	} else {
		document.write("<div id=\""+id+"_Shadow\" style=\"position:absolute;left:8px;top:8px;\"><img id=\""+id+"_DSImage\" src=\""+shadowImgUrl+"\"/></div>");
	}
}

