Amazon = Class.create();
Amazon.prototype = {
	initialize:		function() {
						this.update();
					},
	
	update:			function() {
						this._initDisplay();
						this._startRequest();
					},
	
	_initDisplay:	function() {
					},
	
	_startRequest:	function() {
						var now = new Date();
						var uri = '/amazon/recommend';
						uri += '?';
						uri += now.getTime();
						var ajax = new Ajax.Request(uri,
													{
														method:		'get',
														onComplete:	this._onComplete,
														onFailure:	this._onFailure
													}
												   );
					},
	
	_onFailure:		function(req) {
						// 失敗
						var obj = $('amazon_affiliate2_');
						if(obj) {
							while(obj.hasChildNodes()) {
								obj.removeChild(obj.lastChild);
							}
							var p = obj.appendChild(document.createElement('p'));
							p.appendChild(document.createTextNode('読込失敗'));
							p.setAttribute('style', 'text-indent: 0; margin-top: 0.5em; margin-bottom: 0.5em;');
						}
					},
	
	_onComplete:	function(httpobj) {
						var domdoc = httpobj.responseXML;
						if(!domdoc) {
							this._onFailure();
							return;
						}

						while($('amazon_affiliate2_').hasChildNodes()) {
							$('amazon_affiliate2_').removeChild($('amazon_affiliate2_').lastChild);
						}

						var ul = $('amazon_affiliate2_').appendChild(document.createElement('ul'));
						var x_items = domdoc.getElementsByTagName('item');
						for(var i = 0; i < x_items.length; ++i) {
							var x_item = x_items.item(i);
							var data_url			= "http://www.amazon.co.jp/";
							var data_title			= "(unknown)";
							var data_title_full		= "";
							var data_price			= "?";
							var data_image_url		= '';
							var data_image_width	= 0;
							var data_image_height	= 0;
							
							for(var node = x_item.firstChild; node; node = node.nextSibling) {
								if(node.nodeType == 1) {
									if(node.nodeName == 'url') {
										data_url = node.firstChild.nodeValue;
									} else if(node.nodeName == 'title') {
										data_title = node.getAttribute('short');
										data_title_full = node.firstChild.nodeValue;
									} else if(node.nodeName == 'price') {
										data_price = node.firstChild.nodeValue;
									} else if(node.nodeName == 'image') {
										for(var node2 = node.firstChild; node2; node2 = node2.nextSibling) {
											if(node2.nodeType == 1) {
												if(node2.nodeName == 'url') {
													data_image_url = node2.firstChild.nodeValue;
												} else if(node2.nodeName == 'width') {
													data_image_width = node2.firstChild.nodeValue;
												} else if(node2.nodeName == 'height') {
													data_image_height = node2.firstChild.nodeValue;
												}
											}
										}
									}
								}
							}

							var li = ul.appendChild(document.createElement('li'));
							
							if(data_image_url) {
								var div = li.appendChild(document.createElement('div'));
								div.setAttribute('class', 'text_center');
								div.setAttribute('className', 'text_center');
								
								var a = div.appendChild(document.createElement('a'));
								a.setAttribute('href', data_url);
								a.setAttribute('target', '_blank');
								
								var img = a.appendChild(document.createElement('img'));
								img.setAttribute('src', data_image_url);
								img.setAttribute('width', data_image_width);
								img.setAttribute('height', data_image_height);
								img.setAttribute('alt', '');
								img.setAttribute('title', data_title_full);
							}
							
							var a = li.appendChild(document.createElement('a'));
							a.setAttribute('href', data_url);
							a.setAttribute('target', '_blank');
							a.appendChild(document.createTextNode(data_title));
							if(data_title != data_title_full) {
								a.setAttribute('title', data_title_full);
							}
							
							var div = li.appendChild(document.createElement('div'));
							div.setAttribute('class', 'text_right');
							div.setAttribute('className', 'text_right');
							div.appendChild(document.createTextNode(String.fromCharCode(0xA5) + data_price));
						}
					}
};

function amazonHelp_GetTextContent(node) {
//	if(typeof node.textContent != "undefined") {
//		return node.textContent;
//	} else {
		return node.innerText;
//	}
}

var myAmazon = undefined;

function initAmazon() {
	if(!myAmazon) {
		myAmazon = new Amazon();
	}
}

Event.observe(window, 'load', initAmazon, false);
