(function($){
	jQuery.fn.presentationMapInternalCloudmade = function(options){
		var $pmic = jQuery(this);
		
		var options = jQuery.extend({
			target_div:null,
			lat:40.711667,
			lng:-74.0125,
			zoom:14,
			cloudmade_key:'f827e663f0175263ad50db534dce5ffc'
		},options);
		
		var cloudMade = null;
		var map = null;
		
		var global_overlay = null
		
		function initializeMaps(){
			if(options.target_div != null){
				//old map style: 1173, 2830
				cloudMade = new CM.Tiles.CloudMade.Web({key: options.cloudmade_key,styleId:2830});
				map = new CM.Map(options.target_div, cloudMade);
	    		map.setCenter(new CM.LatLng(options.lat, options.lng), options.zoom);
			} else {
				alert("No Target Div Provided for Map Internals.");
			};
		};
		initializeMaps();
		
		$pmic.restoreDefaults = function(){
			map.setCenter(new CM.LatLng(options.lat, options.lng), options.zoom);
		};
		
		$pmic.addGrayCurtain = function(){
			var $c = jQuery("<div></div>");
			$c.css("backgroundImage","../images/10gray.png");
			$c.css("backgroundColor","red");
			$c.css("width","100%");
			$c.css("height","100%");
			$c.css("display","block");
			$c.css("top","0px");
			$c.css("left","0px");
			jQuery(options.target_div).children(".wml-map-layer").append($c);
		};
		
		$pmic.disableMouseZoom = function(){
			map.disableScrollWheelZoom();
			//map.disableDoubleClickZoom();
			map.disableShiftDragZoom();
			map.disableMouseZoom();
		};
		
		$pmic.fixBackground = function(background){
			jQuery(options.target_div).css("background",background);
		};
		
		$pmic.fixLoader = function(image){
			jQuery(options.target_div).children(".wml-loading-indicator").css("background",image);
		};
		
		$pmic.formatCopyright = function(){
			
			var ct = '© 2009 <a href="http://cloudmade.com" target="_blank">CloudMade</a> - Map data <a href="http://creativecommons.org/licenses/by-sa/2.0/" target="_blank">CCBYSA</a> 2009 <a href="http://openstreetmap.org" target="_blank">OpenStreetMap.org</a> contributors';
			jQuery(options.target_div).children(".wml-copyright").addClass("cloudmade_copyright");
			jQuery(options.target_div).children(".wml-copyright").html(ct);
			
		};
		
		$pmic.showCopyright = function(){
			jQuery(options.target_div).children(".wml-copyright").hide();
		};
		
		$pmic.showCopyright = function(){
			jQuery(options.target_div).children(".wml-copyright").show();
		};
		
		$pmic.setCenter = function(lat,lng){
			map.setCenter(new CM.LatLng(lat,lng));
		};
		
		$pmic.setZoom = function(zoom){
			map.setZoom(zoom);
		};
		
		$pmic.zoomIn = function(){
			map.zoomIn();
		};
		
		$pmic.zoomOut = function(){
			map.zoomOut();
		};
		
		$pmic.getZoom = function(){
			return map.getZoom();
		};
		
		$pmic.makeMarker = function(lat,lng,type){
			var latlng = new CM.LatLng(lat,lng);
			var icon = new CM.Icon();
			if(type == "standard"){
				icon.image = "/images/presentation_map_marker_orange_stroke.png";
				icon.iconSize = new CM.Size(9, 9);
				icon.iconAnchor = new CM.Point(4.5,4.5);
				icon.shadowSize = new CM.Size(0,0);
			} else if(type == "selected"){
				icon.image = "/images/presentation_map_marker_selected_stroke.png";
				icon.iconSize = new CM.Size(15, 15);
				icon.iconAnchor = new CM.Point(7.5,7.5);
				icon.shadowSize = new CM.Size(0,0);
			} else if(type == "temporary"){
				icon.image = "/images/presentation_map_temp_marker.png";
				icon.iconSize = new CM.Size(10, 10);
				icon.iconAnchor = new CM.Point(5,5);
				icon.shadowSize = new CM.Size(0,0);
			};
			var marker = new CM.Marker(latlng,{icon:icon})
			return marker;
		};
		
		$pmic.makeLocationMarker = function(type){
			var latlng = null;
			var icon = new CM.Icon();
			switch(type){
				case "wtc":
					latlng = new CM.LatLng(40.711667, -74.0125);
					icon.image = "/images/MH_mapicon_WTC.png";
					icon.iconSize = new CM.Size(116, 11);
					icon.iconAnchor = new CM.Point(5.5,5.5);
					icon.shadowSize = new CM.Size(0,0);
					break;
				case "shanksville":
					latlng = new CM.LatLng(40.017182, -78.905891);
					icon.image = "/images/MH_mapicon_Shanks.png";
					icon.iconSize = new CM.Size(75, 11);
					icon.iconAnchor = new CM.Point(5.5,5.5);
					icon.shadowSize = new CM.Size(0,0);
					break;
				case "pentagon":
					latlng = new CM.LatLng(38.870989, -77.055961);
					icon.image = "/images/MH_mapicon_Pentagon.png";
					icon.iconSize = new CM.Size(65, 12);
					icon.iconAnchor = new CM.Point(5.5,5.5);
					icon.shadowSize = new CM.Size(0,0);
					break;
			};
			var marker = new CM.Marker(latlng,{icon:icon,clickable:false});
			return marker;
		};
		
		$pmic.generateWTCOverlay = function(){
			map.setCenter(new CM.LatLng(options.lat, options.lng), options.zoom);
			var latLngs = [	new CM.LatLng(40.7131263,-74.0133476),
							new CM.LatLng(40.7110282,-74.0138626),
							new CM.LatLng(40.7101011,-74.0119100),
							new CM.LatLng(40.7119715,-74.0107512)];
			var wtcOverlay = new CM.Polygon(latLngs,"#ff9900",1,0.4,"#ff9900",0.4);
			return wtcOverlay;
		}
		
		$pmic.makeGlobalGrayOverlay = function(){
			var latLngs = [	new CM.LatLng(89,-179),
							new CM.LatLng(89,179),
							new CM.LatLng(-89,179),
							new CM.LatLng(-89,-179)];
			global_overlay = new CM.Polygon(latLngs,"#31415F",1,0.25,"#31415F",0.25);
			return global_overlay;
		}
		
		
		$pmic.addMarker = function(marker,hidden){
			map.addOverlay(marker);
			if(hidden == true){
				marker.hide();
			}
		};
		
		$pmic.destroyMarker = function(item,marker){
			if(item != null){
				CM.Event.removeListener(marker,'click',item.clickHandler);
			};
			map.removeOverlay(marker);
		};
		
		$pmic.zoomToBounds = function(itemBounds){
			var cmBounds = new CM.LatLngBounds(
					new CM.LatLng(itemBounds.minLat,itemBounds.minLng),
					new CM.LatLng(itemBounds.maxLat,itemBounds.maxLng)
				);
			map.setCenter(cmBounds.getCenter());
			map.zoomToBounds(cmBounds);
			map.zoomOut();
		};
		
		$pmic.panTo = function(lat,lng){
			map.setCenter(new CM.LatLng(lat,lng));
		};
		
		$pmic.zoomTo = function(level){
			if(level != null){
				map.setZoom(level);
			};
			
		};
		
		$pmic.viewportContainsMarker = function(item){
			// var vp_bounding = map.getBounds();
			// var point_bounding =new CM.LatLngBounds(new CM.LatLng(item.lat, item.lng), new CM.LatLng(item.lat, item.lng));
			// 
			// return vp_bounding.contains(point_bounding);
			return map.getBounds().contains( new CM.LatLngBounds( new CM.LatLng(item.lat, item.lng), new CM.LatLng(item.lat, item.lng) ) );
		};
		
		$pmic.addClickHandlerToItemMarkers = function(item,marker){
			CM.Event.addListener(marker,'click',item.clickHandler);
		};
		
		$pmic.toggleMarkerSelectedState = function(item){
			if(item.markers.marker.isHidden()){
				item.markers.selectedMarker.hide();
				item.markers.marker.show();
			} else {
				item.markers.marker.hide();
				item.markers.selectedMarker.show();
			}
		};
		$pmic.selectItem = function(item){
			item.markers.marker.hide();
			item.markers.selectedMarker.show();
		};
		$pmic.deselectItem = function(item){
			item.markers.selectedMarker.hide();
			item.markers.marker.show();
		};
		
		
		return $pmic;
	}
})(ns11mmPresentation);