if(!COTO) {
	var COTO = {};	
}

COTO.BookList = function(){
	var AWSLookupURL = 'bin/AWSLookup.php';

	var getItemIdList = function(count, start) {
		var idList = "", tempList, numItems, numItemsToGet, i;

		if(!start) { start = 0; }
		if(count > COTO.BookList.itemList.length) {
			count = COTO.BookList.itemList.length;
		}
		
		tempList = COTO.BookList.itemList.splice(start, count + start);
		idList = tempList.toString();
		
		return idList;
	};

	var cropTitle = function( title, maxLength ) {		
		if(!maxLength) {
			maxLength = 48;
		}
		if(title.length > maxLength ) {
			title = title.substr(0, maxLength) + "&hellip;";
		}
		return title;
	};
	
	var displayBooks = function (response) {		
		var items, container, i, title, author, detailPageURL, titleP, smallImageURL, smallImageHeight, smallImageWidth,
		mediumImageURL, mediumImageHeight, mediumImageWidth, titleA, authorP, imageDiv, imageA, image, bookDiv;
		
		items = response.responseXML.getElementsByTagName("Item");		
		container = COTO.BookList.container;
		
		for(i = 0; i < items.length; i++){
			title = cropTitle(items[i].getElementsByTagName("ItemAttributes")[0].getElementsByTagName("Title")[0].firstChild.data);

			if(items[i].getElementsByTagName("ItemAttributes")[0].getElementsByTagName("Author").length > 0){
				author = items[i].getElementsByTagName("ItemAttributes")[0].getElementsByTagName("Author")[0].firstChild.data;
			}else{
				author = items[i].getElementsByTagName("ItemAttributes")[0].getElementsByTagName("Creator")[0].firstChild.data;
			}

		 	detailPageURL = items[i].getElementsByTagName("DetailPageURL")[0].firstChild.data;
			smallImageURL = items[i].getElementsByTagName("SmallImage")[0].getElementsByTagName("URL")[0].firstChild.data;
			smallImageHeight = items[i].getElementsByTagName("SmallImage")[0].getElementsByTagName("Height")[0].firstChild.data;
			smallImageWidth = items[i].getElementsByTagName("SmallImage")[0].getElementsByTagName("Width")[0].firstChild.data;
			mediumImageURL = items[i].getElementsByTagName("MediumImage")[0].getElementsByTagName("URL")[0].firstChild.data;
			mediumImageHeight = items[i].getElementsByTagName("MediumImage")[0].getElementsByTagName("Height")[0].firstChild.data;
			mediumImageWidth = items[i].getElementsByTagName("MediumImage")[0].getElementsByTagName("Width")[0].firstChild.data;

			bookDiv = document.createElement("div");
			Element.addClassName(bookDiv, "book");

			titleP = document.createElement("p");
			titleA = document.createElement("a");
			titleA.href = detailPageURL;
			Element.addClassName(titleP, "title");
			titleA.innerHTML = title;
			titleP.appendChild(titleA);
			authorP = document.createElement("p");
			Element.addClassName(authorP, "author");
			authorP.innerHTML = author;
			imageDiv = document.createElement("div");
			imageA = document.createElement("a");
			imageA.href = detailPageURL;
			Element.addClassName(imageDiv, "image");
			image = document.createElement("img");
			image.src = smallImageURL;
//			image.height = 100;
			imageA.appendChild(image);
			imageDiv.appendChild(imageA);
		
			bookDiv.appendChild(imageDiv);
			bookDiv.appendChild(titleP);
			bookDiv.appendChild(authorP);
			
			container.appendChild(bookDiv);			
		}
	};
	
	var AWSRequest = function(itemListString){
			new Ajax.Request(AWSLookupURL, {
				method: 'get',
				parameters: 'itemId=' + itemListString,
				contentType: "application/xml",
				onSuccess: function(transport) {
					displayBooks(transport);
				},
				onFailure: function(transport){
					alert(transport.responseText);
				}			
			}
		);		
	};
	
	return {
		container: $("booklist"),
		itemList: [],
		
		getAmazonItems: function(container, itemList) {
			var listIndex, itemListString, count, maxCount, start, iterations;

			listIndex = 0;
			maxCount = 10;		// max # of request returned by AWS

			// Defaults to item with id "bookList" if no container specified
			this.container = $(container) ? $(container) : this.container;		
			
			this.itemList = itemList;
			
			iterations = Math.ceil(this.itemList.length / maxCount);
			while( iterations > 0) {					
				if(this.itemList.length < maxCount) { 
					count = this.itemList.length;
				}else{
					count = maxCount;
				}

				itemListString = getItemIdList(count);				
				AWSRequest(itemListString);

				iterations--;
			}
		}
	};

}();

