/**
 * 2010/6/13 add
 * au one Brand Garden
 * initialize JS
 */

/**
 * 配送先情報デフォルト表示用のグローバル変数
 * default or extension1～4 or add
 * 存在しないまたは省略した場合はdefaultとなる
 */

jq$(function() {
    /**
     * ホバーエフェクトのリスナー
     **/
    //グローバルメニュー
    jq$(".menu_effect").hover(
        function() {
//            allOffMenu();
            jq$(this).attr("src", jq$(this).attr("src").replace("_off", "_ov"));
        },
        function() {
            jq$(this).attr("src", jq$(this).attr("src").replace("_ov", "_off"));
        }
    );
    //左メニューアイコン太字
    jq$(".hover_effect3>div ul>li>a").hover(
        function() {
            jq$(this).parent("li").css("background-image", jq$(this).parent("li").css("background-image").replace("com_arrowlink_off.gif", "com_arrowlink_ov.gif"));
        },
        function() {
            jq$(this).parent("li").css("background-image", jq$(this).parent("li").css("background-image").replace("com_arrowlink_ov.gif", "com_arrowlink_off.gif"));
        }
    );
    //ヘッダメニュー
    jq$(".pc_link_gray").hover(
        function() {
            jq$(this).children('img').attr("src", jq$(this).children('img').attr("src").replace("_off", "_ov"));
        },
        function() {
            jq$(this).children('img').attr("src", jq$(this).children('img').attr("src").replace("_ov", "_off"));
        }
    );
    //性別切り替え
    jq$(".gender_change").hover(
        function() {
            jq$(this).attr("src", jq$(this).attr("src").replace("_off", "_ov"));
        },
        function() {
            jq$(this).attr("src", jq$(this).attr("src").replace("_ov", "_off"));
        }
    );
    //商品サムネイル枠色
    jq$(".item_thumb").hover(
        function() {
            jq$(this).css("border-color", "#e96826");
        },
        function() {
            jq$(this).css("border-color", "#999999");
        }
    );
    //商品サムネイル枠色最後処理
    jq$(".item_thumb_last").hover(
        function() {
            jq$(this).css("border-color", "#e96826");
        },
        function() {
            jq$(this).css("border-color", "#999999");
        }
    );

    /**
     * グローバルメニューポップアップのリスナー
     **/
/*
    jq$("#item_pop_back_layer").hover(eraseItemPop);
    jq$("#brand_pop_back_layer").hover(eraseBrandPop);
    jq$("#brand_category_pop_back_layer").hover(eraseBrandCategoryPop);
*/
    //アイテム一覧クリック
    jq$("#menu_item").click(viewItemPop);
    //ブランド一覧クリック
    jq$("#menu_brand").click(viewBrandPop);
    //ブランドカテゴリ一覧クリック
    jq$("#menu_brand_cate").click(viewBrandCategoryPop);
    //ポップアップ外クリック
    jq$("#item_pop_back_layer").click(eraseItemPop);
    jq$("#brand_pop_back_layer").click(eraseBrandPop);
    jq$("#brand_category_pop_back_layer").click(eraseBrandCategoryPop);
    //クローズアイコンクリック
    jq$("#item_pop_close_button_img").click(eraseItemPop);
    jq$("#brand_pop_close_button_img").click(eraseBrandPop);
    jq$("#brand_category_pop_close_button_img").click(eraseBrandCategoryPop);
    //クローズアイコンホバー
    jq$("#item_pop_close_button_img").hover(
        function() {
            jq$("#item_pop_close_button_img").attr("src", jq$("#item_pop_close_button_img").attr("src").replace("close_button.jpg", "close_button_ov.jpg"));
        },
        function() {
            jq$("#item_pop_close_button_img").attr("src", jq$("#item_pop_close_button_img").attr("src").replace("close_button_ov.jpg", "close_button.jpg"));
        }
    );

  //お届け先設定フォーム表示切り替え
  var addres_form_model = {
    canvas_id          : "address_display",
    selector_id        : "delivery_address",
    max_extension_count: window.transport_to_count || 4,
    selector_extension : {
        extension1 : {id:"address_extension1", label:"お届け先情報1"},
        extension2 : {id:"address_extension2", label:"お届け先情報2"},
        extension3 : {id:"address_extension3", label:"お届け先情報3"},
        extension4 : {id:"address_extension4", label:"お届け先情報4"}
    },
    selector_default: {id:"address_default", label: "ご登録住所"},
    selector_add:     {id:"address_add",     label: "別お届け先を登録"},
    animate: 'fadeIn'
  };

  addressFormobj = new FormEachDisplay(addres_form_model);
  addressFormobj.exec();

  //クレジットフォーム表示切り替え
  var credit_form_model = {
    canvas_id          : "credit_display",
    selector_id        : "credit_type",
    max_extension_count: window.credit_count || 4,
    selector_extension : {
        extension1 : {id:"credit_extension1", label:"登録クレジットカード1"},
        extension2 : {id:"credit_extension2", label:"登録クレジットカード2"},
        extension3 : {id:"credit_extension3", label:"登録クレジットカード3"},
        extension4 : {id:"credit_extension4", label:"登録クレジットカード4"}
    },
    selector_default: {id:"credit_add", label: "ご登録住所"},
    selector_add:     {id:"credit_add", label: "別クレジットカード登録"},
    animate: 'fadeIn',
    default_label: "新しいクレジットカードを登録",
    selector_block_id: "credit_selectbox"
  };
  creditFormobj = new FromEachDisplayForCredit(credit_form_model);
  creditFormobj.exec();

  //ラジオボタンでフォーム入力以外を選択されている場合、入力項目をdisableにする。
  //住所
  jq$('input[name="point_use_flag"]').change(
      function() {
          checked_value = jq$('input[name="point_use_flag"]:checked').val();
          if ( checked_value == 0 ) {
              jq$("#point_value").attr("disabled", "disabled");
          } else {
              jq$("#point_value").removeAttr("disabled");
          }
      }
  );
});


