/**
 * Copyright (c) 2005 by CyberAlerts.
 * http://www.cyberalerts.com
 * All rights reserved.
 */

function el(i){
return document.getElementById(i);
}

function City(id,lat,lng,name){
this.id=id;
this.lat=lat;
this.lng=lng;
if(name)
 this.name=name;
else
 this.name=id.charAt(0).toUpperCase()+id.substr(1).toLowerCase();
if(id=='blankcity')
 this.zoom=13;
else
 this.zoom=5;
}

function Cities(){
this.cities=new Array();
this.cities.push(new City('blankcity',37.062500,-95.677068));
this.current=this.cities[0];
}

Cities.prototype.add=function(id,lat,lng,name){
this.cities.push(new City(id,lat,lng,name));
};

Cities.prototype.find=function(id){
for(var i=0;i<this.cities.length;i++){
 if(this.cities[i].id==id){
  this.current=this.cities[i];
  break;
 }
}
};

Cities.prototype.locations=function(){
var s='';
for(var i=0;i<this.cities.length;i++){
 var c=this.cities[i];
 if(c.id!='blankcity'&&c.id!=this.current.id){
  s+='<location id="'+c.id+'">';
  s+='<point lat="'+c.lat+'" lng="'+c.lng+'"/>';
  s+='<icon q="g"/>';
  s+='<citycode>'+c.id+'</citycode>';
  s+='<info><title>'+c.name+'</title></info>';
  s+='</location>';
 }
}
return s;
}

var cities=new Cities();
//cities.add('albany',42.6525,-73.756667);
cities.add('albuquerque',35.084444,-106.650556);
cities.add('anchorage',61.218333,-149.891667);
cities.add('atlanta',33.75048828125,-84.3810197137227);
cities.add('austin',30.2763671875,-97.73935148465424);
cities.add('bakersfield',35.373333,-119.017778);
cities.add('baltimore',39.3212890625,-76.61534242841374);
cities.add('boise',43.613611,-116.2025);
//cities.add('boston',42.3544921875,-71.05401136404463);
//cities.add('buffalo',42.886389,-78.878611);
cities.add('burlington',44.475833,-73.2125);
//cities.add('calgary',51.039877,-114.080796);
cities.add('charlotte',35.226944,-80.843333);
cities.add('chicago',41.884765625,-87.62789798547303);
cities.add('cincinnati',39.107421875,-84.51200856623727);
cities.add('cleveland',41.499444,-81.695556);
cities.add('columbia',34.000556,-81.035);
cities.add('columbus',39.961111,-82.998889);
cities.add('dallas',32.7900390625,-96.78952410482627,'Dallas/Ft Worth');
cities.add('denver',39.7412109375,-104.9915145003227 ,'Denver/Boulder');
//cities.add('desmoines',41.600556,-93.608889,'Des Moines');
cities.add('detroit',42.439453125,-83.12491888163841,'Detroit/Ann Arbor');
//cities.add('edmonton',53.532790,-113.503383);
cities.add('eugene',44.052222,-123.085556);
cities.add('fresno',36.747778,-119.771389);
cities.add('hartford',41.763611,-72.685556);
cities.add('honolulu',21.306944,-157.858333);
cities.add('houston',29.75732421875,-95.35307630188859);
cities.add('indianapolis',39.768333,-86.158056);
cities.add('inlandempire',34.063333,-117.65,'Inland Empire');
cities.add('jacksonville',30.331944,-81.655833);
cities.add('kansascity',39.099722,-94.578333,'Kansas City');
cities.add('lasvegas',36.165283203125,-115.16624763839982,'Las Vegas');
cities.add('losangeles',34.06005859375,-118.23246254110228,'Los Angeles');
cities.add('louisville',38.254167,-85.759444);
cities.add('maine',43.661389,-70.255833);
cities.add('memphis',35.149444,-90.048889);
cities.add('miami',25.80859375,-80.205829139369);
cities.add('milwaukee',43.038889,-87.906389);
cities.add('minneapolis',44.97705078125,-93.2661138111007,'Minneapolis/St Paul');
cities.add('monterey',36.600278,-121.893611);
//cities.add('montreal',45.512363,-73.675627);
cities.add('nashville',36.165833,-86.784444);
cities.add('newhaven',41.308056,-72.928611,'New Haven');
//cities.add('neworleans',29.954444,-90.075,'New Orleans');
cities.add('norfolk',36.846667,-76.285556);
//cities.add('nj',40.858333,-74.164167,'New Jersey');
//cities.add('ny',40.7554931640625,-73.98480783950978,'New York');
cities.add('omaha',41.258611,-95.9375);
cities.add('orange',33.787778,-117.852222);
cities.add('orlando',28.538056,-81.379444);
//cities.add('ottawa',45.352088,-75.72344);
//cities.add('philadelphia',39.95458984375,-75.1719338651975);
cities.add('phoenix',33.448974609375,-112.08009964944515);
//cities.add('pittsburgh',40.441650390625,-79.98134462038567);
cities.add('portland',45.51806640625,-122.67722437715264);
cities.add('providence',41.823889,-71.413333);
cities.add('raleigh',35.7861328125,-78.6450368460008,'Raleigh/Durham');
cities.add('reno',39.529722,-119.812778);
cities.add('richmond',37.553611,-77.460556);
//cities.add('rochester',43.154722,-77.615833);
cities.add('sacramento',38.57763671875,-121.49705911174348);
cities.add('saltlakecity',40.766845703125,-111.88662215352083,'Salt Lake City');
cities.add('sanantonio',29.423889,-98.493333,'San Antonio');
cities.add('sandiego',32.715087890625,-117.15829067084381,'San Diego');
cities.add('sf',37.7607421875,-122.45163246448864,'San Francisco');
//cities.add('sf_sby',37.357421875,-121.97355479262987,'S.F. South Bay');
//cities.add('sf_eby',37.77734375,-122.21148623487858,'S.F. East Bay');
//cities.add('sf_pen',37.466064453125,-122.17288565515206,'S.F. Peninsula');
//cities.add('sf_nby',38.08935546875,-122.55825865602829,'S.F. North Bay');
cities.add('santabarbara',34.423333,-119.703333,'Santa Barbara');
cities.add('seattle',47.6240234375,-122.333141340656);
cities.add('spokane',47.658889,-117.425);
cities.add('stlouis',38.627222,-90.197778,'St Louis');
cities.add('stockton',37.957778,-121.289722);
cities.add('tampa',27.947222,-82.458611);
//cities.add('toronto',43.723057,-79.392486);
cities.add('tucson',32.221667,-110.925833);
cities.add('tulsa',36.153889,-95.9925);
//cities.add('vancouver',49.26805078125,-123.115141340656);
cities.add('washington',38.91357421875,-77.04121439818337);
//cities.add('winnipeg',49.875822,-97.148721);

