// common.js
function ShowDiv(DivName)
{
	if	(document.layers) {
		document.layers[DivName].display="block"; 
		document.layers[DivName].visibility="visible"; 
	} else if (document.getElementById) {
		document.getElementById(DivName).style.display="block";
		document.getElementById(DivName).style.visibility="visible";
	} else if (document.all) {
		document.all(DivName).style.display="block";
		document.all(DivName).style.visibility="visible";	
	}
	
}

function HideDiv(DivName) 
{
	if	(document.layers) {
		document.layers[DivName].display="none"; 
		document.layers[DivName].visibility="hidden"; 
	} else if (document.getElementById) {
		document.getElementById(DivName).style.display="none";
		document.getElementById(DivName).style.visibility="hidden";
	} else if (document.all) {
		document.all(DivName).style.display="none";
		document.all(DivName).style.visibility="hidden";	
	}
}

function showTip(obj,content) {
	var popin = new getObj("popin")
	var popincontent = new getObj("popin_innertxt")
	var intmoveLeft = 0;
	var intmoveTop = 0;
	
	if (browserName.indexOf('safari') !=-1) {
		intmoveLeft = -12;
		intmoveTop = 30;
	} else {
		intmoveLeft = -12;
		intmoveTop = 10;
	}
	
	if (obj && popin.obj)	{
		popin.style.top = findPosTop(obj) + intmoveTop + 'px';
		popin.style.left = findPosLeft(obj) + intmoveLeft + 'px';
		popincontent.obj.innerHTML = content;
	}
	ShowDiv("popin")
}

// tabswticher(X, Y)
// tabsetX_buttonY
// tabsetX_divY
function tabswitcher(strTabSet, intTabID) {
	strButtons = 'tabset_' + strTabSet + '_button';
	strDivs = 'tabset_' + strTabSet + '_div';
	for (i = 1; i <= 10; i++) {
		try {
			var objTab =   document.getElementById(strButtons + i);
			var objLayer = document.getElementById(strDivs + i);
			
			if (i == intTabID) {
				objTab.className = 'on';
				objLayer.style.display = 'block';
			} else {
				objTab.className = '';
				objLayer.style.display = 'none';
			}
		} catch(e) { i = 100; }
	}
}

// tabswticher(X, Y)
// tabsetX_buttonY
// tabsetX_divY
function areaSwitcherAnim(strAreaSet, intAreaID) {
	strTitle = strAreaSet + '_title';
	strOuter = strAreaSet + '_area';
	for (i = 1; i <= 10; i++) {

			var objTitle = document.getElementById(strTitle + i);
			var objOuter = strOuter + i;
			
			if (i == intAreaID) {
				objTitle.className = 'on';
				layeranimation(objOuter,'show');
			} else {
				if (objTitle != null)
				{
					objTitle.className = '';
					layeranimation(objOuter,'hide');
				}
			}
				try {
		} catch(e) { i = 100; }
	}
}

//Function to turn a layer on and off, and it's associated button, name the same as the layer, with "Img" on the end. The force hide will hide it - cunningly
function swapLayerState(strLayer,strImg,intForceHide) { //Pass in the layer, and image src, but minus the "on" and ".gif"
	var objLayer = document.getElementById(strLayer);
	if (strImg) var objImage = document.getElementById(strLayer + "Img");
	if ((objLayer.style.display == "" || objLayer.style.display == "none") && !intForceHide) {
		objLayer.style.display = "block";
		if (strImg) objImage.src = strImg + 'On.gif';
	} else {
		objLayer.style.display = "none";
		if (strImg) objImage.src = strImg + '.gif';
	}
}

// function to turn a group of buttons on or off, similar to the tab script, but without the layer swap
function setClassOn(strButtons,intButtonID) {
	for (i = 1; i <= 10; i++) {
		try {
			var objTab = document.getElementById(strButtons + i);
			
			if (i == intButtonID) {
				objTab.className = 'on';
			} else {
				objTab.className = '';
			}
		} catch(e) { i = 100; }
	}
}

function getObj(name) {
	if (document.getElementById) {
		this.obj = document.getElementById(name);
		if (this.obj) this.style = document.getElementById(name).style;
	} else if (document.all) {
		this.obj = document.all[name];
		if (this.obj) this.style = document.all[name].style;
	} else if (document.layers) {
		if (document.layers[name]) {
			this.obj = document.layers[name];
			if (this.obj) this.style = document.layers[name];
	   } else {
			this.obj = eval("document.layers." + name + ".layers[name]");
			if (this.obj) this.style = eval("document.layers." + name + ".layers[name]");
	   }
	}
}

// get position scripts
function findPosLeft(obj) {
	var curleft = 0;
	if (obj.offsetParent) {
		while (obj.offsetParent) {
			curleft += obj.offsetLeft;
			obj = obj.offsetParent;
		}
	} else if (obj.x) {
		curleft += obj.x;
	}
	return curleft;
}

