/*

Scripts for USL map page
Copyright Uxbridge Student Lets 2007
Ideas and samples taken from http://www.google.com/apis/maps/documentation
  
*/

  //<![CDATA[

  var map;
  var mmgr;
  var marks = [];
  var dbmarkers = [];
  var ptCenter = new GLatLng(51.541186, -0.482168);
  
  var ALL_ZOOM_LEVEL   = 13;
  var PROP_ZOOM_LEVEL  = 15;

  var IDX_TYPE         = 0;
  var IDX_POINT        = 1;
  var IDX_NAME         = 2;
  var IDX_INFO         = 3;
  var IDX_ICON         = 4;

  var ID_TYPE_HOUSE    = 1;
  

  function initMarkers() {
    // UBL property markers
    dbmarkers[dbmarkers.length] = [ID_TYPE_HOUSE, new GLatLng(51.533786, -0.482268), "Cowley Road", "4 Bed, <br><a href='house1.html'>More info</a>", ""];
    dbmarkers[dbmarkers.length] = [ID_TYPE_HOUSE, new GLatLng(51.543986, -0.487068), "Rockingham Road", "4 Bed, <br><a href='house2.html'>More info</a>", ""];
    dbmarkers[dbmarkers.length] = [ID_TYPE_HOUSE, new GLatLng(51.528186, -0.475668), "Bosanquet Close", "5 Bed, <br><a href='house3.html'>More info</a>", ""];
    dbmarkers[dbmarkers.length] = [ID_TYPE_HOUSE, new GLatLng(51.538686, -0.485768), "Mill Avenue", "5 Bed, <br><a href='house4.html'>More info</a>", ""];
    dbmarkers[dbmarkers.length] = [ID_TYPE_HOUSE, new GLatLng(51.550186, -0.482568), "Braybourne Close", "5 Bed, <br><a href='house5.html'>More info</a>", ""];
    dbmarkers[dbmarkers.length] = [ID_TYPE_HOUSE, new GLatLng(51.551586, -0.481968), "Braybourne Close", "5 Bed, <br><a href='house6.html'>More info</a>", ""];
    dbmarkers[dbmarkers.length] = [ID_TYPE_HOUSE, new GLatLng(51.553186, -0.480268), "Braybourne Close", "5 Bed, <br><a href='house7.html'>More info</a>", ""];
    dbmarkers[dbmarkers.length] = [ID_TYPE_HOUSE, new GLatLng(51.520926, -0.477968), "St Clement Close", "5 Bed, <br><a href='house8.html'>More info</a>", ""];
    dbmarkers[dbmarkers.length] = [ID_TYPE_HOUSE, new GLatLng(51.532886, -0.475668), "Brunel University", "", "blue"];
  }
  
  function listLocations() {
    var txt = "";
    var htxt = "";
    
    txt = '<p class="pinttext"><a class="alink" href="javascript:locationZoom(-1)">Show all property locations</a></p>';
    htxt = '<p class="pinttext"><a class="alink" href="javascript:locationZoom(0)">Cowley Road</a> | '
    	 +  '<a class="alink" href="javascript:locationZoom(1)">Rockingham Road</a> | ' 
    	 +  '<a class="alink" href="javascript:locationZoom(2)">Bosanquet Close</a> | ' 
    	 +  '<a class="alink" href="javascript:locationZoom(3)">Mill Avenue</a><br> ' 
    	 +  '<a class="alink" href="javascript:locationZoom(4)">Braybourne Close</a> | ' 
    	 +  '<a class="alink" href="javascript:locationZoom(5)">Braybourne Close</a> | ' 
    	 +  '<a class="alink" href="javascript:locationZoom(6)">Braybourne Close</a> | '
    	 +  '<a class="alink" href="javascript:locationZoom(7)">St Clement Close</a><br> '
    	 +  '<a class="alink" href="javascript:locationZoom(8)">Brunel University</a>';  
    
    document.getElementById("centrelink").innerHTML = txt;
    document.getElementById("houselink").innerHTML = htxt;
  }
  
  function locationZoom(index) {  
    map.closeInfoWindow();
    if ((index >= 0) && (index < dbmarkers.length)) {
      map.setCenter(dbmarkers[index][IDX_POINT], PROP_ZOOM_LEVEL);
      showMarkerInfo(index);
    } else { 
      map.setCenter(ptCenter, ALL_ZOOM_LEVEL);
    }
  }
    
  function showMarkerInfo(index) {   
    var infotxt = "<strong>" + dbmarkers[index][IDX_NAME] + "</strong><br />&nbsp;<br />" + dbmarkers[index][IDX_INFO];
    marks[index].openInfoWindowHtml(infotxt);
  }
    
  function createMarker(index) {
    var marker;
    var ibase = dbmarkers[index][IDX_ICON];
    if ((ibase == "") || (ibase == null)) {
      marker = new GMarker(dbmarkers[index][IDX_POINT]);
      GEvent.addListener(marker, "click", function() {
      	showMarkerInfo(index);
      });
    } else if (ibase == "blue") {
      var micon = new GIcon(G_DEFAULT_ICON);
      micon.image = "images/bluemarker.png";

      marker = new GMarker(dbmarkers[index][IDX_POINT], { icon : micon});
      GEvent.addListener(marker, "click", function() {
        showMarkerInfo(index);
      });	
    }  
    return marker;
  }

  function getMarkers() {
    for (i = 0; i < dbmarkers.length; i++) {
      marks.push(createMarker(i));
    }
    return marks;
  }
  
  function setupMarkers() {
    // Stopped using the marker manager 'cos it seemed to cause flicker 
    // with non-default icons. Simple .addOverlay doesn't seem to have 
    // this problem, especially with the small number we are dealing with.
//    mmgr = new GMarkerManager(map);
//    mmgr.addMarkers(getMarkers(), 1);
//    mmgr.refresh();

    var m = getMarkers();
    for (i = 0; i < m.length; i++) {
      map.addOverlay(m[i]);
    }  
  }

  function setupMap() {
    if (GBrowserIsCompatible()) {
      map = new GMap2(document.getElementById("map"));
      map.addControl(new GMapTypeControl());
      map.addControl(new GLargeMapControl());
      map.addControl(new GScaleControl());
      map.setCenter(ptCenter, ALL_ZOOM_LEVEL);
      window.setTimeout(setupMarkers, 0);        
    } else {
      document.getElementById("map").innerHTML = "This web browser is not compatible with the mapping functionality, sorry."
    }
  }

  function load() {
    initMarkers();
    setupMap();
    listLocations();
    
    if (document.URL.match("id=1")) {
    	//alert("prop1");
    	locationZoom(0);
    } else if (document.URL.match("id=2")) {
    	//alert("prop2");
    	locationZoom(1);
    } else if (document.URL.match("id=3")) {
    	//alert("prop3");
    	locationZoom(2);
    } else if (document.URL.match("id=4")) {
    	//alert("prop4");
    	locationZoom(3);
    } else if (document.URL.match("id=5")) {
    	//alert("prop5");
    	locationZoom(4);
    } else if (document.URL.match("id=6")) {
    	//alert("prop6");
    	locationZoom(5);
    } else if (document.URL.match("id=7")) {
    	//alert("prop7");
    	locationZoom(6);
    } else if (document.URL.match("id=8")) {
    	//alert("prop8");
    	locationZoom(7);
    }
    
  }

  //]]>

