var home;

if (home == undefined) {
    var home = {};
}

var prefs = {
    hokkaido: {name: '北海道', id: 1, area: 'hokaido'},
    aomori: {name: '青森県', id: 2, area: 'touhoku'},
    iwate: {name: '岩手県', id: 3, area: 'touhoku'},
    miyagi: {name: '宮城県', id: 4, area: 'touhoku'},
    akita: {name: '秋田県', id: 5, area: 'touhoku'},
    yamagata: {name: '山形県', id: 6, area: 'touhoku'},
    fukushima: {name: '福島県', id: 7, area: 'touhoku'},

    tokyo: {name: '東京都', id: 13, area: 'kantou'},
    kanagawa: {name: '神奈川県', id: 14, area: 'kantou'},
    chiba: {name: '千葉県', id: 12, area: 'kantou'},
    saitama: {name: '埼玉県', id: 11, area: 'kantou'},
    ibaraki: {name: '茨城県', id: 8, area: 'kantou'},
    tochigi: {name: '栃木県', id: 9, area: 'kantou'},
    gunma: {name: '群馬県', id: 10, area: 'kantou'},

    yamanashi: {name: '山梨県', id: 19, area: 'hokuriku'},
    nagano: {name: '長野県', id: 20, area: 'hokuriku'},
    niigata: {name: '新潟県', id: 15, area: 'hokuriku'},
    toyama: {name: '富山県', id: 16, area: 'hokuriku'},
    ishikawa: {name: '石川県', id: 17, area: 'hokuriku'},
    fukui: {name: '福井県', id: 18, area: 'hokuriku'},

    aichi: {name: '愛知県', id: 23, area: 'toukai'},
    shizuoka: {name: '静岡県', id: 22, area: 'toukai'},
    mie: {name: '三重県', id: 24, area: 'toukai'},
    gifu: {name: '岐阜県', id: 21, area: 'toukai'},

    osaka: {name: '大阪府', id: 27, area: 'kansai'},
    hyogo: {name: '兵庫県', id: 28, area: 'kansai'},
    kyoto: {name: '京都府', id: 26, area: 'kansai'},
    shiga: {name: '滋賀県', id: 25, area: 'kansai'},
    nara: {name: '奈良県', id: 29, area: 'kansai'},
    wakayama: {name: '和歌山県', id: 30, area: 'kansai'},

    ehime: {name: '愛媛県', id: 38, area: 'shikoku'},
    kagawa: {name: '香川県', id: 37, area: 'shikoku'},
    kohchi: {name: '高知県', id: 39, area: 'shikoku'},
    tokushima: {name: '徳島県', id: 36, area: 'shikoku'},

    okayama: {name: '岡山県', id: 33, area: 'chugoku'},
    hiroshima: {name: '広島県', id: 34, area: 'chugoku'},
    tottori: {name: '鳥取県', id: 31, area: 'chugoku'},
    shimane: {name: '島根県', id: 32, area: 'chugoku'},
    yamaguchi: {name: '山口県', id: 35, area: 'chugoku'},

    fukuoka: {name: '福岡県', id: 40, area: 'kyusyu'},
    saga: {name: '佐賀県', id: 41, area: 'kyusyu'},
    nagasaki: {name: '長崎県', id: 42, area: 'kyusyu'},
    kumamoto: {name: '熊本県', id: 43, area: 'kyusyu'},
    oita: {name: '大分県', id: 44, area: 'kyusyu'},
    miyazaki: {name: '宮崎県', id: 45, area: 'kyusyu'},
    kagoshima: {name: '鹿児島県', id: 46, area: 'kyusyu'},
    okinawa: {name: '沖縄県', id: 47, area: 'kyusyu'}
}

home.pref_tooltip = function(element) {
    this.element      = element;
    this.currentGroup = null;
    this.currentPref  = null;
    this.initialize();
}