/**
 *
 * Gloabal functions
 *
 */

/**
 * グローバルメニューの画像を全てout状態にする
 * @param なし
 * @return void
 */
function allOffMenu() {

    jq$(".menu_effect").each(
        function() {
            jq$(this).attr("src", jq$(this).attr("src").replace("_ov", "_off"));
        }
    );
}

/**
 * ポップアップ（アイテム・ブランド共通）を非表示する
 * @param なし
 * @return void
 */
function eraseItemPop() {
    jq$("#item_pop").css("display", "none");
    jq$("#item_pop_back_layer").css("display", "none");
    jq$("#item_iframe").css("display", "none");
}

function eraseBrandPop() {
    jq$("#brand_pop").css("display", "none");
    jq$("#brand_pop_back_layer").css("display", "none");
    jq$("#brand_iframe").css("display", "none");
}

function eraseBrandCategoryPop() {
    jq$("#brand_category_pop").css("display", "none");
    jq$("#brand_category_pop_back_layer").css("display", "none");
    jq$("#brand_category_iframe").css("display", "none");
}

/*
function eraseItemPop() {
    jq$("#item_pop").slideUp();
    jq$("#item_pop_back_layer").slideUp();
    jq$("#item_iframe").slideUp();
}

function eraseBrandPop() {
    jq$("#brand_pop").slideUp();
    jq$("#brand_pop_back_layer").slideUp();
    jq$("#brand_iframe").slideUp();
}

function eraseBrandCategoryPop() {
    jq$("#brand_category_pop").slideUp();
    jq$("#brand_category_pop_back_layer").slideUp();
    jq$("#brand_category_iframe").slideUp();
}
*/



/**
 * アイテム一覧のポップアップを表示する
 * @param なし
 * @return void
 */
function viewItemPop() {
    eraseItemPop();
    eraseBrandPop();
    eraseBrandCategoryPop();
    jq$(".pop_contents").remove();
    jq$("#item_pop").append('<div class="pop_contents"></div>');
    //ポップアップ外クリック時のイベント取得用にdocumentウィンドウ幅に合わせたレイヤーを設置
    window_width  = jq$(document).width();
    window_height = jq$(document).height();
    jq$("#item_pop_back_layer").css("width", window_width-20 + "px");
    jq$("#item_pop_back_layer").css("height", window_height + "px");
    //jq$("#item_pop").css("height", 450 + "px");
    iframe_width = jq$("#item_pop").width();
    iframe_height = jq$("#item_pop").height();
    jq$("#item_iframe").css("width", iframe_width + "px");
    jq$("#item_iframe").css("height", iframe_height + "px");
    jq$("#item_pop_back_layer").fadeIn();
    jq$("#item_pop").fadeIn();
    jq$("#item_iframe").fadeIn();
}

