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

QLife.Search2009 = {
    /**
     * 初期化処理
     */
    init : function() {
        this.updatePagersOnclick();
        this._initHistory();
        // 履歴復帰チェック
        this._checkRestore();
    },
    /**
     * ブラウザ履歴処理の初期化
     */
    _initHistory : function () {
        // 
        dhtmlHistory.initialize();
        dhtmlHistory.addListener(this.restoreHistory);
    },
   
    // {{{ ブラウザ履歴

    /**
     * ブラウザ履歴の追加
     */
    addHistory : function(pathname) {
        dhtmlHistory.add(pathname, null);
    },
    /**
     * 履歴復帰処理をすでに行った
     */
    restored : false,
    /**
     * ブラウザ履歴の復帰
     */
    restoreHistory : function(newLocation, historyData) {
        try {
            this.restored = true;
            
            if ($('search_result') == null) {
                return false;
            }
            // 復帰用URLがないとき
            if (newLocation == null || newLocation.length == 0) {
                var href = String(window.location.href);
                newLocation = href.replace(/^https*:\/\/[^\/]+(\/search_(hospital|kuchikomi)_[^#]+).*$/, '$1');
            }
            // 復帰処理では履歴を追加しない
            QLife.Search2009.updateResult(null, newLocation, false);
        } catch (e) {
            if (typeof(console) != 'undefined') {
                console.log(e);
            }
        }
    },
    /**
     * 他ページに移動後、戻ってきたときの復帰処理
     */
    _checkRestore : function()
    {
        // すでに履歴を復帰済み、またはGeckブラウザのとき: 終了
        if (this.restored || Prototype.Browser.Gecko) {
            return;
        }
        var href = String(window.location.href);
        var url = href.replace(/^.+#(\/search_(?:hospital|kuchikomi)_[^#]+)$/, '$1');
        if (href != url && url.length > 0) {
            // 復帰処理では履歴を追加しない
            QLife.Search2009.updateResult(null, url, false);
//            if (typeof(console) != 'undefined') {
//                console.log('matched');
//                console.log(url);
//            } else {
//                alert("matched:\n" + url);
//            }
        }
    },
    /**
     * gaイベント追跡
     */
    trackEvent2 : function(category, action, label, value)
    {
        try {
//            alert('category=' + category + ' action=' + action + ' label=' + label);
            if (pageTracker._trackEvent) {
                pageTracker._trackEvent(category, action, label, value);
            }
        } catch (e) {
            this.log(e);
        }
        return true;
    },
    /**
     * ページャーのonclickイベントを更新
     */
    updatePagersOnclick : function(){
        var anchors = $$('div.hospital_pager a');
        if (anchors == null) {
            return;
        }
        anchors.each(function(anc, index) {
            anc.onclick = function() {
                QLife.Search2009.updateResult(this, null, true);
                return false; // hrefによるページ遷移禁止
            };
        });
    },
    /**
     * 検索結果の更新処理
     */
    updateResult : function(anchor, pathname, addHistory) {
       if (anchor != null) {
           pathname = QLife.Common.getPathname(anchor);
       }
       if (pathname == null || pathname.length == 0) {
           return false;
       }
       // GooglwAnalyticsコール
       QLife.Common.gaTrack(pathname);
       // 待機画像
       $('search_result').innerHTML = QLife.Common.IMG_LOADING;
       // 
       var filename = null;
       if (pathname.match(/^\/search_kuchikomi_/)) {
           filename = 'search_item';
       } else {
           filename = 'search_hospital';
       }

       new Ajax.Request('2009qlife_views/' + filename + '.php', {
           method : 'get',
           parameters : {pathname : pathname},
           onSuccess : function(transport) {
               $('search_result').innerHTML = transport.responseText;
               QLife.Search2009.updatePagersOnclick();
           }
       });
       // 履歴を追加
       if (addHistory) {
           QLife.Search2009.addHistory(pathname);
       }
   }
}

Event.observe(window, 'load', function(){
    try {
        QLife.Search2009.init();
    } catch (e) {
        if (typeof(console) != 'undefined') {
            console.log(e);
        }
    }
});

