/***********************************
*            SAWBar
***********************************/
SAWBar = function(divDraw, x0, dx, y0, y1, sColor, sInnerFrameColor){
	this.divDraw_ = divDraw;
	this.x0_ = x0;
	this.dx_ = dx;
	this.y0_ = y0;
	this.y1_ = y1;
	this.sColor_ = sColor;
	this.sInnerFrameColor_ = (sInnerFrameColor) ? sInnerFrameColor : SAWUtils.darkenColor(sColor);
	this.sClass_ = 'SAWGraph__Bar';
	
	this.pixelWidth_ = 0;
	this.pixelHeight_ = 0;
	this.pixelMoveX_ = 0;
	this.pixelMoveY_ = 0;
	
	this.eventNameList = new Array();
	this.eventHandlerList = new Array();
	
	this.iLeft = -1;
	this.iRight = -1;
	this.iTop = -1;
	this.iBottom = -1;

}

SAWBar.prototype.setClass = function(sClass){
	this.sClass_ = sClass;
}

SAWBar.fillHTMLElement = function(element, drawElement, sColor, sInnerBorderColor, sBorderClass){
	var 
		vPos = SAWUtils.getElementAbsolutePosition(element),
		vPos1 = SAWUtils.getElementAbsolutePosition(drawElement),
		ix0 = vPos.left - vPos1.left,
		iy0 = vPos.top - vPos1.top,
		ix1 = ix0 + element.offsetWidth,
		iy1 = iy0 + element.offsetHeight;
	rectangleElement = SAWDraw.rectangle(drawElement, ix0, iy1, ix1, iy0, sColor, (sBorderClass) ? sBorderClass : 'SAWGraph__Bar', (sInnerBorderColor) ? sInnerBorderColor : SAWUtils.darkenColor(sColor));
	return rectangleElement;
}

SAWBar.prototype.forcePixelWidth = function(iWidth){
	this.pixelWidth_ = iWidth;
}

SAWBar.prototype.forcePixelHeight = function(iHeight){
	this.pixelHeight_ = iHeight;
}

SAWBar.prototype.setPixelMove = function(ix, iy){
	this.pixelMoveX_ = ix;
	this.pixelMoveY_ = iy;
}

SAWBar.prototype.setEvent = function(sEvent, sHandler){
	this.eventHandlerList.push(sHandler);
	this.eventNameList.push(sEvent);
}

SAWBar.prototype.getLeftPos = function(){return this.iLeft;}
SAWBar.prototype.getRightPos = function(){return this.iRight;}
SAWBar.prototype.getTopPos = function(){return this.iTop;}
SAWBar.prototype.getBottomPos = function(){return this.iBottom;}


SAWBar.prototype.draw = function(frame){
	var
		xmin = this.x0_ - this.dx_/2,
		xmax = this.x0_ + this.dx_/2,
		ix0 = frame.xAxis_.toScreen(xmin),
		ix1 = frame.xAxis_.toScreen(xmax),
		iy0 = frame.yAxis_.toScreen(this.y0_),
		iy1 = frame.yAxis_.toScreen(this.y1_),
		i;
	
	if(this.pixelMoveX_ != 0) {ix0 += this.pixelMoveX_; ix1 += this.pixelMoveX_;}
	if(this.pixelMoveY_ != 0) {iy0 += this.pixelMoveY_; iy1 += this.pixelMoveY_;}
	if(this.pixelWidth_ > 0) ix1 = ix0 + this.pixelWidth_;
	if(this.pixelHeight_ != 0) iy1 = iy0 - this.pixelHeight_;
	
	if(xmin>=frame.xmin_ && xmax<=frame.xmax_ && this.y0_>=frame.ymin_ && this.y1_<=frame.ymax_) {// user may have set axes bounds
		domEl = SAWDraw.rectangle(this.divDraw_, ix0, iy0, ix1, iy1, this.sColor_, this.sClass_, this.sInnerFrameColor_);
		for(i=0; i<this.eventNameList.length; i++){
			if(domEl.addEventListener){
				domEl.addEventListener(this.eventNameList[i],this.eventHandlerList[i], true);
			}
			else if (domEl.attachEvent){
				domEl.attachEvent('on'+this.eventNameList[i],this.eventHandlerList[i]);
			}
		}
	}
	
	this.iLeft = ix0;
	this.iRight = ix1;
	this.iTop = iy1;
	this.iBottom = iy0;
}

