function sfHover(parent_id) {
	var sfEls = document.getElementById(parent_id).getElementsByTagName("LI");
	for (var i=0; i<sfEls.length; i++) 
	{
		sfEls[i].onmouseover = function() {
			this.className += " sfhover";
		}
		sfEls[i].onmouseout = function() 	{
			this.className = this.className.replace(new RegExp(" sfhover\\b"), "");
		}
	}
}

if (window.attachEvent) {
	window.attachEvent("onload", function() { 
	//	sfHover("linkermenu"); sfHover("bovenmenu"); 
	});
}


function imgBovenMenu(sender, id, on, off)
{
	img = document.getElementById(id +'_img');
	img.src = img.src.replace(/_off/, '_on');
	
	sender.onmouseover = function() { img = document.getElementById(this.id +'_img'); img.src = img.src.replace(/_off/, '_on');	}
	sender.onmouseout = function()  { img = document.getElementById(this.id +'_img'); img.src = img.src.replace(/_on/, '_off');	}
}

function imgBovenMenuInit()
{
	ids = ['m_1', 'm_2', 'm_3', 'm_4', 'm_5', 'm_6', 'm_7']; 
	
	for(i=0; i<ids.length; i++)
	{			
		img = document.getElementById(ids[i] +'_img');
		
		img.onmouseover = function() 	{ 
			this.src = this.src.replace(/_off/, '_on');	
			
			menuitem = document.getElementById(this.id.replace("_img", "")).parentNode;
			menuitem.className += " sfhover";
		}
		
		img.onmouseout = function() 	{ 
			this.src = this.src.replace(/_on/, '_off');	
			
			menuitem_id = this.id.replace("_img", "");
			
			menuitem = document.getElementById(menuitem_id).parentNode;
			menuitem.className = menuitem.className.replace(/sfhover/g, "");
		}
	}
}


/*
function focusOpMenu()
{
	var vorige_a = false;
	
	document.onactivate = document.onfocus = document.onkeydown = document.onmouseover = document.onmouseout = document.onkeypress = function(e) 
	{ 
		var srcObj = e ? e.target : document.activeElement;
		
		if(srcObj.className && srcObj.className == "menuitem")
		{
			srcObj.parentNode.className += " hoverfocus"; 
			
			if(vorige_a)
				vorige_a.parentNode.className = vorige_a.parentNode.className.replace(new RegExp("hoverfocus\\b"), "");
			
			vorige_a = srcObj; 
		}
		else
		{
			try
			{
				a = srcObj.parentNode.parentNode.parentNode.getElementsByTagName('a')[0];
				
				console.log(a.className +  "   " + a.parentNode.className);
				
				if(a.className != "menuitem" && a.parentNode.className.indexOf('hoverfocus') == -1)
				{
					console.log('verbergen!');
					lis = document.getElementById('linkermenu').getElementsByTagName('li');
					for(i=0; i<lis.length; i++)
					{
						lis[i].className = lis[i].className.replace(new RegExp("hoverfocus\\b"), "")
					}
				}
				else
				{
					//a.parentNode.getElementsByTagName('ul')[0].style.left = '140px';
				}
			}
			catch(error)
			{
				console.log(error);

				lis = document.getElementById('linkermenu').getElementsByTagName('li');
				for(i=0; i<lis.length; i++)
				{
					lis[i].className = lis[i].className.replace(new RegExp("hoverfocus\\b"), "")
				}
			}
		}
	}
	
	as = document.getElementById('linkermenu').getElementsByTagName('a');
	var vorige_a = false;
	
	for(i=0; i<as.length; i++)
	{
		a = as[i];
	}

}
*/


(function($){
$.fn.reorder = function() {

  // random array sort from 
  // http://javascript.about.com/library/blsort2.htm
  function randOrd() { return(Math.round(Math.random())-0.5); }

  return($(this).each(function() {
    var $this = $(this);
    var $children = $this.children();
    var childCount = $children.length;

    if (childCount > 1) {
      $children.remove();

      var indices = new Array();
      for (i=0;i<childCount;i++) { indices[indices.length] = i; }
      indices = indices.sort(randOrd);
      $.each(indices,function(j,k) { $this.append($children.eq(k)); });

    }
  }));
} 
})(jQuery);


/******************/
/* Menu functions */
/******************/
window.Menu = {
	delay		: 1500,
	timer		: null,
	menuitem	: null,
	
	/**
	 * apply
	 * @param	string	selector
	 */
	apply: function( selector ) {
		$(selector).hover(Menu.open, Menu.setTimer)
		selector_subsub_li = '>li';
		selector_subsub_ul = '>ul';
		if($.browser.msie && $.browser.version=="6.0")
		{
			selector_subsub_li = 'li';	
			selector_subsub_ul = 'ul';	
			$(selector_subsub_li, selector).hover(
			function()
			{
			  	$(this).addClass('hover');
			},
			function()
			{
			 	$(this).removeClass('hover');
			})			
		}

		$(document).click(Menu.close);
		$('.menu_links_bg').hover(Menu.close);
	},
	
	/** 
	 * cancelTimer
	 */
	cancelTimer: function() {
		if(Menu.timer)	{
			clearTimeout(Menu.timer);
     		Menu.timer = null;
		}
	},
	
	/**
	 * setTimer
	 */
	setTimer: function() {
		Menu.timer = window.setTimeout(Menu.close, Menu.delay);
	},
		
	/** 
	 * close
	 * @param	string	current_menu_id
	 */
	close: function( current_menu_id ) {
		if(Menu.menuitem)	{
			if(Menu.menuitem.data("menuID") != current_menu_id)	
			{
				$(selector_subsub_ul, Menu.menuitem).css({ height: 'auto', zIndex: "" }).stop().hide();
				$(Menu.menuitem).removeClass("hover");
				//$(selector_subsub, Menu.menuitem).removeClass('hover');
			}
		}
	},
			
	/** 
	 * open
	 */
	open: function() {
		current_menu = $(this);
		
		current_menu.addClass("hover");
		//$('li', current_menu).hover( function() { $(this).addClass('hover'); })
		
		// uniek menu id per submenu, dit om bij het sluiten te checken of niet de actieve wordt gesloten
		if(!current_menu.data("menuID"))	{
			current_menu.data("menuID", (Math.random() +''+ Math.random()).replace(/\./g,""))
		}
		
		Menu.cancelTimer();
		Menu.close( current_menu.data("menuID") );
		Menu.menuitem = current_menu;
		
		$(selector_subsub_ul, Menu.menuitem).css({ height: 'auto', zIndex: 100 }).stop().show();	
	}

};

/** 
 * jquery.defaultvalue 
 * @param	string	defaultvalue
*/
$.fn.defaultvalue = function( defVal )
{
	return this.each(function()
	{
		var $input = $(this);
		if($input.val() == "" || $input.val() == defVal)
		{
			$input.addClass("defaultvalue").val(defVal);
		}
		
		$input
			.focus(function() {
				if($input.val() == defVal) 
					$input.val("").removeClass("defaultvalue");
			})
			.blur(function(){
				if($input.val() == "") 
					$input.addClass("defaultvalue").val(defVal);
			});
	});
};


$(function()
{
	/* submenu effectje en timeout */
	Menu.apply("#menu > li");
	
	// default value bij formulieren
	$("input:text[title], textarea[title]").each(function() { 
		$(this).defaultvalue( $(this).attr("title") );
	});
	
//	Menu.apply("#linkermenu li");
});



