function CustomMapTypeControl() {
}

CustomMapTypeControl.prototype = new GControl();

CustomMapTypeControl.prototype.buttonElements = [];

CustomMapTypeControl.prototype.initialize = function(map) {
	mapTypes = map.getMapTypes();
	
	this.container = document.createElement("div");
	this.container.style.height = "30px";
	
	for(var i = 0; i < mapTypes.length; i++) {
		this.addMapType_(mapTypes[i]);
	}

	GEvent.addListener(map, "maptypechanged", 
		GEvent.callback(this, function() {
			var mapType = map.getCurrentMapType();
			
			for(var i = 0; i < this.buttonElements.length; i++) {
				if(this.buttonElements[i].type == mapType)
					CustomMapTypeControl.setBgImage_(this.buttonElements[i].button, "/user_data/css/images/gmap/img_map_type_active.png");
				else
					CustomMapTypeControl.setBgImage_(this.buttonElements[i].button, "/user_data/css/images/gmap/img_map_type.png");
			}
		})
	);

	GEvent.addListener(map, "addmaptype", GEvent.callback(this, this.addMapType_));
	GEvent.addListener(map, "removemaptype", GEvent.callback(this, this.removeMapType_));

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


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

/**
 * @access private
 */
CustomMapTypeControl.prototype.addMapType_ = function(mapType) {
	// GLog.write('Adding map type ' + mapType.getName());
	
	for(var i = 0; i < this.buttonElements.length; i++) {
		if(this.buttonElements[i].type == mapType)
			return;
	}
	
	var mapTypeElement = this.createButtonElement(mapType.getName());
	mapTypeElement.style.left = (this.buttonElements.length * 113) + "px";
	
	this.buttonElements.push({ type: mapType, button: mapTypeElement });
	this.container.style.width = (this.buttonElements.length * 113) + "px";
	this.container.appendChild(mapTypeElement);
	GEvent.addDomListener(mapTypeElement, "click",
		GEvent.callbackArgs(this, function(type) {
			map.setMapType(type);
		},
		mapType
	));
}

/**
 * @access private
 */
CustomMapTypeControl.prototype.removeMapType_ = function(mapType) {
	// GLog.write('Removing map type ' + mapType.getName());
	
	for(var i = 0; i < this.buttonElements.length; i++) {
		if(this.buttonElements[i].type == mapType) {
			GEvent.clearListeners(this.buttonElements[i].button, "click");
			this.container.removeChild(this.buttonElements[i].button);
			this.buttonElements.splice(i, 1);
			this.container.style.width = (this.buttonElements.length * 113) + "px";

			for( ; i < this.buttonElements.length; i++) {
				this.buttonElements[i].button.style.left = (i * 113) + 'px';
			}
			break;
		}
	}
}

/**
 * @access private
 */
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) {
		CustomMapTypeControl.setBgImage_(button, "/user_data/css/images/gmap/img_map_type_active.png");
	}
	else {
		CustomMapTypeControl.setBgImage_(button, "/user_data/css/images/gmap/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);
}

/**
 * @access private
 */
CustomMapTypeControl.setBgImage_ = function(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";
	}
}
