//alert("boonmap.js loaded")
function BoonMap(md, ld, nb, bb){
	var mapdiv = md
	this.listdiv = ld
	this.nextbutton = nb
	this.backbutton = bb
	
	this.nextbutton.style.visibility ="hidden"
	this.backbutton.style.visibility ="hidden"
	
	//class variables
	var map;
	var geocoder = new GClientGeocoder();
	var infobox
	var nearAddress
	
	//custom marker icon
	var boonIcon = new GIcon();
	boonIcon.image = "images/boonmarker.png";
	//boonIcon.shadow = "http://labs.google.com/ridefinder/images/mm_20_shadow.png";
	boonIcon.iconSize = new GSize(52, 62);
	boonIcon.shadowSize = new GSize(22, 20);
	boonIcon.iconAnchor = new GPoint(25, 60);
	boonIcon.infoWindowAnchor = new GPoint(25, 30);
	markerOptions = { icon:boonIcon };	
	var defaultZoom = 4
	var defaultRadius = 50;
	
	// locations and current locations shown
	var markersXml;
	var currentFrom;
	var currentTo;


	//CREATE THE MAP AND LEAVE IT AT THAT UNTIL WE SEARCH
	initMap()

	//initialize map and center on myAdress
	function initMap() {
		//alert("initMap")
		if (GBrowserIsCompatible()) {
			//alert("GBrowserIsCompatible")
			map = new GMap2(mapdiv);
			map.setCenter(new GLatLng(40, -100), defaultZoom);
			map.addControl(new GSmallMapControl());
		}
		//alert("end initMap")
	}
	
	this.searchLocationsNear = function(nearAddress) {
		//alert("searchLocationsNear called")
		this.nearAddress = nearAddress
		var center;
		var thisobj = this
		geocoder.getLatLng(nearAddress, function(latlng) {
			if (!latlng) {
				alert(nearAddress + ' not found');
				return
			} else {
				center = latlng;
				var radius = defaultRadius
				var searchUrl = '/includes/retailerfinder.php?lat=' + center.lat() + '&lng=' + center.lng() + '&radius=' + radius;
				//var searchUrl = '/includes/retailerfinder.php?lat=' + center.lat() + '&lng=' + center.lng() + '&radius=' + radius;
				GDownloadUrl(searchUrl, function(data) {
				var xml = GXml.parse(data);
				thisobj.markersXml = xml.documentElement.getElementsByTagName('marker');
				thisobj.showMarkers(0,6)
				});
			}
		});
	}
	this.showMarkers = function(fromNum,toNum){
		this.currentFrom = Math.max(Math.min(fromNum,this.markersXml.length-1),0);
		this.currentTo = Math.max(Math.min(toNum,this.markersXml.length-1),0);
		//var sidebar = this.listdiv
		this.listdiv.innerHTML = "";
		if (this.markersXml.length == 0) {
			this.listdiv.innerHTML = 'No results found.';
			map.setCenter(new GLatLng(40, -100), this.defaultZoom);
			return;
		}
		map.clearOverlays();
		var bounds = new GLatLngBounds();
		
		for (var i = this.currentFrom; i <= this.currentTo; i++) {
			var name = this.markersXml[i].getAttribute('storeName');
			var address = this.markersXml[i].getAttribute('address1') +" "+this.markersXml[i].getAttribute('address2');
			
			var city = this.markersXml[i].getAttribute('city')
			var province = this.markersXml[i].getAttribute('province')
			var postalCode = this.markersXml[i].getAttribute('postalCode')
			var webAddress = this.markersXml[i].getAttribute('webAddress')
			var phone = this.markersXml[i].getAttribute('phone')
			var distance = parseFloat(this.markersXml[i].getAttribute('distance'));
			var point = new GLatLng(parseFloat(this.markersXml[i].getAttribute('lat')),parseFloat(this.markersXml[i].getAttribute('lng')));
			var marker = this.createMarker(point, name, address, city, province, postalCode, phone, webAddress);
			map.addOverlay(marker);
			var sidebarEntry = this.createSidebarEntry(marker, name, address, phone, distance);
			this.listdiv.appendChild(sidebarEntry);
			bounds.extend(point);
		}
		if (toNum < this.markersXml.length-1){
			this.nextbutton.style.visibility ="visible"
		}else{
			this.nextbutton.style.visibility ="hidden"
		}
		if (fromNum > 0){
			this.backbutton.style.visibility = "visible"
		}else{
			this.backbutton.style.visibility ="hidden"
		}
		
		map.setCenter(bounds.getCenter(), map.getBoundsZoomLevel(bounds));
		
		
	}
	this.nextLocations = function(){
		this.showMarkers(this.currentFrom+7,this.currentTo+7)
	}
	this.prevLocations = function(){
		this.showMarkers(this.currentFrom-7,this.currentFrom-1)
	}
	
	this.fakeSearch = function(){
		this.nearAddress = "63119"
		var marker = this.createMarker(new GLatLng(40, -100),"Arni's shop", "611 N. Rock Hill Road", "St. Louis", "MO", "63119", "1-800-BUY-STUFF")
		map.addOverlay(marker)
	}
	
	this.createMarker = function(point, name, address, city, province, postalCode, phone, webAddress) {
		var marker = new GMarker(point,markerOptions);
		var html = '<b>' + name + '</b> <br/>' + address +"<br/>" + phone
		var weblink = webAddress != "" ? "<br /><a href='"+webAddress+"' target='boonRetailer'>"+webAddress+"</a>" : "";
		html += weblink
		var adressCombo = address +", " +city +", " +province +", " +postalCode
		var directionURL = "http://maps.google.com/maps?"+"daddr="+escape(adressCombo)+"&"+"saddr="+escape(this.nearAddress)
		html += "<br /><a href='"+directionURL+"' target='boonDirections'>Get Directions</a>";
		
		var infoBox = new InfoBox(marker, 220, 135, "mapbox", 55, 50, html) 
		marker.infobox = infoBox
		GEvent.addListener(marker, "click", function() {
			//alert("marker clicked")
			if(map.infobox){
				map.removeOverlay(map.infobox)
			}
			map.panTo(point);
			map.infobox= this.infobox
			map.addOverlay(map.infobox);
		});
		return marker;
	}

	this.createSidebarEntry = function(marker, name, address, phone, distance) {
		var div = document.createElement('div');
		var html = "<b>"+name+"</b><br/>"+address+"<br/>"+phone+"<br/>"+"<span class='text_Green'>~"+distance.toFixed(1)+" miles</span>";
		div.innerHTML = html;
		div.style.cursor = 'pointer';
		div.style.marginBottom = '5px'; 
		GEvent.addDomListener(div, 'click', function() {
			GEvent.trigger(marker, 'click');
		});

		GEvent.addDomListener(div, 'mouseover', function() {
			div.style.backgroundColor = '#eee';
		});
			GEvent.addDomListener(div, 'mouseout', function() {
		div.style.backgroundColor = '#fff';
		});
		return div;
	}
}