﻿dojo.require("dojo.collections.Store");
dojo.require("dojo.charting.Chart");
dojo.require("dojo.json");

dojo.require("dojo.html.layout");

dojo.charting.StyledAxis = function (label, scale, labels) {
	dojo.charting.Axis(label, scale, labels);
	var id     = "dojo-charting-axis-"+dojo.charting.Axis.count++;
	this.getId = function(){ return id; };
	this.nodes = { main: null, axis: null, label: null, labels: null, lines: null, ticks: null };
}

dojo.lang.inherits(dojo.charting.StyledAxis, dojo.charting.Axis);
dojo.lang.extend(dojo.charting.StyledAxis, {
	showAxis:                         true,
	axisWidth:                        1,
	axisColor:                        "#00f",
	labelVerticalOffset:              2,
	labelHorizontalOffset:            4,
	textSize:                         10,
	titleSize:                        "0.75em",
	verticalLabelRightPositionFactor: 2.5,
	verticalLabelLeftPositionFactor:  2,

	titleStyle: function() {
		return "text-anchor:middle;font-family:sans-serif;font-size:" + this.titleSize + ";fill:#000;";
	},

	append: function(newElement) { 
		this.getDrawNode().appendChild(newElement);
	},

	getDrawNode: function() {
		if(this.nodes.main == null) {
			this.nodes.main = this.createDrawNode();
			this.nodes.main.setAttribute("id", this.getId());  //  need a handle if we have to kill parts of the axis def.
		}
		return this.nodes.main;
	},

	drawHorizontalAxis: function(area, kCoord) {
		return this.drawAxis(
			area.left  - this.axisWidth,
			kCoord,
			area.right + this.axisWidth,
			kCoord
		);
	},

	drawVerticalAxis: function(area, kCoord) {
		return this.drawAxis(
			kCoord,
			area.top,
			kCoord,
			area.bottom
		);
	},

	renderHorizontal: function(plotArea, plot, axis, plane) {
		var 
		area   = plotArea.getArea(),				
		kCoord = axis.getCoord(this.origin, plotArea, plot),
		y      = kCoord + this.textSize + this.labelVerticalOffset;
		
		if(this.showAxis) { this.append(this.drawHorizontalAxis(area, kCoord));}
		if(this.showLines) { this.append(this.renderLines(plotArea, plot, plane, y));}
		if(this.showTicks) { this.append(this.renderTicks(plotArea, plot, plane, kCoord));}
		if(this.showLabels) { this.append(this.renderLabels(plotArea, plot, plane, y, this.textSize, "middle"));}

		if(this.showLabel && this.label){ this.append(this.drawHorizontalLabel(plotArea, kCoord));}
	},

	renderVertical: function(plotArea, plot, axis, plane) {
		var 
		area   = plotArea.getArea(),				
		kCoord = axis.getCoord(this.origin, plotArea, plot);
		
		var
		offset, anchor, labelOffset;

		if(this.origin == axis.range.upper) {
			offset      = this.labelHorizontalOffset;
			anchor      = "start";
			labelOffset = this.textSize * this.verticalLabelRightPositionFactor;
		}
		else {
			offset      = -this.labelHorizontalOffset;
			anchor      = "end";
			labelOffset = -this.textSize * this.verticalLabelLeftPositionFactor;
		}

		if(this.showAxis) { this.append(this.drawVerticalAxis(area, kCoord));}
		if(this.showLines) { this.append(this.renderLines(plotArea, plot, plane, kCoord + offset));}
		if(this.showTicks) { this.append(this.renderTicks(plotArea, plot, plane, kCoord));}
		if(this.showLabels) { this.append(this.renderLabels(plotArea, plot, plane, kCoord + offset, this.textSize, anchor));}
		if(this.showLabel && this.label) { this.append(this.drawVerticalLabel(plotArea, kCoord, labelOffset));}
	},

	render: function(plotArea, plot, axis, plane) {
		if(plane == "x"){
			this.renderHorizontal(plotArea, plot, axis, plane);
		} else {
			this.renderVertical(plotArea, plot, axis, plane);
		}
		return this.getDrawNode();
	}
});

