﻿
function FormatMessage(direction, speed)
{
	return "Dirección: " + direction + "\nVelocidad: " + speed + " m/s";
}

function BuildTag()
{
	var
	newTag = "<area shape='poly' coords='" + 
		this.coords + 
		"' title='" + 
		FormatMessage(this.direction, this.speed) + 
		"'/>";
	return newTag;
}

function Area(direction, speed, coords)
{
	this.direction = (360 - (direction % 360)) % 360;
	this.speed     = speed;
	this.coords    = coords;
	this.BuildTag  = BuildTag;
}

function RebuildMap(map, speedListElement)
{
	if(speedListElement == null) {
		return;
	}
	
	speedList = eval(speedListElement.getAttribute('speedList'));
	newHTML   = "";
	index     = 0;
	
	if(map != null) {
		for(i = 0; i < map.childNodes.length; i++) {
			node = map.childNodes[i];
			if((node.nodeName.toLowerCase() == 'area') && (node.shape.toLowerCase() == 'poly')){
				if(IsNonZeroValue(node.title)) {
					newHTML += (new Area(index, speedList[Math.floor(index / 360)], map.childNodes[i].coords)).BuildTag();
				}
				index ++;
			}
		}
		map.innerHTML = newHTML;
	}
}

function IsNonZeroValue(data)
{
	return (data.lastIndexOf('1') == data.length - 1);
}