/**
 * ブランド一覧のポップアップを表示する
 * @param なし
 * @return void
 */
function viewBrandPop() {
    eraseItemPop();
    eraseBrandPop();
    eraseBrandCategoryPop();
    jq$(".pop_contents").remove();
    jq$("#brand_pop").append('<div class="pop_contents"></div>');
    //ポップアップ外クリック時のイベント取得用にdocumentウィンドウ幅に合わせたレイヤーを設置
    window_width  = jq$(document).width();
    window_height = jq$(document).height();
    jq$("#brand_pop_back_layer").css("width", window_width-20 + "px");
    jq$("#brand_pop_back_layer").css("height", window_height + "px");
    //jq$("#brand_pop").css("height", 775 + "px");
    iframe_width = jq$("#brand_pop").width();
    iframe_height = jq$("#brand_pop").height();
    jq$("#brand_iframe").css("width", iframe_width + "px");
    jq$("#brand_iframe").css("height", iframe_height + "px");
    jq$("#brand_pop_back_layer").fadeIn();
    jq$("#brand_pop").fadeIn();
    jq$("#brand_iframe").fadeIn();
}

/**
 * ブランドカテゴリ一覧のポップアップを表示する
 * @param なし
 * @return void
 */
function viewBrandCategoryPop() {
    eraseItemPop();
    eraseBrandPop();
    eraseBrandCategoryPop();
    jq$(".pop_contents").remove();
    jq$("#brand_category_pop").append('<div class="pop_contents"></div>');
    //ポップアップ外クリック時のイベント取得用にdocumentウィンドウ幅に合わせたレイヤーを設置
    window_width  = jq$(document).width();
    window_height = jq$(document).height();
    jq$("#brand_category_pop_back_layer").css("width", window_width-20 + "px");
    jq$("#brand_category_pop_back_layer").css("height", window_height + "px");
    //jq$("#brand_category_pop").css("height", 200 + "px");
    iframe_width = jq$("#brand_category_pop").width();
    iframe_height = jq$("#brand_category_pop").height();
    iframe_left = jq$("#brand_category_pop").css("marginLeft");
    iframe_top = jq$("#brand_category_pop").css("paddingTop");
//alert(iframe_left);
    jq$("#brand_category_iframe").css("width", iframe_width + "px");
    jq$("#brand_category_iframe").css("height", iframe_height + "px");
    jq$("#brand_category_iframe").css("marginLeft", iframe_left);
    jq$("#brand_category_iframe").css("paddingTop", iframe_top);
    jq$("#brand_category_pop_back_layer").fadeIn();
    jq$("#brand_category_pop").fadeIn();
    jq$("#brand_category_iframe").fadeIn();
}



/**************************************************
* FormEachDisplay class
**************************************************/
/**
*
* コンストラクタ
*
* @class FormEachDisplay
* @access public
* @param  object
* @return object
*
* @author yokota@avanza 2010.06.14
*/
function FormEachDisplay(param) {
    this._param = param;
    this.DEFAUL_FORM = false;
}

/**
*
* セレクトボックスによるフォーム出しわけの実行
*
* @class FormEachDisplay
* @access public
* @param  なし
* @return void
*
* @author yokota@avanza 2010.06.14
*/
function FormEachDisplay_exec() {
    this._addEventHndler();
    this._initSelectBox();
}

/**
*
* セレクトボックスの初期化
*
* @class FormEachDisplay
* @access private
* @param  なし
* @return void
*
* @author yokota@avanza 2010.06.14
*/
function _FormEachDisplay_initSelectBox() {
    //デフォルトのセレクタのセット
    var selector_target = "#" + this._param.selector_id;
    if(this._param.selector_default != undefined) {
        var label           = this._param.selector_default.label;
        var tag             = this._makeOptionHtml(this._param.selector_default.id, label);
        //セレクタにアペンド
        jq$(selector_target).append(tag);
        if ( this.DEFAUL_FORM ) {
            this._viewForm(this._param.selector_add.id);
        } else {
            this._viewForm(this._param.selector_default.id);
        }
    }
    //extensionセレクタのセット
    var cnt = 0;
    for ( var key in this._param.selector_extension ) {
        id    = this._param.selector_extension[key].id;
            if ( jq$("#" + id).html() == null ) {
                continue;
            }
        label = this._param.selector_extension[key].label;
        tag   = this._makeOptionHtml(id, label);
        //セレクタにアペンド
        jq$(selector_target).append(tag);
        ++cnt;
    }
    //addセレクタのセット
    if ( cnt < this._param.max_extension_count ) {
        selector_target = "#" + this._param.selector_id;
        label           = this._param.selector_add.label;
        tag             = this._makeOptionHtml(this._param.selector_add.id, label);
        //セレクタにアペンド
        jq$(selector_target).append(tag);
    }
}
/**
*
* セレクトボックスの選択コールバック
*
* @class FormEachDisplay
* @access private
* @param  なし
* @return void
*
* @author yokota@avanza 2010.06.14
*/
function _FormEachDisplay_viewForm(block_id) {

    jq$("#" + this._param.canvas_id + " > div").each(
      function() {
          jq$(this).css("display", "none");       
      }
    );
    jq$("#" + block_id).fadeIn();
}

