//V2.17 07 Jan 2009

var FlyMap = Class.create({ 
	initialize : function() {
		this.markers = new Array();
		this.infoHtmls = new Array();
		this.is_ie6 = (window.external && typeof window.XMLHttpRequest == "undefined");  
		this.iE = this.is_ie6 ? "gif" : "png";
	},
	render : function(tag) {
		var vList= $("vList");
		vList.innerHTML = "Loading Venues...";
		var result = "";
		var rightList = "";
		var bounds = map.getBounds();
		var lon1 = bounds.getSouthWest().lng();
		var lon2 = bounds.getNorthEast().lng();
		var lat1 = bounds.getSouthWest().lat();
		var lat2 = bounds.getNorthEast().lat();
	
		var IconB = new GIcon();
		IconB.iconSize = new GSize(16, 40);
		IconB.image = "/img/mkr/B." + this.iE;
		IconB.iconAnchor = new GPoint(16, 42);
		IconB.infoWindowAnchor = new GPoint(14, 3);
		var IconD = new GIcon();
		IconD.iconSize = new GSize(25, 42);
		IconD.image = "/img/mkr/D." + this.iE;
		IconD.iconAnchor = new GPoint(16, 42);
		IconD.infoWindowAnchor = new GPoint(14, 3);
		var IconS = new GIcon();
		IconS.iconSize = new GSize(16, 40);
		IconS.image = "/img/mkr/S." + this.iE;
		IconS.iconAnchor = new GPoint(16, 42);
		IconS.infoWindowAnchor = new GPoint(14, 3);
		var IconT = new GIcon();
		IconT.iconSize = new GSize(16, 40);
		IconT.image = "/img/mkr/T." + this.iE;
		IconT.iconAnchor = new GPoint(16, 42);
		IconT.infoWindowAnchor = new GPoint(14, 3);
		var IconC = new GIcon();
		IconC.iconSize = new GSize(25, 42);
		IconC.image = "/img/mkr/C." + this.iE;
		IconC.iconAnchor = new GPoint(16, 42);
		IconC.infoWindowAnchor = new GPoint(14, 3);
		var IconE = new GIcon();
		IconE.iconSize = new GSize(30, 40);
		IconE.image = "/img/mkr/E." + this.iE;
		IconE.iconAnchor = new GPoint(16, 42);
		IconE.infoWindowAnchor = new GPoint(14, 3);
		var IconF = new GIcon();
		IconF.iconSize = new GSize(30, 40);
		IconF.image = "/img/mkr/F." + this.iE;
		IconF.iconAnchor = new GPoint(16, 42);
		IconF.infoWindowAnchor = new GPoint(14, 3);
		var IconG = new GIcon();
		IconG.iconSize = new GSize(18, 40);
		IconG.image = "/img/mkr/G." + this.iE;
		IconG.iconAnchor = new GPoint(16, 42);
		IconG.infoWindowAnchor = new GPoint(14, 3);
		var IconO = new GIcon();
		IconO.iconSize = new GSize(18, 40);
		IconO.image = "/img/mkr/O." + this.iE;
		IconO.iconAnchor = new GPoint(16, 42);
		IconO.infoWindowAnchor = new GPoint(14, 3);
		var IconL = new GIcon();
		IconL.iconSize = new GSize(22, 42);
		IconL.image = "/img/mkr/L." + this.iE;
		IconL.iconAnchor = new GPoint(16, 42);
		IconL.infoWindowAnchor = new GPoint(14, 3);	
		var IconZ = new GIcon();
		IconZ.iconSize = new GSize(16, 40);
		IconZ.image = "/img/mkr/Z." + this.iE;
		IconZ.iconAnchor = new GPoint(16, 42);
		IconZ.infoWindowAnchor = new GPoint(14, 3);								
		var resultsXml = "/xmlgen.php?lon1=" + lon1 + "&lon2=" + lon2 + "&lat1=" + lat1 + "&lat2=" + lat2;
		if (tag !=="") { resultsXml += "&tag=" + tag; }
		new Ajax.Request(resultsXml, { 
  			method: 'post',
			asynchronous: true,
 			onComplete: function(transport) {
   		var xmlDoc = transport.responseXML;
	    	var markerElement = xmlDoc.getElementsByTagName("marker");
	    	if (markerElement.length < 1) {
	      	rightList = 'Nothing gay found in this area ';
				if (tag !== "") { rightList += ' tagged with ' + tag; }
	        	rightList += ' sorry honey. Zoom out or try a new search. Please click add if you know otherwise. <br /><img src="/img/line.gif" width="135" height="4" /><br /><a href=/add/>+ Add a venue</a><br /><br />';
				vList.innerHTML = rightList; 
	    	}
	    	if (markerElement.length >= 40) {
	      	rightList += 'Showing ' + markerElement.length + " gay venues in this area";
		  		if (tag !== "") {rightList += ' tagged with ' + tag;}
	     		rightList += ". Zoom in to see more<br /><img src='/img/line.gif' width='135' height='4' />";
	   	}
	    	else {
	      	rightList += 'Showing all gay venues in this area';
		  		if (tag !=="") { rightList += ' tagged with ' + tag; }
	      	rightList += '.<br /><img src="/img/line.gif" width="135" height="4" />';
			}
			this.infoHtmls = [];
			this.markers = [];
			rightList += "<ul>";
	    	for (var i = 0; i < markerElement.length; i++) {
				var node = markerElement[i];
				var point = new GLatLng(parseFloat(node.getAttribute("lat")), parseFloat(node.getAttribute("lon")));	 			   
				var item_id = node.getAttribute("item_id");
				var tel = node.getAttribute("tel");
				var name = node.getAttribute("name"); 
				var address1 = node.getAttribute("address1");
				var address2 = node.getAttribute("address2");
				var city = node.getAttribute("city");
				var st = node.getAttribute("state");
				var url = node.getAttribute("u");
				var image = node.getAttribute("img");
				var type = eval(node.getAttribute("type"));					   
				rightList += '<li><a href="#" onclick="fly2.popup(' + i + ');return false;" title="view this venue">' + name + ' </a></li>';
				var txt = "<div id='infoW'>";
				if(image && image != 'n') { txt += "<div class='photoframe'><a href='/venue/" + url + ".html'><img src=/img/barpic/" + item_id + "t.jpg /></a></div>"; }
				txt += "<img src='/img/b.gif' alt='' width='12' height='12'><a href='/venue/" + url + ".html'><span class='popTitle'>" + name + "</span></a><br />" + address1 + "<br />";
				if(address2 && address2 != 'NULL') { txt += address2 + "<br />"; }
				txt += city + ", " + st;
				if(tel && tel != 'NULL') { txt += "<br /><img src='/img/p.gif' alt='tel' width='12' height='12' />" + tel; }
				txt += "<br /><a href='/venue/" + url + ".html'>Profile|Directions|Reviews|Tags|Website</a><div>";
				vList.innerHTML = rightList + "<li><a href='/add/'>+ Add a venue</a></li></ul><br />";
				var icon = new GIcon(type);
				fly2.createMarker(i,name,point,icon);
				fly2.addMarkers(i);
				fly2.addListeners(i);
				fly2.createInfoWindow(i,txt);
	   		}
	   	}
		});
	},
	wheelZoom :  function(a) {
		if ((a.detail || -a.wheelDelta) < 0){
			map.zoomIn();
		}
   	else{
   		map.zoomOut();
		}
	},
	popup : function(i) {
		this.markers[i].openInfoWindowHtml(this.infoHtmls[i]);
	},
	makePopupCaller : function(i) {
		return function () { fly2.popup(i); };
	},
	createMarker : function(i,n,p,c) {
		this.markers[i] = new GMarker(p, {icon: c, title: n});
	},
	createInfoWindow : function(i,t) {
		this.infoHtmls[i] = t;
	},
	addListeners : function(i) {
		GEvent.addListener(this.markers[i], 'click', fly2.makePopupCaller(i));
	},
	addMarkers : function(i) {
		map.addOverlay(this.markers[i]);
	},
	moveIt : function(dir) {
		var mBounds = map.getBounds();
		var mLon1 = mBounds.getSouthWest().lng();
		var mLon2 = mBounds.getNorthEast().lng();
		var mLat1 = mBounds.getSouthWest().lat();
		var mLat2 = mBounds.getNorthEast().lat();
		var lonCenter = mLon1 + (mLon2 - mLon1) / 2; 
		var latCenter = mLat1 + (mLat2 - mLat1) / 2; 
		if(dir == "left") { map.panTo(new GLatLng(latCenter, mLon1)); }
		if(dir == "right") { map.panTo(new GLatLng(latCenter, mLon2)); }
		if(dir == "down") { map.panTo(new GLatLng(mLat1, lonCenter)); }
		if(dir == "up") { map.panTo(new GLatLng(mLat2, lonCenter)); }
	}
});
