/* Trims whitespace from a string */
function trim(str) {
	var rx = /^\s*(\S*)\s*$/i;
	return str.replace(rx, '$1');
}


/* Does the element <elm> have the className <cname>? */
function hasClassName(elm, cname) {
	if (ua["dom"]) {
		if (elm != null) {
			if (elm.className != null) {
				aCname = elm.className.split(" ");
				for (j=0; j<aCname.length; j++) {
					if (aCname[j] == cname) return true;
				}
			}
		}
	}
	return false;
}


/* Add the className <cname> to element <obj> */
function addClassName(obj, cname) {
	if (obj) {
		if (!hasClassName(obj, cname)) {
			obj.className += (obj.className.length > 0 ? ' '+cname : cname);
		}
	}
}

/* Remove the className <cname> from element <obj> */
function removeClassName(obj, cname) {
	if (obj) {
		var res = '';
		var aCname = obj.className.split(' ');
		for (var i = 0; i < aCname.length; i++) {
			res += (aCname[i] == cname ? '' : ' '+aCname[i]);
		}
		res = trim(res);
		obj.className = res;
	}					
}



var VOID_DATA = '_'; //dummy data for empty input fields
var current_select = null; //points to current active select object


/* Prevent the window event <e> from bubbling up */
function stopBubbles(e) {
	if (!e) var e = window.event;
	e.cancelBubble = true;
	if (e.stopPropagation) e.stopPropagation();
}

/* Highlights any label for an input element with id <forid> */
function hlLabel(forid) {
	var aLabel = document.getElementsByTagName('label');
	var oLabel = null;
	for (var i = 0; i < aLabel.length; i++) {
		if (aLabel[i].htmlFor == forid) {
			oLabel = aLabel[i];
			i = aLabel.length;
		}
	}
	if (oLabel) {
		addClassName(oLabel, 'active');
	}
}

/* Removes highlight from any label for input element with id <forid> */
function uhlLabel(forid) {
	var aLabel = document.getElementsByTagName('label');
	var oLabel = null;
	for (var i = 0; i < aLabel.length; i++) {
		if (aLabel[i].htmlFor == forid) {
			oLabel = aLabel[i];
			i = aLabel.length;
		}
	}
	if (oLabel) {
		removeClassName(oLabel, 'active');
	}
}

/* Closes the custom select drop down list with id <id> */
function closeCustomSelect(id) {
	var obj = document.getElementById('custom_'+id);
	if (obj) {
		uhlLabel(id);
	//	obj.style.zindex = 1;

		for (var i = 0; i < obj.childNodes.length; i++)	{
			var oOpt = obj.childNodes.item(i);
			if (i > 0){
				oOpt.className = 'option';
				oOpt.onclick = function(e) {
					stopBubbles(e);
					clickSelectOption(this);
				}
			}
			else {
				oOpt.className = 'option selected visible root';
				oOpt.onclick = function(e) {
					stopBubbles(e);
					dropSelect(obj);
				}
			}
		}
		current_select = null;
	}
}

/* When clicking a custom select option <obj> */
function clickSelectOption(obj) {
	if (obj) {
		var opar = obj.parentNode;
		var selId = opar.id.replace(/^custom_/, '');
		current_select = document.getElementById(selId);

		var oRoot = obj.cloneNode(true);
		var oSelect = document.getElementById(selId);
		oSelect.selectedIndex = oRoot.getAttribute('index');
		
		oRoot.className = 'option selected visible root';
		oRoot.onclick = function(e) {
			stopBubbles(e);
			dropSelect(opar);
		}

		opar.insertBefore(oRoot, opar.firstChild);

		var j = -1;
		for (var i = 1; i < opar.childNodes.length; i++) {
			var oOpt = opar.childNodes.item(i);
			if (oOpt.getAttribute('index') != oSelect.selectedIndex) {
				oOpt.className = 'option';
				oOpt.onclick = function(e) {
					stopBubbles(e);
					clickSelectOption(this);
				}
			}
			else {
				j = i;
			}
		}
		opar.removeChild(opar.childNodes.item(j));


		uhlLabel(selId);
		setFocusSelect(oSelect);
	}
}


