var PRIMARY_SELECTION_GAP = 10;
var mouse_is_over_primary_selection = false;
var active_primary_li = null;
var active_secondary_li = null;
var primary_timer;
var secondary_timer;

function anim_primary_sel(li, time)
{
	var x  = li.offset().left;
	var w  = li.outerWidth();
	
	//we don't want animation on IE6
	if (!window.XMLHttpRequest) time = 0;
	
	if (time > 0) {
		$("#primary_selection").stop().animate({ left:x, width: w }, time);
		$("#primary_selection hr").stop().animate({ width:x, width: w-PRIMARY_SELECTION_GAP }, time);
	} else {
	  $("#primary_selection").css("left", x);
		$("#primary_selection").css("width", w);
		$("#primary_selection hr").css("width", w-PRIMARY_SELECTION_GAP);
	}
}

function anim_secondary_sel(li, time)
{
  //$("#secondary_menu").offset().top
	var y  =  li.offset().top + li.height()+4;
	
	//we don't want animation on IE6
	if (!window.XMLHttpRequest) time = 0;
	
	if (time > 0) {
		$("#secondary_selection").stop().animate({ top:y }, time);
	} else {
		$("#secondary_selection").css("top", y);
	}
}


function move_back_to_primary_default_if_needed(e)
{
	primary_timer = setTimeout ( function()
	{
		// Check if there is an active selection. If not, use the first li.
		if ( $("#primary_menu .active").length > 0 ) {
			var primary_selection = $("#primary_menu .active");
		}
		else {
			var primary_selection = $("#primary_menu").find("li:first");
		}
		if (!mouse_is_over_primary_selection) anim_primary_sel($(primary_selection), 100);
	}, 800 );
	e.stopPropagation();
}

function move_back_to_secondary_default_if_needed(e)
{
	secondary_timer = setTimeout ( function()
	{
		// Check if there is an active selection. If not, use the first li.
		if ( $("#secondary_menu .active").length > 0 ) {
			var secondary_selection = $("#secondary_menu .active");
		}
		else {
			var secondary_selection = $("#secondary_menu").find("li:first");
		}
		anim_secondary_sel(secondary_selection, 100);
	}, 800 );
	e.stopPropagation();
}

function init_primary_menu()
{
	$("#primary_menu_line").before("<div id='primary_selection'><span class=\"left-white\">&nbsp;&nbsp;&nbsp;</span><span class=\"right-white\">&nbsp;&nbsp;&nbsp;</span></div>");
	$("#primary_selection").css("top", $("#primary_menu_line").offset().top);
	$("#primary_menu li").mouseenter(function(e){	
		var target = $(e.target);
		if (target.is('a')) target = target.parent();
	
	  if (primary_timer) {
	    clearTimeout(primary_timer);
	    primary_timer = null;
	  }
	  	
		anim_primary_sel($(this), 100);
		e.stopPropagation();
	});
	
	// Check if there is an active selection. If not, use the first li.
	if ( $("#primary_menu .active").length > 0 ) {
		var primary_selection = $("#primary_menu .active");
	}
	else {
		var primary_selection = $("#primary_menu").find("li:first");
	}
	
	$("#primary_menu li").mouseleave(move_back_to_primary_default_if_needed);

	$('#primary_selection').mouseenter(function(e) {
		mouse_is_over_primary_selection = true;});
		$("#primary_selection").mouseleave(function(e){
			mouse_is_over_primary_selection = false;
//			move_back_to_primary_default_if_needed(e);
		});
	anim_primary_sel($(primary_selection), 0);
	$(window).bind('resize', function() { anim_primary_sel($(primary_selection), 0); });
}

function init_secondary_menu()
{
  $("#secondary_menu li").click(function() {
    window.location=$(this).find("a").attr("href"); return false;
  });
  
  var m = $("#secondary_menu");
  var x = m.offset().left;
  var y = m.offset().top;
//  m.css("position", "absolute");
	m.css("z-index", 5);  
	
	
	$("#secondary_menu").before("<div id='secondary_selection'></div>");
	
	$("#secondary_selection").css("z-index", 1);
	// Check if there is an active selection. If not, use the first li.
	if ( $("#secondary_menu .active").length > 0 ) {
		var secondary_selection = $("#secondary_menu .active");
	}
	else {
		var secondary_selection = $("#secondary_menu").find("li:first");
	}
	var p = parseInt(secondary_selection.css("padding-left"))-5;
	$("#secondary_selection").css("padding-left", p);
	$("#secondary_selection").width(secondary_selection.width()+p);
	
	$("#secondary_menu li:not(#secondary_selection)").mouseenter(function(e)
	{
		var target = $(e.target);
		if (target.is('a')) target = target.parent();
		
		if (secondary_timer) {
			clearTimeout(secondary_timer);
			secondary_timer = null;
		}
	
		anim_secondary_sel($(this), 100);
		e.stopPropagation();
	});
	
	$("#secondary_menu li").mouseleave(move_back_to_secondary_default_if_needed);
	
	$('#secondary_selection').mouseenter(function(e) { mouse_is_over_secondary_selection = true;});
	$("#secondary_selection").mouseleave(function(e)
	{
		mouse_is_over_secondary_selection = false;
//		move_back_to_secondary_default_if_needed(e);
	});
	
	anim_secondary_sel(secondary_selection, 0);
}



$(document).ready(function ()
{	
	//we wait for the dom to settle before we put selection line onto line
	setTimeout ( function()   
	{
		init_primary_menu();
		init_secondary_menu();
	}, 150);
});
