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


/**
 * 感動口コミローテーション
 */
QLife.Kandou = {
    /**
     * ふき出し内容のCSSセレクタ
     */
    selectorContent : 'div#balloonbase p',
    /**
     * 表示アバター総数
     */
    avatarsCount : 0,
    /**
     * 画像切り替え時に実行する関数
     *
     * function(activeIndex){ ... }
     */
    funcSwitch : null,
    /**
     * 初期化。ローテーション処理を開始する
     *
     * @param string   selectorAvatars アバター画像オブジェクトのCSSセレクタ
     * @param string   selectorContent ふき出し内表示コンテンツのCSSセレクタ
     * @param function funcSwitch      
     */
	init : function(selectorAvatars, selectorContent, funcSwitch){
	   this.selectorContent = selectorContent;
	   this.funcSwitch = funcSwitch;
	   // アバターにマウスイベント追加
	   var avatars = $$(selectorAvatars);
	   this.avatarsCount = avatars.length;
	   for (var i = 0 ; i < this.avatarsCount ; i++) {
		   this._addAvatarListener(avatars[i], i);
	   }
	   // ローテーション開始
	   this.startRotation();
	},
	/**
	 * アバター(画像)にマウスオン/オフイベントを追加
	 */
	_addAvatarListener : function(avatar, index){
       avatar.onmouseover = function(){ QLife.Kandou.mouseOver(index); };
       avatar.onmouseout  = function(){ QLife.Kandou.startRotation(); };
	},

    /**
     * PeriodicalExecuterオブジェクト(prototype.js)
     */
    exec : null,
    /**
     * ローテーション開始
     */
    startRotation : function(){
        if (this.exec == null) {
	        // [ref] PeriodicalExecuter ... prototype.js
	        this.exec = new PeriodicalExecuter(function(){ QLife.Kandou.rotateKuchikomi(); }, 4);
        }
    },
    /**
     * 現在表示している感動口コミインデックス
     */
    currentIndex : 0,
    /**
     * 感動口コミローテーションの切り替え
     */
    rotateKuchikomi : function(){
        if (this.avatarsCount == 0) {
            return;
        }
        var nextIndex = (this.currentIndex + 1) % this.avatarsCount;
        this.switchBalloon(this.currentIndex, nextIndex);
    },
    /**
     * 表示する口コミの切り替え
     *
     * @param int disableIndex 無効にするアバターインデックス
     * @param int enableIndex 有効にするアバターインデックス
     */
    switchBalloon : function(disableIndex, enableIndex){
        // [ref] $$() ... prototype.js
        var spans = $$(this.selectorContent);
        // [ref] Element.setStyle() ... prototype.js
        spans[disableIndex].setStyle({display : 'none'});
        spans[enableIndex].setStyle({display : ''});
        // 表示中の口コミインデックスを更新
        this.currentIndex = enableIndex;
        // ふきだし画像を変更
        if (this.funcSwitch != null) {
            this.funcSwitch(enableIndex);
        }
    },
    /**
     * ローテーションを中断、指定アバターの口コミを表示
     *
     * @param avatarIndex 有効にするアバターインデックス
     */
    mouseOver : function(avatarIndex){
	    // ローテーションを中断
	    if (this.exec != null) {
		    this.exec.stop();
		    this.exec = null;
	    }
        this.switchBalloon(this.currentIndex, avatarIndex);
    }
};

