if (typeof(QLife) == 'undefined') {
    var QLife = {};
}

QLife.Common.addOnload(function(){
    $('balloonbase').className = (Prototype.Browser.IE ? 'b0_ie' : 'b0');

    QLife.Toppage.initTabview(function(){ QLife.TopMap.initMap('', '', ''); });
    QLife.TopMap.init('st', 'map_pref', 'map_rail', 'map_station', 'zipcode1', 'zipcode2', 'address_list', 'tab2_hospital_list', 'map');
    QLife.TopSubject.init(QLife.Common.SITE_JP, 'st', 'tab3_step1', 'tab3_step2', 'tab3_step3', 'tab3_hospital_list');

    // 郵便番号検索時の処理
    var funcBefore = function(){
        $('address_list').innerHTML = QLife.Common.IMG_LOADING;
    };
    var funcUpdated = function(transport){
        var json = transport.responseText.evalJSON();
        var html = '';
        if (json == null || typeof(json.kenname) == 'undefined') {
            html = '不明な郵便番号です';
        } else {
            html = '<a href="javascript:void(0);" onclick="QLife.TopMap.yahooMap.moveTo(\'' +
                json.latitude + ',' + json.longitude + '\');">' +
                (json.kenname + json.cityname + json.name).escapeHTML() + '</a><br />';
        }
        $('address_list').innerHTML = html;
    };
    QLife.Zipcode.addListener('zipcode1', 'zipcode2', funcBefore, funcUpdated);
});

QLife.Toppage = {

    tabView : null,

    initTabview : function(funcInitMap){
        this.tabView = new YAHOO.widget.TabView('tabview_container'); 
        // アクティブなタブを変更
        if (this.activeTabIndex > -1 && this.activeTabIndex != this.tabView.get('activeIndex')) {
            this.tabView.set('activeIndex', this.activeTabIndex);
        }
        // タブ2 切り替えイベント
        this.tabView.getTab(1).addListener(
            'click',
            function(e){
                QLife.Common.gaTrack('/toppage_map');
                funcInitMap(); // 地図初期化
            }
        );
        // タブ3 切り替えイベント
        this.tabView.getTab(2).addListener(
            'click',
            function(e){
                QLife.Common.gaTrack('/toppage_subject');
            }
        );
    },
    
    activeTabIndex : -1,
    /**
     * ブラウザ履歴: 状態復帰処理
     */
    restore : function(state){
        var params = String(state).split('_');
        // アクティブなタブを記憶(onloadで処理)
        if (params.length >= 1 && params[0].match(/^[0-9]+$/)) {
            this.activeTabIndex = params[0];
//            alert('Toppage.restore: activeTabIndex=' + this.activeTabIndex);
        }
    },
    /**
     * キーワード(11位以下)の表示状態を切り替え
     *
     * @param string  divKwall   キーワード表示領域のid
     * @param boolean visible    true:表示する、false:隠す
     * @param string  divOpenId  
     * @param string  divCloseId 
     */
    switchKwall : function(divKwallId, visible, divOpenId, divCloseId){
        $(divKwallId).setStyle({display : (visible ? 'block' : 'none')});
        if (visible) {
            // 開く: 閉じるリンクを有効に
            $(divOpenId).setStyle({display : 'none'});
            $(divCloseId).setStyle({display : 'block'});
        } else {
            // 閉じる: 開くリンクを有効に
            $(divOpenId).setStyle({display : 'block'});
            $(divCloseId).setStyle({display : 'none'});
        }
    }
};

