﻿dojo.require("dojo.collections.Store");
dojo.require("dojo.charting.Chart");
dojo.require("dojo.json");

dojo.require("dojo.html.layout");

dojo.extend(dojo.charting.Chart, {
	render:function(){
		//	summary
		//	Render the chart in its entirety.
		this.node.style.position = "relative";
		for(var i=0; i<this.plotAreas.length; i++){
			var 
			area = this.plotAreas[i].plotArea,
			node = area.initialize();
			
			//node.setAttribute("class", "wirePlotArea");
			node.style.position = "absolute";
			node.style.top  = this.plotAreas[i].top + "px";
			node.style.left = this.plotAreas[i].left + "px";
			
			this.node.appendChild(node);
			area.render();
		}
	}
});

function Point(xData, yData) {
	return {x: xData, y: yData};
}

function getArrowPointsSS(width, length) {
	var
	diagonal  = Math.sqrt(2) * width,
	halfWidth = width / 2;

	return [
		{x: halfWidth, y: 0},
		{x: 0,         y: -length},
		{x: halfWidth, y: 0},
		{x: -diagonal, y: -diagonal},
		{x: -diagonal, y: diagonal},
		{x: halfWidth, y: 0},
		{x: 0,         y: length},
	];
}

function getArrowPoints(width, length) {
	return [
		{x: 0, y: 0},
		{x: 0, y: -length},
		{x: 3, y: -length}
	];
}

function toRadians(angle) {
	return angle * Math.PI / 180;
}

function 
rotatePoint(source, angle) {
	var
	sine         = Math.sin(toRadians(angle)),
	cosine       = Math.cos(toRadians(angle));

	return Point(
		-(source.x * cosine - source.y * sine),		// Inverted, we want angles clockwise
		source.x * sine   + source.y * cosine
	);
}

function
movePoint(point, offset) {
	return Point(point.x + offset.x, point.y + offset.y);
}

function buildArrowPath(x, y, path) {
	var
	svgPath = "M " + x + "," + y + " ";

	for(var i = 0; i < path.length; i++) {
		svgPath += "l " + path[i].x + "," + path[i].y + " ";
	}
	return svgPath; // + " Z";
}

function
calculateMaximum(values) {
	var
	max   = Number.MIN_VALUE;
	for (var i = 0; i< values.length; i++){
		max = Math.max(values[i].speed,max);
	}
    return max;  //  float
}

dojo.mixin(dojo.charting.Plotters, {
	Wind: function(data, plotarea, plot, applyTo){
		var 
		group    = createWindGroup(plotarea),
		maxValue = calculateMaximum(data);
		for (var i=0; i<data.length; i++){
			var
			x      = plot.axisX.getCoord(data[i].x, plotarea, plot),
			y      = plot.axisY.getCoord(0, plotarea, plot),
			length = plot.axisY.range.upper * 0.7,
			speed  = length * data[i].speed / maxValue;
			end    = rotatePoint({x:0, y:speed}, data[i].direction),
			x1     = plot.axisX.getCoord(data[i].x + end.x, plotarea, plot),
			y1     = plot.axisY.getCoord(end.y, plotarea, plot),
			line   = createWindLine(x, y, x1, y1, data[i].text, plotarea.getArea());
			
			group.appendChild(line);
		}
		return group;
	}
});


if(dojo.render.svg.capable){
	dojo.require("dojo.svg");

	function
	createWindGroup(plotarea)
	{
		return document.createElementNS(dojo.svg.xmlns.svg, "g");
	}
	
	function
	createWindLine(x0, y0, x1, y1, title, area)
	{
		var
		line = document.createElementNS(dojo.svg.xmlns.svg, "line");

		line.setAttribute("style", "stroke: rgb(0, 0, 0); stroke-width: 1px;");
		line.setAttribute("title", title);
		line.setAttribute("x1", x0)
		line.setAttribute("y1", y0)
		line.setAttribute("x2", x1);
		line.setAttribute("y2", y1);
		return line;
	}
}
else {
	function
	createWindGroup(plotarea)
	{
		return dojo.charting.Plotters._group(plotarea);
	}

	function
	createWindLine(x0, y0, x1, y1, title, area)
	{
		var
		line = createVMLShape(0, area.right-area.left, area.bottom-area.top);

		line.setAttribute("from", x0 + "," + y0);
		line.setAttribute("to", x1 + "," + y1);
		line.setAttribute("title", title);
		return line;
	}

	function
	createVMLShape(color, width, height)
	{
		var 
		path = document.createElement("v:line");
		path.setAttribute("strokeweight", "1px");
		path.setAttribute("strokecolor",  color);
		path.setAttribute("fillcolor",    "red");
		path.setAttribute("filled",       "true");
		path.setAttribute("coordsize", width + "," + height);
		path.style.position = "absolute";
		path.style.top      = "0px";
		path.style.left     = "0px";
		path.style.width    = width + "px";
		path.style.height   = height + "px";

		return path;
	}
}