/* onmouseover for custom select option */
function overSelectOption(obj) {
	if (obj) {
		addClassName(obj, 'over');
	}
}

/* onmouseout for custom select option */
function outSelectOption(obj) {
	if (obj) {
		removeClassName(obj, 'over');
	}
}

/* Change focus to element <obj> */
function setFocusSelect(obj) {
	if (obj) {
		obj.focus();
	}
}

/* What to do on blur event for element <obj> */
function blurSelect(obj) {
	if (obj) {
		// do nothing ...
	}	
}

/* Open option list for custom select element <obj> */
function dropSelect(obj) {
	if (obj) {
		var selId = obj.id.replace(/^custom_/, '');
		var oSel = document.getElementById(selId);
		hlLabel(selId);

		if (current_select != oSel && current_select != null) {
			closeCustomSelect(current_select.id);
		}

		current_select = oSel;

		var aoptions = Array();
		for (var i = 0; i < obj.childNodes.length; i++)	{
			var oOpt = obj.childNodes.item(i).cloneNode(true);
			aoptions[aoptions.length] = oOpt;
		}

		obj.innerHTML = '';

		for (var i = 0; i < aoptions.length; i++) {
			var oOpt = aoptions[i];

			addClassName(oOpt, 'active');
			addClassName(oOpt, 'visible');
			if (i == aoptions.length - 1) {
				addClassName(oOpt, 'last');
			}

			oOpt.onmouseover = function() {
				overSelectOption(this);
			}

			oOpt.onmouseout = function() {
				outSelectOption(this);
			}

			oOpt.onclick = function(e) {
				stopBubbles(e);
				clickSelectOption(this);
			}
			obj.appendChild(oOpt);
		}
	}
}


