$(document).ready(function(){

	var search_val = $("#search").val();

	$("#search").focus( function(){
		if($(this).val() == search_val){
			$(this).val("");
		}
	});
	$("#search").blur( function(){
		if($(this).val() == ''){
			$(this).val(search_val);
		}
	});
	$("input.search_btn[type=submit]").click( function(){
		var s = $("#search");
		if( s.val() == search_val ){
			s.val("");
			s.focus();
			return false;
		}
	});


	jQuery.fn.fadeToggle = function(speed, easing, callback) {
		return this.animate({opacity: 'toggle'}, speed, easing, callback);
	};

	var archive_limit = 10;

	//if news archive page collapse this long news list on the left
	if( typeof(news_archive) != 'undefined' && news_archive == 1 )
	{
		var archive_count = $("ul.mnu_container li").length;
		if( archive_count > archive_limit )
		{
			$("ul.mnu_container li").each(function(i){
				if( i > archive_limit ){
					$(this).addClass("hidden");
				}
			});

			var a = $('<a href="">'+(archive_limit+1)+' - '+archive_count+' &#x203A;</a>').click(function(){ //&#x2026;
					$("ul.mnu_container li:hidden").fadeToggle(1000);
					$("ul.mnu_container li.more").remove();
					return false;
			});
			$('<li class="more"></li>').appendTo("ul.mnu_container").append(a);
		}
	}


	//graph menu
	Menu = {
		ctime: null,
		timer: [],
		log:  function(s){
			//console.debug(s);
		},
		init: function()
		{
			//bind other (non expandable menus to hide expanded menu on hover)
			$('.stripe2 a:not([id])').mouseover(function(){
				$('.stripe2 a[id]').each(function(){
					Menu.hideMenu( $(this).attr('id').replace('a-','') );
				});
			});

			//find expandable menu and bind events
			$('.stats-menu').each(function(){

				var name = $(this).attr('id').replace('menu-','');

				var a = $('#a-'+name);
				var menu = $(this);

				if( a[0] )
				{
					a.hover(
						function(){
								
							Menu.log('a in');

							if(Menu.timer[name]){
								clearTimeout(Menu.timer[name]);
							}

							//hide other menus
//							$('.stats-menu:not(#menu-'+name+')').hide();
							$('.stats-menu:not(#menu-'+name+')').each(function(){
								Menu.hideMenu( $(this).attr('id').replace('menu-','') );
							});


							var pos = $(this).offset();

							var x = pos.top + $(this).outerHeight({ margin: true }); // - 5;
							var y = pos.left; //+1 border
							if( $.browser.mozilla ){
								y += 1;
							}

							a.addClass('menu');
							menu.css( {top:x, left:y} ).fadeIn();


						},
						function(){

							Menu.log('a out');

							Menu.tryHide(name);
						}			
					);

					menu.hover(
						function(){
							Menu.log('menu in');

							$(this).addClass('hovered');
						},
						function(){
							Menu.log('menu out');

							$(this).removeClass('hovered');
							Menu.tryHide(name);
						}
					);
				}
			});

		},
		tryHide: function(name)
		{
			if(Menu.timer[name]){
				clearTimeout(Menu.timer[name]);
			}
			Menu.timer[name] = setTimeout("Menu.hideMenu('"+name+"')", 500);
		},
		hideMenu: function(name)
		{
			var a = $('#a-'+name);
			var menu = $('#menu-'+name);
			if( menu.hasClass('hovered') == false ){
				a.removeClass('menu');
				menu.hide();
			}else{
				Menu.tryHide(name);
			}
		}
	}

	Menu.init();

});