var initZoom = 10;
var initMapType = G_SATELLITE_MAP;

function Map(){
	this.setLatitude(initLat);
	this.setLongitude(initLng);
	this.setZoomLevel(initZoom);
	this.setMapDiv("mapdiv");
	this.setMapType(G_SATELLITE_MAP);
	this.setWidth(500);
	this.setHeight(300);
	this.allpoints = [];
	this.setWrapper("map_wrapper");
	this.setState("view");
	this.ll = "";
}

Map.prototype.prepare = function(){
	var qs = queryString();
	if(qs["ll"]){
		this.ll = qs["ll"].split(",");
		this.setLatitude(this.ll[0]);
		this.setLongitude(this.ll[1]);	
		this.setZoomLevel(10);
	}
	this.setLocation();
	this.mapdiv.style.width = this.getWidth() + "px";
	this.mapdiv.style.height = this.getHeight() + "px";
}

Map.prototype.loadMap = function(){
	this.prepare();
	if(GBrowserIsCompatible()){
		this.map = new GMap2(this.getMapDiv());
		this.map.setCenter(this.getLocation(), this.getZoomLevel());
		this.map.setMapType(this.getMapType());
		this.map.enableDoubleClickZoom();
		changeBodyClass("loading", "standby");
	}
	else {
		alert('Your browser is unable to support Google Maps. You will need to upgrade to a more recent browser version to view this interactive.');
	}
}

Map.prototype.loadPoints = function(){
	this.allpoints = [];
	this.allmarkers = [];
	var map = this;	
	this.markermngr = new GMarkerManager(map.map);
	if(points!="empty"){
		i=0;
		for(id in points){
			map.allpoints[i] = new Point(points[id],map);
			map.allpoints[i].initialise();
			i++;
		}
		map.map.panTo(new GLatLng(map.allpoints[0].latitude,map.allpoints[0].longitude));
	}
	map.addMarkers();
	//map.addMarkersMM();			
}

Map.prototype.loadPointsAjax = function(datasource){
	this.allpoints = [];
	this.allmarkers = [];
	var map = this;	
	this.markermngr = new GMarkerManager(map.map);
	var request = GXmlHttp.create();
	request.open('GET',datasource,true);
	request.onreadystatechange = function(){
		if(request.readyState == 4 && (request.status == 200 || request.status == 302) ){
			var pointdata = request.responseText;
			eval(pointdata);
			if(points!="empty" && points.length!==0){
				i=0;
				for(id in points){
					map.allpoints[i] = new Point(points[id],map,id);
					if(i<30){
						map.allpoints[i].zoomlevelshow = 1;
					} else {
						map.allpoints[i].zoomlevelshow = 12;
					}
					map.allpoints[i].initialise();
					i++;
				}
				
				map.map.clearOverlays();
				map.map.setZoom(initZoom);
				map.addMarkers();
				map.map.panTo(map.allpoints[0].lat_lng);
				map.searchResults(map.allpoints.length, "search_results_no");
				map.addThumbGallery();
				map.drawTagCloud(tagcloud);
			} else {
				
				hideLoading("search_results_no");
				map.searchResults(0, "search_results_no");
			}
			

			
		} else if (request.readyState !== 4) {
			
		}
		
	}
	request.send(null);
}

Map.prototype.drawTagCloud = function(tagcloud){
	if(!document.getElementById || !document.getElementsByTagName) return;
	if(!document.getElementById("tagcloud_wrapper")) return;
	var wrapper = document.getElementById("tagcloud_wrapper");
	wrapper.innerHTML = unescape(tagcloud);
	fadeUp(wrapper,255,255,153);
}

Map.prototype.addThumbGallery = function(){
	var thumbgallery = new MapList( this, this.allpoints.slice(0,30) );
	thumbgallery.setListType("thumbsgallery");
	thumbgallery.setListElementId("thumb_gallery");
	thumbgallery.setListHighlightId("thumb_gallery_current");
	thumbgallery.setListWrapperId("thumb_gallery_wrapper");
	thumbgallery.loadList();
}

Map.prototype.addMarkersMM = function(){	
	this.markermngr = new GMarkerManager(this.map);
	this.markermngr.addMarkers(this.allmarkers.slice(0,zoomlevel1markers),zoomlevel1);
	this.markermngr.addMarkers(this.allmarkers.slice(0,zoomlevel2markers),zoomlevel2);
	this.markermngr.addMarkers(this.allmarkers,zoomlevel3);
	this.markermngr.refresh();
}

Map.prototype.addMarkers = function(){
	for(id in this.allpoints){
		if(this.allpoints[id].zoomlevelshow <= this.map.getZoom()){
			this.allpoints[id].show();
		} else {
			this.allpoints[id].hide();
		}
	}
}

Map.prototype.searchResults = function(no_results, elementid){
	var element = document.getElementById(elementid);
	element.innerHTML = no_results + " results";
}


Map.prototype.addSingleMarker = function(lat,lng){
	var querystr = lat + "," + lng;
	var latlng = new GLatLng(lat,lng);
	var marker = new GMarker(latlng);
	this.map.addOverlay(marker);
	GEvent.addListener(marker,'click',
		function(){
			document.location = "index.php?ll=" + querystr;
		}
	);
}

Map.prototype.setLocation = function(){
	this.location = new GLatLng(this.getLatitude(),this.getLongitude());
}

Map.prototype.getLocation = function(){
	return this.location;
}

Map.prototype.setLatitude = function(latitude){
	this.latitude = latitude;
}

Map.prototype.getLatitude = function(){
	return this.latitude;
}

Map.prototype.setLongitude = function(longitude){
	this.longitude = longitude;
}

Map.prototype.getLongitude = function(){
	return this.longitude;
}

Map.prototype.setZoomLevel = function(zoomlevel){
	this.zoomlevel = zoomlevel;
}

Map.prototype.getZoomLevel = function(){
	return this.zoomlevel;
}

Map.prototype.setMapDiv = function(mapdiv){
	this.mapdiv = document.getElementById(mapdiv);
}

Map.prototype.getMapDiv = function(){
	return this.mapdiv;
}

Map.prototype.setWrapper = function(wrapper){
	this.wrapper = document.getElementById(wrapper);
}

Map.prototype.getWrapper = function(){
	return this.wrapper;
}

Map.prototype.setMapType = function(maptype){
	this.maptype = maptype;
}

Map.prototype.getMapType = function(){
	return this.maptype;
}

Map.prototype.setWidth = function(width){
	this.width = width;
}

Map.prototype.getWidth = function(){
	return this.width;
}

Map.prototype.setHeight = function(height){
	this.height = height;
}

Map.prototype.getHeight = function(){
	return this.height;
}

Map.prototype.setState = function(state){
	this.state_ = state;
}

Map.prototype.getState = function(){
	return this.state_;
}







