$(document).ready(function(){

	$('#menu ul').bind("mouseover", bindMouseOver);
	$('#menu img').bind("mouseover", bindMouseOver);
	
	//$('#menu il').bind("mouseover", bindMouseOver);
});


bindMouseOver = function(e) {
		closeAll();

	var to = e.toElement || e.originalTarget;
//	console.log('TO = '+to.nodeName +' id: '+to.id);
	if(to.nodeName=='IMG'){
	var to = to.parentNode;
	}

	var submenu = $('#'+to.id+'_sub');
	if (submenu.length===1) {

		var offset = $('#'+to.id).offset();
		
		submenu.css("top",(offset.top+37)+"px");
		submenu.css("left",offset.left+"px");
		
		submenu.show();
		
		$('#'+to.id+'_sub').bind("mouseleave", bindMouseOutSubmenu);
	
	}
}

closeAll =function (){
	$('div.submenu').hide();
}

bindMouseOutSubmenu = function(e) {
	var to = e.relatedTarget || e.toElement;

	if ($(to).hasClass('partoff')===false) {
		closeAll();
	}

}


previousDriver = function() {
	var elements = $('#driver_index li');
	var total = elements.length;
	var found = false;
	
	if ( $(elements[0]).css("display") !== "list-item") {
	
		elements.each(function(i) {
			
			var display = $(this).css("display");
			var task = "";
			

			
			if (found===false) {
				if ((display==='none') && ( ($(elements[i+1]).css("display")==="list-item") || ($(elements[i+1]).css("display")==="block") ) ) {
					task = "list-item";
					$(this).show();
					found=i+3;
				}	
			}


			if (found==i) {
				$(this).hide();
				task = "none";
			}

	
		});
	
	}
}

openSubmenu = function(obj) {

	var submenu = $('#'+obj.id+'_sub');
	if (submenu.length===1) {
	
		var offset = $(obj).offset();
		
		submenu.css("top",(offset.top+37)+"px");
		submenu.css("left",offset.left+"px");
		
		submenu.show();
		
	
	}

}

closeSubmenu = function (obj) {
	console.log(obj.relatedTarget);
	event = obj || window.event;
	console.log(window.event);
	$('#'+obj.id+'_sub').hide();
} 

nextDriver = function() {
	var elements = $('#driver_index li');
	var total = elements.length;
	var found = false;
	
	elements.each(function(i) {
		
		var display = $(this).css("display");
		var task = "";


		if (found===false) {
			if ( ((display==='list-item') || (display==='block')) && ((i+3)<total) ) {
				task = "none";
				$(this).hide();
				found=i+3;
			}
		}
		
		if (found===i) {
			$(this).show();
			task = "list-item";
		}
		


	});

}

function setActive(id){
	var elements = $('#menu li');
	var total = elements.length;
	var found = false;
	
	elements.each(function(i) {	
		var display = $(this).css("display");
		var task = "";
		if (found===false) {
			if ( ((display==='list-item') || (display==='block')) && ((i+3)<total) ) {
				task = "none";
				$(this).hide();
				found=i+3;
			}
		}
		
		if (found===i) {
			$(this).show();
			task = "list-item";
		}
	});
	//alert('wacht even');
}