jQuery(function($){
	
	// MENU
	
	var menu = $("#menu ul.level1");
	var openMenu = null;
	
	function prepareSubMenu(me)
	{	
		var child = $(">ul", me);
		if(child.size() > 0)
		{
			var link = $(">a", me)
			child.hide();
			link.addClass("menu");

			function open(){
				child.show();
				me.addClass("active");
				link.addClass("active");
			}
			function close(){
				child.hide(); 
				me.removeClass("active");
				link.removeClass("active");
			}
			me.hover(open, close);
		
			$(">li", child).each(function(){
				prepareSubMenu($(this));
			});
		}
		else
		{
			me.hover(function(){me.addClass("active");},
							 function(){me.removeClass("active");});
		}
	}
	
	$("li.level1").each(function(){
		prepareSubMenu($(this));
	});
	
	
	// HEADERS
	
	// make first word of h3 orange
	$("h3").each(function(){
		var text = $(this).text(),
				fisrtWord = text.indexOf(" ");
		if(fisrtWord >= 0)
			$(this).html("<span>" + text.substr(0, fisrtWord) + "</span>" + text.substr(fisrtWord));
	});
	
	// HomePage Headers
	
	var headers = ['content-home-top-1', 'content-home-top-3', 'content-home-top-2'];
	var headersIndex = 0; // Math.floor(Math.random() * headers.length);
	
	function nextHeader()
	{
		headersIndex = (headersIndex + 1) % headers.length;
		$("#content-top").css("background-image", "url(" + location.href + "themes/usbsp/css/images/" + headers[headersIndex] + ".jpg)");
	}
	
	if ($("#content").hasClass("home-page")) 
	{
  		setInterval(nextHeader, 10000);
  	}
	
	// Search form
	
	var search = $("#SearchForm_SearchForm_Search");
	
	search
		.focus(function(){
			if(search.val() == "Search")
				search.val("");
		})
		.blur(function(){
			if(search.val() == "")
				search.val("Search");
		});
		
  // Colloms in home page
  
  var h1 = $("#sidebar.home-page").height();
  var h2 = $("div.hp-content-text-inner").height();
  if(h1 && h2 && (h2 < (h1 - 163))){
    $("div.hp-content-text-inner").height(h1 - 163);
  }
  
});