function findPosTop(obj) {
	var curtop = 0;
	var printstring = '';
	if (obj.offsetParent) {
		while (obj.offsetParent) {
			curtop += obj.offsetTop
			obj = obj.offsetParent;
		}
	} else if (obj.y) {
		curtop += obj.y;
	}
	return curtop;
}
//end get position scripts


// AJAX FUNCTIONS
var Message;
var strMessage;

function loadXMLTarget(strURL, strTarget, strMessage, strTest) {
	if (strTest) { alert(strURL); }
	
	// This stops ajax caching
	if (strURL.indexOf("?") > -1) {
		strURL = strURL + "&" + Date();
	} else {
		strURL = strURL + "?cache=" + Date();
	}
	
	if (strTarget == "") {
		alert('Please specify a target');
	}
	// branch for native XMLHttpRequest object
	if (window.XMLHttpRequest) {
		Message = new XMLHttpRequest();
		Message.onreadystatechange = function() {processTargetHTML(strTarget);};
		Message.open("GET", strURL, true);
		Message.send(null);
	// branch for IE/Windows ActiveX version
	} else if (window.ActiveXObject) {
		Message = new ActiveXObject("Microsoft.XMLHTTP");
		if (Message) {
			Message.onreadystatechange =function() {processTargetHTML(strTarget);};
			Message.open("GET", strURL, true);
			Message.send();
		}
	}
}


function processTargetHTML(strTarget) {
	//alert(strTarget);
	var obj = new getObj(strTarget);
    switch (Message.readyState) {
		case 1:
			//obj.obj.innerHTML = "<img src='/images/loading-anim.gif'>";
			break;
		case 4:
			// only if "OK"
			if (Message.status == 200) {
				obj.obj.innerHTML = Message.responseText;
			} else {
				obj.obj.innerHTML = Message.responseText;
				//alert("There was a problem retrieving the XML data:\n" + Message.statusText);
			}
		break;
    }
}
// END AJAX FUNCTIONS


function swapImage(imgSrc,imgTarget) {
	//imgObj=getObj(imgTarget);
	imgObj = document.getElementById(imgTarget);
	imgObj.src=imgSrc;
}
	




// USED TO INITIALISE DROP DOWN MENUS
function startList() {
if (document.all&&document.getElementById) {
navRoot = document.getElementById("nav");
for (i=0; i<navRoot.childNodes.length; i++) {
node = navRoot.childNodes[i];
if (node.nodeName=="LI") {
node.onmouseover=function() {
this.className+=" over";
  }
  node.onmouseout=function() {
  this.className=this.className.replace(" over", "");
   }
   }
  }
 }
}
window.onload=startList;



/*Custom Function*/
function showMyBrief() {
	layeranimation('myBriefInfo','hide');
	layeranimation('myBrief');
}

function showMyBriefInfo() {
	layeranimation('myBrief','hide');
	layeranimation('myBriefInfo');
}






// LAYER ANIMATIONS
//var animationCounter 	= 0;
var animationStep 		= 25;
var animationSpeed 		= 4;

// Start animation, requires a inner layer: "layername_inner"
function layeranimation(strLayer, direction) {
	var maxCallNum = 1;
	var targetLyr = new getObj(strLayer); // outer layer

	intTestHeight = parseInt(targetLyr.style.height);
	if (!direction && intTestHeight <= 1) {
		targetLyrInner = new getObj(strLayer + "_inner"); //inner layer
		targetHeight = parseInt(targetLyrInner.obj.offsetHeight) - 4;
		maxCallNum += Math.ceil(targetHeight/animationStep);
	} else if (!direction && intTestHeight > 1) {
		targetHeight = 1;
		intCurrentHeight = parseInt(targetLyr.style.height);
		maxCallNum += Math.ceil(intCurrentHeight/animationStep);
	} else if (direction == "show") {
		targetLyrInner = new getObj(strLayer + "_inner"); //inner layer
		targetHeight = parseInt(targetLyrInner.obj.offsetHeight);
		maxCallNum += Math.ceil(targetHeight/animationStep);
	} else {
		targetHeight = 1;
		intCurrentHeight = parseInt(targetLyr.style.height);
		maxCallNum += Math.ceil(intCurrentHeight/animationStep);
	}
	//alert("layeranimation -> targetHeight: " + targetHeight + ", maxCallNum: " + maxCallNum);

	targetLyr.style.overflow = 'hidden'; // in case it was turned off
	setTimeout('resizeItemHeight("' + strLayer + '",' + targetHeight + ',' + maxCallNum + ');', animationSpeed);
}

