var map;
var _weatherTimer;
var locations = {}

function loadMap()
{
	window.onresize = function()
	{ 
		var gadget = typeof gadgetMode != 'undefined';
		var w = gadget ? 0 : 128;
		var h = gadget ? 0 : 150;
		document.getElementById("map").style.width = (GetWindowWidthHeight().width-w)+"px";	
		document.getElementById("map").style.height = (GetWindowWidthHeight().height-h)+"px";	
	}
	
	window.onresize();
	
	map = new FE.Map(document.getElementById("map"), {includeBaseLayers : false});
	map.onLoad = onMapLoad;
	map.load();
}
var clusters = {};
function onTickerReady(r) {
 var lines = r.responseText.split('\n');
 lines.pop();
 for(var i=0; i<lines.length; i++){
  var d = lines[i].split(';');
  var d2 = d[1].split(',');
  var lat = parseFloat(d2[0]);
  var lng = parseFloat(d2[1]);
  var key =  d2[0] + '_' + d2[1];
  if (typeof clusters[key] == 'undefined')
   clusters[key] =  [];
  clusters[key].push(d[0]);
  locations[key] = new FE.LatLng(lat, lng);
 }
 show();
}

var upIcon = new FE.Icon('http://freeearth.poly9.com/world-markets-3d/up_g.gif');
var downIcon = new FE.Icon('http://freeearth.poly9.com/world-markets-3d/down_r.gif');

function getAverage(loc) {
 var a = 0;
 var n = 0;
 for(var i=0; i<clusters[loc].length; i++) {
  var ticker = clusters[loc][i];
  var d = marketData[ticker + ':IND'];
  a += parseFloat(d[3]);
  n++;
 }
 if (n == 0) return 0;
 return a / n;
}

function getText(loc) {
 var txt = '';
 for(var i=0; i<clusters[loc].length; i++) {
  var ticker = clusters[loc][i];
  var d = marketData[ticker + ':IND'];
  if (typeof d =='undefined') {
   continue;
  }
  var color = d[2] < 0 ? '0xff0000' : '0x00ff00';
  txt += '<font color="'+color+'">'+ticker+'</font>\n';
 }
 return txt;
}

function show() {
 map.clearOverlays();
 for(var loc in clusters) {
  var localValue = getAverage(loc);
  var ico = localValue < 0 ? downIcon : upIcon;
  getText(loc);
  var color = localValue < 0 ? '0xff0000' : '0x00ff00';
  var txt = '<font color="'+color+'">' + localValue.toFixed(2) + '%</font>';
  var o = new FE.Placemark(locations[loc], ico, txt, '', 12, color);
  map.addOverlay(o);
 }
}

function err(r,e) {
}

function onMapLoad()
{
	this.getLayerManager().addLayer(new FE.Layer.DayNight());
	this.zoomTo(FE.Map.RADIUS*2);
	this.setTargetLatLng(new FE.LatLng(30,170));
	new Ajax.Request('http://freeearth.poly9.com/world-markets-3d/markets.txt', { onComplete : onTickerReady, onException: err});
	this.getCamera().setLngInertia(-0.1);
}

function GetWindowWidthHeight()
{
	var myWidth = 0, myHeight = 0;
	if(typeof(window.innerWidth) == 'number')
	{
		myWidth = window.innerWidth;
		myHeight = window.innerHeight;
	}
	else if(document.documentElement && (document.documentElement.clientWidth || document.documentElement.clientHeight))
	{
		myWidth = document.documentElement.clientWidth;
		myHeight = document.documentElement.clientHeight;
	}
	else if(document.body && (document.body.clientWidth || document.body.clientHeight))
	{
		myWidth = document.body.clientWidth;
		myHeight = document.body.clientHeight;
	}
	
	return {'width' : myWidth, 'height' : myHeight};
}
