/*
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 line_chart(dataSets,setNames) {
   this.maxValue  = 0;
   this.minValue  = 999999999;
   this.data	  = new Array();
   this.na		  = 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.na[i]	= new Array();
	  this.label[i] = new Array();
   }

   this.add = function(name, value, set) {
      if(!name) this.label[set].push("N/A");
      else this.label[set].push(name); 
	  if(!value) {
	  	this.data[set].push(0);
		this.na[set].push(1);
	  } else {	  
	      this.data[set].push(Math.round(value*100)/100);
		  this.na[set].push(0);
	      if(value > this.maxValue) this.maxValue = parseInt(value,10);
		  if(value < this.minValue) this.minValue = parseInt(value,10);
	  }
   }

   this.build = function(myDiv, chartTitle, preLabel, postLabel, chartHeight, betweenPoints, xStart, moveLedgen,useMin) {
      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 bp = betweenPoints;
      else var bp = 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 = "";	  
	  
	  if (this.minValue < 0) useMin = "Yes";
	  
	  /* Y Axis Starting Value */
	  var mySign = 1;
      if (useMin == "Yes") {
		  var myMin = Math.round(this.minValue);
		  var minLen = myMin.toString().length;
		  if(this.minValue<0) {
		  	minLen = minLen - 1;
			minSign = -1;
			var newMin = Math.pow(10,(minLen-1))
			if(newMin==1) newMin = 0;
		    while(newMin<=(myMin*minSign)) {
		  	  if(newMin>99) newMin = newMin+(Math.pow(10,(minLen-1))/2);
			  else newMin = newMin +10;
		    }  
		    if (newMin < 10 && newMin > 0) newMin = 10; 
		    newMin = newMin*minSign; 			
		  } else {
		    var newMin = Math.pow(10,(minLen-1))
		    while(newMin<myMin) {
		  	  newMin = newMin+(Math.pow(10,(minLen-1))/2);
		    }  
			newMin = newMin-(Math.pow(10,(minLen-1))/2);			
		    if (newMin < 0) newMin = 0;
		  }
      } else var newMin = 0;		  
		 
	  /* Max Value on Y Axis */	 
	  var myMax = this.maxValue;
	  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>=100 || newMax==10) var yl=10;
	  else if((newMax/10)>=5) var yl=Math.round(newMax/10);
	  else var yl=Math.round(newMax/5);		  

	  if(newMin<0 && ((-1*newMin) % (newMax/yl) == 0)) {
	    yl = yl + Math.round(-1*newMin/(newMax/yl));
	  } else if (newMin<0 && (newMax/yl)>=(-1*newMin)) {
	  	newMin = -1*newMax/yl;
		yl = yl+1;
	  } else if(newMin<0){
	  	while((-1*newMin) % (newMax/yl) != 0) {
		   newMin = newMin - 1;
		}
		yl = yl + Math.round(-1*newMin/newMax/yl);
	  }
		
	  if(newMin>newMax) newMin=0;
	  var span = newMax - newMin;	

      var sx0 		= sx;	/* Original Starting Point - X Axis */
   	  var fnt    	= 14;	/* X Axis Title Font Size */

      /* length of X Axis */
      var rtmax = sx + (bp+Math.round((bp/2)))*(this.data[0].length)-30;

	  /* Draw Graph Background */
	  jg.setColor("#c0c0c0");
	  jg.fillRect(70,30,rtmax-70,h);
	  
      /* Draw Y Axis Markers */
      jg.setColor("black");
      jg.drawLine(70,Math.round((0))+30,rtmax,Math.round((0))+30);	/* Max Data Point and Line */
      var yLabel = addCommas((Math.round(newMax - (0))).toString());				
      jg.drawString("<div style='width:50px; text-align:right'>" +
	                "<font face='Helvetica' size='2'><b>"+
					preLabel+yLabel+postLabel+"</b></font></div>",10,Math.round(0)+24);  
   
   	  if(span>newMax) var linesAbove = Math.round((newMax/span)*yl);
	  else var linesAbove = yl;
	  if(span>newMax) lineDiv = h*newMax/span/linesAbove;
	  else lineDiv = h/linesAbove;
	  if(span>newMax) var linesBelow = Math.round(((-1*newMin)/span)*yl); 
	  else var linesBelow = 0;  
   
      for(var i=0; i<yl; i++) {
         jg.drawLine(70,Math.round((lineDiv*(i+1)))+30,rtmax,Math.round((lineDiv*(i+1)))+30);
         if(span>newMax) yLabel = addCommas(Math.round(newMax-(newMax/linesAbove*(i+1))).toString());
		 else yLabel = addCommas(Math.round(newMax-(span/linesAbove*(i+1))).toString());		
         jg.drawString("<div style='width:50px; text-align:right'>" +
		               "<font face='Helvetica' size='2'><b>"+
					   preLabel+yLabel+postLabel+"</b></font></div>",10,Math.round((lineDiv*(i+1))-2)+24);
      }
	 
	  /* Box-in Chart */
      jg.drawLine(rtmax,Math.round((0))+30,rtmax,Math.round((h))+30);
      jg.drawLine(70,Math.round((0))+30,70,Math.round((h))+30);
         
	  if (newMin < 0) var offset = 0;
	  else var offset = 0;
	  
	  /* Draw Lines */
	  jg.setStroke(3);	/* Line Thickness */
	  for(var j=0; j<ds; j++) {
	     var oldx, oldy;
		 sx = sx0;
         for(var i=0; i<this.data[j].length; i++) {
            var ht1 = Math.round((this.data[j][i]-newMin)*h/(newMax-newMin));
			if(!ht1) ht1=1;
            if(i >= 1) {
               jg.setColor(this.color[j]);
			   if(i==0 && this.na[j][i]== 0) {
               	  jg.drawLine(oldx, h-oldy+30+offset, sx, h-ht1+30+offset);
				  jg.drawImage("images/marker"+j+".gif",oldx-2, h-oldy-2+30+offset, 8, 8);
				  jg.drawImage("images/marker"+j+".gif",sx-2, h-ht1-2+30+offset, 8, 8);				  				  			  
			   } else if(i!=0 && this.na[j][i] == 0) {
			   	  if(this.na[j][i-1]==0) {
               	     jg.drawLine(oldx, h-oldy+30+offset, sx, h-ht1+30+offset);	
					 jg.drawImage("images/marker"+j+".gif",oldx-2, h-oldy-2+30+offset, 8, 8);
					 jg.drawImage("images/marker"+j+".gif",sx-2, h-ht1-2+30+offset, 8, 8);				  		  	
				  }	else {
				  	 jg.drawImage("images/marker"+j+".gif",oldx-2, h-oldy-2+30+offset, 8, 8);
				     jg.drawImage("images/marker"+j+".gif",sx-2, h-ht1-2+30+offset, 8, 8);				  			  
				  }   
			   }
	        } else {
			   if(this.na[j][0]==0) {
				     jg.drawImage("images/marker"+j+".gif",sx-2, h-ht1-2+30+offset, 5, 5);				  	
			   }		
			}

			/* X Axis Labels */
			if (j == 0) {
               jg.setColor("black");
			   jg.drawStringRect("<font face='Helvetica'><b>"+this.label[j][i]+"</b></font>",sx-20,h+30,rtmax/this.data[j].length,"center");
			}
			
            oldx = sx;
            oldy = ht1;
            sx   = sx+bp+Math.round(bp/2);
         }		  
	  }
	  
	   /* Create Ledgen */
	   if(ds>1 && this.label[0][0] != "") {
          var ledgen = "<div id='ledgen' style='position:relative;padding:2px;border:solid 1px black;" +
		 			  "width:"+(rtmax-70)+"px;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<this.setName.length; i++) {
		    ledgen = ledgen + "<div id='color"+i+"' style='position:relative;width:15px;height:3px;" +
							  "line-height:3px;padding:5px;background-color:" +
							  this.color[i] +
							  ";border:solid 1px black;float:left;'><img src='images/marker"+i+".gif' width='8' height='8' border='0'></div>" +
							  "<div id='text"+i+"' style='position:relative;width:"+
							  (((rtmax*.85)/(this.setName.length+1))-15)+"px;" +
							  "padding:5px;float:left;font-size:10px;text-align:left;'><b>" +
							  this.setName[i] +
							  "</b></div>";
		  }		  
		  ledgen = ledgen + "</td></tr></table></div>";
          jg.drawStringRect(ledgen, 70, h+fnt+4+30+20+ml, rtmax-70, "center"); 
	   }		  
	 
	  jg.setColor("black");
      jg.setFont("Helvetica", fnt,  Font.BOLD);
      jg.drawStringRect(chartTitle, 70, h+fnt+4+30+ml, rtmax-70, "center");
      jg.paint();
   }
}


