$(document).ready(function () {
	// Created By D George Scapin (g@thescapins.com)
	// 
	// There are 3 primary div's involved.  There is a menu div, a menuback div, and the menuitems div
	// The connection is that a user hovers over the menu div and the menuback displays with the menuitems inside the 5th 
	// child div.  The menuback is responsible for the background shell of the menuitems.
	//
	// Upon init, a generic menuback is cloned and the menuitems are stuck inside.  
	//
	// Licensing : None of the following code can be used, reproduced, or copied without written permission 
	// from the author.
	//
	// HISTORY
	// -------
	// 2009-10-29 DGS Initial Creation
	
	$.ajaxSetup({cache: false});	
	
	// if multiple menus, need to do this for each one... if many, maybe a for loop.
	cMenuBack = $("#menu_item1_back").clone().attr("id", "menu7_sub1_back");	
	$('body').append(cMenuBack);	
	$("#menu7_sub1").append("<div class='my-last-child'> </div>");
	$("#menu7_sub1").appendTo("#menu7_sub1_back .menuback5");

	// set width and height of all tops and sides for menu shell
	$('.menu_item_back .menuback2, .menu_item_back .menuback8').each(function(i){
		$(this).width($(this).siblings(".menuback5").width()); 
	});

	$('.menu_item_back .menuback4, .menu_item_back .menuback6').each(function(i){
		$(this).height($(this).siblings(".menuback5").height()); 
	});
		
	// hide all menu and init need2hide var
	$(".menu_item_back").css("display","none");	
	$(".menuback .menu_item").data("need2hide",1);
	
    $(".menuback .menu_item").hover(
      function () {       
		$(this).css("background", "url('/template/template4_images/menu_item_back_hover3.jpg') no-repeat center");
		$(this).children("a").css("background", "url('/template/template4_images/menu_item_back_a_hover.jpg') repeat-x bottom");		
      }, 
      function () {
        $(this).css("background","");
		$(this).children("a").css("background", "");
      }
    );
	
    $(".menuback .menu_item, .menu_item_back").mouseenter(function () { 
		cMenuId = $(this).attr("id").replace("_sub","_item").replace("_back",""); 		
		if (cMenuId != ''){
			cMenuId = "#"+cMenuId;
			$("body").stopTime();		
			$(cMenuId).data("need2hide",0);
			$("body").oneTime(250, function(){
				$(cMenuId).checkHide();
			});
		}
	});
		
	$(".menuback .menu_item, .menu_item_back").mouseleave(function () {
		cMenuId = $(this).attr("id").replace("_sub","_item").replace("_back",""); 
		if (cMenuId != ''){
			cMenuId = "#"+cMenuId;
			$(cMenuId).data("need2hide",1);
			$("body").oneTime(750, function(){
				$(cMenuId).checkHide();
			});				
		}
	});

	$.fn.checkHide = function() {  			
		$('.menu_item_back').each(function(){			
			lNeed2Hide = $(this).getMenu().data("need2hide");
			lNeed2Hide = lNeed2Hide || (lNeed2Hide == undefined);
			if (lNeed2Hide){
				if ($(this).css("display") != "none") {
					$(this).slideUp(150);
				}
			} else {
				if ($(this).css("display") == "none") {
					cLeft = $(this).getMenu().offset().left;
					cTop = $(this).getMenu().offset().top + $(this).getMenu().height() - 1 + parseInt($(this).getMenu().css("padding-top").replace("px",""));	
					
					$(this).css({"z-index":"999999","position":"absolute","left":cLeft+"px","top":((((jQuery.browser.msie) && (jQuery.browser.version == "7.0")) ? 3 : 0) + cTop)+"px"}); 					
					$(this).slideDown(250);
				}
			}
		});				
	}; 

	$.fn.getMenu = function() {
		return $("#"+$(this).attr("id").replace("_sub","_item").replace("_back",""));
	};
	
	$.fn.getMenuBack = function() {
		return $("#"+$(this).attr("id").replace("_item","_sub")+"_back");
	};	
	
});
