hD="0123456789ABCDEF";
function d2h(d) 
{
	var h = hD.substr(d&15,1);
	while(d>15) {d>>=4;h=hD.substr(d&15,1)+h;}
	return h;
}

function h2d(h) 
{
	return parseInt(h,16);
}

function line_graph()
{
	this.ct = 0;

	this.data      = new Array();
	this.x_name    = new Array();
	this.scale     = 1;
	this.mul     = 1;
	this.pf     = 'Sale';
	this.max       = -84000; //MAX INT
	this.omax       = -84000; //MAX INT
	this.user_min  = 84000; 
	this.min	= 0;
	this.omin	= 0;
	this.dw	= 20;
	this.dotcolor	= 'blue';
	this.barcolor	= 'blue';
	this.thickness	= 1;
	this.ycolor	= 'black';
	this.xcolor	= 'black';
	this.btl = 0;

	this.c_array = new Array();
	this.c_array[0] = new Array(255, 192, 95);
	this.c_array[1] = new Array(80, 127, 175);
	this.c_array[2] = new Array(159, 87, 112);
	this.c_array[3] = new Array(111, 120, 96);
	this.c_array[4] = new Array(224, 119, 96);
	this.c_array[5] = new Array(80, 159, 144);
	this.c_array[6] = new Array(207, 88, 95);
	this.c_array[7] = new Array(64, 135, 96);
	this.c_array[8] = new Array(239, 160, 95);
	this.c_array[9] = new Array(144, 151, 80);
	this.c_array[10] = new Array(255, 136, 80);

	this.getColor = function()
	{
		if(this.ct >= (this.c_array.length-1))
		  this.ct = 0;
		else
		  this.ct++;

		return "#" + d2h(this.c_array[this.ct][0]) + d2h(this.c_array[this.ct][1]) + d2h(this.c_array[this.ct][2]);
	}


	this.setScale = function(scale)
	{
		this.scale  = scale;
	}

  // self created
	this.setBarColor = function(value)
	{
		this.barcolor  = value;
	}
	this.setBTL = function(value)
	{
		this.btl  = value;
	}
	this.setXColor = function(value)
	{
		this.xcolor  = value;
	}
	this.setYColor = function(value)
	{
		this.ycolor  = value;
	}
	this.setThickness = function(value)
	{
		this.thickness  = value;
	}
		this.setdw = function(value)
	{
		this.dw  = value;
	}
	this.setDotColor = function(value)
	{
		this.dotcolor  = value;
	}
	this.setPf = function(value)
	{
		this.pf  = value;
	}
// end self created
// end self created
	this.setColor = function(value)
	{
		this.ct = value;
	}

	this.setMax = function(value)
	{
		this.max = value;
	}
	this.setOMax = function(value)
	{
		this.omax = value;
	}
	this.getMax = function()
	{
		alert(this.max);
	}

	this.setMin = function(value)
	{
		this.user_min = (this.user_min < value ? this.user_min : value);
		this.min = this.user_min;
	}
	this.setOMin = function(value)
	{
		this.user_min = (this.user_min < value ? this.user_min : value);
		this.omin = this.user_min;
	}

	this.add = function(x_name, value)
	{
		value = parseFloat(value,10)*this.scale;

		this.x_name.push(x_name);  
		this.data.push(parseFloat(value,10));

		if(value > this.max)
		  this.max = parseFloat(value,10);

		if(value < this.user_min)
		  this.user_min = parseFloat(value,10);
	}

	this.render = function(canvas, title, height)
	{
		if(this.pf == 'Sale' || this.pf == 'sale'){
			var dif = this.omax - this.omin;
			if(dif < 500)
				this.mul = 3;
			else if(dif >= 500 && dif < 1000)
				this.mul = 2.8;
			else if(dif >= 1000 && dif < 1500)
				this.mul = 2.6;
			else if(dif >= 1500 && dif < 2000)
				this.mul = 2.4;
			else if(dif >= 2000 && dif < 2500)
				this.mul = 2.1;
			else if(dif >= 2500 && dif < 2800)
				this.mul = 2;
			else if(dif >= 2800 && dif < 3000)
				this.mul = 1.5;
			else if(dif >= 3000 && dif < 3300)
				this.mul = 1.4;
			else if(dif >= 3300 && dif < 3500)
				this.mul = 1.4;
			else if(dif >= 3500 && dif < 4000)
				this.mul = 1.3;
			else if(dif >= 4000 && dif < 5000)
				this.mul = 1.2;
			else if(dif >= 5000)
				this.mul = 1;
		}else{
			
			var dif = this.omax - this.omin;
			if(dif < 50)
				this.mul = 1.1;
			else if(dif >= 50 && dif < 100)
				this.mul = 1.2;
			else if(dif >= 100 && dif < 200)
				this.mul = 1.1;
			else 
				this.mul = 1;
		}
		var jg = new jsGraphics(canvas);

		var h  = 130;

		if (typeof(height) == "number")
		   h = height;

		var sx = 60; // Distance between price to first figure
		var dw = this.dw;
		var shadow = 1;
		var fnt    = 10;
		
		var rtmax = sx - 25 + (dw+Math.round((dw/2))+shadow)*(this.data.length);
		// Draw markers
		var i;
		jg.setFont('verdana,sans-serif,geneva,helvetica', '9px', Font.BOLD);
		//jg.drawImage('http://devimg.makaan.com/images/3-big.gif',0,60,10,50);
		
		for(i = 1 ; i <= 5 ; i++)
		{
			//v delete code line to remove horrizontal graph line
			jg.setColor('#CCCCCC');
			jg.drawLine(45,Math.round((h/5*i)),rtmax,Math.round((h/5*i)));
			jg.drawLine(45,Math.round((h/5*i))+20,45,Math.round((h/5*i)-20));
			jg.drawLine(rtmax,Math.round((h/5*i))+20,rtmax,Math.round((h/5*i)-20));
			var ff = Math.round((this.omax - (this.omax / 5 * i))/(this.scale)+this.omin);
				//v set vertical title left or  top position
			jg.setColor('#000000');
			if(i != 5)
				jg.drawString(ff+"",15,Math.round((h/5*i)-2)-3);
			else
				jg.drawString("0",15,Math.round((h/5*i)-2)-3);
		}
		// Draw the bar graph
		var color = this.getColor();
		var oldx, oldy;
		//v  set thickness of line
		jg.setStroke(this.thickness);
		
		for(i = 0; i < this.data.length; i++)
		{
			var ht1 = (Math.round(this.data[i]*h/this.omax) - (this.omin*h/this.omax)) ;
			//ht1 = ht1/this.mul;
			var vt = this.data[i];
			if(i >= 1)
			{
				jg.setColor(this.barcolor);
				jg.drawLine(oldx, h-oldy, sx, h-ht1);
			}
			//v set dot color
			jg.setColor(this.dotcolor);
			jg.fillEllipse(sx-2, h-ht1-2, 5, 5,vt);
			
			
			jg.setColor('#000000');
			//bottom line dots
			jg.fillEllipse1(sx, h-2, 2, 2);
			
			//v set bottom line color
			jg.setColor("black");
			// set sx for left and h for top position of bottom line
			jg.drawString(this.x_name[i], sx-this.btl, h+5);

			oldx = sx;
			oldy = ht1;
			sx = sx+dw+Math.round(dw/2)+shadow;
		}
//alert(fnt);
		jg.setFont("Verdana", '9px',  Font.BOLD);
		jg.drawStringRect(title, 0, h+fnt+12, rtmax+10, "center");
		jg.paint();
	}

}
