﻿var map = null;
var geocoder = null;
var directionsPanel;
var directions;
var kml;
var currentplat;
var icons = new Array();
var aicons = new Array();
	
var showarchived = false;	

google.load("maps", "2");      
google.load("search", "1");
	
function showalllocations(){
	//map.setCenter(new GLatLng(47.4126501042028, -122.4974316345202), 9);
	map.setCenter(new GLatLng(la, lo), 9);
}
	
function load() {
	if (GBrowserIsCompatible()) {
        map = new google.maps.Map2(document.getElementById("map"));        
        geocoder = new GClientGeocoder(); 
        map.addControl(new GSmallMapControl());  
        map.addControl(new GMapTypeControl()); 
        map.addMapType(G_PHYSICAL_MAP); //map.setMapType(G_SATELLITE_MAP);	//map.setMapType(G_HYBRID_MAP);
        
        showalllocations();
        
        map.enableContinuousZoom();
		map.enableScrollWheelZoom();

        //for (x = 1; x < addresses.length; x++){
        //	showAddress(addresses[x]);
        //}

		//	directionsPanel = document.getElementById("route");
    	//directions = new GDirections(map, directionsPanel);
 
		//kml = new GGeoXml(thiskml);	
		//GEvent.addListener(kml , "load", kmlloaded()); 

		//map.addOverlay(kml);

		//kml2 = new GGeoXml("http://www.homelandllc.com/dev/newlayout/Plats/kml/crossroads5.kml");	
		//map.addOverlay(kml2);
		createcustomicons();
		drawmarkers();
		currentplat = 1;
    }
}

google.setOnLoadCallback(load);
//	
//function gotonextplat() {
//	for (x = 1; x < platids.length; x++) {
//		if(platids[x] == currentplat) {			
//			if(x+1 >= platids.length) {
//				nextplat=platids[1];
//				if(lats[1] != '') { panto(lats[1],lons[1]); }
//				currentplat = platids[1];
//				document.getElementById('mapplat').innerHTML = PlatNames[1];
//				return;
//			} else {
//				nextplat = platids[x+1];
//				if(lats[x+1] != '') { panto(lats[x+1],lons[x+1]) }
//				currentplat = platids[x+1];
//				document.getElementById('mapplat').innerHTML = PlatNames[x+1];
//				document.getElementById('mapplat').href = "http://www.homelandllc.com/plat.asp?p=" + platids[x+1];
//				return;
//			}	
//		}		
//	}
//}

function createcustomicons(){
	icons.push(new GIcon());
	for (x = 1; x < 16; x++) {
		icons.push(new GIcon());	
		icons[x].iconSize = new GSize(27,43);
		icons[x].iconAnchor = new GPoint(13, 43);
		icons[x].image = "http://www.homelandllc.com/images/i" + x + ".png";
	}
	aicons.push(new GIcon());
	for (x = 1; x < 16; x++) {
		aicons.push(new GIcon());	
		aicons[x].iconSize = new GSize(27,43);
		aicons[x].iconAnchor = new GPoint(13, 43);
		aicons[x].image = "http://www.homelandllc.com/images/a" + x + ".png";
	}
}

function panto (lat, lng) {
	map.panTo(new GLatLng(lat, lng));
	map.setCenter(new GLatLng(lat, lng));
	map.setZoom(12);
	if(false){
		var zoom = map.getZoom();
		var zoomint;
		zoomint = window.setInterval(function() {
         	if(zoom < 16) {
         		map.setZoom(zoom);
         		zoom++;        		
         	} else { 
         		window.clearInterval(zoomint);
         	}
        }, 150);
    } else {
	    //map.setZoom(16);
    }
}

function createmarker(point, platname, platid, status, x) {
    if (!point) {
        alert("invalid point");
    } else {
        //map.setCenter(point, 13);
        //window.alert(status);
        if(status != "Archived") {
        	var marker = new GMarker(point, icons[x]);
        } else {
        	var marker = new GMarker(point, aicons[x]);
		}
        map.addOverlay(marker);
        GEvent.addListener(marker,"onmouseover", function() {currentplat=platid;});
        
        GEvent.addListener(marker,"click", function() {
	        var myHtml = "<a href='http://www.homelandllc.com/plat.asp?p=" + platid + "'>"+ platname + "</a><br/>";
	        //myHtml += description;
	        map.openInfoWindowHtml(point, myHtml);});
	    //marker.openInfoWindowHtml("test");
	}
}

function setplatinfopage(name){
	
	document.getElementById('lots').style.display = 'none';
	document.getElementById('builders').style.display = 'none';
	document.getElementById('pricing').style.display = 'none';		
	document.getElementById('photos').style.display = 'none';
	document.getElementById('contact').style.display = 'none';
	
	document.getElementById(name).style.display = '';
}
    
function kmlloaded()
{	
    map.setCenter(new google.maps.LatLng(thislat, thislng), 17);
	//map.setCenter(new google.maps.LatLng(47.15282659120528,-122.1903369300771), 17);
	//map.setCenter(kml.getDefaultCenter());
}

function getdirections(anum){
	directions.load("409 East Pioneer, Puyallup, WA 98372 to " + addresses[anum]);
}

function showAddress(address) {
  if (geocoder) {
    geocoder.getLatLng(
      address,
      function(point) {
        if (!point) { alert(address + " not found"); } 
        else {
          map.setCenter(point, 12);
          var marker = new GMarker(point);
          map.addOverlay(marker);
          marker.openInfoWindowHtml(address);
        }
      }
    );
  }
}

function addplace(){
	var placediv;
	placediv = document.getElementById("myplaces");
	addresses.push(document.getElementById("address").value);
	placediv.innerHTML += "<a href='javascript:getdirections(" + (addresses.length-1) + ");'>" + document.getElementById("name").value + ", " + document.getElementById("address").value + "</a><br/>";
	
}
