jQuery(function(){
	if(typeof(initAutoScalingNav) == 'function'){
		initAutoScalingNav({
			menuId: "main-nav",
			sideClasses: true,
			minPaddings: 10,
			spacing: 1
		});
	}
	initCustomFile();
	initMoveSearch();
	ieHover('#nav li','hover');
});
// IE6 hover
function ieHover(h_list, h_class){
	if(jQuery.browser.msie && jQuery.browser.version < 7){
		if(!h_class) var h_class = 'hover';
		jQuery(h_list).mouseenter(function(){
			jQuery(this).addClass(h_class);
		}).mouseleave(function(){
			jQuery(this).removeClass(h_class);
		});
	};
};
// init Move Search
function initMoveSearch(){
	var dur = 300;
	var box = jQuery('.search-form div.box');
	var boxW = box.width();
	var nav = jQuery('#nav');
	var lis = nav.find('ul > li');
	lis.each(function(){
		var li = jQuery(this);
		var drop = li.find('div.drop');
		if(drop.length){
			if(li.hasClass('last-child')) {
				var firstLi = drop.find('li:last');
				var lastLi = drop.find('li:first');
			} else {
				var firstLi = drop.find('li:first');
				var lastLi = drop.find('li:last');
			}
			li.mouseenter(function(){
				drop.show();
				var posDropL = firstLi.offset().left;
				var navL = nav.offset().left;
				
				var posDropR = lastLi.offset().left+lastLi.width();
				var navR = navL + nav.width();
				var diff1 = posDropL - navL;
				var diff2 = navR - posDropR;
				if(boxW > diff2){
					var left = posDropL-navL-boxW;
					if (left < 0) {
						box.animate({
							width: boxW+left,
							left:0
						},{duration:dur,queue:false})
					} else {
						box.animate({
							width: boxW,
							left:left
						},{duration:dur,queue:false})
					}
				} else {
					box.animate({
						left:676
					},{duration:dur,queue:false});
				}
			}).mouseleave(function(){
				drop.hide();
				box.animate({
					left:676,
					width: boxW
				},{duration:dur,queue:false});
			});
		}
	});
};
// init Custom File

function initCustomFile()
{
	var inputs = document.getElementsByTagName("input");
	for (var i= 0; i < inputs.length; i++)
	{
		if(inputs[i].className.indexOf("file-input-area") != -1)
		{
			inputs[i].file = inputs[i].parentNode.getElementsByTagName("input").item(1);
			inputs[i].file.readOnly = true;
			inputs[i].onchange = function()
			{
				this.file.value = this.value;
			}
			inputs[i].onmouseover = function()
			{
				this.parentNode.className += " hover";
			}
			inputs[i].onmouseout = function()
			{
				this.parentNode.className = this.parentNode.className.replace(" hover", "");
			}
		}
	}
}

