﻿
function CloseCoMaps()
{
        document.getElementById("GoogleCoMap").style.visibility = "hidden";
    var AllDiv = document.getElementsByTagName("div");
        
    for (x=0;x<AllDiv.length;x++)
    {
        if (AllDiv[x].id.indexOf("MapButton") > -1)
        {
            AllDiv[x].className = "MapButtonNorm";
        }
    }
}

function CoMap(id,name,address,longitude,latitude)
{   
    CloseCoMaps();
    var ifr = document.getElementById("GoogleCoMap");
    var alignto = document.getElementById("MapButton" + id);
        alignto.className = "MapButtonHot";
        
        ifr.style.left = ((alignto.offsetLeft - ifr.offsetWidth) + alignto.offsetWidth) + "px";
        ifr.style.top = (alignto.offsetTop + alignto.offsetHeight - 2) + "px";
        ifr.style.visibility = "visible";
        
    var theForm = document.GoogleCoMapData;
        theForm.name.value = name;
        theForm.address.value = address;
        theForm.longitude.value = longitude;
        theForm.latitude.value = latitude;
        theForm.submit();
}

function CoMapClose()
{
    CloseCoMaps();
    var ifr = document.getElementById("GoogleCoMap");
        ifr.src = "../Components/Default.aspx";
        ifr.style.left = "1px";
        ifr.style.top = "1px";
}


/*
'######### GoogleMapObject ################################
'
'	Allows map object to be persistant once created
'
'==========================================================
*/