home.pref_tooltip.prototype = {

    initialize: function() {
        var _tooltip = this;
        jQuery(jQuery(this.element).find('p')).click(function() {
            _tooltip.close();
        });
    },

    getCurrentGroup: function() {
        return this.currentGroup;
    },

    getCurrentPref: function() {
        return this.currentPref;
    },

    moveTo: function(pref, group) {
        this.currentGroup = group;
        this.currentPref  = pref;
        var prefId = prefs[pref.parentNode.className]['id'];
        jQuery(this.element).find('a')[0].href = '/guide/area' + prefId + '.htm';
        jQuery(this.element).find('a')[1].href = '/guide/line_area' + prefId + '.htm';
        jQuery(this.element).find('a')[3].href = '/guide/map_area' + prefId + '.htm';
        this.locateTo(pref);
        jQuery(this.element).css({zIndex: 20}).show();
    },

    locateTo: function(prefBtn) {
        if (jQuery(this.element).is(':visible')) {
            jQuery(this.element).animate({
                left: jQuery(prefBtn).offset().left + 50 + 'px',
                top: jQuery(prefBtn).offset().top - 15 + 'px'
            }, 100);
        } else {
            jQuery(this.element).css({
                left: jQuery(prefBtn).offset().left + 50 + 'px',
                top: jQuery(prefBtn).offset().top - 15 + 'px'
            }).show();
        }
    },

    close: function() {
        this.inactivateAllPrefBtn();
        jQuery(this.element).hide();
    },

    inactivateAllPrefBtn: function() {
        jQuery('.mapList a').removeClass('on');
    }
}

home.map = function(element, area) {
    this.element = element;
    this.area    = area;
    jQuery('#mapPointer li').hide();
}

home.map.prototype = {

    activate: function() {
        this.addPointer();
    },

    addPointer: function() {
        var _element = this.element;
        jQuery('#mapPointer li').each(function() {
            if (jQuery(this).attr('class') != _element.id) {
                jQuery(this).hide();
            } else if (!jQuery(this).is(':visible')) {
                jQuery(this)
                    .show()
                    .animate({top: '-=10px'}, 0)
                    .animate({top: '+=10px'}, 'slow', 'easeInOutBack');
            }
        });
    },

    inactivate: function() {
        jQuery('#mapPointer li').hide();
    },

    monitor: function() {
        var _area = this.area;
        jQuery(this.element).mouseover(function() {
            _area.activate();
            if (prefTooltip.getCurrentGroup() != null && prefTooltip.getCurrentGroup() != _area.getPrefGroup()) {
                prefTooltip.inactivateAllPrefBtn();
                prefTooltip.close();
            }
        });
    }
}

home.area = function(code) {
    this.code = code;
    this.initialize();
}

home.area.prototype = {

    initialize: function() {
        this.map       = new home.map(jQuery('area#' + this.code)[0], this);
        this.prefGroup = new home.prefGroup(jQuery('#' + this.code + '_area')[0], this)
        this.monitor();
    },

    activate: function() {
        this.inactivateAllAreas();
        this.prefGroup.activate();
        this.map.activate();
    },

    inactivate: function() {
        this.prefGroup.inactivate();
    },

    monitor: function() {
        this.map.monitor();
        this.prefGroup.monitor();
    },

    getCode: function() {
        return this.code;
    },

    getPrefGroup: function() {
        return this.prefGroup;
    },

    inactivateAllAreas: function() {
        for (code in areaList) {
            areaList[code].inactivate();
        }
    }
}

home.prefGroup = function(element, area) {
    this.element = element;
    this.area    = area;
    this.code    = area.getCode();
}

home.prefGroup.prototype = {

    setArea: function(area) {
        this.area = area;
    },

    monitor: function() {
        var _group = this;
        jQuery(this.element).find('a').click(function() {
            prefTooltip.inactivateAllPrefBtn();
            jQuery(this).addClass('on');
            prefTooltip.moveTo(this, _group);
        });
        jQuery(this.element).find('a').hover(function() {
            if (prefTooltip.getCurrentGroup() != null && prefTooltip.getCurrentGroup() != _group) {
                prefTooltip.close();
                prefTooltip.inactivateAllPrefBtn();
            }
            _group.area.activate();
        }, function() {
            if (prefTooltip.getCurrentPref() != null && prefTooltip.getCurrentPref() != this) {
                jQuery(this).removeClass('on');
            }
        });
    },

    activate: function() {
        jQuery(this.element).removeClass(this.code + 'Area').addClass(this.code + 'AreaOn');
    },

    inactivate: function() {
        jQuery(this.element).removeClass(this.code + 'AreaOn').addClass(this.code + 'Area');
    }
}

var areaList = [];
var prefTooltip;
jQuery(function() {
    jQuery(document).ready(function() {
        prefTooltip = new home.pref_tooltip(jQuery('#map_popup')[0]);
        areaList = {
            hokkaido: new home.area('hokkaido'),
            touhoku:  new home.area('touhoku'),
            hokuriku: new home.area('hokuriku'),
            kantou:   new home.area('kantou'),
            toukai:   new home.area('toukai'),
            kansai:   new home.area('kansai'),
            chugoku:  new home.area('chugoku'),
            shikoku:  new home.area('shikoku'),
            kyusyu:   new home.area('kyusyu')
        }
    });
});

