/* menu */
var NowCategory;
var NowSubMenuId;

function MenuOver(SubMenuId)
{
	if (NowSubMenuId)
	{
		if (NowSubMenuId != NowCategory)
			$(NowSubMenuId+'_img').setAttribute('src', '../img/inc/'+NowSubMenuId+'.gif');
		$(NowSubMenuId).hide();
	}

	NowSubMenuId = SubMenuId;
	$(NowSubMenuId+'_img').setAttribute('src', '../img/inc/'+NowSubMenuId+'2.gif');
	$(NowSubMenuId).show();
}

function MenuOut(evt)
{
	Position.prepare();
	if (NowSubMenuId && !Position.within($('MenuBox'), Event.pointerX(evt), Event.pointerY(evt)))
	{
		if (NowSubMenuId != NowCategory)
			$(NowSubMenuId+'_img').setAttribute('src', '../img/inc/'+NowSubMenuId+'.gif');
		$(NowSubMenuId).hide();
	}
}

Event.observe(window.document, 'mousemove', MenuOut, true);

var NowSubMenu = null;
var NowLeftMenu = null;
var NowLeftSubMenu = null;
function SubMenuOver(Key)
{
	var SubMenu = $('sub_'+Key);
	if (SubMenu && (NowSubMenu != SubMenu))
		SubMenu.setAttribute('src', '../img/inc/'+Key+'2.gif');
}
function SubMenuOut(Key)
{
	var SubMenu = $('sub_'+Key);
	if (SubMenu && (NowSubMenu != SubMenu))
		SubMenu.setAttribute('src', '../img/inc/'+Key+'.gif');
}
function LeftMenuOver(Key)
{
	var LeftMenu = $('left_'+Key);
	if (LeftMenu && (NowLeftMenu != LeftMenu) && (NowLeftSubMenu != LeftMenu))
		LeftMenu.setAttribute('src', LeftMenu.getAttribute('src').replace(/\.gif/, '')+'_on.gif');
}
function LeftMenuOut(Key)
{
	var LeftMenu = $('left_'+Key);
	if (LeftMenu && (NowLeftMenu != LeftMenu) && (NowLeftSubMenu != LeftMenu))
		LeftMenu.setAttribute('src', LeftMenu.getAttribute('src').replace(/\_on.gif/, '')+'.gif');
}

function MenuImgChange(MainMenuKey, SubMenuKey, LeftSubMenuKey)
{
	var MainMenu = $('nav_'+MainMenuKey+'_img');
	var SubMenu = $('sub_nav_'+MainMenuKey+'_'+SubMenuKey);
	var LeftMenu = $('left_nav_'+MainMenuKey+'_'+SubMenuKey);
	var LeftSubMenu = $('left_nav_'+MainMenuKey+'_'+LeftSubMenuKey);

	if (MainMenu)
		MainMenu.setAttribute('src', '../img/inc/nav_'+MainMenuKey+'2.gif');
	if (SubMenu)
	{
		NowSubMenu = SubMenu;
		SubMenu.setAttribute('src', '../img/inc/nav_'+MainMenuKey+'_'+SubMenuKey+'2.gif');
	}
	if (LeftMenu && (LeftMenu.getAttribute('src').match('_on.gif') == null))
	{
		NowLeftMenu = LeftMenu;
		LeftMenu.setAttribute('src', LeftMenu.getAttribute('src').replace(/\.gif/, '')+'_on.gif');
	}
	if (LeftSubMenu && (LeftSubMenu.getAttribute('src').match('_on.gif') == null))
	{
		NowLeftSubMenu = LeftSubMenu;
		LeftSubMenu.setAttribute('src', LeftSubMenu.getAttribute('src').replace(/\.gif/, '')+'_on.gif');
	}
}

/* Family Site */
function ShowFamilySite()
{
	var FamilyBox = $('family');
	var BaseBox = $('BaseFamilyBox');

	var offset = Position.positionedOffset(BaseBox);

	Element.setStyle(FamilyBox, {
		left : offset[0] + 'px',
		top : (offset[1] - 147) + 'px'
	});

	BaseBox.hide();
	FamilyBox.show();
}

function HideFamilySite()
{
	$('BaseFamilyBox').show();
	$('family').hide();
}

function FamilySiteOut(evt)
{
	var FamilyBox = $('family');
	if (FamilyBox && !Position.within(FamilyBox, Event.pointerX(evt), Event.pointerY(evt)))
		HideFamilySite();
}

Event.observe(window.document, 'mousemove', FamilySiteOut, true);


/* Sub buttons */
function SubButtonOver(el)
{
	el.setAttribute('src', el.getAttribute('src').replace(/\.gif/, '2.gif'));
}
function SubButtonOut(el)
{
	el.setAttribute('src', el.getAttribute('src').replace(/2\.gif/, '.gif'));
}

/* form check */
function CheckFormValue(form)
{
	var els = Form.getElements(form);
	
	/* check required field */
	var chk = els.detect( function (el, index) {
		if (el.disabled)
			return false;
		else if ((el.getAttribute('isRequire') == 'true') && (el.value.gsub(/[ ]+/, '') == ''))
			return true;
	});
	if (chk != null)
	{
		OutCheckValueMsg(chk, chk.getAttribute('isRequireMsg'));
		return false;
	}
	
	/* check email field */
	var email_pattern = /^[_a-zA-Z0-9-]+@[._a-zA-Z0-9-]+\.[a-zA-Z]*$/;
	var chk = els.detect( function (el, index) {
		if (el.disabled)
			return false;
		else if ((el.getAttribute('isEmail') == 'true') && !email_pattern.test(el.value))
			return true;
	});
	if (chk != null)
	{
		OutCheckValueMsg(chk, chk.getAttribute('isEmailMsg'));
		return false;
	}
	
	return true;
}
function OutCheckValueMsg(el, msg)
{
	alert(msg);
	if ((el.getStyle('display') != 'none') && !el.getAttribute('disabled') && (el.getAttribute('type') != 'hidden'))
		el.focus();
}

function SendForm(form_id, url)
{
	var form = document.forms[form_id];
	if (CheckFormValue(form))
	{
		form.action = url;
		form.submit();
	}
}

/* top button */
function MoveTopButton()
{
	var Top = $('TopButton');
	var h;
	if (Top)
	{
		if (window.innerHeight)
			h = window.innerHeight;
		else if (document.documentElement && document.documentElement.clientHeight)
			h = document.documentElement.clientHeight;
		else if (document.body)
			h = document.body.clientHeight;
		h = h - 30 + document.body.scrollTop;
		/*Element.setStyle(Top, {
			'top' : h+'px'
		});*/
		Top.style.top = h+'px';
	}
}