function GoogleMapObject()
{
	this.map = null;
	this.defaultLatitiude = 33.8521;					//' center on orange county 
	this.defaultLongitude = -117.5700;
	this.defaultZoomLevel = 8;
	this.Listings = null; 	
	/*
	' ===============================================
	' get map object externally
	' ===============================================
	*/
						
	GoogleMapObject.prototype.GetMap = function()
	{
		return this.map;
	}
	
	/*
	' ===============================================
	' pass listings array into class 
	' ===============================================
	*/
							
	GoogleMapObject.prototype.defineListings = function(arrListings)
	{
		this.Listings = arrListings;
	}
	
	/*
	' ===============================================
	' Recenter
	' ===============================================
	*/
	GoogleMapObject.prototype.Recenter = function()
	{
	
	// this code sets the zoom level based on visible points
	/*
		this.map.setCenter(new GLatLng(0,0),0);
		var bounds = new GLatLngBounds();
		for(x=0;x<this.Listings.length;x++) 
		{
			var point = new GLatLng(parseFloat(this.Listings[x][8]),parseFloat(this.Listings[x][7]));
			bounds.extend(point);
		}
		var clat = (bounds.getNorthEast().lat() + bounds.getSouthWest().lat()) /2;
		var clng = (bounds.getNorthEast().lng() + bounds.getSouthWest().lng()) /2;
		this.map.setCenter(new GLatLng(clat,clng),this.map.getBoundsZoomLevel(bounds));
		*/

		this.map.setCenter(new GLatLng(parseFloat(this.defaultLatitiude), parseFloat(this.defaultLongitude)), parseInt(this.defaultZoomLevel));
		
		//this.map.centerAndZoom(new GPoint(parseFloat(this.defaultLatitiude), parseFloat(this.defaultLongitude)), parseInt(this.defaultZoomLevel));
	}


	/*
	' ===============================================
	' AddControls
	' ===============================================
	*/
	GoogleMapObject.prototype.AddControls = function()
	{
		this.map = new GMap2(document.getElementById("map"));
		this.map.addControl(new GLargeMapControl());
		this.map.addControl(new GMapTypeControl());
		this.map.setCenter(new GLatLng(parseFloat(this.defaultLatitiude), parseFloat(this.defaultLongitude)), parseInt(this.defaultZoomLevel));
		//this.map.centerAndZoom(new GPoint(parseFloat(this.defaultLatitiude), parseFloat(this.defaultLongitude)), parseInt(this.defaultZoomLevel));
		GEvent.addListener(this.map, "zoom", function() 
			{
				document.getElementById("nom").style.visibility = 'hidden';
				var map = GMO.GetMap();
				var zoomLevel = map.getZoomLevel();
				if (zoomLevel > 15)
				{
					map.zoomTo(15);
				} 
			}
		);

		GEvent.addListener(this.map, "moveend", function() 
			{
				document.getElementById("loading").style.visibility = 'hidden';
			}
		);
		
		/*
		//alerts with the current lat, lng and zoom level.... helpful for debugging
		GEvent.addListener(this.map, "moveend", function() 
			{
				var map = GMO.GetMap();
				var str = "\nLat: " + map.getCenter().lat();
					str += "\nLng: " + map.getCenter().lng();
					str += "\nZoom: " + map.getZoom();
				alert(str);
			}
		);
		*/
		// initCoords(); // ControlZoom
	}
	/*
	' ===============================================
	' LoadPoints
	' ===============================================
	*/
	GoogleMapObject.prototype.LoadPoints = function()
	{
		this.Clusterer = new Clusterer(this.map);		//' create cluster object //
		this.Clusterer.SetIcon(this.defineGroupIcon());
		for(x=0;x<this.Listings.length;x++) 
		{
			var point = new GPoint(parseFloat(this.Listings[x][8]),parseFloat(this.Listings[x][7]));
			
			var Marker = this.createMarker(point,this.Listings[x][0]);
  				//this.map.addOverlay(Marker);
				this.Clusterer.AddMarker(Marker, "<A HREF=\"javascript:GMO.goMarker('" + this.Listings[x][0] + "');\">" + this.Listings[x][1] + " " + this.Listings[x][2] + "</A>");		
		}
	}
	/*
	' ===============================================
	' createTabbedMarker(point,htmls,labels)
	' creates a tabbed marker and set up the event window
	' ===============================================
	*/
	GoogleMapObject.prototype.createTabbedMarker = function(point,htmls,labels)
	{
		var marker = new GMarker(point,this.defineIcon());
		GEvent.addListener(marker, "click", function() 
			{
				var tabs = [];
				for (var i=0; i<htmls.length; i++) 
				{
					tabs.push(new GInfoWindowTab(labels[i],htmls[i]));
				}
				marker.openInfoWindowTabsHtml(tabs);
			}
		);
		return marker;
	}

	
	/*
	' ===============================================
	' goMarkerBalloon(html,longitude,latitude)
	' Does the work of opening baloon
	' ===============================================
	*/
	GoogleMapObject.prototype.DisplayInstructions = function()
	{
		var My = this;
		var point = new GLatLng(parseFloat(this.defaultLatitiude), parseFloat(this.defaultLongitude))
		this.map.setCenter(point, parseInt(this.defaultZoomLevel));
			GDownloadUrl("instructions.html", function(data) {
				GMO.goMarkerBalloon(data,parseFloat(My.defaultLongitude), parseFloat(My.defaultLatitiude));
			});
		this.map.setCenter(point, parseInt(this.defaultZoomLevel));
	}
	/*
	' ===============================================
	' clearOverlays()
	' clears all overlays
	' ===============================================
	*/
	GoogleMapObject.prototype.clearOverlays = function()
	{
		this.map.clearOverlays();
	}
	/*
	' ===============================================
	' defineGroupIcon()
	' returns the little Orange basket icon
	' ===============================================
	*/
	GoogleMapObject.prototype.defineGroupIcon = function()
	{
		var icon = new GIcon();
			icon.image = "images/orange_basket.png";
			icon.shadow = "images/orange_basket_shadow.png";
			icon.iconSize = new GSize(61, 50);
			icon.shadowSize = new GSize(61, 50);
			icon.iconAnchor = new GPoint(16, 49);
			icon.infoWindowAnchor = new GPoint(31, 1);
		return icon;
	}
	/*
	' ===============================================
	' defineIcon()
	' returns the little Orange icon
	' ===============================================
	*/
	GoogleMapObject.prototype.defineIcon = function()
	{
		var icon = new GIcon();
			icon.image = "http://re.ocrealestatefinder.com/realestate/images/orange-pin.png";
			icon.shadow = "http://re.ocrealestatefinder.com/realestate/images/orange-pin-shadow.png";
			icon.iconSize = new GSize(22, 34);
			icon.shadowSize = new GSize(39, 34);
			icon.iconAnchor = new GPoint(9, 34);
			icon.infoWindowAnchor = new GPoint(11, 1);
		return icon;
	}
	/*
	' ===============================================
	' GetListingData()
	' uses Gmap2 GDownloadUrl to get data
	' ===============================================
	*/
	GoogleMapObject.prototype.GetListingData = function(URLAddress)
	{
		GDownloadUrl(URLAddress, function(data) 
			{
			return data;
			}
		);
	}
	/*
	' ===============================================
	' GetListingData()
	' uses Gmap2 GDownloadUrl to get data
	' ===============================================
	*/
	GoogleMapObject.prototype.CreateBalloon = function(URLAddress,Lng,Lat)
	{
		var My = this;
		GDownloadUrl(URLAddress, function(data) 
			{
				if (data.length > 10)
				{
					if (data.indexOf("<GMAPTABSPLIT>") > -1) // listing has photos
					{
						My.goTabbedMarkerBalloon(data,Lng,Lat);
					}
					else
					{
						My.goMarkerBalloon(data,Lng,Lat);
					}
				}
			}
		);
	}
	
/*
	' ===============================================
	' goTabbedMarkerBalloon(html,longitude,latitude)
	' Does the work of opening baloon
	' ===============================================
	*/
	GoogleMapObject.prototype.goTabbedMarkerBalloon = function(html,longitude,latitude)
	{
		var point = new GPoint(parseFloat(longitude),parseFloat(latitude));
		// here we split data into two panes
		var htmlData = html.split("<GMAPTABSPLIT>");
		var labels = ["Info","Photos"];
		var htmls = [htmlData[0],htmlData[1]];
		var marker = this.createTabbedMarker(point, htmls, labels);
		this.map.addOverlay(marker);
		var tabs = [];
		for (var i=0; i<htmls.length; i++) 
		{
			tabs.push(new GInfoWindowTab(labels[i],htmls[i]));
		}
		marker.openInfoWindowTabsHtml(tabs);
	}
	/*
	' ===============================================
	' goMarkerBalloon(html,longitude,latitude)
	' Does the work of opening baloon
	' ===============================================
	*/
	GoogleMapObject.prototype.goMarkerBalloon = function(html,longitude,latitude)
	{
		var point = new GPoint(parseFloat(longitude),parseFloat(latitude));
			var Marker = this.createMarker(point,html);
	  		this.map.addOverlay(Marker);
			Marker.openInfoWindowHtml(html);
			//this.map.removeOverlay(Marker);
	}
	
	/*
	' ===============================================
	' goMarker(listingID)
	' Pops open baloon when passed listing ID
	' ===============================================
	*/
	GoogleMapObject.prototype.goMarker = function(listingID)
	{
		for(x=0;x<this.Listings.length;x++) 
		{
			if (trim(this.Listings[x][0]) == trim(listingID)) 
			{
				document.getElementById("nom").style.visibility = 'hidden';
				var theURL = "ListingDetail.asp?ListingID=" + listingID;
				var Llong = parseFloat(this.Listings[x][8]);
				var Llat = parseFloat(this.Listings[x][7]);
				this.CreateBalloon(theURL,Llong,Llat);
			}	
		}
	}

	/*
	' ===============================================
	' createMarker(point,html)
	' places marker on map
	' ===============================================
	*/
	GoogleMapObject.prototype.createMarker = function(point,ListingID)
	{
	  var marker = new GMarker(point, this.defineIcon());
	  //var marker = new GMarker(point);
	  GEvent.addListener(marker, 'click', function() {
		GMO.goMarker(ListingID);
		//marker.openInfoWindowHtml(html);
	  });
	  return marker;
	}
}