/* Creates a fancy customized select object to replace standard select element <obj>  */
function customizeSelect(obj, z) {
	if (ua["dom"] && !ua["iemac"]) {
		if (obj) {
			if (obj.options.length > 0)	{

				var oparent = obj.parentNode;

				var oContainer = document.createElement('div');
				oContainer.className = 'custom_select_container';


				oContainer.style.zIndex = 99-z;


				var oDiv = document.createElement('div');
				oDiv.id = 'custom_'+obj.id;
				oDiv.className = 'custom_select';


				if (obj.disabled) {
					addClassName(oDiv, 'disabled');
				}


				if (hasClassName(obj, 'plain')) {
					addClassName(oDiv, 'plain');
				}


				oDiv.style.width = obj.offsetWidth + 'px';

				// Root option
				if (obj.options.length > 0) {
					if (obj.selectedIndex > -1) {
						var oOpt = document.createElement('div');

						var oTL = document.createElement('div');
						addClassName(oTL, 'tl');
						var oTR = document.createElement('div');
						addClassName(oTR, 'tr');
						var oBR = document.createElement('div');
						addClassName(oBR, 'br');
						var oBL = document.createElement('div');
						addClassName(oBL, 'bl');



						addClassName(oOpt, 'option');
						addClassName(oOpt, 'selected');
						addClassName(oOpt, 'root');

	
						var oIcon = document.createElement('div');
						addClassName(oIcon, 'icon');
						if (!hasClassName(obj, 'plain')) {
							oIcon.innerHTML = '<div class="txt">Change</div>';
						}
						else {
							oIcon.innerHTML = '<div class="txt">&nbsp;</div>';
						}

						var oValue = document.createElement('div');
						addClassName(oValue, 'value');
						oValue.innerHTML 
								= '<div class="txt">' 
								+ (obj.options[obj.selectedIndex].text != '' ? obj.options[obj.selectedIndex].text : '&nbsp;');
								+ '</div>';

						var oReset = document.createElement('div');
						addClassName(oReset, 'reset');

						
						oBL.appendChild(oIcon);
						oBL.appendChild(oValue);
						oBL.appendChild(oReset);

						oBR.appendChild(oBL);
						oTR.appendChild(oBR);
						oTL.appendChild(oTR);

						oOpt.appendChild(oTL);


						oOpt.onclick = function(e) {
							stopBubbles(e);
							if (!obj.disabled) {
								dropSelect(oDiv);
							}
						}

						oOpt.setAttribute('index', obj.selectedIndex);

						oDiv.appendChild(oOpt);
					}
				}
			
				// Other options
				for (var i = 0; i < obj.options.length; i++) {
					if (i != obj.selectedIndex) {
						var oOpt = document.createElement('div');
						addClassName(oOpt, 'option');
						if (i == obj.options.length - 1) {
							addClassName(oOpt, 'last');
						}

						oOpt.setAttribute('index',i);


						var oTL = document.createElement('div');
						addClassName(oTL, 'tl');
						var oTR = document.createElement('div');
						addClassName(oTR, 'tr');
						var oBR = document.createElement('div');
						addClassName(oBR, 'br');
						var oBL = document.createElement('div');
						addClassName(oBL, 'bl');



						var oIcon = document.createElement('div');
						addClassName(oIcon, 'icon');
						if (!hasClassName(obj, 'plain')) {
							oIcon.innerHTML = '<div class="txt">Change</div>';
						}
						else {
							oIcon.innerHTML = '<div class="txt">&nbsp;</div>';
						}

						var oValue = document.createElement('div');
						addClassName(oValue, 'value');
						oValue.innerHTML 
								= '<div class="txt">' 
								+ (obj.options[i].text != '' ? obj.options[i].text : '&nbsp;');
								+ '</div>';

						var oReset = document.createElement('div');
						addClassName(oReset, 'reset');

						oBL.appendChild(oIcon);
						oBL.appendChild(oValue);
						oBL.appendChild(oReset);

						oBR.appendChild(oBL);
						oTR.appendChild(oBR);
						oTL.appendChild(oTR);

						oOpt.appendChild(oTL);

			

						oDiv.appendChild(oOpt);
					}
				}

				oContainer.appendChild(oDiv);
				oparent.appendChild(oContainer);
			}

			//Hide the select object outside the visible document area:
			addClassName(obj, 'hidden');
		}
	}
}

function resetCustomizedInputs(frm) {
	var frmElements = frm.elements;
	for (var i = 0; i < frmElements.length ; i++) {
		var frmItem = frmElements.item(i);

		if (hasClassName(frmItem, 'customized')) {
			switch(frmItem.type) {
				case 'select-one' :
					var selId = frmItem.selectedIndex;
					var custSel = document.getElementById('custom_' + frmItem.id);
					if (custSel != null) {
						var custOpts = custSel.childNodes;
						for (var j = 0; j < custOpts.length; j++) {
							if (hasClassName(custOpts.item(j), 'option')) {
								if (custOpts.item(j).getAttribute('index') == selId) {
									clickSelectOption(custOpts.item(j));
									j = custOpts.length;
								}
							}
						}
					}
					break;
				case 'radio' :
				case 'checkbox' :
					var oImg = document.getElementById('custom_' + frmItem.id);
					if (oImg) {
						oImg.setState(frmItem.checked);
					}
					break;
			}

		}
	}
}

