var theRules = getRules();

// prevent display of navigation until page has loaded, to avoid odd placement
registerEventNode('load', window, showLeftNav);
function showLeftNav()
{
	if (document.getElementById('left'))
	{ document.getElementById('left').style.visibility = 'visible'; }
}

function setHeight(leftId, centreId, rightId, contentId)
{
	if (!leftId) { leftId = 'left'; }
	if (!centreId) { centreId = 'bodycontent'; }
	if (!rightId) { rightId = 'right'; }
	if (!contentId) { contentId = 'content'; }
	
	clearHeight(leftId, centreId, rightId, contentId);
	var leftPadding = 0;
	var maincontentPadding = 0;
	var bodycontentPadding = 0;
	var rightPadding = 0;
	
	var leftHeight = 0, mainHeight = 0, rightHeight = 0, contentHeight = 0;
	
	if (document.getElementById(leftId))
	{ leftHeight = document.getElementById(leftId).offsetHeight; }
	if (document.getElementById(centreId))
	{ mainHeight = document.getElementById(centreId).offsetHeight; } 
	if (document.getElementById(rightId))
	{ rightHeight = document.getElementById(rightId).offsetHeight; } 
	if (document.getElementById(contentId))
	{ contentHeight = document.getElementById(contentId).offsetHeight; } 
	
	// right-hand side must be extended to not overlap the language bar, and the logo on the group site
	rightHeight += 50;
	if (document.getElementById('rightlogo'))
	{ rightHeight += 90; }

	
	if (document.styleSheets[0].rules)
	{ mainHeight += 10; }

	var offHeight = leftHeight;
	offHeight = ( mainHeight > offHeight ) ? mainHeight : offHeight;
	offHeight = ( rightHeight > offHeight ) ? rightHeight : offHeight;
	
	// include a minimum height
	offHeight = ( 500 > offHeight ) ? 500 : offHeight ;
	
	if (document.styleSheets[0].rules)
	{ contentHeight -= 10; }

	if (contentHeight < 0)
	{ contentHeight = 0; }

	// firefox
	if (document.styleSheets[0].cssRules)
	{
		for (i=0; i<theRules.length; i++)
		{
			if (theRules[i].selectorText == '#'+centreId || theRules[i].selectorText == '*[ID"'+centreId+'"]')
			{ theRules[i].style.setProperty('height', (offHeight-bodycontentPadding)+'px', null); }
			
			if (theRules[i].selectorText == '#'+leftId || theRules[i].selectorText == '*[ID"'+leftId+'"]')
			{ theRules[i].style.setProperty('height', (offHeight-leftPadding)+'px', null); }
			
			if (theRules[i].selectorText == '#'+rightId || theRules[i].selectorText == '*[ID"'+rightId+'"]')
			{ theRules[i].style.setProperty('height', (offHeight-rightPadding)+'px', null); }
			
			if (theRules[i].selectorText == "#maincontent" || theRules[i].selectorText == '*[ID"maincontent"]')
			{ theRules[i].style.setProperty('height', (offHeight+maincontentPadding)+'px', null); }
			
			if (theRules[i].selectorText == '#'+contentId || theRules[i].selectorText == '*[ID"'+contentId+'"]')
			{ theRules[i].style.setProperty('height', (contentHeight)+'px', null); }
		}
	}
	// IE
	else if (document.styleSheets[0].rules)
	{
		for (i=0; i<theRules.length; i++)
		{
			if(theRules[i].selectorText == '#'+centreId)
			{ theRules[i].style.height = (offHeight-bodycontentPadding); }
			
			if(theRules[i].selectorText == '#'+leftId)
			{ theRules[i].style.height = (offHeight-leftPadding); }
			
			if(theRules[i].selectorText == '#'+rightId)
			{ theRules[i].style.height = (offHeight-rightPadding); }
			
			if(theRules[i].selectorText == "#maincontent")
			{ theRules[i].style.height = (offHeight+maincontentPadding); }
			
			if(theRules[i].selectorText == '#'+contentId)
			{ theRules[i].style.height = contentHeight; }
		}
	}
}

function clearHeight(leftId, centreId, rightId, contentId){
	// firefox
	if (document.styleSheets[0].cssRules){
		for (i=0; i<theRules.length; i++)
		{
			if (theRules[i].selectorText == '#'+centreId || theRules[i].selectorText == '*[ID"'+centreId+'"]')
			{ theRules[i].style.setProperty('height', 'auto', null); }
			
			if (theRules[i].selectorText == '#'+leftId || theRules[i].selectorText == '*[ID"'+leftId+'"]')
			{ theRules[i].style.setProperty('height', 'auto', null); }
			
			if (theRules[i].selectorText == '#'+rightId || theRules[i].selectorText == '*[ID"'+rightId+'"]')
			{ theRules[i].style.setProperty('height', 'auto', null); }
			
			if (theRules[i].selectorText == "#maincontent" || theRules[i].selectorText == '*[ID"maincontent"]')
			{ theRules[i].style.setProperty('height', 'auto', null); }
			
			if (theRules[i].selectorText == '#'+contentId || theRules[i].selectorText == '*[ID"'+contentId+'"]')
			{ theRules[i].style.setProperty('height', 'auto', null); }
		}
	}
	// IE
	else if (document.styleSheets[0].rules){
		for (i=0; i<theRules.length; i++)
		{
			if(theRules[i].selectorText == '#'+centreId)
			{ theRules[i].style.height = 'auto'; }
			
			if(theRules[i].selectorText == '#'+leftId)
			{ theRules[i].style.height = 'auto'; }
			
			if(theRules[i].selectorText == '#'+rightId)
			{ theRules[i].style.height = 'auto'; }
			
			if(theRules[i].selectorText == "#maincontent")
			{ theRules[i].style.height = 'auto'; }
			
			if(theRules[i].selectorText == '#'+contentId)
			{ theRules[i].style.height = 'auto'; }
		}
	}
}

