jQuery.fn.animationNav = function(_options){
	// defaults options	
	var _options = jQuery.extend({
		duration:1300,
		addBG:'<div class="bg-gal-tab"><a href="#"><span>&nbsp;</span></a></div>',
		bgClass :'bg',
		animEffect: 'easeOutBack'
	},_options);

	return this.each(function(){
		var _obg = jQuery(this);
		var _liWidth = [];
		var _aWidth = [];
		var _lis = jQuery(_obg).find('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'); */
		if($('div.bg-gal-tab'))
		_lis.each(function(i, el){
			_liWidth[i] = jQuery(el).outerWidth(true);
			_aWidth[i] = jQuery(el).width();
		});
		
		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];
			}
			$('div.bg-gal-tab', _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];
			}
			$('div.bg-gal-tab', _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($('div.bg-gal-tab', _obg).css('left'));
			$('div.bg-gal-tab', _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(){
	$('div.gallery-bar').animationNav();
});