// Do the resize
function resizeItemHeight (strL, tHeight, maxCalls) {

	var targetLyrRsz = new getObj(strL);
	if (targetLyrRsz.obj) {
		CHeight = parseInt(targetLyrRsz.style.height);

		if (CHeight < tHeight) { // decide which way to resize
			CHeight += animationStep;
			if (CHeight > tHeight) CHeight = tHeight;
		} else {
			CHeight -= animationStep;
			if (CHeight < tHeight) CHeight = tHeight;
		}
	// alert("CHeight: " + CHeight + ", targetLyrRsz.style.height: " + targetLyrRsz.style.height);

		targetLyrRsz.style.height = CHeight + "px";

	//alert("CHeight: " + CHeight + ", setTimeout('resizeItemHeight(\"'" + strL + '",' + tHeight + ',' + maxCalls + ');'+", animationSpeed)");
		//if (maxCalls > 0 && ((intStepAmount > 0 && CHeight < tHeight) || (intStepAmount < 0 && CHeight > tHeight))) {
		if (maxCalls > 0 && CHeight != tHeight) {
			maxCalls--;
			setTimeout('resizeItemHeight("' + strL + '",' + tHeight + ',' + maxCalls + ');', animationSpeed);
		} else{
			if(tHeight == 1) { //this is incase the internal elements also contain show hide elements
				targetLyrRsz.style.overflow = 'hidden';
			} else {
				targetLyrRsz.style.overflow = 'visible';
			}
		}
	}
}


function resizeItemWidth (strL, tWidth, maxCalls) {
	var targetLyrRsz = new getObj(strL);
	// alert(strL + " : " + targetLyrRsz.style.width);
	if (targetLyrRsz.obj) {
		CWidth = parseInt(targetLyrRsz.style.width);

		if (CWidth < tWidth) { // decide which way to resize
			CWidth += animationStep;
			if (CWidth > tWidth) CWidth = tWidth;
		} else {
			CWidth -= animationStep;
			if (CWidth < tWidth) CWidth = tWidth;
		}
		// alert("CWidth: " + CWidth + ", targetLyrRsz.style.height: " + targetLyrRsz.style.height);
		if(parseInt(CWidth).toString() != "NaN") {
			targetLyrRsz.style.width = CWidth + "px";
			if (maxCalls > 0 && CWidth != tWidth) {
				maxCalls--;
				setTimeout('resizeItemWidth("' + strL + '",' + tWidth + ',' + maxCalls + ');', animationSpeed);
			}
		} else {
			if (targetLyrRsz.style.display == 'none') {
				targetLyrRsz.style.display = 'block';
			} else if (targetLyrRsz.style.display == 'block') {
				targetLyrRsz.style.display = 'none';
			}
		}
	}
}


function trim(stringToTrim) {
	return stringToTrim.replace(/^\s+|\s+$/g,"");
}

function ClosePopIn(id)
{
	document.getElementById(id).style.display = "none";
	document.getElementById(id).style.visible = "hidden";
}

function ShowPopIn(id)
{
	document.getElementById(id).style.display = "";
	document.getElementById(id).style.visible = "visible";
}

function ShowContentWarning()
{
	document.getElementById("popinmybrief").style.display = "";
	document.getElementById("popinmybrief").style.visible = "visible";
}

function ShowMyBriefIntroFlash()
{
	document.getElementById("popinmybrief").style.display = "";
	document.getElementById("popinmybrief").style.visible = "visible";
	setTimeout("loadFlashContent('/flash/briefcase3.swf','divMyBriefBody',700,400);", 250);
}

function ShowMyBriefIntroFlashNoTimer()
{
	document.getElementById("popinmybrief").style.display = "";
	document.getElementById("popinmybrief").style.visible = "visible";
	loadFlashContent('/flash/briefcase3.swf','divMyBriefBody',700,400);
}


function CreateBookmarkLink(title, url) 
{ 
	if (window.sidebar) 
	{ 
		// Mozilla Firefox Bookmark		
		window.sidebar.addPanel(title, url,"");	
	} 
	else if( window.external ) 
	{ 
		// IE Favorite		
		window.external.AddFavorite( url, title); 
	}	
	else if(window.opera && window.print) 
	{ 
		// Opera Hotlist
		return true; 
	} 
}

function ChangeTitle(title)
{
	document.title = title;
}

function ShareThisPage (ddl, url, title)
{
	var diggurl = "http://digg.com/submit?phase=2&amp;url=" + url;
	var technoratiurl = "http://technorati.com/faves/?add=" + title + "&amp;url=" + url;
	var deliciousurl = "http://del.icio.us/post?title=" + title + "&amp;url=" + url;
	var myyahoourl = "http://myweb2.search.yahoo.com/myresults/bookmarklet?t=" + title + "&amp;u=" + url;
	
	if (ddl[ddl.selectedIndex].value == "")
	{
		return false;
	}
	else if (ddl[ddl.selectedIndex].value == "digg")
	{
		window.open(diggurl, "diggwindow");
		return false;
	}
	else if (ddl[ddl.selectedIndex].value == "technorati")
	{
		window.open(technoratiurl, "technoratiwindow");
		return false;
	}
	else if (ddl[ddl.selectedIndex].value == "delicious")
	{
		window.open(deliciousurl, "deciciouswindow");
		return false;
	}
	else if (ddl[ddl.selectedIndex].value == "myyahoo")
	{
		window.open(myyahoourl, "myyahoowindow");
		return false;
	}
	else 
	{
		return false;
	}
}
