/***************************************
Amazon Ads JavaScript.
----------------------------------------
        Copyright (C) 2006 www.xlune.com
**********************************C@NI*/

/*
 * count
 */
var amzCount = ++amzCount || 0;

/*
 * amzAds Object
 */
var amzAds = amzAds ? amzAds : {};

/*
 * amzAds Actions
 */
amzAds.actions = amzAds.actions ? amzAds.actions : [];

/*
 * onload flg
 */
amzAds.onLoad = false;


/*
 * config
 */
amzAds.config = new (function(){
	this.apiUrl = 'http://aws.xlune.com/amazoni_xslt_proxy/ja';
	this.amzid_prefix = "amazon_box_";
	this.isNotAdult = true;
	this.paramSearchBase = {
		'Service':'AWSECommerceService',
		'AssociateTag':'xlune-22',
		'AWSAccessKeyId':'1BRM8KMFT6YV3RH94DR2',
		'Operation':'ItemSearch',
		'ContentType':'text/javascript',
		'SearchIndex':'Books',
		'ResponseGroup':'Medium',
		'Version':'2007-10-29',
		'ItemPage':'1',
		'Style':'http://ads.xlune.com/amz/xml2json_ecs4.xsl',
		'amzid': null
	};
	this.paramAsinBase = {
		'Service':'AWSECommerceService',
		'AssociateTag':'xlune-22',
		'AWSAccessKeyId':'1BRM8KMFT6YV3RH94DR2',
		'Operation':'ItemSearch',
		'ContentType':'text/javascript',
		'ResponseGroup':'Medium',
		'Version':'2007-10-29',
		'ItemPage':'1',
		'Style':'http://ads.xlune.com/amz/xml2json_ecs4.xsl',
		'amzid': null
	};
	this.noimg = 'http://ads.xlune.com/amz/noimage.gif';
});
/*
Books: 和書
ForeignBooks: 洋書
Electronics: エレクトロニクス
VideoGames: ゲーム
DVD: DVD
Software: ソフトウェア
Apparel: アパレル＆シューズ
Toys: おもちゃ
Classical: クラシック音楽
HealthPersonalCare: ヘルス＆ビューティー
Hobbies: ホビー
Kitchen: ホーム＆キッチン
Music: ミュージック
MusicTracks: ミュージックトラック
SportingGoods: スポーツ＆アウトドア
VHS: VHS
Video: ビデオ
Watches: 時計
Baby: ベビー＆マタニティ
*/



/*
 * Script Tags Ajax
 */
amzAds.ajax = function(url,num){
	this.urlCall = url;
	this.idNum = num;
	this.initialize();
};
amzAds.ajax.prototype = {
	initialize: function(){
		this.module = document.createElement('script');
		this.module.setAttribute("type", "text/javascript");
		this.module.setAttribute("charset", "utf-8");
		this.module.setAttribute("id", amzAds.config.amzid_prefix + this.idNum +'_loader');
		this.head = document.getElementsByTagName("head").item(0);
	},
	update: function(param){
		var i,url;
		var arr = [];
		url = this.urlCall;
		if(typeof(param) == 'object'){
			for(i in param){
				arr.push(i+'='+param[i]);
			}
		}
		arr.push('noCache='+(new Date()).getTime());
		url += '?'+arr.join('&');
		this.module.setAttribute("src", url);
		this.head.appendChild(this.module);
	}
};



/*
 * Loader
 */
amzAds.loader = function(){
	amzAds.onLoad = true;
	if(amzAds.actions.length > 0){
		var o = amzAds.actions.shift();
		var ajax = new amzAds.ajax(o.url,o.count);
		ajax.update(o.param);
	}
};

/*
 * filter
 */
amzAds.filter = function(_list){
	var newObj = [];
	for(var i=0,max=_list.length;i<max;i++){
		if(!_list[i]) continue;
		if(amzAds.config.isNotAdult && _list[i]['isAdult']) continue;
		newObj.push(_list[i]);
	}
	return newObj;
};


/*
 * Writer
 */
amzAds.write = function(_boxId, _list, _keys, _mode){
	_list = this.filter(_list);
	if(_list.length > 0) {
		var _num = Math.floor(Math.random()*(( _list.length-2 >= 0 )?_list.length-2:0));
		var _box = document.getElementById(_boxId);
		if(_box) _box.innerHTML = amzAds.template(_list[_num]);
	}else{
		if(_keys){
			amzAds.make(
				_keys,
				(_mode) ? _mode : '',
				_boxId
			);
		}
	}
	//amzAds.loader();
};


/*
 * Template
 */
amzAds.template = function(_o){
	_o['image-s'] = _o['image-s'] || amzAds.config.noimg;
	var _html = '\
	<div class="ads-amz-box"> \
		<div class="ads-amz-left"> \
			<a href="'+_o['url']+'"> \
				<img src="'+_o['image-s']+'" alt="'+_o['title']+'" /> \
			</a> \
		</div> \
		<div class="ads-amz-right"> \
			<h4 class="ads-amz-title"> \
				<a href="'+_o['url']+'"> \
				'+_o['title']+' \
				</a> \
			</h4> \
			<p class="ads-amz-text">'+_o['catalog']+'</p> \
			<p class="ads-amz-text">'+_o['listprice']+'</p> \
		</div> \
	</div> \
	';
	return _html;
};


/*
 * onLoad
 */
//if(!amzCount){
	if(window.addEventListener) {
		window.addEventListener('load', amzAds.loader, false);
	} else if (window.attachEvent) {
		window.attachEvent('onload', amzAds.loader);
	};
//}


amzAds.make = function (_key,_mode,_amzid){
	var param;
	var keys = '';
	if(t = _key.match(/^#(.+)$/)){
		param = amzAds.config.paramAsinBase;
		param['ItemId'] = t[1];
		param['Operation'] = 'ItemLookup';
		param['IdType'] = 'ASIN';
	}else{
		var _str = _key.replace(/%20/g, "");
		if(_str.match(/,/)){
			keys = _str.split(",");
			_str = keys.shift();
			keys = keys.join(',');
		}
		param = amzAds.config.paramSearchBase;
		param['Keywords'] = encodeURI(_str);
		param['Operation'] = 'ItemSearch';
	}
	param['amzid'] = _amzid;
	
	if(_mode) param['Operation'] = _mode;
	param['amzkeys'] = keys;
	//if(_arg.length > 2) param['max'] = _arg[2];
	
	amzAds.actions.push({
		url:amzAds.config.apiUrl,
		count:_amzid,
		param:param
	});
	
	if(amzAds.onLoad) amzAds.loader();
};

(function(){
	var s,_arg,t,ajax;
	
	document.write('<div id="'+(amzAds.config.amzid_prefix + amzCount)+'"></div>');
	
	s = document.getElementsByTagName("script");
	
	for(var i=0,max=s.length; i<max; i++){
		if(s.item(i) && typeof(s.item(i).src) == 'string' 
			&& s.item(i).src.match(/(^|\/)amazon\.js($|\?)/)){
			s = s.item(i).src.match(/\?(.+)$/);
		}
	}
	
	if(!s || s.length < 2) return false;
	
	_arg = s[1].match(/:/) ? s[1].split(':') : [s[1]] ;
	if(_arg[0].match(/,/)) _arg[0] += ',Web';
	amzAds.make(
		_arg[0],
		(_arg.length > 1) ? _arg[1] : '',
		amzAds.config.amzid_prefix + amzCount
	);
})();


