Event.observe(window, 'load', switchAreaListener);

function switchAreaListener()
{
    jQuery("ul#areaMenu a").get().each(function(e) {
        Event.observe(e, 'click', switchArea);
    });
    jQuery("ul.areaSubMenu a").get().each(function(e) {
        Event.observe(e, 'click', switchPref);
    });
}

function switchArea()
{
    jQuery("#" + this.parentNode.id + "_01 a").get().each(function(e) {
        addPref(e.id);
        updateContents(e.id);
    });
    jQuery("ul#areaMenu a").get().each(function(e) {
        e.className = "";
    });
    this.className = "current";

    $A(document.getElementsByClassName("prefList")).each(function(e) {
        e.style.display = "none";
    });
    jQuery("ul.areaSubMenu a").get().each(function(e) {
        e.className = "";
    });
    jQuery("#" + this.parentNode.id + "_01 a").get().each(function(e) {
        e.className = 'current';
        $("pref" + e.id).style.display = "block";
    }.bind(this));

    jQuery("ul.areaSubMenu").get().each(function(ul) {
        ul.parentNode.style.display = "none";
    });
    $('area' + this.id).style.display = "block";
}

function switchPref()
{
    $$("ul.areaSubMenu a").each(function(e) {
        e.className = "";
    }.bind(this));
    updateContents(this.id);
    addPref(this.id);
    $A(document.getElementsByClassName("prefList")).each(function(e) {
        e.style.display = "none";
    });
    $('pref' + this.id).style.display = "block";
    this.className = "current";
}

function addPref(id)
{
    var dat = new Date();
    dat.setTime(dat.getTime() + 60*60*24*7*1000);
    expire = dat.toGMTString();
    document.cookie = 'pref_id=' + id + ';path=/;expires='+expire;
}

function updateContents(pref_id)
{
    if ($('pref' + pref_id).innerHTML.match(/^\s*$/g)) {
        new Ajax.Updater(
            'pref' + pref_id,
            '/home/get_recommended_pref',
            {
                method: 'GET',
                parameters: 'id=' + pref_id,
                asynchronous: false
            }
        );
    }
}

