var Mypage = Class.create();

// 左枠のエレメント
Mypage.left_side = 'm-mypage-left';

// メニュー部のエレメント
Mypage.menu_element = 'm-mypage-menu';

// Top - ポイント付きコンテンツの利用状況
Mypage.top_pointstatus = 'm-mypage-box-pointstatus';
// Top - プレゼント応募状況
Mypage.top_presentstatus = 'm-mypage-box-presentstatus';
// Top - お知らせ
Mypage.top_info   = 'm-mypage-box-info';
// Top - マイクリップ
Mypage.top_myclip = 'm-mypage-box-myclip';
// Top - キーワード
Mypage.top_keyword = 'm-mypage-box-samekeyword';
// Top - 注目商品情報
Mypage.top_goodsinfo = 'm-mypage-box-goodsinfo';
// Top - お気に入り店舗
Mypage.top_shop = 'm-mypage-box-favshop';
// Top - ポイントアップ情報
Mypage.top_pointup = 'm-mypage-box-pointup';
// Top - オトクメール一覧
Mypage.top_otokumail = 'm-mypage-box-otokumail';
// Top - リサーチメール一覧
Mypage.top_researchmail = 'm-mypage-box-researchmail';

// Top - ランキング投票履歴
Mypage.top_ranking_vote = 'm-mypage-box-ranking-vote';
// Top - ランキング作成履歴
Mypage.top_ranking_make = 'm-mypage-box-ranking-make';
// Top - ランキングコメント履歴
Mypage.top_ranking_comment = 'm-mypage-box-ranking-comment';
// Top - 商品評価履歴
Mypage.top_shopping_read = 'm-mypage-box-shopping-read';
// Top - 商品閲覧履歴
Mypage.top_shopping_review = 'm-mypage-box-shopping-review';


