function initPage()
{
	clearFormFields({
		clearInputs: true,
		clearTextareas: true,
		passwordFieldText: false,
		addClassFocus: "focus",
		filterClass: "default"
	});
	initDropDown();
	initAutoScalingNav({
		menuId: 'nav',
		ie6LiHover: false,
		liWidth: false
	})
}
function initAutoScalingNav(o) {
	if (o.widthPercentage == 'undefined') {
		o.widthPercentage = 'false';
	}
	if (!o.hoverClass) {
		o.hoverClass = 'hover';
	}
	if (!o.childTagName) {
		o.childTagName = 'a';
	}
	if (!o.widthShift) {
		o.widthShift = 0;
	}
	if (!o.firstClass) {
		o.firstClass = 'first';
	}
	if (!o.lastClass) {
		o.lastClass = 'last';
	}
	if (!o.widthShiftParent) {
		o.widthShiftParent = 0;
	}
	if (o.ie6LiHover == 'undefined') {
		o.mini = false;
	}
	if (o.mini == 'undefined') {
		o.mini = false;
	}
	if (o.firtsLast == 'undefined') {
		o.firtsLast = true;
	}
	if (o.childFirtsLast == 'undefined') {
		o.childFirtsLast = true;
	}
	if (o.liWidth == 'undefined') {
		o.liWidth = true;
	}
	var classAutoScalingStart = 'AutoScaling-active';
	var classAutoScalingReadyt = 'AutoScaling-ready';
	
	if (o.menuClass) {
		var _box = document.getElementsByTagName('ul');
		for (var i = 0; i < _box.length; i++) {
			if (_box[i].className.indexOf(o.menuClass) != -1) {
				var _container = _box[i];
				setClass(_container,classAutoScalingStart);
				process();
			}
		}
	}
	if (o.menuId) {
		var _boxid = document.getElementById(o.menuId);
		if (_boxid) {
			var _container = _boxid;
			setClass(_container,classAutoScalingStart);
			process();
		}
	}
	function setClass(_object, _class){
		if (_object.className.indexOf(_class) == -1) {
			_object.className += ' ' + _class;
		}
	}
	function process(){
		var _box_li = [];
		var _holder = _container.getElementsByTagName('li');
		_box_li._W = _container.offsetWidth - o.widthShiftParent;
		var _WLi = 0;
		var _WEl = 0;
		for (var j = 0; j < _holder.length; j++) {
			if (o.ie6LiHover) {
					if(typeof(document.body.style.maxHeight) == 'undefined'){
					_holder[j].onmouseover = function() {
						this.className += ' '+o.hoverClass;
					}
					_holder[j].onmouseout = function() {
						this.className = this.className.replace(' '+o.hoverClass, "");
					}
				}
			}
			if (_holder[j].parentNode == _container) {
				var _el = _holder[j].getElementsByTagName(o.childTagName);
				for (var k = 0; k < _el.length; k++) {
					if (_el[k].parentNode == _holder[j]) {
						_holder[j]._el = _el[k];
						_holder[j]._elW = _el[k].offsetWidth;
						_WEl = _WEl + _holder[j]._elW;
					}
				}
				_holder[j]._w = _holder[j].offsetWidth;
				_WLi = _WLi + _holder[j]._w;
				_box_li.push(_holder[j]);
			}
		}
		_box_li._WEl = _WEl;
		_box_li._WLi = _WLi;
		_setWidth();
		
		function _setWidth(){
			var _countLi = _box_li.length;
			if (o.firtsLast && _countLi > 1) {
				_box_li[0].className += ' ' + o.firstClass;
				_box_li[_countLi - 1].className += ' ' + o.lastClass;
				if (o.childFirtsLast) {
					_box_li[0]._el.className += ' ' + o.firstClass;
					_box_li[_countLi - 1]._el.className += ' ' + o.lastClass;
				}
			}
			var _width = (_box_li._W - _box_li._WLi) / _countLi;
			if (o.widthPercentage) {
				var version = navigator.userAgent.toLowerCase();
				var _balance = 100;
				for (var i = 0; i < _countLi; i++) {
					_box_li[i].PercentageW = Math.round((_box_li[i]._w / _box_li._WLi) * 100);
					_balance = _balance - _box_li[i].PercentageW;
				}
				for (var i = 0; i < _countLi; i++) {
					if (i == _countLi - 1) {
						if (navigator.appName.indexOf("Microsoft Internet Explorer") != -1) {
							if (version.indexOf("msie 6") != -1 || version.indexOf("msie 7") != -1) {
								break;
							}
						}
					}
					if (_balance > 0) {
						_box_li[i].style.width = (_box_li[i].PercentageW + 1) + '%';
						_balance = _balance - 1;
					} else {
						if (_balance < 0) {
							_box_li[i].style.width = (_box_li[i].PercentageW - 1) + '%';
							_balance = _balance + 1;
						} else {
							_box_li[i].style.width = _box_li[i].PercentageW + '%';
						}
					}
				}
			} else {
				if (o.liWidth) {
					if (o.mini && _countLi > 1) {
						_width = (_box_li._W - _box_li._WLi) / (_countLi - 1);
						if (o.widthShift == 0) {
							_width = Math.round(_width);
							var _balance = _box_li._W - (_width * (_countLi - 1) + _box_li._WLi);
						} else {
							_width = Math.round(_width - o.widthShift);
							var _balance = _box_li._W - ((_width * (_countLi - 1) + _box_li._WLi) + o.widthShift * (_countLi - 1));
						}
						var _miniWidth = Math.round(_width / 2);
						_balance = _balance + (_width - (_miniWidth * 2));
						for (var i = 0; i < _countLi; i++) {
							if (i == (_countLi - 1)) {
								_box_li[i].style.width = _box_li[i]._w + _miniWidth + _balance + 'px';
							} else {
								if (i == 0) {
									_box_li[i].style.width = _box_li[i]._w + _miniWidth + 'px';
								}
								else {
									_box_li[i].style.width = _box_li[i]._w + _width + 'px';
								}
							}
						}
					} else {
						if (o.widthShift == 0) {
							_width = Math.round(_width);
							var _balance = _box_li._W - (_width * _countLi + _box_li._WLi);
						} else {
							_width = Math.round(_width - o.widthShift);
							var _balance = _box_li._W - ((_width * _countLi + _box_li._WLi) + o.widthShift * (_countLi));
						}
						for (var i = 0; i < _countLi; i++) {
							if (i == (_countLi - 1)) {
								_box_li[i].style.width = _box_li[i]._w + _width + _balance + 'px';
							} else {
								_box_li[i].style.width = _box_li[i]._w + _width + 'px';
							}
						}
					}
				}
				else {
					var _widthChild = (_box_li._W - _box_li._WEl) / _countLi;
					if (o.mini && _countLi > 1) {
						_widthChild = (_box_li._W - _box_li._WEl) / (_countLi - 1);
						if (o.widthShift == 0) {
							_widthChild = Math.round(_widthChild);
							var _balance = _box_li._W - (_widthChild * (_countLi - 1) + _box_li._WEl);
						} else {
							_widthChild = Math.round(_widthChild - o.widthShift);
							var _balance = _box_li._W - ((_widthChild * (_countLi - 1) + _box_li._WEl) + o.widthShift * (_countLi - 1));
						}
						var _miniWidth = Math.round(_widthChild / 2);
						_balance = _balance + (_widthChild - (_miniWidth * 2));
						for (var i = 0; i < _countLi; i++) {
							if (i == (_countLi - 1)) {
								_box_li[i]._el.style.width = _box_li[i]._elW + _miniWidth + _balance + 'px';
							} else {
								if (i == 0) {
									_box_li[i]._el.style.width = _box_li[i]._elW + _miniWidth + 'px';
								} else {
									_box_li[i]._el.style.width = _box_li[i]._elW + _widthChild + 'px';
								}
							}
						}
					} else {
						if (o.widthShift == 0) {
							_widthChild = Math.round(_widthChild);
							var _balance = _box_li._W - (_widthChild * _countLi + _box_li._WEl);
						} else {
							_widthChild = Math.round(_widthChild - o.widthShift);
							var _balance = _box_li._W - ((_widthChild * _countLi + _box_li._WEl) + o.widthShift * (_countLi));
						}
						for (var i = 0; i < _countLi; i++) {
							if (i == (_countLi - 1)) {
								_box_li[i]._el.style.width = _box_li[i]._elW + _widthChild + _balance + 'px';
							} else {
								_box_li[i]._el.style.width = _box_li[i]._elW + _widthChild + 'px';
							}
						}
					}
				}
			}
			setClass(_container,classAutoScalingReadyt);
		}
	}
}
function initDropDown()
{
	var nav = document.getElementById("nav");
	if(nav) {
		var lis = nav.getElementsByTagName("li");
		for (var i=0; i<lis.length; i++) {
			if(lis[i].getElementsByTagName("ul").length > 0) {
				lis[i].className += " has-drop-down"
				lis[i].getElementsByTagName("a")[0].className += " has-drop-down-a"
			}
			lis[i].onmouseover = function()	{
				this.className += " hover";
			}
			lis[i].onmouseout = function() {
				this.className = this.className.replace(" hover", "");
			}
		}
	}
}

