/**
 * @name MapIconMaker
 * @version 1.1
 * @author Pamela Fox
 * @copyright (c) 2008 Pamela Fox
 * @fileoverview This gives you static functions for creating dynamically
 *     sized and colored marker icons using the Charts API marker output.
 */

/*
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License. 
 */
var MapIconMaker = {};
var map = null;
var mgr = null;

MapIconMaker.createFlatIcon = function (opts) {
  var width = opts.width || 32;
  var height = opts.height || 28;

  switch(opts.continent){
	  case 1:  var primaryColor = '0071C1';	break // eu
	  case 2:  var primaryColor = '8766B8';	break // am
	  case 3:  var primaryColor = '666666';	break // af
	  case 4:  var primaryColor = 'B7CB00';	break // az
	  case 5:  var primaryColor = 'FF9900';	break // oc
  }

  var shadowColor = '000000';
  var label = MapIconMaker.escapeUserText_(opts.label) || "";
  var labelColor = opts.labelColor || "#000000";
  var labelSize = opts.labelSize || 0;
  var shape = opts.shape ||  "circle";
  var shapeCode = (shape === "circle") ? "it" : "itr";
  var baseUrl = sbase+"/externals/cache.php?cht=" + shapeCode;
  var iconUrl = baseUrl + "&chs=" + width + "x" + height + "&chco=" + primaryColor.replace("#", "") + "," + shadowColor.replace("#", "") + "&chl=" + label + "&chx=" + labelColor.replace("#", "") + "," + labelSize;
  var icon = new GIcon(G_DEFAULT_ICON);
  icon.image = iconUrl + "&chf=bg,s.00000000" + "&ext=.png";
  icon.iconSize = new GSize(width, height);
  icon.shadowSize = new GSize(0, 0);
  icon.iconAnchor = new GPoint(width / 2, height / 2);
  icon.infoWindowAnchor = new GPoint(width / 2, height / 2);
  icon.printImage = iconUrl + "&chof=gif";
  icon.mozPrintImage = iconUrl + "&chf=bg,s,ECECD8" + "&chof=gif";
  icon.transparent = iconUrl + "&chf=a,s,ffffff01&ext=.png";
  icon.imageMap = [0, 0, width, 0, width, height, 0, height];

  return icon;
};

MapIconMaker.escapeUserText_ = function (text) {
  if (text === undefined) {
    return null;
  }
  text = text.replace(/@/, "@@");
  text = text.replace(/\\/, "@\\");
  text = text.replace(/'/, "@'");
  text = text.replace(/\[/, "@[");
  text = text.replace(/\]/, "@]");
  return encodeURIComponent(text);
};

function getIcon(label, c){
	var iconOptions = {};
	if(label.length > 6){ iconOptions.width = 53; }
	else{
		if(label.length > 3){ iconOptions.width = 40; }
		else{ iconOptions.width = 27; }
	}
	iconOptions.primaryColor = "#0152AB";
	iconOptions.shadowColor = "#000000";
	iconOptions.label = label;
	iconOptions.labelSize = 11;
	iconOptions.labelColor = "#FFFFFF";
	iconOptions.shape = "block";
	iconOptions.continent = c;
	return MapIconMaker.createFlatIcon(iconOptions);
}

function load() {
  if (GBrowserIsCompatible()) {
	map = new GMap2(document.getElementById("map"));
	map.setCenter(new GLatLng(50, 20), 3);
	map.addControl(new GLargeMapControl3D());
	window.setTimeout(setupMarkers, 1000);
  }
}

function setupMarkers(){
	markersArray = [];
	for(key in ctr){
		var point = new GLatLng(ctr[key][2], ctr[key][3]);
		markersArray.push(createMarker(point, getIcon(key, ctr[key][1]), ctr[key][0]));  
		//markersArray.push(new GMarker(point));
	}
	cluster= new ClusterMarker(map, { markers: markersArray, clusteringEnabled: false } );
	cluster.refresh();
}


function toEurope(){ map.panTo(new GLatLng(50, 20)) }
function toAmerica(){ map.panTo(new GLatLng(15, -100)) }
function toAfrica(){ map.panTo(new GLatLng(0, 20)) }
function toAsia(){ map.panTo(new GLatLng(40, 95)) }
function toOceania(){ map.panTo(new GLatLng(-25, 150)) }


function createMarker(point, icon, id) {
	var marker = new GMarker(point, {icon: icon, draggable:false});
	GEvent.addListener(marker, "click", function() {
		markerClickEvent(marker, id);
	});
	return marker;
}
