function CustomMapTypeControl() {
}

CustomMapTypeControl.prototype = new GControl();

CustomMapTypeControl.prototype.initialize = function(map) {
	var container = document.createElement("div");
	container.style.width = "226px";
	container.style.height = "30px";
	
	mapTypes = map.getMapTypes();

	/*
	for(var i = 0; i < mapTypes.length; i++) {
		var mapTypeElement = this.createButtonElement(mapTypes[i].getName());
		container.appendChild(mapTypeElement);
		GEvent.addDomListener(mapTypeElement, "click", function() {
			var n = i;
			GLog.write(n);
			// map.setMapType(this.mapType);
		});
		
	}
	*/
	var mapElement = this.createButtonElement(mapTypes[0].getName(), true);
	mapElement.style.left = "0px";
	
	var satelliteElement = this.createButtonElement(mapTypes[1].getName(), false);
	satelliteElement.style.left = "113px";

	container.appendChild(mapElement);
	GEvent.addDomListener(mapElement, "click", function() {
		map.setMapType(mapTypes[0]);
		setBgImage(mapElement, "/pix/kuvakkeet/img_map_type_active.png");
		setBgImage(satelliteElement, "/pix/kuvakkeet/img_map_type.png");
		/*
		mapElement.style.background = "url(http://kokouspaikat-fe.dev.toimivanet.com/pix/kuvakkeet/img_map_type_active.png) no-repeat left top";
		satelliteElement.style.background = "url(http://kokouspaikat-fe.dev.toimivanet.com/pix/kuvakkeet/img_map_type.png) no-repeat left top";
		*/
	});

	container.appendChild(satelliteElement);
	GEvent.addDomListener(satelliteElement, "click", function() {
		map.setMapType(mapTypes[1]);
		setBgImage(satelliteElement, "/pix/kuvakkeet/img_map_type_active.png");
		setBgImage(mapElement, "/pix/kuvakkeet/img_map_type.png");
		/*
		satelliteElement.style.background = "url(http://kokouspaikat-fe.dev.toimivanet.com/pix/kuvakkeet/img_map_type_active.png) no-repeat left top";
		mapElement.style.background = "url(http://kokouspaikat-fe.dev.toimivanet.com/pix/kuvakkeet/img_map_type.png) no-repeat left top";
		*/
	});

	map.getContainer().appendChild(container);
	return(container);
}

CustomMapTypeControl.prototype.getDefaultPosition = function() {
	return new GControlPosition(G_ANCHOR_TOP_RIGHT, new GSize(7, 7));
}

CustomMapTypeControl.prototype.createButtonElement = function(text, active) {
	var button = document.createElement("div");
  
	button.appendChild(document.createTextNode(text));
	// button.style.display = "inline";
	button.style.color = "white";
	button.style.width = "113px";
	button.style.height = "30px";

	if(active) {
		setBgImage(button, "/pix/kuvakkeet/img_map_type_active.png");
	}
	else {
		setBgImage(button, "/pix/kuvakkeet/img_map_type.png");
	}
	
	button.style.font = "small Arial";
	button.style.margin = "0px";
	// button.style.border = "1px solid black";
	// button.style.padding = "5px 10px";
	button.style.lineHeight = "25px";
	button.style.textAlign = "center";
	button.style.verticalAlign = "bottom";
	button.style.cursor = "pointer";
	button.style.position = "absolute";

	return(button);
}

function setBgImage(element, src) {
	var nv = navigator.appVersion;
	if (nv.match("MSIE 6.0") || nv.match("MSIE 5.")) {
		element.style.filter = "progid:DXImageTransform.Microsoft.AlphaImageLoader(src='"+ src +"', sizingMethod='crop')";
	}
	else {
		element.style.background = "url(" + src + ") no-repeat left top";
	}
}
