var map;
var bounds = new GLatLngBounds();
var mappoints = new Array();
var arrLegend = new Array();

    var baseIcon = new GIcon();
      baseIcon.image = "images/iconb.png";
     // baseIcon.shadow = "shadow.png";
		baseIcon.iconSize = new GSize(15, 25);
	baseIcon.shadowSize = new GSize(10, 17);
	baseIcon.iconAnchor = new GPoint(4, 17);
	baseIcon.infoWindowAnchor = new GPoint(4, 2);
	baseIcon.infoShadowAnchor = new GPoint(9, 12);
	

    var iconb = new GIcon(baseIcon);
    iconb.image = "images/iconb.png";
   
   var icony = new GIcon(baseIcon);
    icony.image = "images/icony.png";   
   
   var icong = new GIcon(baseIcon);
   icong.image = "images/icong.png";

    var icont = new GIcon(baseIcon);
   icont.image = "images/icont.png";
   
   var iconp = new GIcon(baseIcon);
    iconp.image = "images/iconp.png";

   var icono = new GIcon(baseIcon);
    icono.image = "images/icono.png";

   var iconr = new GIcon(baseIcon);
    iconr.image = "images/iconr.png";



	
	
function initmap(){

		if(GBrowserIsCompatible()){
			map = new GMap2(document.getElementById("map"));
			var loc = new GLatLng(38.568837, -121.7577838);
			map.setCenter(loc, 14);
			map.addControl(new GLargeMapControl());		
		//	map.addControl(new GMapTypeControl());	
		}
		


	
		filterMap("value");	
		 centerAndZoomOnBounds(bounds); 
		 drawTable();
		
		resizeStuff();

		
		

 
		
}


window.onload = initmap;
window.onunload = GUnload;
window.onresize = resizeStuff;



function resizeStuff(){
	//var diff = document.getElementById("map").offsetHeight - document.getElementById("infoBox").offsetHeight;
	//document.getElementById("listwrap").style.height = (diff-20)+"px";
	
	//document.getElementById("consolebox").style.top = document.getElementById("map").offsetTop + 20 +"px";
	

}


function hideShowMenu(){

	var t = document.getElementById("consoleMenu");
	var a = document.getElementById("hideShowMenu");

	if(t.style.display =="none"){
		t.style.display ="block";
		a.innerHTML = "hide";
		
	}	
	else{
		t.style.display="none";
		a.innerHTML = "show";
	}	
		
}




function drawTable(){

		var tablerows='<i>Click on column headings to sort table. Click on address link to go to map point</i><table id="tablerows" cellpadding="10" class="sortable"><thead><tr><th>Addr. #</th><th>Street</th><th>Year purchased</th><th>Size (sq. ft.)</th><th>Value</th><th>Owner</th></tr></thead>';
		for(var i=0; i< kjarr.length; i++){
			
			
			if(i%2==0)
				tablerows+='<tr class="odd">';
			else
				tablerows+="<tr>";
				
				
			var addy = kjarr[i][0]['address'].split(" ");	
			
			if(addy[2] ==undefined){
				addy[2]="";
			}
			if(addy[3]==undefined){
				addy[3]="";
			}
			
			tablerows+= '<td><a href="#" onclick="gotoMapPoint(\'kjarr['+i+'][0]\')">'+addy[0]+'</a></td><td><a href="#" onclick="gotoMapPoint(\'kjarr['+i+'][0]\')">'+addy[1]+" "+addy[2]+" "+addy[3]+"</a></td><td>"+kjarr[i][0]['year']+"</td><td>"+kjarr[i][0]['size']+"</td><td>"+showMoney(kjarr[i][0]['value'])+"</td><td>"+kjarr[i][0]['owner']+"</td></tr>";
			
		
		}
		tablerows+="</table>";
		
		document.getElementById("divtable").innerHTML = tablerows;
sorttable.makeSortable(document.getElementById("tablerows"));

}