function getRules()
{
	if (document.styleSheets[0].cssRules)
	{ return document.styleSheets[0].cssRules; }
	// IE
	else if (document.styleSheets[0].rules)
	{ return document.styleSheets[0].rules; }
}

function changeClass(id, classname) {
	var el=id.parentNode;
	var attributeNode = el.getAttributeNode("class");
	if(attributeNode){
		if(classname!="") attributeNode.value = classname;
		else el.removeAttribute("class");
	}
	else
		el.setAttribute("class", classname);
} 

var blnSubpageLinks=false;
var curPos, curNum;
curNum=0;
function turnPageLinks(curLi, num){
	if(curPos!=undefined) {
		curPos.className="";
	}
	curLi.className="on";
	//alert(num+"  "+curNum+"   "+(num+1));
	if (curLi.toString().indexOf('#'+(num+1))>0) {
		if(curNum!=num) document.getElementById("pagelinks").style.display="none";
		document.getElementById("pagelinks").style.display="block";
		blnSubpageLinks=true;
	}
	else{
		document.getElementById("pagelinks").style.display="none";
	}
	curPos=curLi;
	curNum=num;
}

function removeClassname(){
	var allClass=document.getElementById("pagelinks").li;
	//for(int i=0; i<allClass.length; i++){
	//}
}

// cookie functions
function createCookie(name,value,days) {
  if (days) {
    var date = new Date();
    date.setTime(date.getTime()+(days*24*60*60*1000));
    var expires = "; expires="+date.toGMTString();
  }
  else expires = "";
  document.cookie = name+"="+value+expires+"; path=/";
};

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 null;
};

// currency convertor window
function popCurrencyConvert()
{
	window.open('central/currency.html', 'convertor', 'width=350,height=200,resizable=yes,scrollbars=yes');
}


// quirksmode.org browser detection
var BrowserDetect = {
	init: function () {
		this.browser = this.searchString(this.dataBrowser) || "An unknown browser";
		this.version = this.searchVersion(navigator.userAgent)
			|| this.searchVersion(navigator.appVersion)
			|| "an unknown version";
		this.OS = this.searchString(this.dataOS) || "an unknown OS";
	},
	searchString: function (data) {
		for (var i=0;i<data.length;i++)	{
			var dataString = data[i].string;
			var dataProp = data[i].prop;
			this.versionSearchString = data[i].versionSearch || data[i].identity;
			if (dataString) {
				if (dataString.indexOf(data[i].subString) != -1)
					return data[i].identity;
			}
			else if (dataProp)
				return data[i].identity;
		}
	},
	searchVersion: function (dataString) {
		var index = dataString.indexOf(this.versionSearchString);
		if (index == -1) return;
		return parseFloat(dataString.substring(index+this.versionSearchString.length+1));
	},
	dataBrowser: [
		{ 	string: navigator.userAgent,
			subString: "OmniWeb",
			versionSearch: "OmniWeb/",
			identity: "OmniWeb"
		},
		{
			string: navigator.vendor,
			subString: "Apple",
			identity: "Safari"
		},
		{
			prop: window.opera,
			identity: "Opera"
		},
		{
			string: navigator.vendor,
			subString: "iCab",
			identity: "iCab"
		},
		{
			string: navigator.vendor,
			subString: "KDE",
			identity: "Konqueror"
		},
		{
			string: navigator.userAgent,
			subString: "Firefox",
			identity: "Firefox"
		},
		{
			string: navigator.vendor,
			subString: "Camino",
			identity: "Camino"
		},
		{		// for newer Netscapes (6+)
			string: navigator.userAgent,
			subString: "Netscape",
			identity: "Netscape"
		},
		{
			string: navigator.userAgent,
			subString: "MSIE",
			identity: "Explorer",
			versionSearch: "MSIE"
		},
		{
			string: navigator.userAgent,
			subString: "Gecko",
			identity: "Mozilla",
			versionSearch: "rv"
		},
		{ 		// for older Netscapes (4-)
			string: navigator.userAgent,
			subString: "Mozilla",
			identity: "Netscape",
			versionSearch: "Mozilla"
		}
	],
	dataOS : [
		{
			string: navigator.platform,
			subString: "Win",
			identity: "Windows"
		},
		{
			string: navigator.platform,
			subString: "Mac",
			identity: "Mac"
		},
		{
			string: navigator.platform,
			subString: "Linux",
			identity: "Linux"
		}
	]

};
BrowserDetect.init();