function Marker(point,color,node){
this.id=node.getAttribute("id");
var icon=new GIcon(baseIcon);
icon.image="marker"+color+".png";
var marker=new GMarker(point,icon);
this.marker=marker;
this.click=function(){

//  marker.openInfoWindowXslt(node,"info.xsl");
  if (!Marker.xsl) {
    var request = GXmlHttp.create();
    request.open('GET', 'info.xsl?' + Math.random(), true);
    request.onreadystatechange = function() {
      if (request.readyState == 4) {
        Marker.xsl = request.responseXML;
        marker.openInfoWindowHtml(xsltProcess(node, Marker.xsl));
      }
    };
    request.send(null);
  } else {
    marker.openInfoWindowHtml(xsltProcess(node, Marker.xsl));
  }

};
GEvent.addListener(this.marker,"click",this.click);
map.addOverlay(this.marker);
}

var timer=null;

function Markers(){
this.markers=new Array();
this.locations=null;
}

Markers.prototype.add=function(point,color,xmlDoc){
this.markers.push(new Marker(point,color,xmlDoc));
};

Markers.prototype.load=function(locations){
this.locations=locations;
this.loadNext(0);
};

Markers.prototype.loadNext=function(i){
for(var j=0;j<10;j++){
 if(i>=this.locations.length)return;
 var l=this.locations[i];
 var color="green";
 var icons=l.getElementsByTagName("icon");
 if(icons.length>0){
  var q=icons[0].getAttribute("q");
  if(q=='o')
   color='red';
  else if(q=='p')
   color='yellow';
 }
 var points=l.getElementsByTagName("point");
 if(points.length>0){
  var point=new GPoint(parseFloat(points[0].getAttribute("lng")),parseFloat(points[0].getAttribute("lat")));
  this.add(point,color,l);
 }
 ++i;
}
var self=this;
if(timer)clearTimeout(timer);
timer=setTimeout(function(){self.loadNext(i)},1);
};

Markers.prototype.clean=function(point,color,xmlDoc){
this.markers=new Array();
map.clearOverlays();
};

Markers.prototype.click=function(id){
for(var i=0;i<this.markers.length;i++){
 if(this.markers[i].id==id){
  this.markers[i].click();
  return;
 }
}
};

var markers=new Markers();

function Listing(){
this.type=null;
this.xml=null;
this.xmlDoc=null;
this.xsl=null;
this.sort='city';
}

