var ContentLoadedEvent = new function()
{
	var DOM_LOAD_ELEMENT_ID = "dom-load";
	var fnList = new Array();
	var timer = null;
	
	this.domLoadElementId = DOM_LOAD_ELEMENT_ID;
	
	this.add = function(fn)
	{
		fnList.push(fn);
	}
	
	this.runFnList = function()
	{
		window.onload = null;
		clearInterval(timer);
		for(var i=0,fn;fn=fnList[i];i++)
		{
			fn();
		}
	}

	var domLoad = new function()
	{
		timer = setInterval(function() { // doesnt work in IE/Mac
		if((document.getElementsByTagName("body")[0] != null || document.body != null) && document.getElementById(ContentLoadedEvent.domLoadElementId)) {
				ContentLoadedEvent.runFnList();
			}
		}, 250);
		if (typeof document.addEventListener != "undefined") document.addEventListener("DOMContentLoaded", function() { ContentLoadedEvent.runFnList(); } , null); // Mozilla only
		else window.onload = function() { if(document.getElementById(ContentLoadedEvent.domLoadElementId) == undefined) window.defaultStatus = "Error: domLoadElementId[\""+ContentLoadedEvent.domLoadElementId+"\"] is not embeded.";}; // IE Response
	}
	this.domLoad = domLoad;
}


var Region = new function() {
	
	var ID = "region";
	var SELECT_ID = "region-select"
	var init = new function()
	{
		ContentLoadedEvent.add(create);
	}
	function create()
	{
		if(document.getElementById(ID))
		{	
			var ulElm = document.getElementById(ID).getElementsByTagName("ul")[0];
			if(typeof(ulElm)!='undefined'){
				var liList = ulElm.getElementsByTagName("li");
				if(liList.length)
				{
					var labelElm = document.createElement("label");
					labelElm.setAttribute("for",SELECT_ID);
					labelTxt = document.createTextNode(ulElm.getAttribute("title"));
					labelElm.appendChild(labelTxt);
					var i = 0;
					var iLength = liList.length;
					var selectElm = document.createElement("select");
					selectElm.setAttribute("id",SELECT_ID);
					do
					{
							var liElm = liList[i];
						var aElm = liElm.getElementsByTagName("a")[0];
						var optionElm = document.createElement("option");
						optionElm.setAttribute("value",aElm.href);
						if(liElm.className == "sys_active") optionElm.setAttribute("selected","selected");
						var optionTxt = document.createTextNode(aElm.firstChild.nodeValue);
						optionElm.appendChild(optionTxt);
						selectElm.appendChild(optionElm);
					} while(++i<iLength)
					ulElm.parentNode.replaceChild(selectElm,ulElm);
					selectElm.onchange = function()
					{
						document.location.href = this.value;
					}
					document.getElementById(ID).insertBefore(labelElm,selectElm);
				}
			}
		}
	}
}


var Search = new function()
{
	var FORM_ID = "search-form";
	var INPUT_ID = "SearchBox_SearchTextbox";
	var LABEL_ID = "SearchBox_SearchLabel";

	var init = new function()
	{
		ContentLoadedEvent.add(create);
	}

	function create()
	{
		inputElm = document.getElementById(INPUT_ID);
		labelElm = document.getElementById(LABEL_ID);
		if(inputElm && labelElm)
		{
			inputElm.defaultValue = labelElm.firstChild.nodeValue;
			inputElm.value = inputElm.defaultValue;
			inputElm.onfocus = function ()
			{
				Search.focus(this);
			}
		}
	}
	
	this.focus = function(inputElm)
	{
		if(inputElm.value == inputElm.defaultValue) inputElm.value="";
		inputElm.parentNode.parentNode.className="focus";
		inputElm.onblur = function()
		{
			if(trim(this.value).length == 0)
			{
				this.value = this.defaultValue
			}
			this.parentNode.parentNode.className="";
		};
	};
	this.submit = function(id)
	{
		formElm = document.getElementById(id);
		if(formElm)
		{
			if(formElm.onsubmit == undefined || (formElm.onsubmit != undefined && formElm.onsubmit()))
			{
				formElm.submit();
			}
		}
		return false;
	}
}

