/**
 *  amazonアフィリエイト
 *  prototype.js,Affi_UTILが必要です。
 */
var Affi_amazon = function() {
  var baseURL_;
  var amazon_affi_id = 'shashin-bu-22'

  function draw( json ) {
    var _s_tags = '<div style="padding-top: 15px;">';
    var _e_tags = '<br style="clear:both;" /></div>';
    var id  = json.shift();
    if( id )  {
      var html  = '';
      var cnt   = 0;
      var max   = json.length;
      for( ; cnt<max; ) {
        if( (cnt%3)==0 && cnt!=max ) html += _s_tags;
        var item  = json[ cnt ];
        if( item.asin ) {
          ++cnt;

          url  = 'http://www.amazon.co.jp/exec/obidos/ASIN/' + item.asin + '/' + amazon_affi_id + '/ref=nosim';
          if( (cnt%3)==0 ) {
            html += '<div align="center" style="float: left; width: 160px;">'
          }
          else {
            html += '<div align="center" style="float: left; width: 160px; margin-right: 20px;">'
          }
          if( item.imgMedium ) {
            html += '<a href="' + url + '" target="_blank"><img src="' + item.imgMedium + '" alt="" border="0"></a><br>';
          }
          else {
            html += '<a href="' + url + '" target="_blank"><img src="http://wms.assoc-amazon.jp/JP/img/noImageAvailable-90px.gif" alt="" border="0"></a><br>';
          }
          html += '<div style="margin-top: 3px;"><a href="' + url + '">' + Affi_UTIL.STRING.HtmlSpecialChars( item.title ) + '</a><br>';
          if( item.avgRating!=null && item.totalReviews!=null ) {
            var rating  = new Number( item.avgRating );
            var reviews = new Number( item.totalReviews );
            if( reviews>0 ) {
                if( rating>4.75 )       html += '<img src="http://g-images.amazon.com/images/G/01/detail/stars-5-0.gif"/>';
                else if( rating>4.25 )  html += '<img src="http://g-images.amazon.com/images/G/01/detail/stars-4-5.gif"/>';
                else if( rating>3.75 )  html += '<img src="http://g-images.amazon.com/images/G/01/detail/stars-4-0.gif"/>';
                else if( rating>3.25 )  html += '<img src="http://g-images.amazon.com/images/G/01/detail/stars-3-5.gif"/>';
                else if( rating>2.75 )  html += '<img src="http://g-images.amazon.com/images/G/01/detail/stars-3-0.gif"/>';
                else if( rating>2.25 )  html += '<img src="http://g-images.amazon.com/images/G/01/detail/stars-2-5.gif"/>';
                else if( rating>1.75 )  html += '<img src="http://g-images.amazon.com/images/G/01/detail/stars-2-0.gif"/>';
                else if( rating>1.25 )  html += '<img src="http://g-images.amazon.com/images/G/01/detail/stars-1-5.gif"/>';
                else if( rating>0.75 )  html += '<img src="http://g-images.amazon.com/images/G/01/detail/stars-1-0.gif"/>';
                else if( rating>0.25 )  html += '<img src="http://g-images.amazon.com/images/G/01/detail/stars-0-5.gif"/>';
                else                    html += '<img src="http://g-images.amazon.com/images/G/01/detail/stars-0-0.gif"/>';
                html += '(' + reviews + ')<br />';
            }
          }
          if( item.newPrice )       html += '(\u65B0\u54C1)' + item.newPrice;
          else if( item.usedPrice ) html += '(\u4E2D\u53E4)' + item.usedPrice;
          html += '</div></div>';
          //
          if( (cnt%3)==0 && cnt!=max ) html += _e_tags;
        }
      }
      if( html!='' ) {
        $( id ).innerHTML = html + _e_tags;
      }
    }
  }

  function success_amazon( obj ) {
    try {
      res = eval( "(" + obj.responseText + ")" );
      draw( res );
    }
    catch( e ) {
    }
  }
  function failire_amazon( obj ) {
  }

  return {
    SetBaseURL: function( url ) {
      baseURL_ = url;
    },
    CallJSON: function( keyword, maxCount )  {
      var url = baseURL_ + '../kabegami/affi/amazon.php/?code=u';
      if( null!=keyword ) url += '&word=' + encodeURIComponent( keyword );
      if( maxCount==null )        maxCount  = { b:3 };
      if( maxCount.b!=null )      url = url+'&mode=b&json=AMAZON_Book&max='+maxCount.b;
      else if( maxCount.d!=null ) url = url+'&mode=d&json=AMAZON_DVD&max='+maxCount.d;

      new Ajax.Request( url, {
          method: 'get',
          onSuccess: success_amazon,
          onFailure: failire_amazon
        } );
    }
  }
}();

