var setHome = false;

$(function(){
        var curActiveMenu;
		var isActMnu = 0;
		var curMnuIndex = 1;
		var mnuImgPos=0;
		
        if($('#row_topmenu li').length > 0 ){
		
				//if there is any active menu, set it's background
				curActiveMenu = $('#row_topmenu li.level1').find('span.actmnu:first').parent();
				if( curActiveMenu ){						
					mnuImgPos = getMenuPosValue( $(curActiveMenu).attr('id') );
					$('#menu_bottom').css({backgroundPosition: '0px '+mnuImgPos+'px'});	
				}
				
                //bind image zoom in
                $('#row_topmenu li.level1').bind("mouseover", function(e){
                        
						//$(this)	- current selected item
                        //if this menu is current or active do not do anything
						isActMnu = $(this).find('span.actmnu:first').length;						
						mnuImgPos = getMenuPosValue( $(this).attr('id') );
						if( isActMnu == 0){
							
							//we need to remove the active status of current active menubar
							//else we will observer grey bottom!!!
							if(curActiveMenu){
							setTimeout(function(){setHomeMenu(curActiveMenu)}, 150);
								$(curActiveMenu).find('span:first').removeClass('actmnu');								
								setHome = false;
							}
							//else go to it's parent and find the index of current item and set the default menu bottom bg						
							$('#menu_bottom').css({backgroundPosition: '0px '+mnuImgPos+'px'});														
						}
						isActMnu = 0;												
						
						
                });

                //bind mouse out
                $('#row_topmenu li.level1').bind("mouseout", function(e){
						
						//$(this)	- current selected item
                        //if this menu is current or active do not do anything
						isActMnu = $(this).find('span.actmnu:first').length;
						if( isActMnu == 0){
							//else go to it's parent and find the index of current item and set the default menu bottom bg							
							$('#menu_bottom').css({backgroundPosition: '0px 0px'});
						}
						if(curActiveMenu){
							//$(curActiveMenu).find('span:first').addClass('actmnu');	
							setHome = true;
							setTimeout(function(){setHomeMenu(curActiveMenu)}, 150);
							//mnuImgPos = getMenuPosValue( $(curActiveMenu).attr('id') );
							//$('#menu_bottom').css({backgroundPosition: '0px '+mnuImgPos+'px'});	
						}
						isActMnu = 0;                
                });		

        }
});

function getMenuPosValue(menuId){
	mnuImgPos = 0;
	switch(menuId){
		case "mnu_1":
			mnuImgPos = -91;
			break;
		case "mnu_2":
			mnuImgPos = -91 * 2;
			break;
		case "mnu_3":
			mnuImgPos = -91 * 3;
			break;
		case "mnu_4":
			mnuImgPos = -91 * 4;
			break;
		case "mnu_5":
			mnuImgPos = -91 * 5;
			break;
	}
	return mnuImgPos;
}

function setHomeMenu(actMnu){
	var mnuImgPos;
	if(setHome){
		$(actMnu).find('span:first').addClass('actmnu');	
		mnuImgPos = getMenuPosValue( $(actMnu).attr('id') );
		$('#menu_bottom').css({backgroundPosition: '0px '+mnuImgPos+'px'});	
	}
}
