/*
AWSearch
Amazon 'The Fastest' search library

- original by ma.la
- first coded by gorou
- international version by TEDDY-G
	って言ってもAmazonのURL変えたのとSCRIPTタグ挿入先変えただけなんだよね

You can use and distribute this package only under the artistic license.


*/

/*
AWSearch
Amazon最速検索ライブラリ

- gorou < http://rails2u.com/ >
- original by ma.la < http://ma.la/ >

- ver 0.1 2005/12/02
-- 公開

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

* 利用例
var dev_token = "あなたのdeveloper token";
var xslt = "http://example.com/awsearch.xsl";
var aws = new AWSearch(dev_token, xslt, {
  mode: 'music-jp',
  onSuccess: function(result){ alert(result.items[0].title + ' - ' + result.items[0].creator.join(',')); }
});
aws.search('Beatles');
aws.search('Billy Joel');

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

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

** options (awsの第三引数) 
{
      asid: 'nomusinolife-22',
      searchType: 'KeywordSearch',
      mode: 'books-jp',
      type: 'lite', // lite or heavy
      page: 1,
      onCreate: function(){},
      onSuccess: function(){},
      locale: 'jp'
}

検索結果resultの中身の例
{
  total: '468', // トータルカウント
  asid: 'nomusinolife-22', // アソシエイトid
  items: { // 結果のitemの配列
    {
      asin: '19827364911', //ASIN
      title: 'タイトル',
      creator: ['ごろう', 'antipop君'], // 制作者の配列
      Manufacturer: '出版社',
      OurPrice: 680, // 定価
      ListPrice: 625, // 販売価格
      UserPrice: 100, // 中古価格
      pubDate: '2005/01', // 発売日
      isExist: true, // 存在するか
      stat: '通常一週間以内に発送', //発送時期
      review: [] // レビューの配列
    },
    { .....
    },
  }
}
*/

var AWSearch = function(dev_token, xslt, options){
  return new AWSearch.Search(dev_token, xslt, options);
};

AWSearch.results = {};
AWSearch.callbacks = {};

AWSearch.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);
    }
  }
};

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

  setOptions: function(options) {
    this.options = {
      asid: 'nomusinolife-22',
      searchType: 'KeywordSearch',
      mode: 'books-jp',
      type: 'lite',
      page: 1,
      locale: 'jp'
    };
    AWSearch.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.amazon.com/onca/xml3?t=' + asid + '&dev-t=' + this.dev_token + '&' + searchType + '=' + encodeURI(keyword) + '&mode=' + mode + '&type=' + type + '&page=' + page + '&f=' + this.xslt + '&locale=' + locale;
//      script.src = 'http://xml-jp.amznxslt.com/onca/xml3?t=' + asid + '&dev-t=' + this.dev_token + '&' + searchType + '=' + encodeURI(keyword) + '&mode=' + mode + '&type=' + type + '&page=' + page + '&f=' + this.xslt + '&locale=' + locale;
      name = mode + '_' + keyword + '_' + page;
    }
    AWSearch.callbacks[name] = AWSearch.Utils.bind(function(result){
      this.count = result.count;
      if(this.options.onSuccess)
        AWSearch.Utils.bind(this.options.onSuccess, this)(result);
    }, this);

    if(typeof this.options.onCreate == 'function')
      this.options.onCreate();
    //document.body.appendChild(script);
	//--------
    var list=document.documentElement.getElementsByTagName("*");
	for(var i=0; i<list.length; i++) {
   		if(list[i].tagName=="HEAD") {
       		break;
   		}
   	}
	var HeadTag=list[i];
	HeadTag.insertBefore(script, list[i+1]);

  }
};

