﻿// labels items marked as 'label'
//

dojo.charting.DateStyledAxis = function (minValue, maxValue, data) {
	dojo.charting.StyledAxis();
	this.range               = {lower: minValue, upper: maxValue};
	this.origin              = "0";
	this.labels              = this.createLabels(data);
	this.showAxis            = false;
	this.labelVerticalOffset = -(axisX.textSize / 2);
	this.nodes = { main: null, axis: null, label: null, labels: null, lines: null, ticks: null };
	this._labels             = new Array();
}

dojo.lang.inherits(dojo.charting.DateStyledAxis, dojo.charting.StyledAxis);

dojo.lang.extend(dojo.charting.DateStyledAxis, {
	getLabelValue: function(data) {
		if(data.label) {
			return data.label;
		}
		else {
			return ".";
		}
	},

	createLabels: function(data) {
		var
		startValue = this.range.lower,
		upperValue = this.range.upper,
		delta      = (this.range.upper - this.range.lower) / (data.length - 1),
		labels     = new Array();

		for(i = 0; i < data.length; i++) {
			labels.push({ label: this.getLabelValue(data[i]), value: data[i].x });
		}
		return labels;
	},

	getLabels: function() {
		if(this.nodes.labels == null) {
			this.nodes.labels = this.createDrawNode();
			this.nodes.labels.setAttribute("id", this.getId()+"-labels");
		}
		return this.nodes.labels;
	},
	
	appendLabel: function(newElement) { 
		this.getLabels().appendChild(newElement);
	},

	removeExistingLabels: function() {
		if(this.getLabels()){
			while(this.getLabels().childNodes.length > 0){
				this.getLabels().removeChild(this.getLabels().childNodes[0]);
			}
			if(this.getLabels().parentNode){
				this.getLabels().parentNode.removeChild(this.getLabels());
				this.getLabels() = null;
			}
		}
		this.nodes.labels = null;
	},

	renderLabels: function(
		/* dojo.charting.PlotArea */plotArea, 
		/* dojo.charting.Plot */    plot, 
		/* string */                plane,
		/* float */                 coord,
		/* int */                   textSize,
		/* string */                anchor
	){
		this.removeExistingLabels();
		
		var
		labelWidth        = textSize * 6,
		startPoint        = this.getCoord(this._labels[0].value, plotArea, plot),
		endPoint          = this.getCoord(this._labels[this._labels.length - 1].value, plotArea, plot),
		totalWidth        = endPoint - startPoint,
		maximumLabelCount = totalWidth / labelWidth,
		valueDelta        = (this.range.upper - this.range.lower),
		minimumSeparation = valueDelta / maximumLabelCount,
		lastLabelPosition = this.range.lower - minimumSeparation * 2;

		for(var i=0; i<this._labels.length; i++){
			var
			labelItem = this._labels[i];
			if((labelItem.label.length > 1) && ((labelItem.value - lastLabelPosition) >= minimumSeparation)){
				var
				x = this.getCoord(labelItem.value, plotArea, plot),
				y = coord;
				
				if(plane == "y") {
					var
					temp = x;
					x    = y; 
					y    = temp;
				}
				
				this.appendLabel(this.createLabel(labelItem.label, x, y, textSize, anchor, plane));
				lastLabelPosition = labelItem.value;
			}
		}
		return this.getLabels();
	}
});

if(dojo.render.svg.capable){
	dojo.require("dojo.svg");
	dojo.lang.extend(dojo.charting.DateStyledAxis, {
		createLabel: function(label, x, y, textSize, anchor, plane){
			var text = document.createElementNS(dojo.svg.xmlns.svg, "text");
			text.setAttribute("x", x);
			text.setAttribute("y", (plane=="x"?y:y+2));
			text.setAttribute("style", "text-anchor:"+anchor+";font-family:sans-serif;font-size:"+textSize+"px;fill:#000;");
			text.appendChild(document.createTextNode(label));
			return text;
		}
	});
}
else {
	dojo.lang.extend(dojo.charting.DateStyledAxis, {
		createLabel: function(label, x, y, textSize, anchor, plane){
			var 
			text                  = document.createElement("div");
			text.innerHTML        = label;
			text.style.fontSize   = textSize + "px";
			text.style.fontFamily = "sans-serif";
			text.style.position   = "absolute";
			text.style.top        = y - textSize + "px";

			if(anchor == "center"){
				text.style.left      = x + "px";
				text.style.textAlign = "center";
			} else if (anchor == "left"){
				text.style.left      = x + "px";
				text.style.textAlign = "left";
			} else if (anchor == "right"){
				text.style.right     = x + "px";
				text.style.textAlign = "right";
			}

			document.body.appendChild(text);
			if(plane == "x") {
				text.style.left = x - (text.offsetWidth / 2) + "px";
			}
			else {
				text.style.top  = y - (text.offsetWidth / 2) + "px";
			}

			return text;
		}
	});
}