
var defaultLat = '43.6482492';
var defaultLong = '-79.3925285';
var defaultZoom = 13;

function addLoadEvent(func) {
  var oldonload = window.onload;
  if (typeof window.onload != 'function') {
    window.onload = func;
  } else {
    window.onload = function() {
      if (oldonload) {
        oldonload();
      }
      func();
    }
  }
}

var map = null;

function initMap() {
  map = new VEMap('map');
  map.LoadMap();
  map.SetScaleBarDistanceUnit(VEDistanceUnit.Kilometers);
  map.SetCenterAndZoom(new VELatLong(defaultLat, defaultLong), defaultZoom);
  // document.getElementById("map").firstChild.firstChild.style.cursor = 'default';
  // map.AttachEvent('onclick', showCurrentMapStats);
}

function loadCondos(xml) {
  var root = xml.documentElement;
  // alert(xml.xml);
  if ((root != null) && (root.childNodes.length > 0)) {
    map.Clear();
    map.DeleteAllPolygons();
    var m_latt,m_longt,m_zm,m_pts;
    m_latt = $attrValue(root, 'latt');m_longt = $attrValue(root, 'longt');m_zm = $attrValue(root, 'zoom');m_pts = $attrValue(root, 'pts');

    if (!m_latt && !m_longt && !m_zm)
 	  map.SetCenterAndZoom(new VELatLong(defaultLat, defaultLong), defaultZoom);
    else
 	  map.SetCenterAndZoom(new VELatLong(m_latt, m_longt), m_zm);
 	  
 	if (m_pts != null) {
 	  var points = new Array();
 	  var ps = m_pts.split(';');
 	  for (var i=0; i<ps.length; i++) {
 	    points.push(new VELatLong(ps[i].split(',')[0],ps[i].split(',')[1]));
 	  }
      var poly = new VEPolygon('pg', points, new VEColor(100,200,200,0.20), new VEColor(100,200,200,0.5), 2);
      map.AddPolygon(poly);
 	}    

    var nodes = root.childNodes;
    var id,name,addr,latt,longt,st,fd,hl;

    for (var i=0; i < nodes.length; i++) {
	  id = $attrValue(nodes[i], 'id');
	  name = $attrValue(nodes[i], 'name');
	  addr = $attrValue(nodes[i], 'addr');
	  latt = $attrValue(nodes[i], 'latt');
	  longt = $attrValue(nodes[i], 'long');
	  st = $attrValue(nodes[i], 'new');
	  fd = $attrValue(nodes[i], 'fd');
	  hl = $attrValue(nodes[i], 'hl');
	  blg = $attrValue(nodes[i], 'blg');
	  lgo = $attrValue(nodes[i], 'lgo');

      var img = st == 'true' ? 'images/gr_pin.gif' : 'images/or_pin.gif';
      var desc = $format((blg != null ? '<img src="images/bldg-icons/{2}" style="padding:3px" /><br>':'') + '{0}' + (fd != '' ? '<br><br><a hidefocus="true" href="Condos/{1}/Home.aspx">Home</a>':'') + (hl == 'true' ? '&nbsp;|&nbsp;<a hidefocus="true" href="Condos/{1}/Listings.aspx">Listings</a>' : ''), addr, fd, blg);
      desc += $format('<div style="padding-top:4px"><a href="mailto:askcondo@gmail.com?subject=Subscribe to {0} Updates">Subscribe to Updates</a></div>', addr);
      
      if (latt && longt) {
        var pos = new VELatLong(latt, longt);
        var pin = new VEPushpin('c' + id, pos, img, name, desc);
        map.AddPushpin(pin);
	  }
	} // for
  }
}

function stateChange() {
  if (req.readyState == 4) {
    if (req.status == 200){
      loadCondos(req.responseXML);
  } else
    alert('Problem retrieving Condos. Please try again later.');
  }
}

function fillMap(i) {
  var index = isNaN(parseInt(i)) ? 1 : parseInt(i);

  if (window.XMLHttpRequest)
    req = new XMLHttpRequest();
  else if (window.ActiveXObject)
    req = new ActiveXObject("Microsoft.XMLHTTP");

  if (req != null) {
    req.onreadystatechange = stateChange;
    req.open('POST', 'Handlers/GetCondos.ashx');
    req.send('<req><k>' + sKey + '</k><i>' + index + '</i></req>');
  }
  else {
    alert('Your browser does not support XMLHTTP. You will be unable to view the map.');
  }
}

function showCurrentMapStats(e) {
  var x = e.mapX;
  var y = e.mapY;
  var pixel = new VEPixel(x, y);

  var stat = 'Center: ' + map.GetCenter().Latitude + ',' + map.GetCenter().Longitude +
    '\nZoom: ' + map.GetZoomLevel() + '\nLatitude_OnClick: ' + map.PixelToLatLong(pixel).Latitude + '\nLongitude_OnClick: ' + map.PixelToLatLong(pixel).Longitude;
  alert(stat);
}