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

QLifePet.Common = {
    /**
     * ローディング画像
     */
    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の選択値を復帰する
     *
     * @param string selectId      selectのid
     * @param int    selectedIndex 選択するインデックス
     */
    restoreSelect : function(selectId, selectedIndex){
        var select = $(selectId);
        if (select == null) {
        } else {
            if (select.length > selectedIndex) {
                select.selectedIndex = selectedIndex;
            }
        }
    },

    /**
     * フォントサイズ: 小
     */
    FONTSIZE_S : 1,
    /**
     * フォントサイズ: 中
     */
    FONTSIZE_M : 2,
    /**
     * フォントサイズ: 大
     */
    FONTSIZE_L : 3,
    /**
     * フォントサイズ変更<a>のid属性値
     */
    FONTSIZE_ANCHORS_ID : ['fontsize1', 'fontsize2', 'fontsize3'],
    /**
     * フォントサイズを変更する
     *
     * @param int    fontsize       フォントサイズ(1-3)
     * @param string activeAnchorId アクティブにする<a>タグのid属性値
     */
    updateFontsize : function(fontsize, activeAnchorId){
        // フォントサイズの決定
        var size1 = '', size2 = '', size3 = '';
        switch (fontsize) {
        case this.FONTSIZE_S:
            size1 = '10px'; size2 = '12px'; size3 = '15px';
            break;
        case this.FONTSIZE_M:
            size1 = '12px'; size2 = '14px'; size3 = '16px';
            break;
        case this.FONTSIZE_L:
            size1 = '14px'; size2 = '16px'; size3 = '18px';
            break;
        }
        // フォントサイズの更新
        var elements = $$('body, th, td, h1, h2, h3, h4, h5, *.size1, *.size2, *.size3');
        var lines = [];
        for (var k = 0, length2 = elements.length ; k < length2 ; k++) {
            var e = elements[k];
            if (e.hasClassName('size1')) {
                e.setStyle({fontSize : size1});
            } else if (e.hasClassName('size3')) {
                e.setStyle({fontSize : size3});
            } else {
                e.setStyle({fontSize : size2});
            }
        }
        // アイコンのスタイル変更
        for (var i = 0, length = this.FONTSIZE_ANCHORS_ID.length ; i < length ; i++) {
            var e = $(this.FONTSIZE_ANCHORS_ID[i]);
            if (e == null) {
                alert('id=' + this.FONTSIZE_ANCHORS_ID[i] + ' does not exist.');
            } else {
                e.removeClassName('active');
            }
        }
        $(activeAnchorId).addClassName('active');
        // Cookieにフォントサイズ設定を保存
        new Ajax.Request('fontsize_update.php', {
            method : 'get',
            parameters : {size : fontsize}
        });
    },
    /**
     * 地図印刷ウィンドウを開く
     */
    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(/^\/pet\//) == -1) {
            pathname = '/pet/' + 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){
//        alert('pageTracker._trackPageview(\'' + path + '\');'); // TODO test
        if (pageTracker) {
	        pageTracker._trackPageview(path);
        } else {
            alert('pageTracker is undefined.');
        }
    }
};

