/**
 * @author kfrank
 * @version 1.0
 * @type {Object} Singleton - used write affiliate product link to page.
 * @copyright 10/25/07
 * @requires from YUI - http://yui.yahooapis.com/2.3.0/build/yahoo-dom-event/yahoo-dom-event.js
 * @requires from EXPOTV - http://www.expotv.com/js/sites/holiday/common.js
 * @requires from EXPOTV - http://www.expotv.com/js/sites/holiday/VideoHandler.js
 */
EXPOTV.Holiday.AffiliateHandler = new function()
{
	var self = this;
	
	this.init = function() {
		self.productListEl = yud.get("productlist");
		EXPOTV.Holiday.VideoHandler.onSetSelection.subscribe(EXPOTV.Holiday.AffiliateHandler.renderProductList);
	}
	
	this.clearProductList = function()
	{
		self.productListEl.innerHTML = "";
	}
	
	this.renderProductList = function(type, args)
	{
		self.clearProductList();
		var videoObj = args[0];
		for(var i=0; i < videoObj.products.length; i++)
		{
			var product = videoObj.products[i]
			
			var listEl = document.createElement("li");
			if(i == videoObj.products.length - 1 ) yud.addClass(listEl, "last");
			
			var containerEl = document.createElement("span");
			yud.addClass(containerEl, "container");
			
			listEl.appendChild(containerEl);
			
			var imgEl = document.createElement("img");
			imgEl.setAttribute("src", product.imageSource);
			imgEl.setAttribute("alt", product.title);
			
			containerEl.appendChild(imgEl);
			
			var productRightEl = document.createElement("span");
			yud.addClass(productRightEl, "productright");
			
			containerEl.appendChild(productRightEl);
			
			var h4El = document.createElement("h4");
			h4El.appendChild(document.createTextNode(product.title));
			productRightEl.appendChild(h4El);
			
			for(var j=0; j < product.affiliates.length; j++)
			{
				
				var affiliate =  product.affiliates[j];
				var anchorEl = document.createElement("a");
				anchorEl.setAttribute("href", affiliate.link);
				anchorEl.setAttribute("title", product.title);
				anchorEl.setAttribute("target", "_blank");
				anchorEl.innerHTML = affiliate.text;
				productRightEl.appendChild(anchorEl);
				if(product.affiliates.length > 1 && j < product.affiliates.length - 1)
				{
					var breakEl = document.createElement("br");
					productRightEl.appendChild(breakEl);								
				}
			}
			self.productListEl.appendChild(listEl);
		}
	}
}