function doWR(keyword_class_el,insert_div,panel_div) { 
	
	var kw = getKeywords(keyword_class_el);
	var qs = "?";
	kw.each(function(n){ qs = qs + "kw="+n+"&" });
	
	var proxy_uri = "/invent/prox/index.php";
        var wr_uri = "http://esf.lesc.doc.ic.ac.uk:8080/esfdemo/esf.groovy"+qs;
        wr_uri = proxy_uri + "?proxy_url="+wr_uri;

	//	wr_uri = "http://127.0.0.1:8080/esfdemo/esf.groovy"+qs;
	logme("url:"+wr_uri);
	
	new Ajax.Request(wr_uri, { 
	onSuccess: function(transport) { 
		var json;
		try {
			
			json = transport.responseText.evalJSON();
			
		}
		catch(e) { logme(e) }
		//create a box out of results and update 
		 
		  doJson(json,6,insert_div,panel_div,kw);   
		   
		
	},
	onFailure: function(e) { 
		logme("error"+e)
		
		}
	} ); 
	
}

// make boxes, update acc. to rank
/**
* json: the evaluated json feed as an object
* insert_div: id of div where the wr html is to be inserted.
* panel_div:
* kw: a list of keywords to evaluate
* 
*/
function doJson(json,up_to,insert_div,panel_div,kw) {

    // chuck the 0 rank items
   // json = json.reject(function(n){ return n.rank == 0})
	// sort on json.rank
	json = json.sort(sortDescRank);
	//(m * 216/n+72)
	var total = 9;
	var boxes = makeHtmlItems(json,kw.size(),total);
	boxes.each(function(n){
		$(insert_div).insert(n);
		logme(n);
	});
}

// turn json rss items into html elements.
// TODO this is too complicated -- need a generic method. 

function makeHtmlItems(json,total_keys,up_to) {
    var id = 0;
	var boxes = [];
	var trdiv = new Element('div');

	json.each(function(n){
			id++;
			logme("box " + id);
			
			// var rank = n.rank;
			var dims = 72;
		
			try {
			 dims =  ((n.rank * 216) / total_keys)+72;
			 
			}
			catch(e) {
				logme(e);
			}
			
			var boxclass = null;
			try {
				// prevents over-population
				boxclass = "wr_box_"+dims;
				if (id < 9) {
					var el = makeBox(n.title,n.desc,n.thumb,n.link,"wbox"+id,n.creator,n.creatorLink,boxclass);			
					boxes.push(setImageDims(el,'img.wr_thumb',dims));
				}
			
			}
			catch(e){
					logme("insert error: "+e);
			}
			
// 3 to a row
		//$(insert_div).insert("<img height=\"100\" src=\""+n.thumb+"\"/>");
			// Effect.Tooltip doesn't seem to work.
			// new Effect.Tooltip("wbox"+id, content, {title : 'title', className: 'class', offset: {x:40, y:40}})
		
		logme("go");
			try { 
			//	new Draggable("wbox"+id); // get from object!!!
				
			}
			catch(e) {
			   logme("Draggable error: "+e)
			}
			if (id > up_to)  
				throw $break;
			
		});
	

 return boxes;
}

/**
* Set the dimensions on the rss item image inside an html group and returns the root el
*
* @param	el	the root el of the group
* @param	cssSel	the css selector for the img element to be sized
* @param 	width  the width of the element 
*/

//TODO setting only width may not work with 

function setImageDims(el,cssSel,width) {
		el.down('img.wr_thumb').setStyle(
							{'width':width+'px'});
					

return el;
}

function sortDescRank(a, b)
{
   return b.rank - a.rank;
}

function rollout(id) {
	$(id).toggle();

}
function doJsonRanked(json) {

	
	// sort on json.rank
	var j = json.properties();
	json.each(function(pair){
	
	// ranked level
		var t = pair.key;
		var o = pair.value;
		// only first 6
	
		pair.value.each(function(n){
	//     peers

			$('w1_inner').insert(makeBox(n.title,n.desc,n.thumb,n.link,0));  
			});
	
		});
}

function logme(msg) {
	
    //	$('update1').insert('<div class="log"><table><tr><td>[log] </td><td>'+msg+'</td></tr></table></div>');
	
}

function getKeywords(keyword_class_el){

	
	var kw = [];
	// todo use keywords in meta tag? 
//	$(keyword_el).descendants().each(function(n){ 
	$$(keyword_class_el).each(function(n){ 
		
		n.descendants().each(function(d){
			if (d.elementName = "p"){
				var kwtext = d.innerHTML.stripTags().strip();
				kw.push(kwtext);
				logme("getting keyword: \"" + kwtext+"\"");
			}
		});
		
	});
	return kw;	
}

// obsolete -- now handled by wrBox.makeContentBox()


//TODO add rank
//TODO small sidebar: image + rollover
//      mouseover goes to rollover which displays full content. 
/**
*  makes a what's related html widget
* 
* @param	title	from rss
* @param	desc 	from rss
* @param	thumb	from rss
* @param	link	from rss
* @param	id		id of element -- obsolete
* @param	creator from rss
* @param	creatorLink from rss
* @param	boxclass	from rss
*
*/
function makeBox(title,desc,thumb,link,id,creator,creatorLink,boxclass){
   
		logme("making box for thumb: " + thumb);
		
		//var contenthtml = mcb(title,desc,thumb,link,creator,creatorLink,boxclass);
		var wr = new wrBox();
		
		// TODO, refactor so we have only one call here wr.makeWRBoxStyle1();
	
		var contenthtml = wr.makeContentBox(title,desc,thumb,link,creator,creatorLink,boxclass);
		//		var contenthtml = wr.makeContentBox(title,cropString(desc,14),thumb,link,creator,creatorLink,boxclass);
		return wr.getbox(thumb,contenthtml,title);
		

}

function cropString(str,n) {
	var out;
	if (str.length < 1) {return str};
	var a = str.split(' ');
	if (a.length < n) {out =  str;	}
	else { out = a.slice(0,n).join(" ");}
	return out+" ";

}