function customizeInput(obj) {
	if (obj.type == 'button' || obj.type == 'submit' || obj.type == 'reset') {
		var oparent = obj.parentNode;
		var oTable = document.createElement('table');
		oTable.className = 'custom_input_button';
		oTable.insertRow(0);
		oTable.rows.item(0).insertCell(0);

		var oCell = oTable.rows.item(0).cells.item(0);
		
		var oTxt = document.createElement('a');
		oTxt.href = '#';
		oTxt.className = 'txt';

		if (obj.disabled) {
			addClassName(oTxt, 'disabled');
		}

		oTxt.onclick = function() {
			if (!obj.disabled) {
				obj.click();
				if (obj.type == 'reset') {
					resetCustomizedInputs(obj.form);
				}
			}
			return false;
		}

		var oTL = document.createElement('span');
		oTL.className = 'tl';
		var oTR = document.createElement('span');
		oTR.className = 'tr';
		var oBR = document.createElement('span');
		oBR.className = 'br';
		var oBL = document.createElement('span');
		oBL.className = 'bl';

		
		oBL.innerHTML = obj.value;
		
		oBR.appendChild(oBL);
		oTR.appendChild(oBR);
		oTL.appendChild(oTR);
		oTxt.appendChild(oTL);

		
		
		oCell.appendChild(oTxt);

		
		oparent.insertBefore(oTable, obj);
		addClassName(obj, 'hidden');

	}
	else if (obj.type == 'radio') {
		var oparent = obj.parentNode;

		var oImg = document.createElement('img');
		oImg.className = 'radio';
		oImg.id = 'custom_' + obj.id;

		oImg.setState = function(b) {
			oImg.src = '/site/images/radio_'+ (b ? 'on' : 'off') +'.gif';
		}

		oImg.setState(obj.checked);


		obj.onclick = function() {

			if (this.form[this.name].length > 0) {
				for (var i = 0; i < this.form[this.name].length; i++) {
					var cInp = this.form[this.name][i];
					var cObj = document.getElementById('custom_' + cInp.id);
					if (cObj != null) {
						cObj.setState(cInp.checked);
					}
				}
			}
			else {
				var cObj = document.getElementById('custom_' + this.id);
				cObj.setState(this.checked);
			}
		}


		oImg.onclick = function() {
			obj.click();
		}

		
		oparent.insertBefore(oImg, obj);
		addClassName(obj, 'hidden');

		initLabel(obj);
	}
	else if (obj.type == 'checkbox') {
		var oparent = obj.parentNode;
		var oImg = document.createElement('img');
		oImg.className = 'checkbox';
		oImg.id = 'custom_' + obj.id;

		oImg.setState = function(b) {
			oImg.src = '/site/images/checkbox_'+ (b ? 'on' : 'off') +'.gif';
		}
	
		oImg.setState(obj.checked);

		oImg.onclick = function() {
			obj.click();
		}

		obj.onclick = function() {
			oImg.setState(obj.checked);
		}

		oparent.insertBefore(oImg, obj);
		addClassName(obj, 'hidden');

		initLabel(obj);
	}
}


/* Handles clicks within the document; closes any open custom selects */
function clickDocument() {
	if (current_select != null) {
		closeCustomSelect(current_select.id);
	}
}

/* Handles tab keys in document; closes any open custom selects */
function keydownDocument(e) {
	if (e.keyCode == 9){
		if (current_select != null) {
			closeCustomSelect(current_select.id);
		}
	}
}


function initLabel(obj) {
	//Get all labels:
	var aLabel = document.getElementsByTagName('label');

	//Iterate through all labels:
	for (var i = 0; i < aLabel.length; i++) {
		//Got the right label?
		if (aLabel[i].htmlFor == obj.id) {
			//Add the onclick function:
			aLabel[i].onclick = function(e) {
				//Call click method of input element
				obj.click();
				//Prevent click event from bubbling up:
				stopBubbles(e);
				//Stop normal onclick behaviour:
				return false;
			}
			//Stop iterating:
			i = aLabel.length;
		}
	}
}

