
var markers, map, houseIcon;
document.onload = initFormChecker();

function initFormChecker()
{
   if ( document.getElementById('fpgmap') )
   {
      // Enable
      if ( document.getElementById('fpmap') && GBrowserIsCompatible() )
      {
         document.getElementById('fpmap').className = 'enabled';
         map = new GMap2(document.getElementById('fpgmap'));
         var center = new GLatLng(53.6461, -3.0097);
         map.addControl(new GSmallMapControl());
         map.setCenter(center,13);
         if ( document.getElementById('sale') )
            document.getElementById('sale').onclick = showForSale;
         if ( document.getElementById('rent') )
            document.getElementById('rent').onclick = showToLet;
         if ( document.getElementById('both') )
            document.getElementById('both').onclick = showBoth;
      }
      initHouses();
      
      houseIcon = new GIcon();
      houseIcon.image = 'images/house-icon.gif';
      houseIcon.shadow = 'images/house-icon-shadow.gif';
      houseIcon.iconSize = new GSize(34, 21);
      houseIcon.shadowSize = new GSize(34, 21);
      houseIcon.iconAnchor = new GPoint(17, 18);
      houseIcon.infoWindowAnchor = new GPoint(17, 1);
      
      addBoth();
   }
   else
      setTimeout('initFormChecker()',10);
}

function addForSale()
{
   markers = new Array(houses.length);
   for ( i=0; i<houses.length; i++ )
   {
      if ( houses[i].type == 'sale' )
      {
         houses[i].marker = new GMarker(new GLatLng(houses[i].latitude,houses[i].longitude),houseIcon);
         map.addOverlay(houses[i].marker);
         houses[i].alertWindow = '<table class="houseInfo"><tr><td class="image"><img src="images/no-photo.jpg" /></td><td>' + houses[i].streetname + '<br />&pound;' + houses[i].price + '<br /><a href="view-details.jsp?id=' + houses[i].id + '">View details</a></td></tr></table>';
         if ( houses[i].mainphoto > 0 )
            houses[i].alertWindow = '<table class="houseInfo"><tr><td class="image"><img src="showPhoto?id=' + houses[i].mainphoto + '" /></td><td>' + houses[i].streetname + '<br />&pound;' + houses[i].price + '<br /><a href="view-details.jsp?id=' + houses[i].id + '">View details</a></td></tr></table>';
         houses[i].marker.myID = i;
         GEvent.addListener(houses[i].marker, 'click', showHouse);
      }
   }
}
function addToLet()
{
   markers = new Array(houses.length);
   for ( i=0; i<houses.length; i++ )
   {
      if ( houses[i].type == 'rent' || houses[i].type == 'room' )
      {
         houses[i].marker = new GMarker(new GLatLng(houses[i].latitude,houses[i].longitude),houseIcon);
         map.addOverlay(houses[i].marker);
         houses[i].alertWindow = '<table class="houseInfo"><tr><td class="image"><img src="images/no-photo.jpg" /></td><td>' + houses[i].streetname + '<br />&pound;' + houses[i].price + '<br /><a href="view-details.jsp?id=' + houses[i].id + '">View details</a></td></tr></table>';
         if ( houses[i].mainphoto > 0 )
            houses[i].alertWindow = '<table class="houseInfo"><tr><td class="image"><img src="showPhoto?id=' + houses[i].mainphoto + '" /></td><td>' + houses[i].streetname + '<br />&pound;' + houses[i].price + '<br /><a href="view-details.jsp?id=' + houses[i].id + '">View details</a></td></tr></table>';
         houses[i].marker.myID = i;
         GEvent.addListener(houses[i].marker, 'click', showHouse);
      }
   }
}
function addBoth()
{
   for ( i=0; i<houses.length; i++ )
   {
      houses[i].marker = new GMarker(new GLatLng(houses[i].latitude,houses[i].longitude),houseIcon);
      map.addOverlay(houses[i].marker);
      houses[i].alertWindow = '<table class="houseInfo"><tr><td class="image"><img src="images/no-photo.jpg" /></td><td>' + houses[i].streetname + '<br />&pound;' + houses[i].price + '<br /><a href="view-details.jsp?id=' + houses[i].id + '">View details</a></td></tr></table>';
      if ( houses[i].mainphoto > 0 )
         houses[i].alertWindow = '<table class="houseInfo"><tr><td class="image"><img src="showPhoto?id=' + houses[i].mainphoto + '" /></td><td>' + houses[i].streetname + '<br />&pound;' + houses[i].price + '<br /><a href="view-details.jsp?id=' + houses[i].id + '">View details</a></td></tr></table>';
      houses[i].marker.myID = i;
      GEvent.addListener(houses[i].marker, 'click', showHouse);
   }
}

function showHouse()
{
   this.openInfoWindow(houses[this.myID].alertWindow);
}

function showForSale()
{
   map.clearOverlays();
   addForSale();
}
function showToLet()
{
   map.clearOverlays();
   addToLet();
}
function showBoth()
{
   map.clearOverlays();
   addBoth();
}

