var map_div = document.getElementById("map");
var toolbox_div = document.getElementById("toolbox");
var content_div = document.getElementById("content");

var main_img = document.getElementById("main");

var map = new GMap2(map_div);
var large_control = new GLargeMapControl(); 
var small_control = new GSmallMapControl();

var tools = Object();

map.addControl(large_control);
map.addControl(new GScaleControl());
map.addControl(new GOverviewMapControl());

map.setCenter(new GLatLng(-33.918282, 151.22569),14, G_SATELLITE_MAP);

function getWindowSize(){
  var e = new Object();
  if(window.self && self.innerWidth){
    e.width = self.innerWidth;
    e.height = self.innerHeight;
  }else if(document.documentElement && document.documentElement.clientHeight){
    e.width = document.documentElement.clientWidth;
    e.height = document.documentElement.clientHeight;
  }else{
    e.width = document.body.clientWidth;
    e.height = document.body.clientHeight;
  }
  return e;
}

var head_height = 20;

var limit = 285;

var last_height = limit + 1;
var last_width  = 0;

var width_multiplier = 1.0;
var hieght_multiplier = 1.0;

var img_width = 400;
var img_height = 300;
var img_aspect = img_width / img_height;

var q = 0;

window.onresize = function(){
	var NS = getWindowSize();
	
	cur_width = (NS.width) * width_multiplier; // - tool_width);
	cur_height = (NS.height) * hieght_multiplier - head_height;

	map_div.style.width = cur_width + 'px';
	map_div.style.height = cur_height + 'px';

	toolbox_div.style.top = (50) + 'px';
	toolbox_div.style.left = (cur_width - 71 - 9) + 'px';
	
	content_div.style.height = cur_height + 'px';
	content_div.style.width = (NS.width - cur_width) + 'px';

	if (cur_height > last_height) {
		if (last_height < limit && cur_height > limit) {
			map.removeControl(small_control);
			map.addControl(large_control);
		}
	} else {
		if (last_height > limit && cur_height < limit) {
			map.removeControl(large_control);
			map.addControl(small_control);
		}
	}

	last_height = cur_height;
	last_width = cur_width;

	var center = map.getCenter()
	map.checkResize();
	map.setCenter(center);
};

window.onresize();

function change_size(x, y) {
	width_multiplier = x;
	hieght_multiplier = y;
	window.onresize();
}

var cur_tool = null;
var cur_inner_div = null;

function add_tool(toolname, tool_script, tool_id) {

	var script = document.createElement('script');
	script.setAttribute('src', tool_script);
	document.body.appendChild(script);
	var tool_div = document.createElement('div');
	var inner_div = document.createElement('div');

	tool_div.setAttribute('class', 'toolouter');
	inner_div.setAttribute('class', 'toolinner');

	tool_div.appendChild(inner_div);
	inner_div.innerHTML = toolname;
	toolbox_div.appendChild(tool_div);
	tool_div.onclick = function() {
		if (q == 0) {
			inner_div.style["borderColor"] = 'grey white white grey';
			inner_div.style["fontWeight"] = 'bold';
			change_size(0.7, 1)
			cur_inner_div = inner_div;
			cur_tool = tools[tool_id]
			cur_tool.tool();
			q = 1;
		} else {
			cur_inner_div.style["borderColor"] = 'white grey grey white';
			cur_inner_div.style["fontWeight"] = '';
			if (cur_tool == tools[tool_id]) {
				cur_tool = null;
				cur_inner_div = null;
				change_size(1, 1);
				q = 0;
			} else {
				inner_div.style["borderColor"] = 'grey white white grey';
				inner_div.style["fontWeight"] = 'bold';
				change_size(0.7, 1)
				cur_inner_div = inner_div;
				cur_tool = tools[tool_id]
				cur_tool.tool();
			}
		}
	}
}

GEvent.addListener(map, "zoom", function () {
			   if (cur_tool && cur_tool.zoom) {
				   cur_tool.zoom();
			   }
		   });

GEvent.addListener(map, "click", function (overlay, point) {
			   if (cur_tool) {
				   cur_tool.click(overlay, point);
			   }
		   });

GEvent.addListener(map, "moveend", function (overlay, point) {
			   /*update_location();			   */
			   if (cur_tool && cur_tool.moveend) {
				   cur_tool.moveend(overlay, point);
			   }
		   });


add_tool("Length", "length.js", "lf");
add_tool("Mark", "mark.js", "mark");
add_tool("NSWGeo", "nswgeo.js", "nswgeo");
/*
add_tool("Things", "things.js", "things")
add_tool("GPX", "showgpx.js", "showgpx")
*/
add_tool("About", "about.js", "about");


/* Add the TileInfo custom map */
var tilelayer = new GTileLayer(new GCopyrightCollection("benno.id.au"), 0, 19);
tilelayer.getTileUrl = function (point, zoom) {
	var url = "http://www.benno.id.au/map/tileinfo.cgi?zoom=" + zoom + "&x=" + point.x + "&y=" + point.y;
	return url;
}
tilelayer.getCopyright = function (point, zoom) {
	return "TileInfo (c) Ben Leslie"
};

map.addMapType(new GMapType([G_SATELLITE_MAP.getTileLayers()[0], tilelayer], 
			     G_SATELLITE_MAP.getProjection(), 
			     "TileInfo",
			    {errorMessage:_mMapError}
			    ));

/* Map Type control must be added *after* adding any custom maps */
map.addControl(new GMapTypeControl());
