$(document).ready(function(){
	var $el, leftPos, newWidth,
	$mainNav2 = $("#mainNav");
	$mainNav2.append("<div id='magic-line-two'></div>");
	
	$magicLineTwo = $("#magic-line-two");
	$magicLineTwo.corner(8);
		origLeft =  $(".current_page_item_two a").position().left;
		origWidth = $magicLineTwo.width();
		origColor = $(".current_page_item_two a").attr("rel");
	
	
	$("#mainNav li a").each(function(index){
		$(this).attr("rel","#"+navColors[index]);
		
	});
	
	$(".drop-down").hover(function(){$(this).show()},function(){$(this).hide()});
	
	$(".submenu").each(function(index){
		
		var $dd = $(this).parents().eq(2); // drop-down
		var $pl = $dd.prev(); // link primario
		var colorOver = $(this).attr("rel");
		var colorOut = "#FFF";
		$(this).attr("rel",colorOver);
		$(this).hover(
			function(){
				$(this).css({color:colorOver});
				var corigLeft =  $pl.position().left;
				var corigWidth = $pl.width();
				var corigColor = $pl.attr("rel");
				$magicLineTwo.stop().animate({
					left: corigLeft,
					width: newWidth,
					backgroundColor: corigColor
				})
				//firebugConsole("info",$pl.width())	;
				
			},
			function(){
				$(this).css({color:colorOut})
				$magicLineTwo.stop().animate({
					left: origLeft,
					width: origWidth,
					backgroundColor: origColor
				})
				
			}
		);	
		$dd.css({position:"absolute",left:$pl.position().left+"px"});
		//$dd.offset({left:($pl.position().left)});
	});
	
	
	$("#mainNav li").find("a").not(".submenu").click(function(){
		if($(this).attr("href")!="#"){
			$o = $(this);
			$(".current_page_item_two").removeClass("current_page_item_two");
			$o.parent().addClass("current_page_item_two");
			
			origLeft =  $o.position().left;
			origWidth = $magicLineTwo.width();
			origColor = $o.attr("rel");
			
			subMenuContButton = false;
			
			//firebugConsole("info",origLeft+" "+origWidth+" "+origColor)
		}
	});
	
	$(".submenu").click(function(){
		
		//$(".current_page_item_two").removeClass("current_page_item_two");
		$o = $(this).parents().eq(3).find("a");
		//$o.parent().addClass("current_page_item_two");
		subMenuContButton = $(this).attr("rel");
		origLeft =  $o.position().left;
		origWidth = $magicLineTwo.width();
		origColor = $o.attr("rel");
		
		$magicLineTwo.stop().animate({
			left: leftPos,
			width: newWidth,
			backgroundColor: origColor
		})
		
	//	firebugConsole("info",origLeft+" "+origWidth+" "+origColor)	;
				
	});
	
	
	

	setProperties();			
	
	$("#mainNav li").find("a").not(".submenu").hover(function() {
			$el = $(this);
			leftPos = $el.position().left;
			newWidth = $el.parent().width();
		$magicLineTwo.stop().animate({
			left: leftPos,
			width: newWidth,
			backgroundColor: $el.attr("rel")
		})
			
			$el.next(".drop-down").show();
			//alert($el.attr("id").replace("gop","dd"))
			//firebugConsole("info",$el.index)	;
	//alert($el.next(".drop-down").attr("class"))
	
		
		$el.mouseout(function(){$el.next(".drop-down").hide()});
		
		//firebugConsole("info",newWidth)	;
		
	}, function() {
		if($(this).attr("href")=="#"){
			$el.mouseout(function(){$el.next(".drop-down").hide()});
		}
			$magicLineTwo.stop().animate({
				left: origLeft,
				width: origWidth,
				backgroundColor: origColor
			}); 
		
	});	

	
	$("#gop_1").click().hover();
	

});

function setProperties(){
	
	$magicLineTwo
		.width($(".current_page_item_two").width())
		.height($("#mainNav").height())
		.css("left", $(".current_page_item_two a").position().left)
}