Mypage.prototype = {
  initialize : function() {},

  /**
   * ベースURLの取得
   * 
   * @return Base URL
   */
  getCurrentBaseURL : function() {
    return document.location.protocol + "//" + document.location.hostname + "/";
  },

  /**
   * 左枠部分の表示
   *
   * @param 履歴モードフラグ
   */
  setLeftSide : function(history) {
    var url   = getCurrentBaseURL();
    var param = 'nv=member_common_info';

    if (history) {
        param += '&history=' + history;
    }

    this.loadContents(url, param, Mypage.left_side);
  },

  /**
   * マイページメニューの表示
   *
   * @param 現在選択中のアクション
   */
  setMypageMenu : function(action) {
    var url   = getCurrentBaseURL();
    var param = 'nv=member_mypage_menu&current_action=' + action;

    this.loadContents(url, param, Mypage.menu_element);
  },

  /**
   * 他人の左枠部分の表示
   *
   * @param 履歴モードフラグ
   * @param 他人のメンバーID(暗号化)
   */
  setOtherLeftSide : function(history, other_id) {
    var url   = getCurrentBaseURL();
    var param = ['nv=member_common_other_info',
		 '&other_id=',
		 other_id].join('');

    if (history) {
      param += '&history=' + history;
    }

    this.loadContents(url, param, Mypage.left_side);
  },

  /**
   * 他人のマイページメニュー表示
   *
   * @param 現在選択中のアクション
   * @param 他人のメンバーID(暗号化)
   */
  setOtherMypageMenu : function(action, other_id) {
    var url   = getCurrentBaseURL();
    var param = ['nv=member_mypage_other_menu',
		 '&other_id=',
		 other_id,
		 '&current_action=',
		 action].join('');

    var ajax  = new Ajax.Updater(Mypage.menu_element,
				 url, {
				   parameters : param,
				   method : 'get',
				   requestHeaders: ['If-Modified-Since', 'Thu, 1 Jan 1970 00:00:00 GMT']
				 });
  },

  /**
   * Topページコンテンツ表示
   *
   * @param 現在選択中のアクション
   */
  setTopContents : function(action) {
    var url   = getCurrentBaseURL();

    var contents = {};
    // メイン
    if (action == 'member_mypage_top') {
      // ポイント付きコンテンツの利用状況
      contents['member_mypage_top_pointstatus'] = Mypage.top_pointstatus;
      // プレゼント応募状況
      contents['member_mypage_top_presentstatus'] = Mypage.top_presentstatus;
      // お知らせ
      contents['member_mypage_top_information'] = Mypage.top_info;
      // キーワード
      contents['member_mypage_top_keyword'] = Mypage.top_keyword;
      // 注目商品情報
      contents['member_mypage_top_goodsinfo'] = Mypage.top_goodsinfo;
      // マイクリップ
      contents['member_mypage_top_myclip'] = Mypage.top_myclip;
      // お気に入り店舗
      contents['member_mypage_top_shop'] = Mypage.top_shop;
      // ポイントアップ情報
      contents['member_mypage_top_pointup'] = Mypage.top_pointup;
      // オトクメール一覧
      contents['member_mypage_top_otokumail'] = Mypage.top_otokumail;
      // リサーチメール一覧
      contents['member_mypage_top_researchmail'] = Mypage.top_researchmail;
    } else if (action == 'member_mypage_top_history') { // 履歴
      // ランキング投票履歴
      contents['member_mypage_top_ranking_history_vote'] = Mypage.top_ranking_vote;
      // ランキング作成履歴
      contents['member_mypage_top_ranking_history_make'] = Mypage.top_ranking_make;
      // ランキングコメント履歴
      contents['member_mypage_top_ranking_history_comment'] = Mypage.top_ranking_comment;
      // 商品閲覧履歴
      contents['member_mypage_top_shopping_history_read'] = Mypage.top_shopping_read;
      // ランキングコメント履歴
      contents['member_mypage_top_shopping_history_review'] = Mypage.top_shopping_review;
    }
      
    for (var act in contents) {
      var param   = 'nv=' + act;
      var element = contents[act];
      this.loadContents(url, param, element);
    }

  },

  /**
   * Topページコンテンツ表示 他人のページ
   *
   * @param 現在選択中のアクション
   * @param 対象のメンバーID(暗号化)
   */
  setOtherTopContents : function(action, other_id) {
    var buf   = document.URL.split("?");
    var url   = getCurrentBaseURL();

    var contents = {};
    // メイン
    // キーワード
    contents['member_mypage_other_top_keyword'] = Mypage.top_keyword;
    // マイクリップ
    contents['member_mypage_other_top_myclip'] = Mypage.top_myclip;
    // ランキング作成履歴
    contents['member_mypage_other_top_ranking_history_make'] = Mypage.top_ranking_make;
    // お気に入り店舗
    contents['member_mypage_other_top_shop'] = Mypage.top_shop;

    for (var act in contents) {
      var param   = 'nv=' + act + '&other_id=' + other_id;
      var element = contents[act];
      this.loadContents(url, param, element);
    }

  },

  /**
   * コンテンツを指定エレメントにロードする
   *
   * @param URL
   * @param リクエストパラメータ
   * @param エレメント名
   */
  loadContents : function(url, param, element) {

    var ajax  = new Ajax.Updater(element,
				 url, {
				   parameters : param,
				   method : 'get',
				   requestHeaders: ['If-Modified-Since', 'Thu, 1 Jan 1970 00:00:00 GMT']
				 });
  }

};


/**
 * ベースURLの取得
 * 
 * @return Base URL
 */
function getCurrentBaseURL() {
  return document.location.protocol + "//" + document.location.hostname + "/";
}


/**
 * 件数選択時の自動遷移
 */
function submitLimitForm(location)
{
    var name = 'limit-form-' + location;
    var form = $(name);

    var elems  = Form.getElements(form);
    var params    = '';
    var delimiter = '';

    elems.each(function (v, idx) {
                   if (v.name == 'delimiter') {
		       delimiter = v.value;
                   } else {
                       params += '/' + v.name + '/' + v.value;
                   }
               }
    );

    window.location.href = form.action + '/' + delimiter + params;
}