function clearFormFields(o)
{
	if (o.clearInputs == null) o.clearInputs = true;
	if (o.clearTextareas == null) o.clearTextareas = true;
	if (o.passwordFieldText == null) o.passwordFieldText = false;
	if (o.addClassFocus == null) o.addClassFocus = false;
	if (!o.filterClass) o.filterClass = "default";
	if(o.clearInputs) {
		var inputs = document.getElementsByTagName("input");
		for (var i = 0; i < inputs.length; i++ ) {
			if((inputs[i].type == "text" || inputs[i].type == "password") && inputs[i].className.indexOf(o.filterClass) == -1) {
				inputs[i].valueHtml = inputs[i].value;
				inputs[i].onfocus = function ()	{
					if(this.valueHtml == this.value) this.value = "";
					if(this.fake) {
						inputsSwap(this, this.previousSibling);
						this.previousSibling.focus();
					}
					if(o.addClassFocus && !this.fake) {
						this.className += " " + o.addClassFocus;
						this.parentNode.className += " parent-" + o.addClassFocus;
					}
				}
				inputs[i].onblur = function () {
					if(this.value == "") {
						this.value = this.valueHtml;
						if(o.passwordFieldText && this.type == "password") inputsSwap(this, this.nextSibling);
					}
					if(o.addClassFocus) {
						this.className = this.className.replace(o.addClassFocus, "");
						this.parentNode.className = this.parentNode.className.replace("parent-"+o.addClassFocus, "");
					}
				}
				if(o.passwordFieldText && inputs[i].type == "password") {
					var fakeInput = document.createElement("input");
					fakeInput.type = "text";
					fakeInput.value = inputs[i].value;
					fakeInput.className = inputs[i].className;
					fakeInput.fake = true;
					inputs[i].parentNode.insertBefore(fakeInput, inputs[i].nextSibling);
					inputsSwap(inputs[i], null);
				}
			}
		}
	}
	if(o.clearTextareas) {
		var textareas = document.getElementsByTagName("textarea");
		for(var i=0; i<textareas.length; i++) {
			if(textareas[i].className.indexOf(o.filterClass) == -1) {
				textareas[i].valueHtml = textareas[i].value;
				textareas[i].onfocus = function() {
					if(this.value == this.valueHtml) this.value = "";
					if(o.addClassFocus) {
						this.className += " " + o.addClassFocus;
						this.parentNode.className += " parent-" + o.addClassFocus;
					}
				}
				textareas[i].onblur = function() {
					if(this.value == "") this.value = this.valueHtml;
					if(o.addClassFocus) {
						this.className = this.className.replace(o.addClassFocus, "");
						this.parentNode.className = this.parentNode.className.replace("parent-"+o.addClassFocus, "");
					}
				}
			}
		}
	}
	function inputsSwap(el, el2) {
		if(el) el.style.display = "none";
		if(el2) el2.style.display = "inline";
	}
}
if (window.addEventListener)
	window.addEventListener("load", initPage, false);
else if (window.attachEvent)
	window.attachEvent("onload", initPage);

