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

QLife.Common = {
    SITE_JP : 1,
    SITE_PET : 2,
    /**
     * ローディング画像
     */
    IMG_LOADING : '<img src="/pet/images/ajax-loader.gif" width="16" height="16" />読み込み中...',
    /**
     * window.onload時に実行する関数オブジェクトを追加する
     */
    addOnload : function(func){
        if (window.addEventListener) {
            window.addEventListener('load', func, false);
        } else if (window.attachEvent) {
            window.attachEvent('onload', func);
        }
    },
    /**
     * 現在のURLから、「/pet/」以下の末尾部分を返す
     */
    getUrlSuffix : function(){
        var url = String(window.location)
        return url.replace(/^https*:\/\/[^\/]+(.*)$/, '$1');
    },
    /**
     * selectのoptionを削除(先頭を除く)
     *
     * @param Element sl select要素
     */
    removeOptions : function(sl){
        sl.selectedIndex = 0;
        var len = sl.options.length;
        for (var i = 1 ; i < len; i++) {
            sl.options[1] = null;
        }
    },
    /**
     * selectの選択値を復帰する
     *
     * @param string selectId selectのid
     * @param int    value    選択する値(<option>のvalue値)
     */
    restoreSelect : function(selectId, value){
        var select = $(selectId);
        if (select == null) {
            return;
        }
        for (var i = 0, length = select.length ; i < length ; i++) {
            if (value == select.options[i].value) {
                select.selectedIndex = i;
                return;
            }
        }
    },
    /**
     * アクティブなタブを切り替え
     *
     * @param YAHOO.widget.TabView tabView     TabViewオブジェクト
     * @param int                  activeIndex アクティブにするタブのインデックス(0-)
     * @param Array                divIds      タブパネル領域のid属性値一覧(配列)
     */
    switchTab : function(tabView, activeIndex, divIds){
        if (activeIndex == -1) {
            return;
        }
        var done = false;
        if (tabView != null) {
	        try {
	            if (tabView.get('activeIndex') != activeIndex) {
	                tabView.set('activeIndex', activeIndex);
	            }
	            done = true;
	        } catch(e) {
	            done = false;
	        }
        }
        if (! done) {
            for (var i = 0, length = divIds.length ; i < length ; i++) {
                $(divIds[i]).setStyle({display : (activeIndex == i ? 'block' : 'none')});
            }
        }
    },
    /**
     * 地図印刷ウィンドウを開く
     */
/*    printMap : function(hid){
        window.open(
            'printmap.php?hid=' + String(hid).escapeHTML(),
            'printmap',
            'width=710,height=600,menubar=yes,toolbar=yes,location=yes,resizable=yes'
            );
    },
*/    /**
     * <a>オブジェクトのpathname属性値を返す
     *
     * @param object anchor <a>オブジェクト
     */    
    getPathname : function(anchor){
        var pathname = '';
        if (anchor.outerHTML) {
            // IEのとき(IE6でpathnameが文字化けする対策)
            pathname = anchor.outerHTML.replace(/^.*\s+href="([^"#]+)(?:#[^"]+)*".*$/, '$1');
        } else {
            pathname = anchor.pathname;
        }
        // 先頭にhttp://...が含まれるとき、除去
        pathname = pathname.replace(/^https*:\/\/[^\/]+(.*)$/, '$1');
        // 絶対URLに変更
        if (pathname.search(/^\//) == -1) {
            pathname = '/' + pathname;
        }
        return pathname;
    },
    /**
     * Google Analyticsの任意コール(Ajax処理時など)
     *
     * @see http://www.google.com/support/googleanalytics/bin/answer.py?answer=55521
     * @param string path URL文字列
     */
    gaTrack : function(path){
	    // ローカルホストのとき、離脱
	    if (String(location.href).match(/^https*:\/\/[^\.]+\/.*/)) {
	       alert('gaTrack: localhost');
	       return;
	    }
	    // パスがないとき、離脱
        if (path == null || path.length == 0) {
            return;
        }
        if (typeof(pageTracker) == 'undefined') {
//            alert('pageTracker is undefined.');
        } else {
            pageTracker._trackPageview(path);
        }
    }
};

