function comap(div){
	this.div = div;
	this.currenttrail = 0;
	this.comapctrl = new comapcontrol(this);
	this.findtrails = 0;
	this.geo = new GClientGeocoder();
	this.lat = [];
	this.lng = [];
	this.bounds = new GLatLngBounds();
	this.map;
	this.radar_layer;
	this.radarOn = false;
	this.radaroverlay;
	this.lasttrailloaded = 0;
	
	this.status=[];
	this.status[G_GEO_SUCCESS]            = "Success";
	this.status[G_GEO_MISSING_ADDRESS]    = "Missing Address";
	this.status[G_GEO_UNKNOWN_ADDRESS]    = "Unknown Address";
	this.status[G_GEO_UNAVAILABLE_ADDRESS]= "Unavailable Address";
	this.status[G_GEO_BAD_KEY]            = "Bad Key";
	this.status[G_GEO_TOO_MANY_QUERIES]   = "Too Many Queries";
	this.status[G_GEO_SERVER_ERROR]       = "Server Error"
	this.geoXml;
	this.radaroverlay;
	this.mainurl = "http://maps.coloradohikers.com/";
	this.minimainurl = "http://maps.coloradohikers.com/minimap.php";
	
	
	
	this.zoomto = function(lat,lng){
		this.map.setCenter(new GLatLng(lat,lng), 17);
		
}

this.showWeather = function(){
	   if (this.radarOn) {
          	this.map.removeOverlay(this.radaroverlay);
          	this.radarOn = false;
		  	$("radar").innerHTML = "<a href=\"javascript:void(0);\" onclick=\"comap.showWeather();\">Turn On Weather Radar</a>";
       } else {
			$("radar").innerHTML = "<a href=\"javascript:void(0);\" onclick=\"comap.showWeather();\">Turn Off Weather Radar</a>";

        	this.map.addOverlay(this.radaroverlay);
        	this.radarOn = true;
      } 
}
this.addothers = function(sw,ne){
		if(this.findtrails != 0){		  	
					this.geoXml = new GGeoXml("http://www.mayerlarson.com/coloradohikers/co100/loadkml.php?" + "ymin=" + this.map.getBounds().getSouthWest().lat() + "&ymax=" + this.map.getBounds().getNorthEast().lat() + "&xmax=" + this.map.getBounds().getSouthWest().lng() + "&xmin=" + this.map.getBounds().getNorthEast().lng());
					this.map.addOverlay(this.geoXml);
					//this.map.addOverlay(this.radaroverlay);
				//	this.gx = new GGeoXml("http://www.gearthhacks.com/forums/downloads.php?do=file&act=down&id=12632");
				//	this.map.addOverlay(this.gx);

				//	$("viewshows").innerHTML = "http://maps.coloradohikers.com/loadkml.php?" + "ymin=" + comap.map.getBounds().getSouthWest().lat() + "&ymax=" + comap.map.getBounds().getNorthEast().lat() + "&xmax=" + comap.map.getBounds().getSouthWest().lng() + "&xmin=" + comap.map.getBounds().getNorthEast().lng();
				  }
				  this.findtrails = 0;
}

this.createlink = function(){
	var varstring = "?lat=" + this.map.getCenter().lat() + "&lng=" + this.map.getCenter().lng() + "&zoom=" + "13" + "&trail=" + this.lasttrailloaded; // + "&maptype=" + this.map.getCurrentMapType().getName()
	var url = this.mainurl + varstring;
	var miniurl = this.minimainurl + varstring;
	$("email").href = url;
	$("urllink").value = url;
	//$("htmlobject").value = "<object type=\"text/html\" data=\""+ miniurl + "\" style=\"border: 0pt none ;\" height=\"400\" width=\"400\"></object>";
	$("iframe").value = "<IFRAME SRC=\"" + miniurl + "\" TITLE=\"rukind maps\" height=\"400\" width=\"400\"></IFRAME>";
}

this.makelink = function(){
	var varstring = "?lat=" + this.map.getCenter().lat() + "&lng=" + this.map.getCenter().lng() + "&zoom=" +"13" + "&trail=" + this.lasttrailloaded; // + "&maptype=" + this.map.getCurrentMapType().getName()
	var url = this.mainurl + varstring;
	var miniurl = this.minimainurl + varstring;
	$("urllink").value = url;
	//$("htmlobject").value = "<object type=\"text/html\" data=\""+ miniurl + "\" style=\"border: 0pt none ;\" height=\"400\" width=\"400\"></object>";
	$("iframe").value = "<IFRAME SRC=\"" + miniurl + "\" TITLE=\"colorado hikers maps\" height=\"400\" width=\"400\"></IFRAME>";
	new Effect.BlindDown('links');
}

    this.getWindowHeight = function () {
	    if (self.innerHeight) return self.innerHeight;
	    if (document.documentElement && document.documentElement.clientHeight)
	        return document.documentElement.clientHeight;
	    if (document.body) return document.body.clientHeight;
	    return 0;
    }
	
    this.getWindowWidth = function () {
	    if (self.innerWidth) return self.innerWidth;
    	if (document.documentElement && document.documentElement.clientWidth)
	        return document.documentElement.clientWidth;
	    if (document.body) return document.body.clientWidth;
	    return 0;
    }
	
	 this.resizer = function () {  
		this.div.style.height = getInsideWindowHeight() - 50 + 'px';
		this.div.style.width = getInsideWindowWidth() - 350 + 'px';
    } 
	
	this.clearOverlays = function(){
		//this.map.removeOverlay(radaroverlay);
		this.map.clearOverlays();
	}
	
	this.LoadKML = function(id, map){
		this.findtrails = 1;
		//this.map.removeOverlay(radaroverlay);
		this.map.clearOverlays();
		
		this.lasttrailloaded = id;
		this.geoXml = new GGeoXml("http://maps.coloradohikers.com/loadkml.php?id=" + id,centerontrail);
		this.map.addOverlay(this.geoXml);
		//this.geoXml.gotoDefaultViewport(this.map);
		//this.map.panTo(this.geoXml.getDefaultCenter());

		
	}
	 
	this.OpenTrail = function(id){
		//alert(id);
		if(id!=0){
			
			this.currenttrail = id;
			this.LoadKML(id,this.map);
		}
	}

    this.load = function(lat,lng,zoom,version) {
        if (GBrowserIsCompatible()) {
			if(version) {
				//main map
					this.resizer();
					this.map = new GMap2($("map") );
					this.map.setMapType(G_PHYSICAL_MAP );
					this.map.addControl(new GLargeMapControl());
					this.map.addControl(new GMapTypeControl());
					this.map.addMapType(G_PHYSICAL_MAP);
					this.createlayer();
					this.map.removeMapType(G_HYBRID_MAP);
					this.map.removeMapType(G_NORMAL_MAP);
					this.map.addControl(new GLargeMapControl());
			//		this.map.addOverlay( this.radaroverlay );
					//this.map.removeMapType(G_SATELLITE_MAP);
			} else {
				//mini map
					this.map = new GMap2($("map") );
					this.createlayer();
					this.map.setMapType(G_PHYSICAL_MAP );
					//this.map.setMapType(this.topoMap);
					this.map.disableDragging();
					this.map.removeMapType(G_HYBRID_MAP);
					this.map.removeMapType(G_NORMAL_MAP);
					this.map.removeMapType(G_PHYSICAL_MAP);
					this.map.removeMapType(G_SATELLITE_MAP);
			}

				this.map.setCenter(new GLatLng(lat,lng),zoom);
				
        }
		
    }
	
	this.WMSGetTileUrl = function (tile,zoom)
{
	var southWestPixel=new GPoint(tile.x*256,(tile.y+1)*256);
	var northEastPixel=new GPoint((tile.x+1)*256,tile.y*256);
	var southWestCoords=G_NORMAL_MAP.getProjection().fromPixelToLatLng(southWestPixel,zoom);
	var northEastCoords=G_NORMAL_MAP.getProjection().fromPixelToLatLng(northEastPixel,zoom);
	var bbox=southWestCoords.lng()+','+southWestCoords.lat()+','+northEastCoords.lng()+','+northEastCoords.lat();
	//var transparency=this.transparent?'&TRANSPARENT=TRUE':'';
return this.baseUrl+'?VERSION=1.1.1&REQUEST=GetMap&LAYERS='+this.layer+'&STYLES=&SRS=EPSG:4326&BBOX='+bbox+'&WIDTH=256&HEIGHT=256&FORMAT='+this.format+'&BGCOLOR=0xCCCCCC&EXCEPTIONS=INIMAGE';//+transparency;
}

	
	this.topoTiles = function (a,b) {			    
			//var f = "http://maps.coloradohikers.com/Layer_NewLayer/" + TileToQuadKey(a.x,a.y,b) + ".png";
			//var f = "http://topos.rukind.org/Layer_24000/" + TileToQuadKey(a.x,a.y,b) + ".png";
			var zoom = comap.map.getZoom();
			var tilex = (a.x+1);
			var tiley = (a.y+1);
			//var f2 = "http://terraserver-usa.com/tile.ashx?t=2&s=12&x=" + tilex + "&y=" + tiley + "&z=" + zoom;
			var southWestPixel=new GPoint(a.x*256,(a.y+1)*256);
			var northEastPixel=new GPoint((a.x+1)*256,a.y*256);
			var southWestCoords=G_NORMAL_MAP.getProjection().fromPixelToLatLng(southWestPixel,zoom);
			var northEastCoords=G_NORMAL_MAP.getProjection().fromPixelToLatLng(northEastPixel,zoom);
			var bbox=southWestCoords.lng()+','+southWestCoords.lat()+','+northEastCoords.lng()+','+northEastCoords.lat();
			//var transparency=this.transparent?'&TRANSPARENT=TRUE':'';
			var tswms= 'http://www.terraserver-usa.com/ogcmap6.ashx' +'?VERSION=1.1.1&REQUEST=GetMap&LAYERS='+'DRG'+'&STYLES=&SRS=EPSG:4326&BBOX='+bbox+'&WIDTH=256&HEIGHT=256&FORMAT='+'image/jpeg'+'&BGCOLOR=0xCCCCCC&EXCEPTIONS=INIMAGE';//+transparency;
			//$("title").innerHTML = tswms;
		//	alert(f);
			return tswms;
	}
		
	this.createlayer = function(){
		this.topoLayer = new GTileLayer(new GCopyrightCollection(''),12,19); 
		this.topoLayer.getTileUrl = this.topoTiles; 
		this.topoLayer.getCopyright = function(a,b) {return "www.coloradohikers.com 2008";}; 
		this.topoLayer.isPng = function() {return true;};		
		this.topoMap = new GMapType([this.topoLayer], 
		      G_SATELLITE_MAP.getProjection(), "topos",{errorMessage:"no topos here"}); 
		this.topoMap.getTextColor = function() {return "#0000FF";};
		this.map.addMapType(this.topoMap); 
		
		//hybrid layer map

		this.topoHybridLayer = new Array();
		this.topoHybridLayer[0] = G_SATELLITE_MAP.getTileLayers()[0];
	         this.topoHybridLayer[1] = new GTileLayer(new GCopyrightCollection('') , 12, 19);
		this.topoHybridLayer[1].getTileUrl = this.topoTiles;
		this.topoHybridLayer[1].getCopyright = function(a,b) {return "www.coloradohikers.com 2008";}; 
		this.topoHybridLayer[1].getOpacity = function () {return 0.75;};//opacity of the non transparent part
		if(navigator.userAgent.indexOf("MSIE") == -1)
			this.topoHybridLayer[1].isPng = function() {return true;};

	        this.topoSatMap = new GMapType(this.topoHybridLayer, 
			G_SATELLITE_MAP.getProjection(), 'Imagery with topos',{errorMessage:"", alt:"Show imagery with topos"}); 
		this.topoSatMap.getTextColor = function() {return "#FFFFFF";};
		//this.map.addMapType(this.topoSatMap); 
	    this.hc = new GHierarchicalMapTypeControl();
		this.hc.addRelationship(G_SATELLITE_MAP, this.topoSatMap , "topos");
		this.map.addControl(this.hc);
		
					//start radar
			this.radar_layer= new GTileLayer(new GCopyrightCollection(""),1,17);
			this.radar_layer.getTileUrl= this.getradarWMS;
			//this.radar_layer.getCopyright = function(a,b) {return "www.coloradohikers.com 2008";}; 
			this.radar_layer.getOpacity = function() {return 0.5;}
			this.radar_layer.isPng = function() {return true;}
			this.radaroverlay = new GTileLayerOverlay(this.radar_layer); 
			//end radar
 
	}

	
	 this.getradarWMS = function (tile,zoom) {
		var llp = new GPoint(tile.x*256,(tile.y+1)*256);
		var urp = new GPoint((tile.x+1)*256,tile.y*256);
		var ll = G_NORMAL_MAP.getProjection().fromPixelToLatLng(llp,zoom);
		var ur = G_NORMAL_MAP.getProjection().fromPixelToLatLng(urp,zoom);
		var dt = new Date();
		var nowtime = dt.getTime();
		var tileurl = "http://mesonet.agron.iastate.edu/cgi-bin/wms/nexrad/n0r.cgi?";
		tileurl+="&request=GetMap&service=WMS&version=1.1.1&layers=nexrad-n0r&styles=";
		tileurl+="&format=image/png&bgcolor=0x333333&transparent=true&srs=EPSG:4326";
		tileurl+="&bbox="+ll.x+","+ll.y+","+ur.x+","+ur.y;
		tileurl+="&width=256&height=256&reaspect=false&cachetime="+nowtime;
		return tileurl;
	}
//end of comap class
}

comap.prototype.onMapClick = function(){
	this.LoadWPT(this.map);
}

