var check_cat,check_text;
var current_cat=0;
var current_text='';
var geocoder,map, directory, xheight;
var dfl='search chamber directory';
var busysearch=false;
var markers = [];
var metac='';

function set_meta_category(cat_id) {
	metac='&metac=' + cat_id;
}

function addToOverlay(x) {
  var xa = x.address.replace(/PO Box.*/i, '');
  var address = xa+' '+x.city+', '+x.state+' '+x.zip;
  geocoder.getLatLng(address, function(p){
    try {
      x._marker = new GMarker(p);
      map.addOverlay(x._marker);
      markers[x.memberid] = x._marker;
      GEvent.addListener(x._marker, "click", function() {
  	jQuery.get('/directory/gather.pl?details=1&id=' + x.memberid + '&ref=' + window.location.href, function(data, textStatus) {
  	  map.openInfoWindowHtml(p, data);
	});
      });
    } catch(e){
    };
  });
}
function updateList2() {
  updateList.sem=null;

  var ltobj = document.getElementById('maplt');
  map.setMapType(G_SATELLITE_MAP);
  var o = ltobj.scrollTop;
  var h = ltobj.clientHeight;

  var a = document.getElementById('maplgtop');
  var b = document.getElementById('maplgmid');
  var c = document.getElementById('maplgbottom');

  var th=directory.length * xheight;
  var td = parseInt(h / xheight);
  if ((h%xheight)!=0) td++;

  if ((o % xheight) != 0) {
    o -= (o % xheight);td++;
  }
  var m = o / xheight;

  var i;
  var html = [];
  map.clearOverlays();
  for (i = 0; i < td; i++) {
    var j = directory[i + m];
    if ((i+m)>=directory.length) {
      continue;
    }
    html[html.length]='<div style="background-color:#ffffff;height:'+xheight+'px;"><a href="javascript: void(0); " class="poplink" id="' + j.memberid + '" title="click for more information"><div class="itembox ' + j.category_class + ' ' + j.company_first_letter + '"><ul>';
    html[html.length]='<li><b>'+j.company+'</b></li>';
    html[html.length]='<li>'+j.city+', '+j.state+' '+j.zip + '</li>';
    html[html.length]='<li>' + j.phone+'</li>';
 //   if(j.url != undefined) html[html.length]='<li><a href="http://'+j.url+'" target="_new" title="visit website">'+j.url+'</a></li>';
    html[html.length]='</ul></div></a></div>';
    addToOverlay(j);
  }
  b.innerHTML=html.join('');

  a.style.height=o+'px';
  var g = ((th-o)-(td*xheight))-xheight; if (g < 0) g = 0; c.style.height=g+'px';
}


function updateList() {
  if (updateList.sem) clearTimeout(updateList.sem);
  updateList.sem=setTimeout(updateList2,100);
}

function mapReady2() {
  busysearch=false;
  if (check_text != current_text || check_cat != current_cat) {
    updateResults();
  } else {
    if (!map) {
      map = new GMap2(document.getElementById('map_canvas'));
      map.setCenter(new GLatLng(38.774489, -76.076283), 8);
      map.setUIToDefault();
      $('#maplt').bind('scroll',updateList);
    }
    setTimeout(updateList2,10);
  }
}
function updateResults() {
  if (busysearch) { return; }
  busysearch=true;

  var ltobj = document.getElementById('maplt');
  var a = document.getElementById('maplgtop');
  var b = document.getElementById('maplgmid');
  var c = document.getElementById('maplgbottom');
  a.style.height='0px';
  b.style.height='0px';
  c.style.height='0px';
  b.innerHTML='';
  ltobj.scrollTop=0;
  setTimeout(updateResults2,100);
}
function updateResults2() {
  $.getScript('/directory/gather.pl?json=1' + metac + '&cat=' + parseInt(current_cat) + '&q=' + escape(current_text) + '&ref=' + window.location.href, mapReady2);
}
function loadCategory(cx) {
  if (!cx)cx=0;
  current_cat = cx;
  updateResults();
}
function updateSearch2() {
  var e = document.getElementById('search');
  updateSearch.sem=null;
  var nv = (e.value == dfl) ? '' : e.value;
  if (current_text != nv) {
    current_text=nv;
    updateResults();
  }
}
function updateSearch() {
  var e = document.getElementById('search');
  if (e._oldvalue != e.value) {
    e._oldvalue = e.value;
  }
  if (updateSearch.sem) clearTimeout(updateSearch.sem);
  updateSearch.sem=setTimeout(updateSearch2,100);
}

function mapReady() {
  if(window.METACAT) { set_meta_category(window.METACAT);window.METACAT=null; }

  geocoder = new GClientGeocoder();
  $('#dirbox').append('<p>You can search by category, company name, or contact name using the search box below.</p><div class=\"searching\"><input class="empty" type="text" id="search" name="search" onblur="if(this.value==\'\'){this.className=\'empty\';this.value=\''+dfl+'\';}else this.className=\'\';" onfocus="if(this.value==\''+dfl+'\')this.value=\'\';this.className=\'\';" onkeypress="updateSearch();" onmouseup="updateSearch();" onpaste="updateSearch();" onDOMInput="updateSearch();" onkeydown="updateSearch();" onchange="updateSearch();" value="'+dfl+'"/></div><div id="sels"></div><div id="mapcan"><div style="float:right;width:450px;"><div id="map_canvas" style="width:400px;height:400px;"></div></div><div id="maplt"><div id="maplg"><div id="maplgtop"></div><div id="maplgmid"></div><div id="maplgbottom"></div></div></div></div>');

  $('#sels').load('/directory/gather.pl?categories=1&ref=' + window.location.href + metac);
  loadCategory();

  xheight = document.getElementById('xbox').offsetHeight;

  $('.poplink').live('click', function() {
	(function(id){
	 jQuery.get('/directory/gather.pl?details=1&id=' + id + '&ref=' + window.location.href, function(data, textStatus) {
		 var marker = markers[id]; if (marker) marker.openInfoWindowHtml(data);
		 });
	 })(this.id);
  });
}
$(document).ready(mapReady);