if(dojo.render.svg.capable){
	dojo.require("dojo.svg");

	dojo.lang.extend(dojo.charting.StyledAxis, {
		createDrawNode: function() {
			return document.createElementNS(dojo.svg.xmlns.svg, "g");
		},

		drawAxis: function(x0, y0, x1, y1)
		{
			var
			line = document.createElementNS(dojo.svg.xmlns.svg, "line");

			line.setAttribute("style", "stroke:" + this.axisColor + ";stroke-width:" + this.axisWidth + "px;");
			line.setAttribute("x1", x0)
			line.setAttribute("y1", y0)
			line.setAttribute("x2", x1);
			line.setAttribute("y2", y1);
			return line;
		},

		drawLabel: function(x, y, transform) {
			var 
			text = document.createElementNS(dojo.svg.xmlns.svg, "text");
			
			text.setAttribute("x", x);
			text.setAttribute("y", y);
			text.setAttribute("style", this.titleStyle());
			if(transform) {
				text.setAttribute("transform", transform);
			}
			text.appendChild(document.createTextNode(this.label));
			return text;
		},

		drawHorizontalLabel: function(plotArea, kCoord) {
			return this.drawLabel(
				plotArea.size.width / 2,
				kCoord + (this.textSize * 2.5)
			);
		},

		drawVerticalLabel: function(plotArea, kCoord, labelOffset) {
			var 
			x = kCoord + labelOffset,
			y = plotArea.size.height / 2;
			
			return this.drawLabel(
				x,
				y,
				"rotate(-90, " + x + ", " + y + ")"
			);
		}

	});
}
else {
	dojo.lang.extend(dojo.charting.StyledAxis, {
		createDrawNode: function() {
			return document.createElement("div");
		},
		
		drawAxis: function(x0, y0, x1, y1)
		{
			var 
			line = this.nodes.axis = document.createElement("v:line");
			line.setAttribute("strokecolor", this.axisColor);
			line.setAttribute("strokeweight", this.axisWidth +"px");
			line.setAttribute("from", x0 + "px," + y0 + "px");
			line.setAttribute("to",   x1 + "px," + y1 + "px");
			
			line.style.position  = "absolute";
			line.style.top       = "0px";
			line.style.left      = "0px";
			line.style.antialias = "false";

			return line;
		},

		drawHorizontalLabel: function(plotArea, kCoord) {
			var
			x = plotArea.size.width / 2,
			y = kCoord + (this.textSize * 2.5);

			var 
			text = document.createElement("div");
			text.style.fontFamily = "sans-serif";
			text.style.fontWeight = "normal";
			text.style.position   = "absolute";
			text.style.textAlign  = "center";
			text.innerHTML        = this.label;
			text.style.fontSize   = this.textSize + "px";

			text.style.top        = y + "px";
			text.style.left       = x + "px";
			document.body.appendChild(text);
			text.style["left"]    = x - (text.offsetWidth / 2) + "px";
			return text;
		},

		drawVerticalLabel: function(plotArea, kCoord, labelOffset) {
			var 
			x = kCoord + labelOffset,
			y = plotArea.size.height / 2;

			var 
			text = document.createElement("div");
			text.style.fontFamily  = "sans-serif";
			text.style.fontWeight  = "normal";
			text.style.position    = "absolute";
			text.style.textAlign   = "center";
			text.innerHTML         = this.label;
			text.style.fontSize    = this.textSize + "px";
			text.style["left"]     = x - this.textSize + "px"; // should be left or right depending on position
			document.body.appendChild(text);

			text.style.height      = plotArea.size.height + "px";
			text.style.writingMode = "tb-rl";
			text.style.filter      = "flipV() flipH()"
			text.style.top         = y - (text.offsetHeight / 2) + "px";
			return text;
		}
		
//		createTitleText()
	});
}
