/*
BWSearch ビッダーズ最速検索ライブラリ
- 101 < http://zerosp.com/ >
- ver 0.1 2005/12/09
-- 公開

BWSearchはgorou氏とma.la氏のsourceをビッダーズ用に修正したものです。

Amazon最速ライブラリ
- gorou < http://rails2u.com/ >
- original by ma.la < http://ma.la/ >


This script is under Artistic License.
http://www.opensource.jp/artistic/ja/Artistic-ja.html

* 利用例
var user_id = "あなたのアフィリエイトID";
var xslt = "http://example.com/bwsearch.xsl";
var bws = new BWSearch(user_id, xslt, {
  link_id: 'あなたのリンクID',
  categ_id: '2',
  onSuccess: function(result){ alert(result.items[0].ItemName); }
});
bws.search('Beatles');
bws.search('Billy Joel');

また
bws.options.categ_id = '2';
bws.options.onCreate = function(){ alert('seaching...'); };
などとオブジェクトのインスタンスを作成後、optionsを変更することができます。

通常利用では、onCreate(検索開始直前)、onSuccess(検索終了後)
関数を定義し、その関数内で処理を行います。

** options (awsの第三引数) 
{
      link_id: '0',
      categ_id: '',
      at: '',
      tf: 'xml_lite6',
      page: 1,
      ipp: 10,
      charset: 'UTF-8',
      enc: 'UTF-8'
      onCreate: function(){},
      onSuccess: function(){},
}

検索結果resultの中身の例
{
  total: '468', // トータルカウント
  user_id: '0Aq5y5lfSmKVEb2NRcuJ.8G--', // ユーザーID
  items: { // 結果のitemの配列
    {
      ItemNo: '01234567', // 商品番号
      ItemName: 'タイトル',
      ExhibitorId: '0123456', // 販売者ID
      BidNum: '459', // 販売数 or 入札数
      CurrentPrice: '2730円', // 価格
      EndDate: '12/9 13:58', // 終了日
      RemainTime: '1時間', // 終了までの時間(オークション)
      CategoryId: '1019210237', // カテゴリID
      CategoryPath: 'グルメ・ドリンク&gt;グルメ&gt;お菓子&gt;洋菓子&gt;ケーキ・タルト&gt;その他', // カテゴリのパス
      CategoryIdPath: '80&gt;909&gt;90938&gt;90927&gt;9092705&gt;909270501&gt;1019210237', // カテゴリのパスID
      ImageUrl: '画像URI',
      ItemNum: '740', // 出品数
      AuctionType: 'FP', // 出品区分
      TradeMark: '店名', // 法人の場合のみ
    },
    { .....
    },
  }
}
*/

var BWSearch = function(user_id, xslt, options){
  return new BWSearch.Search(user_id, xslt, options);
};

BWSearch.results = {};
BWSearch.callbacks = {};

BWSearch.Utils = {
  extend: function(destination, source) {
    for (property in source) {
      destination[property] = source[property];
    }
    return destination;
  },

  bind: function(method, object) {
    return function() {
      return method.apply(object, arguments);
    }
  }
};

BWSearch.Search = function(){ this.initialize.apply(this, arguments); };
BWSearch.Search.prototype = {
  initialize: function(user_id, xslt, options){
    if(!user_id)
      throw('user_id is required.');
    if(!xslt)
      throw("xslt's uri is required.");
    this.user_id = user_id;
    this.xslt = xslt;
    this.setOptions(options);
  },

  setOptions: function(options) {
    this.options = {
      link_id: '273642',
      categ_id: '0',
      at: '',
      tf: 'xml_lite4',
      page: 1,
      ipp: 10,
      charset: 'UTF-8',
      enc: 'UTF-8'
    };
    BWSearch.Utils.extend(this.options, options || {});
  },

  search: function(keyword){
    if(!keyword)
      return false;
    var script = document.createElement('script');
    script.charset = 'UTF-8';
    var name = '';
    with(this.options){
      script.src = 'http://xml.bidders.co.jp/bep/xml?link_id=' + link_id + '&user_id=' + this.user_id + '&keyword=' + encodeURI(keyword) + '&categ_id=' + categ_id + '&at=' + at + '&tf=' + tf + '&page=' + page + '&ipp=' + ipp + '&xsl=' + this.xslt + '&charset=' + charset + '&enc=' + enc;
      name = categ_id + '_' + keyword + '_' + page;
    }
    BWSearch.callbacks[name] = BWSearch.Utils.bind(function(result){
      this.count = result.count;
      if(this.options.onSuccess)
        BWSearch.Utils.bind(this.options.onSuccess, this)(result);
    }, this);

    if(typeof this.options.onCreate == 'function')
      this.options.onCreate();
    document.body.appendChild(script);
  }
};
