  function KudzuSearchResponse( ) { }
  
  function requestStylesheet(stylesheet_url) {
     stylesheet = document.createElement("link");
     stylesheet.rel = "stylesheet";
     stylesheet.type = "text/css";
     stylesheet.href = stylesheet_url;
     stylesheet.media = "all";
     document.lastChild.firstChild.appendChild(stylesheet);
  }

  function requestContent( url ) {
    var script = document.createElement('script');
     
	var parameters="";
	if( kudzuMerchantId != null ) {
		parameters += "?merchantId="+kudzuMerchantId;	
	}  
	 
	if( kudzuNumResults != null ) {
		parameters += "&minRating=" + kudzuMinRating;
	}
	
	if( kudzuNumResults != null ) {
		parameters += "&numResults=" + kudzuNumResults;
	}
 	
    script.src = url + parameters;

    document.getElementsByTagName('head')[0].appendChild(script);
  }

	function make_ad_field(value) {
		var field = document.createElement('div');
			
		field.appendChild(document.createTextNode(value));
			
		return field;
	}
		
	function create_ad_html(ad) {
		var ad_div = document.createElement('div'); 

		var img_tag = document.createElement('img');
			img_tag.setAttribute('src','http://images.kudzu.com/img/extScript/stars-' + ad.rating + '.gif' );

		var img_div = document.createElement('div');
			img_div.appendChild(img_tag);
				
		ad_div.appendChild(img_div);
		
		if (ad.review.length > 150) {
		    ad.review = ad.review.substr(0,150);
		    ad.review = ad.review.substr(0,ad.review.lastIndexOf(" "));
		    ad.review += "...";
		}
		var review_div = make_ad_field(ad.review);
		review_div = make_ad_field(ad.review );
		
		var title_div = make_ad_field(ad.title );
		title_div.className='kudzu_reviews_title';
		title_div.style.color='#'+kudzuTitleColor;
		ad_div.appendChild(title_div);
		ad_div.appendChild(review_div);
			
		return ad_div;
	}
	
	function remove_all_children(par) {
		while(par.hasChildNodes()) {
			par.removeChild(par.firstChild);
		}
	}

	function update_ad_div(target_div,data) {
		var main_link = document.getElementById('kudzu_reviews_unit');
		 
		var ad_div = document.createElement('div');
		ad_div.setAttribute( 'id', 'kudzu_reviews_target' );
 
		for(var i=0;i<data.length;i++) {
			ad_div.appendChild(create_ad_html(data[i]));
		}
		
		var header_div = document.createElement( 'div' );
		header_div.setAttribute( 'id', 'kudzu_reviews_header' );		 
		header_div.innerHTML = data[0].header;	
		
 
		var footer_div = document.createElement('div');  
		footer_div.innerHTML = data[0].footer;
		
		main_link.appendChild( header_div );			
		main_link.appendChild( ad_div );
		main_link.appendChild( footer_div ); 				
	}

	function initialize_kudzu_ads(data) {
		var target_div = document.getElementById("kudzu_reviews_unit");
			
		if (target_div) {
			update_ad_div(target_div,data);
		}
	}

	KudzuSearchResponse.prototype.render = function(){
	  var BASE_URL = 'http://api.kudzu.com';
  	  var STYLESHEET = BASE_URL + "/css/remoteReviews.css"
  	  var CONTENT_URL = BASE_URL + '/JSONReviewsResults';
      
	  this.serverResponse = function(data) {
	     if (!data) return;
	     initialize_kudzu_ads(data); 
	  }
	
	  requestStylesheet(STYLESHEET);

	  requestContent(CONTENT_URL);
	  	  
	  var no_script = document.getElementById('no_script');
	  if (no_script) { no_script.style.display = 'none'; }
	}



