﻿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 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);
        
        map.enableContinuousZoom();
		map.enableScrollWheelZoom();
        
        map.setCenter(new GLatLng(thislat, thislng), 14);
        
        //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 showarchive() {
        var arc = document.getElementById('archive');
        if(arc) { if (arc.style.display == "none") { arc.style.display = ""; } else { arc.style.display = "none"; } }
        if (showarchived) {
            showarchived = false;
            document.getElementById('archivebutton').innerHTML = "show archived plats";
            drawmarkers();
        } else {
            showarchived = true;
            document.getElementById('archivebutton').innerHTML = "hide archived plats";
            drawmarkers();
        }
    }
    
//	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 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 {
					if(showarchived){
					    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;
					} else {
					    if(platstatus[x+1] != "Archived") {
					        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;
					    } else {
					        nextplat = platids[x+1];
					        currentplat = platids[x+1];
					    }
					}
				}	
			}		
		}
    }
    
	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(11);
	    }
    }

    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 showalllocations(){
		//map.setCenter(new GLatLng(thislat, thislng), 11);
		map.setCenter(new GLatLng(la, lo), 9);
	}
	
	function createmarker(point, platname, platid, status, x) {
	    if (!point) {
	        alert("invalid point");
	    } else {
	        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('documents').style.display = 'none';
		
    	document.getElementById(name).style.display = '';
    }
        
    function kmlloaded()
    {	
	    map.setCenter(new google.maps.LatLng(thislat, thislng), 12);
    	//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, 9);
              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/>";
		
	}

