var ValidClick = function(cfg) {setTimeout(function(){ValidClick(cfg);},500);};

(function setup_validclick_feed(){
	var loader = new YAHOO.util.YUILoader();
	loader.addModule({
		name: "worthAThousandWords",
		type: "js",
		varName: "worthAThousandWords",
		fullpath: "http://js.WorthAThousandWords.com/IA.jsh?pid=5400.4&subid=yellowpagecity"	
	});
	loader.require("connection", "dom", "event", "worthAThousandWords");
	loader.onSuccess = init;
	loader.insert();
	
	function init(){		
		ValidClick = function(config){
			var defaults = {
				city: "",
				state: "",
				keyword: "",
				startIndex: 0,
				endIndex: 10,
				titleLength: 35,
				descLength: 90,
				imgHeight: 40,
				makeDescLink: false,
				showTitle: true,
				container: "valid-click-feed",
				display: function(o){
					if(o.responseXML.getElementsByTagName("LISTING").length < 1){
						handleFailure(o);
						return;
					}
					var xmlDom = o.responseXML.documentElement;
					
					if(xmlDom.getElementsByTagName("LISTING").length < config.endIndex)
						config.endIndex = xmlDom.getElementsByTagName("LISTING").length;
						
					var div = YAHOO.util.Dom.get(config.container);
					div.innerHTML = config.showTitle ? "<div id='validClick-sponsored'>Sponsored Listings</div>" : "";
					
					var ul = document.createElement("ul");
					for(var i = config.startIndex; i<config.endIndex; i++){
						var re = new RegExp(/<b>|<\/b>/gi);
						
						var listing = xmlDom.getElementsByTagName("LISTING")[i];
						var listingName = listing.getElementsByTagName("TITLE")[0].firstChild.nodeValue.replace(re, "");
						var listingDesc = listing.getElementsByTagName("DESCRIPTION")[0].firstChild.nodeValue.replace(re, "");
						var listingURL = unescape(listing.getElementsByTagName("LINK")[0].firstChild.nodeValue);
						var listingHost = unescape(listing.getElementsByTagName("SITEHOST")[0].firstChild.nodeValue);
	
						var li = document.createElement("li");
						var divCont = document.createElement("div");
						divCont.className = "valid-click-feed-div";
						var aimg = document.createElement("a");
						aimg.setAttribute("href", listingURL);
						aimg.setAttribute("title", listingName);
						aimg.setAttribute("target", "_blank");
						
						var img = document.createElement("img");
						var imgsrc = GetIAProductImageDetails(
							config.city + "+" + config.state,
							listingName.replace(/'/g, ""), 
							listingDesc.replace(/'/g, ""),
							listingHost.replace(/'/g, ""),
							'',
							false
						);
						img.setAttribute("src", imgsrc);
						img.setAttribute("height", config.imgHeight);
						img.setAttribute("width", config.imgHeight);
						img.setAttribute("style", "height:" + config.imgHeight + "px;width:" + config.imgHeight + "px;padding: 0px 6px 0px 0px !important;border: 0;");
						img.style.padding = "0px 6px 0px 0px";
						
						aimg.appendChild(img);
						divCont.appendChild(aimg);
						
						var h5 = document.createElement("h5");
						h5.className = "feedlink";
						
						var a = document.createElement("a");
						a.setAttribute("href", listingURL);
						a.setAttribute("title", listingName);
						a.setAttribute("target", "_blank");
						a.className = "feed-title";
						a.innerHTML = truncate_str(listingName, config.titleLength);
						
						var description;
						if(config.makeDescLink){
							description = document.createElement("a");
							description.setAttribute("href", listingURL);
							description.setAttribute("title", listingName);
							description.setAttribute("target", "_blank");
							description.className = "feed-desc";
							description.innerHTML = truncate_str(listingDesc, config.descLength);
						}
						else{
							description = document.createElement("p");
							description.className = "feedDescription";
							description.innerHTML = truncate_str(listingDesc, config.descLength);
						}
						
						h5.appendChild(a)
						divCont.appendChild(h5);
						divCont.appendChild(description);
						li.appendChild(divCont);
						ul.appendChild(li);
					}
					div.appendChild(ul);
				}
			};
				
			config = extend(defaults, config);
			
			function handleSuccess(o){
				config.display(o);
				YAHOO.util.Get.script("http://feed.validclick.com/check.php?affid=1435");
			}
			function handleFailure(o){
				YAHOO.util.Dom.get(config.container).innerHTML = "<p class='valid-click-message'>There are currently no Sponsored Listings.</p>";
			}
			function getData(){
				YAHOO.util.Dom.get(config.container).innerHTML = "<p class='valid-click-message'>Loading Sponsored Listings...</p>";
				var sUrl = "/proxy_validclick.asp"
					+ "?city=" + config.city
					+ "&state=" + config.state
					+ "&keyword=" + config.keyword
					+ "&results=" + config.endIndex;
				var request = YAHOO.util.Connect.asyncRequest("GET", sUrl, { 
					success: handleSuccess,
					failure: handleFailure,
					timeout: 4000,
					cache: false
				});
			}
			YAHOO.util.Event.onContentReady(config.container, getData);	
		}
	}
	
	function truncate_str(str, length, truncation){
		length = length || 30;
		truncation = truncation || '...';
		return str.length > length ?
			str.slice(0, length - truncation.length) + truncation : String(str);
	}
	
	function extend(defaults, config){
		var retVal = {};
		for (var property in defaults)
    		retVal[property] = defaults[property];
		for (var property in config)
			retVal[property] = config[property];
		return retVal;	
	}
})();