var currentMenu = null;
var prevMenu = null;
var keepMenu = false;
var cookiedata = document.cookie;

$(document).ready(function() {
    initTopMenu();
    initShopMenu();

    $('#bottom').before('<div id="middle"><\/div>');
    randomBackgroundImage();

    /*
    if(cookiedata.indexOf('popup20080205=done') < 0)
    {
        var popup = window.open('popup.html', 'welcome', 'width=400, height=210');
        popup.focus();
    }
    */

    /* $('.shopNoticeImage').fadeOut('fast', function() {$(this).fadeIn();}); */
});

function showShopMenu()
{
	currentMenu = $(this).parent().next('ul').get(0);

    if(prevMenu != null && prevMenu != currentMenu)
    {
		$(prevMenu).slideUp('fast');
	}

    if(currentMenu != null)
	{
		//subMenu[n].style.display = "block";
		$(currentMenu).slideDown('fast');
	}
}

function hideShopMenu()
{
	prevMenu = currentMenu;
	currentMenu = null;
	setTimeout("hideShopMenuTO()", 1000);
}

function hideShopMenuTO()
{
	if(prevMenu != null && prevMenu != currentMenu && keepMenu == false)
	{
		//prevMenu.style.display = "none";
		$(prevMenu).fadeOut('fast');
	}
}

function showShopMenu2()
{
	keepMenu = true;
	currentMenu = this;
}

function hideShopMenu2()
{
	keepMenu = false;
	hideShopMenu();
}

function onMenu()
{
	this.style.backgroundColor = "#fef9ee";
}

function offMenu()
{
	this.style.backgroundColor = "transparent";
}

function initTopMenu()
{
	var member = document.getElementById("member");
	var topMenu = document.getElementById("topMenu").getElementsByTagName("li");

	for(i = 0; i < topMenu.length; i++)
	{
		topMenu[i].onmouseover = onMenu;
		topMenu[i].onmouseout = offMenu;
	}

	if(member)
	{
		var login = member.getElementsByTagName("a");
		login[0].innerHTML = "<img src=\"/img/" + login[0].innerHTML + ".gif\" alt=\"" + login[0].innerHTML + "\" />";
		login[1].innerHTML = "<img src=\"/img/" + login[1].innerHTML + ".gif\" alt=\"" + login[1].innerHTML + "\" />";
	}
}
function initShopMenu()
{
	var shopLink, listItem;

	$('.shop').each(function(i) {
	    shopLink = $(this).children('a');
	    shopLink.html('<img src="/img/shop' + (i + 1) + '.gif" alt="' + shopLink.html() + '" />');
	    shopLink.children().hover(showShopMenu, hideShopMenu);
	});

	$('.shop ul').each(function(i) {
	    $(this).hover(showShopMenu2, hideShopMenu2);
	    $(this).children().hover(onMenu, offMenu);
	});

    shopLink = $('#iShop').children('a');
    shopLink.html('<img src="/img/shop.gif" alt="' + shopLink.html() + '" />');
}

function randomBackgroundImage()
{
	var m = 16;
	var n = Math.round(Math.random() * m);

	document.getElementById("middle").style.backgroundImage = "url(/img/fp_pic" + n + ".jpg)";
}
