(function($) {
	$.fn.movingArrow = function(options){
		// default configuration properties
		var defaults = {
			arrow:			'arrow',
			active:			'active',
			secondNav:		'second_nav',
			toggleDuration: 500,
			moveDuration:	500,
			delay:			100
		}; 
		
		var options = $.extend(defaults, options);

		this.each(function() {  
			var arrow;
			var arrowYVisible;
			var arrowYHidden;
		
			function getActiveLink() {
				return $('.'+options.active).children('a');
			}
			function getSecondNavOfLink(a) {
				return $(a).parent().children('.'+options.secondNav);
			}
			function getParentLinkOfSecondNav(snav) {
				return $(snav.parents('li').get(1)).children('a');
			}
			function showSecondNavOfLink(a) {
				getSecondNavOfLink(a).show();
			}
			function hideSecondNavOfLink(a) {
				getSecondNavOfLink(a).hide();
			}
			function hideOtherSecondNavs(a) {
				$('.'+options.secondNav).not($(a).parent().find(options.secondNav)).hide();
			}
			function hideSecondNavOfLinkIfNotActive(a) {
				if(!a.parent('li').hasClass(options.active)) {
					hideSecondNavOfLink(a);
				}
			}
			function getLeftFromOffset(x) {
				var offcut = arrow.offset().left - arrow.position().left;
				return x - offcut;
			}
			function moveArrow(x,callback) {
				var left = getLeftFromOffset(x);
				if(arrow.position().left != left)
				{
					arrow
						.stop(true, false)
						.animate(
							{
								left:left+"px"
							},
							{
								duration:options.moveDuration, 
								easing:"swing", 
								complete:callback
							}
						);
				} else
				{
					if(callback)
						callback();	
				}
			}
			function moveArrowToLink(a) {
				var callback = function(){
					showSecondNavOfLink(a);
				};
				
				var x = a.offset().left;
						
				if(getActiveLink().length == 0)
				{
					showArrow(x,function(){ 
						moveArrow(x,callback); 
					});
				}
				else {
					moveArrow(x,callback); 
				}
			}
			function resetArrow() {
				var a = getActiveLink();
				if(a.length > 0)
				{
					moveArrow(a.offset().left,function(){
						showSecondNavOfLink(a);
					});
				}
				else {
					hideArrow();
				}
			}
			function showArrow(x,callback) {
				var a_div = arrow
					.stop(true, false)
					.show()
					.find('div')
				var y = a_div.position().top;
				
				// move arrow to x, if it is hidden
				if(y == arrowYHidden)
				{
					var offcut = arrow.offset().left - arrow.position().left;
					x = x - offcut;
					arrow.css('left',x+'px');
				}
				
				// show arrow, if its not completely visible
				if(y != arrowYVisible) 
				{
					a_div
						.stop(true, false)
						.animate(
							{
								top:arrowYVisible+"px"
							},
							{
								duration:options.toggleDuration, 
								easing:"swing",
								complete:callback
							});
				} 
				else 
				{
					callback();	
				}
			}
			function hideArrow(callback) {
				arrow
					.stop(true, false)
					.find('div')
					.stop(true, false)
					.animate(
					{
						top:arrowYHidden+"px"
					},
					{
						duration:options.toggleDuration,
						easing:"swing",
						complete:callback
					});
			}
			
			
			var obj = $(this);
			
			arrow = $('.'+options.arrow);
			
			// choose an arrow
			arrow = arrow.filter(':visible');
			if(arrow.length > 0) // an arrow is visible, use this one
			{
				arrow
					.parent('li')
						.addClass(options.active);
			} else // no arrow is visible, use first arrow
			{
				arrow = $('.'+options.arrow+':first');
				arrow
					.show();
					
				arrowYVisible = arrow.find('div').position().top;
				arrowYHidden = -(arrow.find('div').height());
				
				arrow
					.find('div')
						.css('top', arrowYHidden+'px');
			}
			
			// remove all other arrows
			$('.'+options.arrow+':hidden').remove();
			
			var timeout; // Timeout for arrow reseting and second nav hiding
			
			//  MAIN NAV
			obj.children('ul').children('li').children('a').each(function(){
				var a = $(this);
				a.mouseover(function(){
					clearTimeout(timeout);
					hideOtherSecondNavs(a);
					moveArrowToLink(a);
				});
				a.mouseout(function(){
					timeout = setTimeout(function() {
						hideSecondNavOfLinkIfNotActive(a);
						resetArrow();
					}, options.delay);
				});
			});
			
			// SECOND NAV
			$('.'+options.secondNav).find('a').mouseover(function(){
				clearTimeout(timeout);
			});
			$('.'+options.secondNav).find('a').mouseout(function(){
				var a = getParentLinkOfSecondNav($(this));
				if(!a.parent('li').hasClass(options.active)){
					timeout = setTimeout ( function() {
						hideSecondNavOfLink(a);
						resetArrow();
					}, options.delay );
				}
			});			
		});
	};
})(jQuery);




