/*

 * jQuery animationNav v1.1.1 

 *

 * Copyright (c) 2008 Taranets Aleksey

 * www: markup-javascript.com

 * Licensed under the MIT License:

 * http://www.opensource.org/licenses/mit-license.php

 */



jQuery.fn.animationNav = function(_options){

    // defaults options	

    var _options = jQuery.extend({

	    duration:1300,

	    addBG:'<li class="bg"><a href="#"><span>&nbsp;</span></a></li>',

	    bgClass :'bg',

		animEffect: 'easeOutBack'

    },_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('.active'));

	    var _timer = false;

	    var _pLeft = parseInt(jQuery(_obg).css('paddingLeft'));

	    var _visible = false;

	    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.animationNav.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];

			}

			$('li.bg', _obg)

				.stop()

				.animate({width:_activeAWidth +'px'},{queue:false,duration:150, easing:'linear'})

				.animate({

					left:_left+'px'

				},{queue:false,duration:_options.duration, easing:_options.animEffect});

			_left = _pLeft;

	    }

	    jQuery.fn.animationNav.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.bg', _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, easing:_options.animEffect});

			_left = _pLeft;

	    }

	    jQuery.fn.animationNav.hideThis = function(_activeIndex, _obg){

			var _activeAWidth = _aWidth[_activeIndex];

			var _left = parseInt($('li.bg', _obg).css('left'));

			$('li.bg', _obg).fadeOut(200);

	    }

	    

	    if (_lis.filter('.active').length) {

	    	var _lisIndex = _lis.index(_lis.filter('.active'));

			jQuery.fn.animationNav.animateThis(_lisIndex, _obg);

	    } else {

			jQuery(_obg).children('.'+_options.bgClass).css('display','none');

	    }

    

	    _links.hover(function(){

			_linkIndex = _links.index(jQuery(this));

			if (_timer) clearTimeout(_timer);

			if (_lis.filter('.active').length || _visible) {

				jQuery.fn.animationNav.animateThis(_linkIndex, _obg);

			} else {

				jQuery.fn.animationNav.showThis(_linkIndex, _obg);

				_visible = true;

			}

	    }, function() {

			var _this = this;

			_timer = setTimeout(function() {

				if (_lis.filter('.active').length) {

					var _lisIndex = _lis.index(_lis.filter('.active'));

					jQuery.fn.animationNav.animateThis(_lisIndex, _obg);

				} else {

					var _linksIndex = _links.index($(_this));

					jQuery.fn.animationNav.hideThis(_linksIndex, _obg);

					_visible = false;

				}

			}, 800);

	    });

	    

    });

}



$(document).ready(function(){

    $('ul.mainNav').animationNav();

});


