var m_on = false;
$(document).ready(function() {
  // load the nav
	var current = $(".current").children("a");
	var menu_on = false;
	var menu_item = false;
	var current_on = false;
	var current_menu = false;
	if ($("#nav")) {
		$("#nav li").mouseover(	function(e) {
			if(menu_on) clearTimeout(menu_on);
			if(menu_item) clearTimeout(menu_item);
			if(current_on) clearTimeout(current_on);
			if(current_menu) clearTimeout(current_menu);

			//HIDE CURRENT MENU
			current.children("ul").filter(":visible").hide();
			current.removeClass("over");
			$(this).siblings("li").removeClass("current");
		});
		$("#nav li").mouseout(	function(e) {
			//alert("1");
			if(menu_on) clearTimeout(menu_on);
			if(menu_item) clearTimeout(menu_item);


		});
		$("#nav>ul>li").mouseover( function(e) {
			if(menu_on) clearTimeout(menu_on);
			if(menu_item) clearTimeout(menu_item);
			if(current_on) clearTimeout(current_on);
			if(current_menu) clearTimeout(current_menu);
			//HIDE OPEN MENUS
			$(this).siblings("li").children("ul").filter(":visible").hide();
			$(this).siblings("li").children("a").removeClass("over");

			//ADD ROLLOVER
			$(this).children("a").addClass("over");

			//SHOW SUBMENU
			$(this).children("ul").show();
		});
		$("#nav>ul>li").mouseout(function(e) {

				//alert("2");
			//CLEAR ROLLOVER
			//menu_item = $("#nav>ul>li").children("a").removeClass("over");
			menu_item = setTimeout('$("#nav>ul>li").children("a").removeClass("over")', 500);
			//CLEAR SUBMENU
			
			//menu_on = $("#nav>ul>li").children("ul").filter(":visible").hide();
			menu_on = setTimeout('$("#nav>ul>li").children("ul").filter(":visible").hide()', 500);
	  	});
		$("#nav>ul>li li").mouseover(function(e){

			if(menu_item) clearTimeout(menu_item);
			if(menu_on) clearTimeout(menu_on);
			if(current_on) clearTimeout(current_on);
			if(current_menu) clearTimeout(current_menu);
			//SHOW SUBMENU
			$("ul", $(this)).filter(":hidden").show();
		});
		$("#nav>ul>li li").mouseout( function(e){
			if(menu_item) clearTimeout(menu_item);
			if(menu_on) clearTimeout(menu_on);
			if(current_on) clearTimeout(current_on);
			if(current_menu) clearTimeout(current_menu);
			$("ul", $(this)).filter(":visible").hide();
		});
	}

	// load the search onfocus/blur events
	if (search_el = $("#search")) {
		search_el.focus(function(e) {
			$(this).val("")
		});
		search_el.blur(function(e) {
			if ($(this).val() == "") $(this).val("search")
		});
		search_el.val("search");
	}
	
	if($("#sidebar-map")) {
  		mapper = {
  			map_timeouts: [],
  			link_id: false,
  			hide: function(link_id) {
  				$(link_id).hide();
  				mapper.map_timeouts = [];
  				mapper.link_id = false;
  			}
  		}
  		$("#sidebar-map a.mapper").mouseover( function(e) {
  			mapper.link_id = '#' + this.id + '-insert-map';
  			if($(mapper.link_id).length > 0) {
  				$(mapper.link_id).show();
  				$(mapper.link_id).bind("mouseover", function(e) {
  					if (!mapper.link_id) mapper.link_id = this.id
            jQuery.each(mapper.map_timeouts, function() {
              clearTimeout(this);
            });
            mapper.map_timeouts = [];
  					$(mapper.link_id).show();
  				});
          $(mapper.link_id).bind("mouseout", function(e) {
           mapper.map_timeouts.push(setTimeout('mapper.hide("' + mapper.link_id + '");', 1500));
          });
  		}
  	  });  
  	}
	
	if($(".operations").length > 0) {
		// if the function argument is given to overlay, it is assumed to be the onBeforeLoad event listener
		$(function() { 
		    // if the function argument is given to overlay, 
		    // it is assumed to be the onBeforeLoad event listener 
		    $("a[rel]").overlay({
	            expose: '#333', 
	            opacity: 0.7, 
	            closeSpeed: 1000,
		        onBeforeLoad: function() { 
		            // grab wrapper element inside content 
		            var wrap = this.getContent().find("div.wrap");
	                wrap.load(this.getTrigger().attr("href"));
		        }
		    }); 
		});
		$(".block-header a").bind("mouseover",function() {
			link_el = this.id;
			link_array = link_el.split('-');
			map_td = link_array[0] + '-' + link_array[1];
			$("#"+map_td).addClass("over");
		});

		$(".block-header a").bind("mouseout", function() {
			link_el = this.id;
			link_array = link_el.split('-');
			map_td = link_array[0] + '-' + link_array[1];
			$("#"+map_td).removeClass("over");
		});
	}
});

