
$(document).ready(function () {
	
	$("h1").each(function (k, v) {
		$(v).replaceWith('<div class="h1_l"><div class="h1_r"><h1 class="h1_c">' + $(v).text() + '</h1></div></div>');
	});

	$(".pstyle1").each(function (k, v) {
		$(v).mouseenter(function () {
			$(".pstyle1_tl_norm", this).stop().animate({opacity: 0});
			$(".pstyle1_tl_sel", this).stop().animate({opacity: 1});
			$(".pstyle1_bl_norm", this).stop().animate({opacity: 0});
			$(".pstyle1_bl_sel", this).stop().animate({opacity: 1});
		});
		$(v).mouseleave(function () {
			$(".pstyle1_tl_norm", this).stop().animate({opacity: 1});
			$(".pstyle1_tl_sel", this).stop().animate({opacity: 0});
			$(".pstyle1_bl_norm", this).stop().animate({opacity: 1});
			$(".pstyle1_bl_sel", this).stop().animate({opacity: 0});
		});
	});
	
	document.menu_hdm = null;
	document.menu_ddm = null;
	document.menu_state = 0;
	document.menu_hide_timer = 0;
	$(".menu_w").each(function (k, v) {
		// example id: link_products >> menu_products
		if (v.id) {
			var mname = "#menu_" + v.id.substr(5);
			var menu = $(mname);
			$("body").append(menu);
			menu.css({position: "absolute", display: "none", overflow: "hidden"});
			$(v).attr({
				menu_name: mname,
				menu_width: menu.width(),
				menu_height: menu.height()
			});
			
			$(".menu_dd_cnt", menu).each(function (k, v) {
				$(v).css({
					width: menu.width() - 60,
					overflow: "hidden"
				});
			});
			
			$(v).mouseenter(function () {
				var hd = $(this);
				var dd = $(hd.attr("menu_name"));

				if (document.menu_ddm == dd && document.menu_state == 1)
					return;

				if (document.menu_hide_timer > 0) {
					clearTimeout(document.menu_hide_timer);
					document.menu_hide_timer = 0;
				}

				if (document.menu_ddm == dd) {
					document.menu_ddm.queue("fx", []);						
					document.menu_ddm.stop();						
				}
				else {
					if (document.menu_ddm != null) {
						document.menu_hdm.removeClass("menu_w_hover");
						document.menu_ddm.queue("fx", []);						
						document.menu_ddm.stop();						
						document.menu_ddm.hide();
					}

					var pos = hd.offset();
					dd.css({left: pos.left - 8, top: pos.top + hd.height(), width: hd.attr("menu_width") / 2, height: hd.attr("menu_height") / 2, display: "block", opacity: 0});
				}

				document.menu_hdm = hd;
				document.menu_ddm = dd;
				document.menu_state = 2;
				document.menu_hdm.addClass("menu_w_hover");

				dd.animate({opacity: 1, width: hd.attr("menu_width"), height: hd.attr("menu_height")});
				dd.queue(function () {
					document.menu_state = 1;
					$(this).dequeue();
				});
			});
			
			$(v).mouseleave(function () {
				document.menu_hide_timer = setTimeout('hide_menu()', 500);
			});

			$(menu).mouseenter(function () {
				if (document.menu_hide_timer > 0) {
					clearTimeout(document.menu_hide_timer);
					document.menu_hide_timer = 0;
				}

			});

			$(menu).mouseleave(function () {
				document.menu_hide_timer = setTimeout('hide_menu()', 500);
			});
		}
		else {
			$(v).mouseenter(function () {
				if (document.menu_ddm != null) {
					document.menu_hdm.removeClass("menu_w_hover");
					document.menu_hdm = null;
					document.menu_ddm.queue("fx", []);						
					document.menu_ddm.stop();						
					document.menu_ddm.hide();
					document.menu_ddm = null;
				}
				
				$(this).addClass("menu_w_hover");
			});
			
			$(v).mouseleave(function () {
				$(this).removeClass("menu_w_hover");
			});
		}
	});
	
});

function hide_menu() {
	if (document.menu_ddm) {
		document.menu_ddm.animate({opacity: 0});
		document.menu_ddm.queue(function () {
			if (document.menu_hdm) {
				document.menu_hdm.removeClass("menu_w_hover");
				document.menu_hdm = null;
			}
			
			document.menu_ddm = null;
			document.menu_hide_timer = 0;
			
			$(this).hide();
			$(this).dequeue();
		});
	}
}