Listing.prototype.loadXml=function(type,city,pricerange,node){
this.type=type;
var self=this;
var request=GXmlHttp.create();
request.open('GET','list.cgi?type='+type+'&city='+city+'&range='+pricerange+'&n='+Math.round(Math.random()*100000),true);
request.onreadystatechange=function(){
 if(request.readyState==4&&request.status==200){
  markers.clean();
  self.xml=request.responseText;
  if(self.xml){
   self.load(node);
   markers.load(self.xmlDoc.documentElement.getElementsByTagName("location"));
  }else{
   node.innerHTML='<div style="padding:8px"><b>No listings at that price range</b></div>';
  }
 }
};
request.send(null);
};

Listing.prototype.load=function(node){
var xmlStr = '<?xml version="1.0"?>' +
 '<page>'+
 '<ltype>'+this.type+'</ltype>'+
 '<lsort>'+this.sort+'</lsort>'+
 '<overlay>'+
 this.xml+
 '</overlay>'+
 '</page>';

//this.xmlDoc=GXml.parse(xmlStr);
this.xmlDoc = xmlParse(xmlStr);

if(!this.xsl){
 var self=this;
 var request=GXmlHttp.create();
 request.open('GET', 'panel.xsl?' + Math.random(), true);
 request.onreadystatechange=function(){
  if(request.readyState==4){

//   self.xsl=GXslt.create(request.responseXML);
//   self.xsl.transformToHtml(self.xmlDoc,node);
   self.xsl = request.responseXML;
   node.innerHTML = xsltProcess(self.xmlDoc, self.xsl);

   self.fixHeader();
  }
 };
 request.send(null);
}else{

// this.xsl.transformToHtml(this.xmlDoc,node);
 node.innerHTML = xsltProcess(this.xmlDoc, this.xsl);

 this.fixHeader();
}
};

Listing.prototype.fixHeader=function(){
var p=el('listingspanel');
if(p&&p.rows&&p.rows[0]&&p.rows[0].cells&&p.rows[0].cells[0]){
 el('lhdr0').style.width=(p.rows[0].cells[0].clientWidth-1)+'px';
 el('lhdr1').style.width=(p.rows[0].cells[1].clientWidth-1)+'px';
 el('lhdr2').style.width=(p.rows[0].cells[2].clientWidth-1)+'px';
 el('lhdr3').style.width=(p.rows[0].cells[3].clientWidth-1)+'px';
 el('lhdr4').style.width=(p.rows[0].cells[4].clientWidth-1)+'px';
 if(p.rows[0].cells[5])
  el('lhdr5').style.width=(p.rows[0].cells[5].clientWidth-1)+'px';
 nodeHeader.style.display='block';
}
};

Listing.prototype.setSort=function(field){
var upper=(this.sort==this.sort.toUpperCase());
if(field==this.sort)
 if(upper)
  this.sort=field.toLowerCase();
 else
  this.sort=field.toUpperCase();
else
 this.sort=field;
};

var listing=new Listing();

var baseIcon=new GIcon();
baseIcon.shadow="http://www.google.com/mapfiles/shadow50.png";
baseIcon.iconSize=new GSize(20,34);
baseIcon.shadowSize=new GSize(37,34);
baseIcon.iconAnchor=new GPoint(9,34);
baseIcon.infoWindowAnchor=new GPoint(9,2);
baseIcon.infoShadowAnchor=new GPoint(18,25);

var nodeMap,nodeHeader,nodePanel;
var map;

function init(city,apa){
if(GBrowserIsCompatible()){
 nodeMap=el('map');
 nodeHeader=el('hdr');
 nodePanel=el('panel');
 resizeMap();
 window.onresize=resizeMap;
 map=new GMap(nodeMap);
 map.addControl(new GSmallMapControl());
 map.addControl(new GMapTypeControl());
 if(city&&apa)
  setCity(city,apa);
 else
  loadPage();
}
}

function setCity(city,type){
var l=document.listchooser;
l.city.value=city;
l.type.value=type;
loadPage();
}

