function maxArray( arr ) {
	// this function calculates the maximum value in the associative array
	var max = 0;
	for(var i in arr){
  	    if (arr[i] > max)
  	    		max = arr[i];
  	}
  	return max;
}

function minArray( arr ) {
	// this function calculates the minimum value in the associative array
	var min;
	var count = 0;
	for(var i in arr){
		// initialize minimum value with first value of associative array
		if (count==0)
			min = arr[i];
		if (arr[i] < min)
  	    		min = arr[i];
  	    	count++;
  	}
  	return min;
}

function drawTagCloud(map, maxStyle, dburl) {
	// this function draws the tag cloud using the associative javascript array generated by the Notes view
	// calculate the maximum entries in the cloud. if not found, no tags are found
	var maxEntries = maxArray(map);
	if (maxEntries==0) {
		document.write('&nbsp;');
	}
	
	//	calculate the minimum entries in the cloud
	var minEntries = minArray(map);
	
	// calculate the range
	var range = maxEntries - minEntries;
	if (range <= 0)
		range = 1; 
	
	// loop through the tag map to draw each tag.
	for(var tag in map){
  		drawTag(tag, map[tag], minEntries, maxEntries, maxStyle, dburl);
  	}
}

function drawTag(tag, count, min, max, maxStyle, dburl) {
session = ""
if(location.href.indexOf("&session=")>-1){
session = location.href
session = session.substr(session.indexOf("&session=")+9,32)
session = "&session="+session;
}
url = dburl + '/search?OpenAgent&query=' + tag + session;

  // this function renders a tag line. first determine
  // the size tag to use for this tag count
  sizeTag = Math.round((((maxStyle-1)/(max-min))*count) +(1*max-maxStyle*min)/(max-min));
  
  // now write the link to the page
if(sizeTag.toString()=="NaN"){sizeTag="1"}
  document.write('<a href="' + url + '" class="tag' + sizeTag + '">' + tag + '</a> ');
}

