//
//  カテゴリリストと場所リストを扱う
//
var CAPsList  = function() {
  var ie6_          = false;
  var categoriesID_ = new Array();
  var placesID_     = new Array();

  var categories_   = null;
  var places_       = null;

  var categoriesOK_ = false;
  var placesOK_     = false;

  var removeFavoritePlace_  = false;

  var upCategory0Title_   = '';
  var category0Title_     = '';
  var upPlace0Title_      = '';
  var place0Title_        = '';

  //ブラウザバージョン確認
  if( BrowserVer!=undefined ) {
    if( BrowserVer.isIE && !BrowserVer.isOverIE7 )  {
      //alert( 'IE6!' );
      ie6_  = true;
    }
  }

  //------------------------------------------------

  //IE対応Select-option生成
  function createOption( base, name, value )  {
    var opt;
    if( BrowserVer.isIE ) {
      opt = new Option( name, value );
    }
    else  {
      opt = document.createElement( 'option' );
      opt.value     = value;
      opt.appendChild( document.createTextNode( name ) );
    }
    //
    var len = base.options.length;
    base.options[ len ] = opt;
  }
  //IE6対応セレクト
  function selectObject( base, index )  {
    try {
      if( ie6_ )  {
        var opt = base.options[ index ].selected  = true;
        //base.options[ index ].selected  = true;
      }
      base.selectedIndex  = index;
    }
    catch( e )  {
      //alert( e );
    }
  }

  //小カテゴリ切り替え
  function changeCategory( uid, id, no )  {
    if( categories_!=null ) {
      var init_index  = 0;
      var index       = $( uid ).selectedIndex;
      var base        = $( id );
      base.innerHTML  = "";
      createOption( base, category0Title_, 0 );
      if( index>0 ) {
        var data  = categories_[ index-1 ].category;
        var max   = data.length;
        var cnt   = 0;
        while( cnt<max )  {
          createOption( base, data[ cnt ].name, data[ cnt ].id );
          //
          if( data[ cnt ].id==no )  {
            init_index  = cnt + 1;
          }
          ++cnt;
        }
      }
      selectObject( base, init_index );
    }
  }

  //大カテゴリ追加
  function insertUpCategory( uid, id, uno, no, cFunc ) {
    if( categories_!=null ) {
      var init_index  = 0;
      var base        = $( uid );
      base.innerHTML  = "";
      createOption( base, upCategory0Title_, 0 );
      var max   = categories_.length;
      var cnt   = 0;
      while( cnt<max )  {
        createOption( base, categories_[ cnt ].name, categories_[ cnt ].uid );
        //
        if( categories_[ cnt ].uid==uno )  {
          init_index  = cnt + 1;
        }
        ++cnt;
      }
      selectObject( base, init_index );
      //if( init_index!=0 ) {
        changeCategory( uid, id, no );
      //}
      //選択変更イベントの追加
      var e = function( func )  {
        var _uid    = uid;
        var _id     = id;
        var _cFunc  = cFunc;
        return {
          eventFunc: function()  {
            func( _uid, _id );
            if( _cFunc!=undefined )  {
              _cFunc();
            }
          }
        };
      }( changeCategory );
      Event.observe( base, 'change', e.eventFunc, false );
    }
  }

  //カテゴリリスト読み込み完了
  function  onSuccess_LoadCategoriesList( obj ) {
    try {
      res = eval( "(" + obj.responseText + ")" ); //JSON解析
      categories_ = res;
      //
      var m = categoriesID_.length;
      var c = 0;
      while( c<m )  {
        var d = categoriesID_[ c ];
        insertUpCategory( d.uid, d.id, d.uno, d.no, d.func );
        ++c;
      }
    } catch( e ) {
    }
    categoriesOK_ = true;
  }
  function  onFailure_LoadCategoriesList( obj ) {
    //特に処理なし
    categoriesOK_ = true;
  }

  //------------------------------------------------

  //場所変更
  function changePlace( uid, id, no ) {
    if( places_!=null ) {
      var init_index  = 0;
      var index       = $( uid ).selectedIndex;
      var base        = $( id );
      base.innerHTML  = "";
      createOption( base, place0Title_, 0 );
      if( index>0 ) {
        var data  = places_[ index-1 ].place;
        var max   = data.length;
        var cnt   = 0;
        while( cnt<max )  {
          createOption( base, data[ cnt ].name, data[ cnt ].id );
          //
          if( data[ cnt ].id==no )  {
            init_index  = cnt + 1;
          }
          ++cnt;
        }
      }
      selectObject( base, init_index );
    }
  }

  //大場所追加
  function insertUpPlace( uid, id, uno, no, cFunc ) {
    if( places_!=null ) {
      var init_index  = 0;
      var base        = $( uid );
      base.innerHTML  = '';
      createOption( base, upPlace0Title_, 0 );
      var max   = places_.length;
      var cnt   = 0;
      while( cnt<max )  {
        createOption( base, places_[ cnt ].name, places_[ cnt ].uid );
        //
        if( places_[ cnt ].uid==uno ) {
          init_index  = cnt + 1;
        }
        ++cnt;
      }
      selectObject( base, init_index );
      //if( init_index!=0 ) {
        changePlace( uid, id, no );
      //}
      //選択変更イベントの追加
      var e = function( func )  {
        var _uid    = uid;
        var _id     = id;
        var _cFunc  = cFunc;
        return {
          eventFunc: function()  {
            func( _uid, _id );
            if( _cFunc!=undefined )  {
              _cFunc();
            }
          }
        };
      }( changePlace );
      Event.observe( base, 'change', e.eventFunc, false );
    }
  }

  //場所リスト読み込み完了
  function  onSuccess_LoadPlacesList( obj ) {
    try {
      res = eval( "(" + obj.responseText + ")" ); //JSON解析
      places_ = res;
      //
      if( removeFavoritePlace_ )  {
        //人気観光地を取り除く
        var pmax  = places_.length;
        var pcnt  = 0;
        var plist = new Array();
        while( pcnt<pmax )  {
          if( places_[ pcnt ].uid!=-1 ) {
            plist.push( places_[ pcnt ] );
          }
          ++pcnt;
        }
        places_ = plist;
      }
      //
      var m = placesID_.length;
      var c = 0;
      while( c<m )  {
        var d = placesID_[ c ];
        insertUpPlace( d.uid, d.id, d.uno, d.no, d.func );
        ++c;
      }
    } catch( e ) {
    }
    placesOK_ = true;
  }
  function  onFailure_LoadPlacesList( obj ) {
    //特に処理なし
    placesOK_ = true;
  }

  //------------------------------------------------
  //------------------------------------------------

  return  {
    /**
     *  カテゴリ0タイトルの設定
     */
    SetCategory0Title: function( up, sub )  {
      upCategory0Title_ = up;
      category0Title_   = sub;
    },
    /**
     *  場所0タイトルの設定
     */
    SetPlace0Title: function( up, sub )  {
      upPlace0Title_    = up;
      place0Title_      = sub;
    },
    /**
     *  人気観光地を取り除く
     */
    SetRemoveFavoritePlace: function( flag )  {
      removeFavoritePlace_  = flag;
    },

    /**
     *  カテゴリSelectのIDを指定（htmlID, htmlID, 初期番号, 初期番号）
     */
    SetCategoriyID: function( uid, id, init_uno, init_no, changeFunc ) {
      categoriesID_.push( { uid: uid, id:id, uno:init_uno, no:init_no, func:changeFunc } );
    },
    /**
     *  場所SelectのIDを指定
     */
    SetPlaceID: function( uid, id, init_uno, init_no, changeFunc ) {
      placesID_.push( { uid: uid, id:id, uno:init_uno, no:init_no, func:changeFunc } );
    },

    /**
     *  カテゴリリストのリセット
     */
    ResetCategory: function( uid, id )  {
      $( uid ).selectedIndex  = 0;
      if( ie6_ )  {
        $( uid ).options[ 0 ].selected  = true;
      }
      changeCategory( uid, id, 0 );
    },
    /**
     *  場所リストのリセット
     */
    ResetPlace: function( uid, id ) {
      $( uid ).selectedIndex  = 0;
      if( ie6_ )  {
        $( uid ).options[ 0 ].selected  = true;
      }
      changePlace( uid, id, 0 );
    },

    /**
     *  カテゴリリストの読み込み
     */
    LoadCategoriesList: function()  {
      new Ajax.Request( '/shashin-bu/syscontents/categorieslist/type/json/',
        {
          method: 'get',
          onSuccess: onSuccess_LoadCategoriesList,
          onFailure: onFailure_LoadCategoriesList
        } );
    },
    /**
     *  場所リストのよみこみ
     */
    LoadPlacesList: function()  {
      new Ajax.Request( '/shashin-bu/syscontents/placeslist/type/json/',
        {
          method: 'get',
          onSuccess: onSuccess_LoadPlacesList,
          onFailure: onFailure_LoadPlacesList
        } );
    },

    /**
     *  準備完了？
     */
    IsReady:function() {
      if( ie6_ )  {
        if( categoriesOK_ && placesOK_ )  return true;
        return false;
      }
      return  true;
    }
  };
}();