/**
 * キーワード部分の切り替え
 *
 * @param キーワードタイプ 0: shopping 1: ranking
 */
function updateTopKeyword(type)
{
    var url     = getCurrentBaseURL();
    var param   = "nv=member_mypage_top_keyword&type=" + type;
    var element = Mypage.top_keyword;
    var ajax    = new Ajax.Updater(element,
				   url, {
				     parameters : param,
				     method : 'get',
		                     requestHeaders: ['If-Modified-Since', 'Thu, 1 Jan 1970 00:00:00 GMT']
				   });
}

/**
 * キーワード部分の切り替え(他人のページ用)
 *
 * @param キーワードタイプ 0: shopping 1: ranking
 * @param 対象のメンバーID(暗号化)
 */
function updateOtherTopKeyword(type, other_id)
{
    var url     = getCurrentBaseURL();
    var param   = ["nv=member_mypage_other_top_keyword&other_id=",
		   other_id,
		   "&type=",
		   type].join('');

    var element = Mypage.top_keyword;
    var ajax    = new Ajax.Updater(element,
				   url, {
				     parameters : param,
				     method : 'get',
				     requestHeaders: ['If-Modified-Since', 'Thu, 1 Jan 1970 00:00:00 GMT']
				   });
}

/**
 * フォームのサブミット
 *
 * @param フォームのid
 */
function submitForm(id)
{
  var frm = $(id);
  frm.submit();
}

// 画像サイズ調整
function imageAutoReSize( inImage , maxW, maxH )
{
    var maxRatio = maxW / maxH;
    var TmpImage = new Image();
    TmpImage.src = inImage.src;
    var TmpW = TmpImage.width;
    var TmpH = TmpImage.height;
    var TmpRatio = TmpW / TmpH;
    if ( !TmpW ) {
        inImage.width = maxW;
        return;
    }
    if (TmpW < maxW && TmpH < maxH) {
        inImage.width = TmpW;
        inImage.height = TmpH;
        return new Array(inImage.width, inImage.height);
    }

    if(TmpRatio < maxRatio){
        inImage.width = Math.floor(maxH * TmpRatio);
        inImage.height = maxH;
    } else {
        inImage.width = maxW;
        inImage.height = Math.floor(maxW / TmpRatio);
    }
}


/**
 * キーワード検索
 *
 * @param type
 * @param word
 */
function execKeywordSearch(type, word)
{
    var f = $('keyword-search-form');
    var search_word = document.createElement('input');
    search_word.type = 'hidden';
    search_word.value = word;

    if (type == '0') {
	search_word.name = 'review_keyword';
    } else if (type == '1') {
	search_word.name = 'sdw';
	var sdk   = document.createElement('input');
	sdk.type  = 'hidden';
	sdk.name  = 'sdk';
	sdk.value = 300;
	f.appendChild(sdk);
    }

    f.appendChild(search_word);
    f.submit();
}

/**
 * 公開状態変更
 *
 * @param ランキングID
 * @param 公開状態
 */
function updateRankingStatus(id, status)
{
  var params = ["nv=member_mypage_ranking_history_make_update",
                "&rankingid=",
                id,
                "&open_flag=",
                status].join('');
  var url    = '/';
  var elem = "open-flag-status-" + id;
  var ajax = new Ajax.Updater(elem, url, {
    method: "post",
    parameters: params
  });
}

/**
 * Top ポイントアップ情報の切り替え
 *
 * @param type
 * @param word
 */
function ShowPointUpList(StyleNo) {
    switch ( StyleNo ) {
        // もっと見る
        case 1:
            document.getElementById("limit-table").style.display = "block";
            document.getElementById("non-limit-table").style.display = "none";
            break;
        // 閉じる
        case 2:
            document.getElementById("limit-table").style.display = "none";
            document.getElementById("non-limit-table").style.display = "block";
            break;
    }
}