function displayPropInfo(prop){ 

	document.getElementById("propaddress").innerHTML = prop["address"];
	document.getElementById("propowner").innerHTML = prop["owner"];
	document.getElementById("propsize").innerHTML = prop["size"];
	document.getElementById("propvalue").innerHTML = showMoney(prop["value"]);
	document.getElementById("propyear").innerHTML = prop['year'];
	document.getElementById("propcode").innerHTML = prop['code'];
document.getElementById("propempty").innerHTML = prop['empty'];
}





function drawLegend(){

	var txt="<table>";
	for(var id in arrLegend){
	
		txt+="<tr><td>"+id+"</td><td><img src=\""+arrLegend[id]+"\" /></td></tr>";
	
	
	}
	txt+="</table>";
	
	document.getElementById("legend").innerHTML=txt;
}

function filterMap(filter){
closePicture();
	map.clearOverlays();
	delete arrLegend;
	arrLegend = new Array();
	
		for(var i=0; i< kjarr.length; i++){
			
		
			
			loadPoint(new GLatLng( kjarr[i][0]['lati'],  kjarr[i][0]['longi']), (kjarr[i])[0], filter); 
		
		}
		
	drawLegend();	
	centerAndZoomOnBounds(bounds); 

}




function loadPoint(latlng, prop,filter){

	var marker;
	var p = prop[filter];

	switch(filter){
		case "size":
			if(p <= 3000){
				marker=new GMarker(latlng, icont);
				
			}else if(p <= 5000){
				marker=new GMarker(latlng, icong);
				
			}else if(p <= 7000){
				marker=new GMarker(latlng, iconb);
				
			}else{
				marker=new GMarker(latlng, iconp);
				
			}
			
			arrLegend['3,000 sq. ft. or less']="images/icont.png";
			arrLegend['3,001-5,000 sq. ft.']="images/icong.png";
			arrLegend['5,001-7,000 sq. ft.']="images/iconb.png";
			arrLegend['7,000+ sq. ft.']="images/iconp.png";
		break;
		case "value":
			if(p <= 15000){
				marker=new GMarker(latlng, iconr);
				
			}else if(p <= 45000){
				marker=new GMarker(latlng, icony);
				
			}else if(p <= 80000){
				marker=new GMarker(latlng, iconb);
				
			}else if(p <= 150000){
				marker=new GMarker(latlng, icong);
				
			}			
			else{
				marker=new GMarker(latlng, iconp);
				
			}	
			
			arrLegend['$15,000 or less']="images/iconr.png";
			arrLegend['$15,001-$45,000']="images/icony.png";
			arrLegend['$45,001-$80,000']="images/iconb.png";
			arrLegend['$80,001-$150,000']="images/icong.png";
			arrLegend['$150,001+']="images/iconp.png";
		break;
		case "code":
			if(p == "Yes"){
				marker=new GMarker(latlng, iconr);
				
			}			
			else if(p=="No"){
				marker=new GMarker(latlng, icong);
				
			}else{
				marker=new GMarker(latlng, icony);
			}
		
			arrLegend['Yes']="images/iconr.png";
			arrLegend['No']="images/icong.png";
			arrLegend['Unknown']="images/icony.png"
		
		break;	
		case "empty":
			if(p.match("Empty")){
				marker=new GMarker(latlng, iconr);
				
			}			
			else if(p.match("present")){
				marker=new GMarker(latlng, icong);
				
			}	
			else{
				marker=new GMarker(latlng, icony);
				
			}			
			arrLegend['Empty lot']="images/iconr.png";
			arrLegend['Building present']="images/icong.png";
			arrLegend['Building, but appraised at less than $75,000']="images/icony.png";
		
		break;			
		case "owner":
			if(p.match("KYNSHIP DEVELOPMENT CO")){
				marker=new GMarker(latlng, icony);
				
			}			

			else if(p.match("ACADEMY")){
				marker=new GMarker(latlng, iconp);
				
			}
			else if(p.match("ST HOPE DEVELOP")){
				marker=new GMarker(latlng, iconb);
				
			}				
			else if(p.match("LIVING TRUST")){
				marker=new GMarker(latlng, icono);
				
			}
		
			else if(p.match("GEORGIA") ){
				marker=new GMarker(latlng, iconr);
				
			}			
		
			else {
				marker=new GMarker(latlng);
				
			}			
			arrLegend['Kynship Development Co.']="images/icony.png";
			arrLegend['St. Hope Academy']="images/iconp.png";
			arrLegend['St. Hope Development Co.']="images/icong.png";
			arrLegend['Georgia West']="images/iconr.png";
			arrLegend['Kevin M. Johnson Living Trust']="images/icono.png";
		break;	
		
			case "year":
			if(p <= 1999){
				marker=new GMarker(latlng, icont);
				
			}else if(p ==2000 ){
				marker=new GMarker(latlng, icony);
				
			}else if(p >=2001 && p <= 2003){
				marker=new GMarker(latlng, iconb);
				
			}else if(p >= 2004){
				marker=new GMarker(latlng, iconp);
				
			}			
			else{
				marker=new GMarker(latlng);
				
			}	
			
			arrLegend['1999 or before']="images/icont.png";
			arrLegend['2000']="images/icony.png";
			arrLegend['2001 to 2003']="images/iconb.png";
			arrLegend['2004 and after']="images/iconp.png";
	
		break;		
		 default:
			marker=new GMarker(latlng, iconp);
		break;
	
	
	
	}



	function gotoPoint(){
		
		var txt="<b>Address</b><br />"+prop['address']+"<br /><b>Owner</b><br />"+prop['owner'];
		//txt+='<img src="images/'+prop['id']+'.jpg" />';
		displayPropInfo(prop);
		marker.openInfoWindowHtml(txt);
		showPicture(prop);
		map.panTo(latlng);

	}
	
	
	GEvent.addListener(marker, 'click', gotoPoint);	
	map.addOverlay(marker);	
	bounds.extend(latlng);
	prop.marker=marker;
	

	
}