function _FormEachDisplay_addEventHndler() {
    var self = this;
    jq$("#" + this._param.selector_id).change(
        function() {
            var target = jq$(this).val()
            self._viewForm(target);
        }
    );
}

function _FormEachDisplay_makeOptionHtml(id, label) {
    var tag = '<option id="' 
          + id
          + '_selector'  
          + '" value="' 
          + id 
          + '">' + label 
          + '</option>';
    return tag;
}
//プロトタイプ宣言
FormEachDisplay.prototype.exec            =  FormEachDisplay_exec;
FormEachDisplay.prototype._initSelectBox  = _FormEachDisplay_initSelectBox;
FormEachDisplay.prototype._addEventHndler = _FormEachDisplay_addEventHndler;
FormEachDisplay.prototype._viewForm       = _FormEachDisplay_viewForm;
FormEachDisplay.prototype._makeOptionHtml = _FormEachDisplay_makeOptionHtml;
FormEachDisplay.prototype.formForce       = FormEachDisplay_formForce;

function _FromEachDisplayForCredit_initSelectBox() {
    var selector_target = "#" + this._param.selector_id;
    //extensionセレクタのセット
    var cnt = 0;
    for ( var key in this._param.selector_extension ) {
        id    = this._param.selector_extension[key].id;
        if ( cnt == 0 ) {
            var first_extension_id = id;
        }
        if ( jq$("#" + id).html() == null ) {
            continue;
        }
        label = this._param.selector_extension[key].label;
        tag   = this._makeOptionHtml(id, label);
        //セレクタにアペンド
        jq$(selector_target).append(tag);
        ++cnt;
    }
    //addセレクタのセット
    if ( cnt < this._param.max_extension_count ) {
        selector_target = "#" + this._param.selector_id;
        label           = this._param.selector_add.label;
        tag             = this._makeOptionHtml(this._param.selector_add.id, label);
        //セレクタにアペンド
        jq$(selector_target).append(tag);
    }
    if ( cnt == 0 ) {
        this._viewForm(this._param.selector_add.id);
        jq$("#" + this._param.selector_block_id + " > select").remove();
        jq$("#" + this._param.selector_block_id).html(this._param.default_label);
    } else if ( this.DEFAUL_FORM ) {
        this._viewForm(this._param.selector_add.id);
    } else {
        this._viewForm(first_extension_id);
    }
}

//function FormEachDisplay_formForce() {
//    var selector_target = "#" + this._param.selector_id;
//    jq$(selector_target).val(this._param.selector_add.id);
//    this._viewForm(this._param.selector_add.id);
//}

// 2010.08.17@avanza ishida modify
function FormEachDisplay_formForce(target_id) {
    if ( target_id ) {
      var selector_target = "#" + this._param.selector_id;
      jq$(selector_target).val(target_id);
      this._viewForm(target_id);
    }
}


/** クレジットカードフォーム用派生クラス **/
function FromEachDisplayForCredit(param) {
    this._param = param;
    this.DEFAUL_FORM = false;
}

//プロトタイプ宣言
FromEachDisplayForCredit.prototype.exec            =  FormEachDisplay_exec;
FromEachDisplayForCredit.prototype._initSelectBox  = _FromEachDisplayForCredit_initSelectBox;
FromEachDisplayForCredit.prototype._addEventHndler = _FormEachDisplay_addEventHndler;
FromEachDisplayForCredit.prototype._viewForm       = _FormEachDisplay_viewForm;
FromEachDisplayForCredit.prototype._makeOptionHtml = _FormEachDisplay_makeOptionHtml;
FromEachDisplayForCredit.prototype.formForce       = FormEachDisplay_formForce;
