var section = new String();
var current_section = new String();
var hovered=false;
var hovers = {
	about_us: 0,
	residential: 0,
	commercial: 0,
	media_center: 0,
	careers: 0,
	contact: 0
};
var sections = [
	{ name: "about_us", link: null, element: null, submenu: null, hovered: 0 },
	{ name: "residential", link: null, element: null, submenu: null, hovered: 0 },
	{ name: "commercial", link: null, element: null, submenu: null, hovered: 0 },
	{ name: "media_center", link: null, element: null, submenu: null, hovered: 0 },
	{ name: "careers", link: null, element: null, submenu: null, hovered: 0 },
	{ name: "contact", link: null, element: null, submenu: null, hovered: 0 }
];
var backdrop = null;

function preload() {
 	var images = new Array();
 	var dir = 'imagini/meniu/';
 	for (i=0;i<sections.length;i++) {
  		images[i] = new Image();
  		images[i].src = dir + sections[i].name + '_over.jpg';
 	}
}

function menu() {
	backdrop = $("<ul id=\"backdrop\"><li></li></ul>");
	for (var index = 0; index < sections.length; index++) {
		sections[index].element = $("ul#meniu_princ li#nav_" + sections[index].name).addClass("Dinamic");
		sections[index].link = sections[index].element.find(">a");
		sections[index].submenu = sections[index].element.find(">ul");
	}
	section = current_section = $("body").attr("id");
	var left = $("ul#meniu_princ li#nav_" + section + ">a").css("left");
	var width = $("ul#meniu_princ li#nav_" + section + ">a").css("width");
	var underline = $("<li id=\"underline\"></li>").css("left", left).css("width", width);
	var arrow_left = parseInt(left.replace("px", "")) + parseInt(width.replace("px", "")) / 2 - 4.5;
	var arrow = $("<li id=\"arrow\">&nbsp;<img src=\"/imagini/meniu/sageata.gif\" alt=\"CB\" />&nbsp;</li>").css("left", arrow_left);
	$("ul#meniu_princ").append(underline).append(arrow);

	hovered = false
	$("ul#meniu_princ").hover(function() {
		hovered = true;
	}, function() {
		hovered = false;
		setTimeout(go_home, 100);
	});
	$("ul#meniu_princ>li>a").hover(function() {
		var left = $(this).css("left");
		var width = $(this).css("width");
		$("#underline").stopAll().animate({
			left: left,
			width: width
		}, 700);
		var arrow_left = parseInt(left.replace("px", "")) + parseInt(width.replace("px", "")) / 2 - 4.5;
		$("#arrow").stopAll().animate({
			left: arrow_left
		}, 700,function(){
		show_section(hovered_section);});
		var hovered_section = $(this).parent().attr("id").replace("nav_", "");
		var now = new Date();
		hovers[hovered_section] = now.getTime();
		//setTimeout("show_section(\"" + hovered_section + "\");", 300);
	}, function() {
		var now = new Date();
		var hovered_section = $(this).parent().attr("id").replace("nav_", "");
		hovers[hovered_section] = now.getTime() + 700;
	});
}
function go_home() {
	if (!hovered){
	var left = $("ul#meniu_princ li#nav_" + section + ">a").css("left");
	var width = $("ul#meniu_princ li#nav_" + section + ">a").css("width");
	$("#underline").stopAll().animate({
		left: left,
		width: width
	}, 700);
	var arrow_left = parseInt(left.replace("px", "")) + parseInt(width.replace("px", "")) / 2 - 4.5;
	$("#arrow").stopAll().animate({
		left: arrow_left
	}, 700, function() { show_section(section, true); });
	}
}
function show_section(hovered_section, override) {
	if (typeof(override) == "undefinde") {
		override = false;
	}
	var now = new Date();
	if ((override == true && current_section != section) || (hovers[hovered_section] > 0
			&& hovered_section != current_section
			/*&& hovers[hovered_section] + 650 < now.getTime()*/)) {
		var current_submenu = $("ul#meniu_princ li#nav_" + current_section + " ul#submeniu_" + current_section);
		var hovered_submenu = $("ul#meniu_princ li#nav_" + hovered_section + " ul#submeniu_" + hovered_section);
		var current_section_index = 0;
		var hovered_section_index = 0;
		for (var index = 0; index < sections.length; index++) {
			if (sections[index].name == current_section) {
				current_section_index = index;
			} else if (sections[index].name == hovered_section) {
				hovered_section_index = index;
			}
			if (current_section_index > 0 && hovered_section_index > 0) {
				break;
			}
		}
		var duration = 200;
		if (hovered_section_index > current_section_index) {
			current_submenu.after(backdrop);
			current_submenu.SlideOutDown(duration);
			hovered_submenu.SlideInUp(duration, function() {
				$("#backdrop").remove();
				if (hovered_section != section || current_section != section) {
					current_section = hovered_section;
				}
			});
		} else {
			hovered_submenu.after(backdrop);
			current_submenu.SlideOutUp(duration);
			hovered_submenu.SlideInDown(duration, function() {
				$("#backdrop").remove();
				if (hovered_section != section || current_section != section) {
					current_section = hovered_section;
				}
			});
		}
	}
}