function gotoMapPoint(prop){
		prop = eval(prop);
		displayPropInfo(prop);
		showPicture(prop);
		map.setCenter(map.getCenter(), 17);
		map.panTo(new GLatLng(prop['lati'], prop['longi']) );
		var txt="<b>Address</b><br />"+prop['address']+"<br /><b>Owner</b><br />"+prop['owner'];
		prop.marker.openInfoWindowHtml(txt);
		

}



function centerAndZoomOnBounds(bounds) {
var center = bounds.getCenter();
var newZoom = map.getBoundsZoomLevel(bounds);
  if (map.getZoom() != newZoom) {
		
		//map.setCenter(center, newZoom);
  } else {
		map.panTo(center);
  }
} 

function trace(txt){

document.getElementById("debug").innerHTML += txt+"<br/>";
}


function showMoney(str){
	var s ="$";
	if(str.length == 4){
	
		s+= str.substring(0,1)+","+str.substring(1);
	}else if(str.length==5){
		s+= str.substring(0,2)+","+str.substring(2);
	}else if(str.length==6){
		s+= str.substring(0,3)+","+str.substring(3);
	}else if(str.length==7){
		s+= str.substring(0,1)+","+str.substring(1,4)+","+str.substring(4);
	}
	

	return s;
}

function showPicture(prop){
	var p = document.getElementById("propPic");
	p.style.display="block";
	var txt='<img src="'+"images/"+prop["id"]+'.jpg" />';
	txt+="<br />"+prop["address"];
	txt+='&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; see right for details &nbsp;&nbsp;&nbsp;<a href="#" onclick="closePicture()">close picture</a> ';
	p.innerHTML = txt;
}

function closePicture(){
	var p = document.getElementById("propPic");
	p.style.display="none";

}


