/*
 * jQuery moveNavigation v1.1.0 
 *
 * Copyright (c) 2008 Taranets Aleksey
 * email: aleks_tar@ukr.net
 * Licensed under the MIT License:
 * http://www.opensource.org/licenses/mit-license.php
 */

jQuery.fn.moveNavigation = function(_options){
	// defaults options	
	var _options = jQuery.extend({duration:500,addBG:'<li class="current"><span>&nbsp;</span></li>',liBgClass:'current',delay:100},_options);
	
	return this.each(function(){
		var _obg = jQuery(this);
		var _liWidth = [];
		var _aWidth = [];
		var _lis = jQuery(_obg).children('li');
		var _links = jQuery(_obg).find('a');
		var _linkIndex = _links.index(_links.filter('.current-menu-item'));
		var _timer = false;
		var _pLeft = parseInt(jQuery(_obg).css('paddingLeft'));
		var _visible = false;
		var _active = _lis.filter('.current-menu-item');
		jQuery(_lis).filter(':last-child').css('background','none');
		
		_lis.each(function(i, el){
			_liWidth[i] = jQuery(el).outerWidth(true);
			_aWidth[i] = jQuery(el).width();
		});
		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;
			for (var j = _activeIndex-1; j >= 0; j--) {
				_left += _liWidth[j];
			}
			jQuery('li.'+_options.liBgClass, _obg)
				.animate({width:_activeAWidth +'px'},{queue:false,duration:200, easing:'swing'})
				.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];
			}
			jQuery('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(jQuery('li.'+_options.liBgClass, _obg).css('left'));
			jQuery('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.animateThis(_lisIndex, _obg);
		} else {
			jQuery(_obg).children('li.'+_options.liBgClass).css('display','none');
		}
		_links.hover(function(){
			_linkIndex = _links.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;
			}
		}, function() {
			var _this = 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(jQuery(_this));
					jQuery.fn.moveNavigation.hideThis(_linksIndex, _obg);
					_visible = false;
				}
		}, _options.delay);
		});
	});
}
jQuery.noConflict();
jQuery(window).load(function(){jQuery('#nav').moveNavigation();});
