/*
This code was created by the New Hampshire Housing and Finance Authority. 
Use of any part of this code is prohibited without written permission.
http://www.nhhfa.org
Created: 12/2006
By: Sean Vickers
*/
function area_graph(dataSets,setNames) {
   this.maxValue  = 0;
   this.data	  = new Array();
   this.label     = new Array();
   this.color	  = new Array("#6f80ce",
   							  "#8e3e81",
							  "#ffffcc",
							  "#91c8bb",
							  "purple",
							  "gray",
							  "blue",
							  "lightblue",
							  "tan",
							  "lime");

   if (typeof(dataSets) == "number") var ds = dataSets;
   else var ds = 1;
   
   if (setNames) {
      this.setName = new Array();
	  for(var i=0; i<ds; i++) {
	     if(setNames.indexOf("|")>=0) {
		    this.setName.push(setNames.substring(0,setNames.indexOf("|")));
			setNames = setNames.substring(setNames.indexOf("|")+1);
		 }
		 else this.setName.push(setNames);
	  }
   }
   
   for(var i=0; i<ds; i++) {
      this.data[i]  = new Array();
	  this.label[i] = new Array();
   }

   this.add = function(name, value, set) {
      this.label[set].push(name);  
      this.data[set].push(Math.round(value*100)/100);  
      if(value > this.maxValue) this.maxValue = Math.round(value);
	  if(value < this.minValue) this.minValue = Math.round(value);
   }	

   this.build = function(myDiv, chartTitle, chartHeight, betweenPoints, preLabel, postLabel, xStart, moveLedgen) {
      var jg = new jsGraphics(myDiv);

	  /* Chart Height  */
      if (typeof(chartHeight) == "number") var h = chartHeight;
      else var h = 300;	   	   
	   
	  /* Distance Between Points */
      if (typeof(betweenPoints) == "number") var dw = betweenPoints;
      else var dw = 25;	
	  
	  /* X Axis Starting Position */
      if (typeof(xStart) == "number") var sx = xStart;
      else var sx = 90;	 
	  
	  /* Move Ledgen  */
      if (typeof(moveLedgen) == "number") var ml = moveLedgen;
      else var ml = 0;			     
	  
	  if(!preLabel) preLabel = "";
	  if(!postLabel) postLabel = "";	  

	  /* Max Value on Y Axis */	 
	  var myMax = this.maxValue;
	  if(myMax<5) var newMax = 5;
	  else if (myMax<10) var newMax = 10;
	  else {
		  var maxLen = myMax.toString().length;
		  var newMax = Math.pow(10,(maxLen-1))
		  while(newMax<=myMax) {
		      if(newMax>99) newMax = Math.round(newMax+(Math.pow(10,(maxLen-1))/2));
			  else newMax = newMax + 10;
		  } 
	  }

	  /* Adjust newMin, newMax, and number of Markers */
	  if(newMax<=10) var yl=5; 
	  else if(newMax>=100) var yl=10;
	  else if((newMax/10)>=5) var yl=Math.round(newMax/10);
	  else var yl=Math.round(newMax/5);		

      var sx0 		= sx;
      var fnt    	= 14;	/* X Axis Title Font Size */

      /* length of X Axis */
      var rtmax = sx + 30 + (dw*(this.data[0].length)) - 40;
      
	  /* Draw Graph Background */
	  jg.setColor("#c0c0c0");
	  jg.fillRect(60,40,rtmax-60,h);	  
	  
      // Draw markers
      jg.setColor("black");

      jg.drawLine(60,Math.round((0))+40,rtmax,Math.round((0))+40);	/* Max Data Point and Line */
      var ff = addCommas((Math.round(newMax - (0))).toString());				  
      jg.drawString("<div style='width:50px; text-align:right'><font face='Helvetica' size='2'><b>"+ff+"</b></font></div>",4,Math.round(0)+34);  

      for(var i=1; i<=yl; i++) {					/* Y Axis Lines and Values */
         jg.drawLine(60,Math.round((h/yl*i))+40,rtmax,Math.round((h/yl*i))+40);
         var ff = addCommas((Math.round(newMax-(newMax/yl*i))).toString());		
         jg.drawString("<div style='width:50px; text-align:right'><font face='Helvetica' size='2'><b>"+ff+"</b></font></div>",4,Math.round((h/yl*i)-2)+34);
      }

      jg.drawLine(rtmax,Math.round((0))+40,rtmax,Math.round((h))+40);
      jg.drawLine(60,Math.round((0))+40,60,Math.round((h))+40);
 
      for (var i=0; i<ds; i++) {
         var color = this.color[i];        
         jg.setStroke(2);
         sx0 = sx;
		 
         for(var j=0; j<this.data[i].length; j++) {
            var ht1 = Math.round(this.data[i][j]*h/newMax);

            if (j+1 != this.data[i].length) {
               for(var k=0; k<dw; k++) {
               	  var ht2 = Math.round(((((Math.round(this.data[i][j+1]*h/newMax)) - ht1) / dw) * k) + ht1);
				  if (ht2==0) ht2=1;
				  if(!ht2) ht2=1;
				  
            	  jg.setColor(color);
				  jg.setZIndex(i);
            	  jg.mkDiv(sx0+k, h+41-ht2, 1, ht2);
               }
            }
			if(i==0) {
	            jg.setColor("black");
	            jg.drawString("<font face='Helvetica'><b>"+this.label[i][j]+"</b></font>", sx0-8, h+40);
  			}
	    	sx0 = sx0+dw;
         }
		 jg.setColor("black");
      } 

	  /* Create Ledgen */
	  if(ds>1) {
         var ledgen = "<div id='ledgen' style='position:relative;padding:2px;border:solid 1px black;" +
		 			  "width:75%;background-color:white;color:black;text-align:center;'>" +
					  "<table cellspacing='0' cellpadding='0'><tr><td><font size='1'>" +
					  "&nbsp;&nbsp;</font></td><td>";
		 for(var i=0; i<ds; i++) {
		    ledgen = ledgen + "<div id='color"+i+"' style='position:relative;width:10px;height:5px;" +
							  "line-height:5px;padding:5px;background-color:" +
							  this.color[i] +
							  ";border:solid 1px black;float:left;'>&nbsp;</div>" +
							  "<div id='text"+i+"' style='position:relative;width:"+(((rtmax)/ds)*.60)+"px;" +
							  "padding:3px;float:left;font-size:10px;font-weight:bold;text-align:left;'>" +
							  this.setName[i] +
							  "</div>";
		 }		  
		 ledgen = ledgen + "</td></tr></table></div>";
         jg.drawStringRect(ledgen, 0, h+50+15+ml, rtmax, "center"); 
	  }	 	  
	  
      jg.setFont("Helvetica", fnt,  Font.BOLD);
      jg.drawStringRect(chartTitle, 0, h+fnt+4+40, rtmax+20, "center");
      jg.paint();
   }
}
