var nowMenuIndex = '';
	
var delay = (function(){
	  var timer = 0;
	  return function(callback, ms){
	    clearTimeout (timer);
	    timer = setTimeout(callback, ms);
	  };
	})();

$(document).ready(function(){
	$("body").click(function(e){
		if (nowMenuIndex != '')
		{
			var target = $(e.target);
			var targetID = target.attr("id");
			targetID = targetID.substr(0,9);
			
			if (targetID != 'menuBrand')
			{
				closeMainMenu(nowMenuIndex);
				nowMenuIndex = '';
			}else{
				if (nowMenuIndex)
				{
					closeMainMenu(nowMenuIndex);
					nowMenuIndex = '';
				}
			}
		}
	});
});

$(window).resize(function() {
	if (nowMenuIndex != '')
	{
		var menuIndex = nowMenuIndex;
		var appUpObj = $('#menuBrand_'+menuIndex);
		var appUpContentObj = $('#menuBrandContent_'+menuIndex);
	    var appUpContentHeight = appUpObj.innerHeight();
	    var offsetObj = $('#menuBrandTarget_'+menuIndex).offset();
	    appUpObj.css('top',offsetObj.top - appUpContentHeight + 38);
	    appUpObj.css('left',offsetObj.left);
	}
});


function setMenuPosition(menuIndex)
{
	if (nowMenuIndex != '')
	{
		closeMainMenu(nowMenuIndex);
	}
	
	var appUpObj = $('#menuBrand_'+menuIndex);
	var appUpContentObj = $('#menuBrandContent_'+menuIndex);
    var appUpContentHeight = appUpObj.innerHeight();
    
    var offsetObj = $('#menuBrandTarget_'+menuIndex).offset();
    appUpObj.css('top', offsetObj.top - appUpContentHeight + 38);
    appUpObj.css('left', offsetObj.left);
    appUpContentObj.css('top', appUpContentHeight);
    appUpObj.css('display', '');
    nowMenuIndex = menuIndex;
    
    appUpContentObj.animate({
	    top: 0
	  }, 1000, function() {
		  
	  });
}

function closeMainMenu(menuIndex)
{
	$('#menuBrandContent_'+menuIndex).animate({
	    top: $('#menuBrand_'+menuIndex).innerHeight()
	  }, 500, function() {
		  $('#menuBrand_'+menuIndex).css('display', 'none');
	  });
}