function initSelectLabel(obj) {
	//Get all labels:
	var aLabel = document.getElementsByTagName('label');

	//Point to custom select field:
	var custom_select = document.getElementById('custom_'+obj.id);

	//Iterate through all labels:
	for (var i = 0; i < aLabel.length; i++) {

		//Got the right label?
		if (aLabel[i].htmlFor == obj.id) {

			//Add the onclick function:
			aLabel[i].onclick = function(e) {

				//Prevent click event from bubbling up:
				stopBubbles(e);

				//Drop down the custom select:
				if (!obj.disabled) {
					dropSelect(custom_select);
				}

				//Stop normal onclick behaviour:
				return false;
			}

			//Stop iterating:
			i = aLabel.length;
		}
	}
}

function initDynamicContent() {
	if (ua['dom']) {
		makeVisible('footer');
		makeVisible('site_menu');
		makeVisible('newsletter_menu');



		var aSelect = document.getElementsByTagName('select');
		for (var i = 0; i < aSelect.length; i++) {
			var oSel = aSelect[i];
			if (hasClassName(oSel, 'customized')) {
				if (!ua['iemac']) {
					customizeSelect(oSel, i);
					oSel.onblur = function() {
						blurSelect(this);
					}
					initSelectLabel(oSel);		
				}
				else {
					oSel.onfocus = function() {
						hlLabel(this.id);
					}
					oSel.onblur = function() {
						uhlLabel(this.id);
					}										
				}
			}
		}


		if (!ua['iemac']) {
			var aInput = document.getElementsByTagName('input');
			for (var i = 0;  i < aInput.length; i++) {
				var oInput = aInput[i];
				if (hasClassName(oInput, 'customized')) {
					customizeInput(oInput);
				}
			}
		}

		// Set onclick and onkeydown handlers.
		if (!ua["iemac"]) {
			document.onclick = function(e) {
				clickDocument(e);
			}

			document.onkeydown = function(e) {
				if (!e) var e = window.event;
				keydownDocument(e);
			}
		}



	}
}



function showGridCell(id) {
	var obj = document.getElementById(id);
	if (obj) {
		obj.style.visibility = 'visible';
	}
}

function showGrid() {
	if (ua['dom'] && !ua['iemac'] && !ua['ie5']) {
		var oGrid = document.getElementById('gridtable');
		if (oGrid) {
			var rows = oGrid.rows;
			var timer = 0;
			var delay = 60;

			var gridCells = Array();

			var gridRoot = null;
			var gridLines = Array();

			var gridRootPos = -1;


			for (var i = 0; i < rows.length; i++) {
				var cells = rows.item(i).cells;
				
				for (var j = 0; j < cells.length; j++) {
					var cell = cells.item(j);
					if (hasClassName(cell, 'root')) {
						gridRoot = cell;
					}
					else if (hasClassName(cell, 'entertainment') || hasClassName(cell, 'maintenance') || hasClassName(cell, 'work') || hasClassName(cell, 'ghost')) {
						gridCells[gridCells.length] = cell;
					}
					else if (hasClassName(cell, 'name') || hasClassName(cell, 'line') || hasClassName(cell, 'plus')) {
						gridLines[gridLines.length] = cell;
					}
				
					var cell = cells.item(j);
					cell.id =  'gridcell_'+i+'_'+j;
				}
			}


			//Show root cell:
			if (gridRoot != null) {
				gridRoot.style.visibility = 'visible';
			}


			//Show cells:
			if (gridCells.length > 0) {
				do {
					var pos = Math.floor((Math.random() * gridCells.length + 1)) - 1;
					var id = gridCells[pos].id;
					setTimeout("showGridCell('"+ id + "')", timer * delay);
					timer++;
					gridCells.splice(pos, 1); //Not supported by IE/Mac or IE5/Win
				}
				while (gridCells.length > 0);
			}

			//Show lines:
			if (gridLines.length > 0) {
			
				for (var i = 0;i < gridLines.length; i++) {
					setTimeout("showGridCell('"+ gridLines[i].id + "')", timer * delay);
					timer++;
				}
			}

		}
	}
}


if (ua["safari"]) {
	addLoadEvent(initDynamicContent);
}

addLoadEvent(showGrid);