function initPage() {
	initAutoScalingNav({
		menuId: 'nav',
		firstClass: 'first-child',
		lastClass: 'last-child',
		firtsLast: true,
		liWidth: true
	});
}

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);
		}
	}
}
if (window.addEventListener)
	window.addEventListener("load", initPage, false);
else if (window.attachEvent)
	window.attachEvent("onload", initPage);