function loadPage(){
if(timer)clearTimeout(timer);
timer=null;
nodeHeader.style.display='none';
nodePanel.innerHTML='<div style="color:#ccc;padding:10px;font-weight:bold">Loading...</div>';
var l=document.listchooser;
cities.find(l.city.value);
map.centerAndZoom(new GPoint(cities.current.lng,cities.current.lat),cities.current.zoom);
if(l.city.value!='blankcity'){
 var type=l.type.value;
 if(type=='apa'){
  listing.loadXml(type,l.city.value,l.apa_price.value,nodePanel);
  el('apaprice_span').style.display='inline';
  el('rfsprice_span').style.display='none';
  nodeHeader.innerHTML=
   '<div id="lhdr0" class="lhdr" style="border-left:solid 1px #dddddd"><a class="lhdra" href="javascript:setSort(\'pics\')" onMouseOver="window.status=\'Sort by pictures\';return true" onMouseOut="window.status=\'\'">pics</a></div>'+
   '<div id="lhdr1" class="lhdr"><a class="lhdra" href="javascript:setSort(\'price\')" onMouseOver="window.status=\'Sort by price\';return true" onMouseOut="window.status=\'\'">price</a></div>'+
   '<div id="lhdr2" class="lhdr"><a class="lhdra" href="javascript:setSort(\'rooms\')" onMouseOver="window.status=\'Sort by bedrooms\';return true" onMouseOut="window.status=\'\'">bd</a></div>'+
   '<div id="lhdr3" class="lhdr"><a class="lhdra" href="javascript:setSort(\'description\')" onMouseOver="window.status=\'Sort by description\';return true" onMouseOut="window.status=\'\'">description</a></div>'+
   '<div id="lhdr4" class="lhdr"><a class="lhdra" href="javascript:setSort(\'city\')" onMouseOver="window.status=\'Sort by city\';return true" onMouseOut="window.status=\'\'">city</a></div>'+
   '<div id="lhdr5" class="lhdr"><a class="lhdra" href="javascript:setSort(\'POSTED\')" onMouseOver="window.status=\'Sort by date\';return true" onMouseOut="window.status=\'\'">date</a></div>';
 }else if(type=='rfs'){
  listing.loadXml(type,l.city.value,l.rfs_price.value,nodePanel);
  el('apaprice_span').style.display='none';
  el('rfsprice_span').style.display='inline';
  nodeHeader.innerHTML=
   '<div id="lhdr0" class="lhdr" style="border-left:solid 1px #dddddd"><a class="lhdra" href="javascript:setSort(\'pics\')" onMouseOver="window.status=\'Sort by pictures\';return true" onMouseOut="window.status=\'\'">pics</a></div>'+
   '<div id="lhdr1" class="lhdr"><a class="lhdra" href="javascript:setSort(\'price\')" onMouseOver="window.status=\'Sort by price\';return true" onMouseOut="window.status=\'\'">price</a></div>'+
   '<div id="lhdr2" class="lhdr"><a class="lhdra" href="javascript:setSort(\'description\')" onMouseOver="window.status=\'Sort by description\';return true" onMouseOut="window.status=\'\'">description</a></div>'+
   '<div id="lhdr3" class="lhdr"><a class="lhdra" href="javascript:setSort(\'city\')" onMouseOver="window.status=\'Sort by city\';return true" onMouseOut="window.status=\'\'">city</a></div>'+
   '<div id="lhdr4" class="lhdr"><a class="lhdra" href="javascript:setSort(\'POSTED\')" onMouseOver="window.status=\'Sort by date\';return true" onMouseOut="window.status=\'\'">date</a></div>';
 }
}else{
 var xmlStr=
  '<?xml version="1.0"?>'+
  '<page>'+
  cities.locations()+
  '</page>';
 var xmlDoc=GXml.parse(xmlStr);
 markers.clean();
 markers.load(xmlDoc.documentElement.getElementsByTagName("location"));
 nodePanel.innerHTML=
  '<div style="padding:8px">'+
  '<p>Drag the map with your mouse, or double-click to center.</p>'+
  '<p>Click on an <img src="markergreen.png"/> icon to select a city.</p>'+
  '<p>In each city, click on the <img src="markerred.png"/> and <img src="markeryellow.png"/> icons to see listings.'+
  'You can also click the <img src="circlered.gif"/> and <img src="circleyellow.gif"/> icons next to each listing.</p>'+
  '<p>Yellow icons have pictures.</p>'+
  '<p>Set the price range using the drop-down menu above.</p>'+
  '</div>';
}
}

function setSort(field) {
listing.setSort(field);
listing.load(nodePanel);
}

function getAbsoluteX(e){
var x=0;
while(e!=null){
 x+=e.offsetLeft;
 e=e.offsetParent;
}
return x;
}

function getAbsoluteY(e){
var y=0;
while(e!=null){
 y+=e.offsetTop;
 e=e.offsetParent;
}
return y;
}

function resizeMap(e){
var w=0;
var h=0;
if(window.innerWidth){
 w=window.innerWidth;
 h=window.innerHeight;
}else if(document.documentElement&&document.documentElement.clientWidth){
 w=document.documentElement.clientWidth;
 h=document.documentElement.clientHeight;
}else if(document.body&&document.body.clientWidth){
 w=document.body.clientWidth;
 h=document.body.clientHeight;
}
if(w>1024){
nodeMap.style.width=w-444+'px';
}
if(h>600){
nodeMap.style.height=h-getAbsoluteY(nodeMap)-11+'px';
var n=el('listing');
n.style.height=h-getAbsoluteY(n)-11+'px';
nodePanel.style.height=h-getAbsoluteY(nodePanel)-11+'px';
}
}

function showLocationInfo(node){
markers.click(node.id);
}