var PNG = new function()
{
	var init = new function()
	{
		if(document.all) 
		{
			ContentLoadedEvent.add(set);
		}
	}
	function set()
	{
		var imgPNGList = getElementsByClassName("sys_png-image",document,"img");
		if(imgPNGList.length)
		{
			UFO.createCSS(".sys_png-image","visibility:hidden;")
			var i = imgPNGList.length-1;
			do
			{
				var imgElm = imgPNGList[i];
				imgElm.onload = function()
				{
					this.onload = null;
					this.style.filter="progid:DXImageTransform.Microsoft.AlphaImageLoader(src='"+this.src+"',sizingMethod='scale')";
					this.style.width = this.width+"px";
					this.style.height = this.height+"px";
					this.style.visibility="inherit";
					this.src="/SiteElements/Images/transparent.gif";
				}
				imgElm.src = imgElm.src;
			} while(i--)
		}
	}
}

// ===================================================================

function trim(str) {
 // skip leading and trailing whitespace
 // and return everything in between
  str=str.replace(/^\s*(.*)/, "$1");
  str=str.replace(/(.*)\s*$/, "$1");
  return str;
}

function getElementsByClassName(className,nodeElm,elmType)
{
	var elementList = [];
	var elementsByClassName = [];
	if(elmType) elementList = nodeElm.getElementsByTagName(elmType);
	else elementList = document.all || document.getElementsByTagName("*");
	for(var i=0,elm;elm=elementList[i];i++)
	{
		if(hasClassName(elm,className))
		{
			elementsByClassName.push(elm);
		}
	}
	return elementsByClassName;
}


function hasClassName(elm,className)
{
	var re = RegExp("(^|\\s)"+className+"(\\s|$)");
	return re.test(elm.className);
}

function getNextSibling(elm){
	nextElm=elm.nextSibling;
	while(nextElm.nodeType!=1){
		nextElm = nextElm.nextSibling;
	}
	return nextElm;
}

function getPreviousSibling(elm){
	previousElm=elm.previousSibling;
	while(previousElm.nodeType!=1){
		previousElm = previousElm.nextSibling;
	}
	return previousElm;
}
var CompanyDD= new function() {
	
	var ID = "SpecialOfferCompanySelect";
	var SELECT_ID = "SpecialOfferCompanyUl-select"
	var init = new function()
	{
		ContentLoadedEvent.add(create);
	}
	function create()
	{
		if(document.getElementById(ID))
		{	
			var ulElm = document.getElementById(ID).getElementsByTagName("ul")[0];
			if(typeof(ulElm)!='undefined'){
				var liList = ulElm.getElementsByTagName("li");
				if(liList.length)
				{
					var labelElm = document.createElement("label");
					labelElm.setAttribute("for",SELECT_ID);
					labelTxt = document.createTextNode(ulElm.getAttribute("title"));
					labelElm.appendChild(labelTxt);
					var i = 0;
					var iLength = liList.length;
					var selectElm = document.createElement("select");
					selectElm.setAttribute("id",SELECT_ID);
					do
					{
							var liElm = liList[i];
						var aElm = liElm.getElementsByTagName("a")[0];
						var optionElm = document.createElement("option");
						optionElm.setAttribute("value",aElm.href);
						if(liElm.className == "sys_active") optionElm.setAttribute("selected","selected");
						var optionTxt = document.createTextNode(aElm.firstChild.nodeValue);
						optionElm.appendChild(optionTxt);
						selectElm.appendChild(optionElm);
					} while(++i<iLength)
					ulElm.parentNode.replaceChild(selectElm,ulElm);
					selectElm.onchange = function()
					{
						document.location.href = this.value;
					}
					document.getElementById(ID).insertBefore(labelElm,selectElm);
				}
			}
		}
	}
}
