$(document).ready(function(){
	 $('#nav').moveNavigation();
	 $('#nav').NavDropDown();
});

jQuery.fn.moveNavigation = function(_options){
    // defaults options	
    var _options = jQuery.extend({
	    duration:400,
	    addBG:'<li class="hover-link"></li>',
	    liBgClass :'hover-link',
		delay: 100
    },_options);

    return this.each(function(){
	    var _obg = jQuery(this);
	    var _liWidth = [];
	    var _aWidth = [];
	    var _lis = jQuery(_obg).children('li');
	    var _links = _lis.find('> a');
	    var _linkIndex = 0;
	    var _timer = false;
	    var _pLeft = 13;
	    var _visible = false;
		var _active = _lis.filter('.current-menu-item');
	    jQuery(_lis).filter(':last-child').css('background','none');
		_ul = _obg.find('ul');
	    
	    _lis.each(function(i, el){
			_liWidth[i] = jQuery(el).outerWidth()+10;
			_aWidth[i] = _links.eq(i).outerWidth();
	    });
	    if (jQuery.browser.msie) jQuery(_options.addBG).insertBefore(jQuery(_obg).find('li:first-child'));
	    else jQuery(_obg).append(_options.addBG);
	    		
	    jQuery.fn.moveNavigation.animateThis = function(_activeIndex, _obg){
			var _activeLiWidth = _liWidth[_activeIndex];
			var _activeAWidth = _aWidth[_activeIndex];
			var _left = _pLeft;
			var _thisUl = _lis.eq(_activeIndex).children('ul');
			for (var j = _activeIndex-1; j >= 0; j--) {
				_left += _liWidth[j];
			}
			$('li.'+_options.liBgClass, _obg)
				.animate({width:_activeAWidth +'px'},{queue:false,duration:200, easing:'linear'})
				.animate({
					left:_left+'px'
				},{queue:false,duration:_options.duration});
			_left = _pLeft;
	    }
	    jQuery.fn.moveNavigation.showThis = function(_activeIndex, _obg){
			var _activeLiWidth = _liWidth[_activeIndex];
			var _activeAWidth = _aWidth[_activeIndex];
			var _left = _pLeft;
			for (var j = _activeIndex-1; j >= 0; j--) {
				_left += _liWidth[j];
			}
			$('li.'+_options.liBgClass, _obg)
				.css({
					left:(_left+(_activeAWidth/2))+'px',
					width:0,
					opacity:0,
					display: 'block'
				})
				.animate({
					width:_activeAWidth +'px',
					opacity:1,
					left:_left+'px'
				},{queue:false,duration:_options.duration});
			_left = _pLeft;
	    }
	    jQuery.fn.moveNavigation.hideThis = function(_activeIndex, _obg){
			var _activeAWidth = _aWidth[_activeIndex];
			var _left = parseInt($('li.'+_options.liBgClass, _obg).css('left'));
			$('li.'+_options.liBgClass, _obg).fadeOut(200);
	    }
	    
	    if (_lis.filter('.current-menu-item').length) {
	    	var _lisIndex = _lis.index(_lis.filter('.current-menu-item'));
			jQuery.fn.moveNavigation.showThis(_lisIndex, _obg);
	    } else {
			jQuery(_obg).children('li.'+_options.liBgClass).css('display','none');
	    }
	    _lis.mouseenter(function(){
			_linkIndex = _lis.index(jQuery(this));
			_active.removeClass('current-menu-item');
			if (_timer) clearTimeout(_timer);
			if (_active.length || _visible) {
				jQuery.fn.moveNavigation.animateThis(_linkIndex, _obg);
			} else {
				jQuery.fn.moveNavigation.showThis(_linkIndex, _obg);
				_visible = true;
			}
	    }).mouseleave(function() {
			var _this = this;
			_linkIndex = _lis.index(jQuery(this));
			_timer = setTimeout(function() {
				_active.addClass('current-menu-item');
				if (_active.length) {
					var _lisIndex = _lis.index(_active);
					jQuery.fn.moveNavigation.animateThis(_lisIndex, _obg);
				} else {
				var _linksIndex = _links.index($(_this));
					jQuery.fn.moveNavigation.hideThis(_linksIndex, _obg);
					_visible = false;
				}
		}, _options.delay);
	    });
	    
    });
}

jQuery.fn.NavDropDown=function(j){
	var _style = (jQuery.browser.msie && jQuery.browser.version < 8) ? 'slide' : 'slide&fade';
	var j=jQuery.extend({
		duration:300,
		hoverElement:'li',
		dropHolder:'div.drop',
		hoverClass:'hover',
		showEffect:_style,
		timer : 50
	},j);
	
	return this.each(function(){
		var f=jQuery(this);
		var g=jQuery(j.hoverElement+':has('+j.dropHolder+')',f);
		var h=j.duration;
		var _time;
		g.each(function(i,a){
			a=jQuery(a);
			var b=$(j.dropHolder+' :first',a);
			var c=$(j.dropHolder,a);
			a.h=b.outerHeight();
			var d={};
			var e={};
			if(j.showEffect=='slide'){
				c.css({'height':0,'overflow':'hidden'});
				b.css({'marginTop':-a.h,'overflow':'hidden'});
				d.SE={height:a.h};
				d.ME={marginTop:0};
				e.SE={height:0};
				e.ME={marginTop:-a.h}
			}
			if(j.showEffect=='fade'){
				c.css({'opacity':0});
				d.SE={opacity:1};
				e.SE={opacity:0};
				d.ME={};
				e.ME={}
			}
			if(j.showEffect=='slide&fade'){
				c.css({'height':0,'overflow':'hidden','opacity':0});
				b.css({'marginTop':-a.h,'overflow':'hidden'});
				d.SE={height:a.h,opacity:1};d.ME={marginTop:0};
				e.SE={height:0,opacity:0};
				e.ME={marginTop:-a.h}
			}
			a.hoverEl=false;
			a.hover(function(){
				var ty = $(this);
				if(this.timer)clearTimeout(this.timer);
				_time = setTimeout(function(){
					a.hoverEl=true;
					ty.addClass(j.hoverClass);
					c.animate(d.SE,{queue:false,duration:h});
					b.animate(d.ME,{queue:false,duration:h})
				}, a.timer);
			},function(){
				if(_time) clearTimeout(_time);
				this.timer=setTimeout(function(){
					a.hoverEl=false;
					b.animate(e.ME,{queue:false,duration:h});
					c.animate(e.SE,{queue:false,duration:h,complete:function(){
						if(!a.hoverEl)$(a).removeClass(j.hoverClass)
					}})
				},a.timer)
			})
		})
	})
};