/***********************************
*            SAWGraph
***********************************/
SAWGraph = function(divChart, divWhole){
	// divChart is the div on which the chart will be drawn
	// divWhole is the div that may contain the legend etc - it is used to color the background
	if(!divWhole) divWhole = divChart;
	
	divNew = document.createElement('div');
	divNew.id = divChart.id+"_SAWdiv";
	this.elid_ = divNew.id;
	this.divDraw_ = divNew;
	this.divSize_ = divChart;
	this.divWhole_ = divWhole;
	divNew.innerHTML = '';
	divChart.appendChild(divNew);
	
	this.bUserXBounds_ = false;
	this.bUserYBounds_ = false;
	
	this.ymin_ = 0;
	this.ymax_ = 1;
	this.xmin_ = 0;
	this.xmax_ = 1;
	
	this.sBackgroundClass_ = 'SAWGraph__Background';
	
	this.init();
}

SAWGraph.prototype.init = function(){
	this.frame_ = new SAWFrame(this.divDraw_, this.divSize_);
	this.bars_ = new Array();
	this.bEmpty_ = true;
	this.legendBars_ = new Array();
}

SAWGraph.prototype.clear = function(){
	this.divDraw_.innerHTML = '';
	this.divSize_.removeChild(this.divDraw_);
	
	for(i=0; i<this.legendBars_.length; i++){
		element = this.legendBars_[i][0];
		rectangleElement = this.legendBars_[i][4];
		if(rectangleElement!=0){
			element.removeChild(rectangleElement);
		}
	}
	
	this.init();
}

SAWGraph.prototype.setBackgroundClass = function(sBackgroundClass){
	this.sBackgroundClass_= sBackgroundClass;
}

SAWGraph.prototype.getFrame = function(){
	return this.frame_;
}

SAWGraph.prototype.addBar = function(x0, dx, y0, y1, sColor, sInnerBorderColor, sBorderClass){
/**
	adds a bar centered at x0 with width dx, values from y0 to y1 -- all real space values
	returns the created SAWBar object - if further customization is required.
*/
	if(y0>y1){_ft = y1; y1=y0; y0=_ft;}
	if(this.bEmpty_){
		if(!this.bUserXBounds_){
			this.xmin_ = x0-dx/2;
			this.xmax_ = x0+dx/2;
		}
		if(!this.bUserYBounds_){
			this.ymin_ = y0;
			this.ymax_ = y1;
		}
		this.bEmpty_ = false;
	}
	else{
		if(!this.bUserXBounds_){
			this.xmin_ = Math.min(this.xmin_, x0-dx/2);
			this.xmax_ = Math.max(this.xmax_, x0+dx/2);
		}
		if(!this.bUserYBounds_){
			this.ymin_ = Math.min(this.ymin_, y0)
			this.ymax_ = Math.max(this.ymax_, y1);
		}
	}
	var deltax = (this.xmax_ - this.xmin_)*0.025, deltay = (this.ymax_ - this.ymin_)*0.025;
	this.frame_.setBounds(this.xmin_-deltax, this.xmax_+deltax, this.ymin_-deltay, this.ymax_+deltay);

	vBar = new SAWBar(this.divDraw_, x0, dx, y0, y1, sColor, sInnerBorderColor);
	if(sBorderClass) vBar.set(sBorderClass);
	this.bars_.push(vBar);
	
	return vBar;
}

SAWGraph.prototype.addLegendBar = function(element, sColor, sInnerBorderColor, sBorderClass){
	this.legendBars_.push(new Array(element, sColor, sInnerBorderColor, sBorderClass, 0));
}

SAWGraph.prototype.setXBounds = function(xmin, xmax){
	this.xmin_ = xmin;
	this.xmax_ = xmax;
	
	var deltax = (this.xmax_ - this.xmin_)*0.025, deltay = (this.ymax_ - this.ymin_)*0.025;
	this.frame_.setBounds(this.xmin_-deltax, this.xmax_+deltax, this.ymin_-deltay, this.ymax_+deltay);
	
	this.bUserXBounds_ = true;
}

SAWGraph.prototype.setYBounds = function(ymin, ymax){
	this.ymin_ = ymin;
	this.ymax_ = ymax;

	var deltax = (this.xmax_ - this.xmin_)*0.025, deltay = (this.ymax_ - this.ymin_)*0.025;
	this.frame_.setBounds(this.xmin_-deltax, this.xmax_+deltax, this.ymin_-deltay, this.ymax_+deltay);

	this.bUserYBounds_ = true;
}


SAWGraph.prototype.draw = function(){
	this.frame_.computeAxes_();
	this.frame_.draw();

	for(i=0; i<this.bars_.length; i++){
		this.bars_[i].draw(this.frame_);
	}
	
	this.divWhole_.className = this.sBackgroundClass_;
	
	for(i=0; i<this.legendBars_.length; i++){
		element = this.legendBars_[i][0];
		sColor  = this.legendBars_[i][1];
		sInnerBorderColor = this.legendBars_[i][2];
		sBorderClass  = this.legendBars_[i][3];
		this.legendBars_[i][4] = SAWBar.fillHTMLElement(element, element, sColor, sInnerBorderColor, sBorderClass);
	}
}

