/*
This app was written by Marcel Tjandraatmadja (marcel.tjandra@gmail.com).

Originally posted at http://marcelpad.wordpress.com.
*/


var centerLatitude = 48.88119;
var centerLongitude = 2.32465;
var startZoom = 12;
var currZoom = startZoom;
var markersArray = new Array();
var map;

function Marker(marker,description) 
{
  this.marker = marker;
  this.description = description;
}


function init()
{
   if (GBrowserIsCompatible()) 
   {
//      map = new GMap2(document.getElementById("map"));
//      map.addControl(new GSmallMapControl());
      
      // adding click event for the map
      GEvent.addListener(map, "click", function(overlay, location) {
         
         if( overlay )
         {
            // clicked on something other than the map
            
         }
         else
         {
            
            // create a new marker
         }
         
         
      });
      
      
      
    }

}

function createMarker(lang,currency)
{
//alert(currency);
/*

   var latitude = trim(document.forms[0].latitude.value);
   var longitude = trim(document.forms[0].longitude.value);
   var description = trim(document.forms[0].description.value);
   
   description = description.replace(/[<>]/g,"");

   // simple validation
   if(!Number(latitude) || !Number(longitude))
   {
      alert("Latitude and longitude must be numbers");
      return;
   }
   
   if( latitude.length == 0 || longitude.length == 0 )
   {
      alert("Latitude and longitude cannot be empty");
      return;
   }
  */ 
//   alert(coords[1]);
   
//   alert(coords.length);
  for (var i=0;i<coords.length;i++) {
   
//        showAddress(info_mls_addr[i],info_mls[i],info_mls_zip[i],'');
coordsData=coords[i].split("|");
latitude=coordsData[0];
longitude=coordsData[1];
Curphotos=images[i];
//alert(latitude);
   // create new location object
   var location = new GLatLng(latitude, longitude);
   
   // add marker
    summaryData=summary[i].split("|");
    cur_hotel_id = summaryData[0];    
    description = summaryData[1];
    addMarker(location,cur_hotel_id,lang,currency,Curphotos);   
   
   // move center mantaining current zoom level
//   map.panTo(location, map.getZoom());   
   }
   
}





function addMarker(location,hotel_id,lang,currency,curphotos)
{
   
   // create new gicon to use with the marker
   var customIconHotel = new GIcon();
   
   // put in random image
   var pos = Math.floor(Math.random()*3);
   var images = new Array("images/bed.png");
   customIconHotel.image = images[0];
   
   customIconHotel.iconSize = new GSize(25, 22);
   customIconHotel.iconAnchor = new GPoint(10, 34);
   customIconHotel.infoWindowAnchor = new GPoint(10, 0);
   customIconHotel.shadow = "images/bed_shadow.png";
   customIconHotel.shadowSize = new GSize(49, 32)
   
   
   
   // create new marker
   var marker = new GMarker(location,customIconHotel);
   
   var latitude = location.lat();
   var longitude = location.lng();

   
   GEvent.addListener(marker, 'click',
      function() 
      {
      

    var tabs = [];  
   tabs.push(new GInfoWindowTab('Summary', "<iframe src='summary_hotel.php?hotel_id="+hotel_id+"&lang="+lang+"&currency="+currency+"' frameborder='0' SCROLLING=no width='350' height='220'></iframe>"));      
//   tabs.push(new GInfoWindowTab('Summary', lang));  
   
   
   tabs.push(new GInfoWindowTab('Details', "<iframe src='details_hotel.php?hotel_id="+hotel_id+"&lang="+lang+"&currency="+currency+"' frameborder='0' SCROLLING=auto width='350' height='100%'></iframe>"));  
   tabs.push(new GInfoWindowTab('Photos', curphotos));     
   
   tabs.push(new GInfoWindowTab('Book it', "<iframe src='book_form.php?hotel_id="+hotel_id+"&lang="+lang+"&currency="+currency+"' frameborder='0' SCROLLING=no width='350' height='200'></iframe>"));        
   // Add tabs to the InfowWindow  
   marker.openInfoWindowTabsHtml(tabs);  
   
      
//         var htmlString = "<font color=\"#6387A5\" size=\"1\" face=\"Verdana, Arial, Helvetica, sans-serif\">"+description+"<br/>" + "(" + latitude + "," + longitude + ")</font>";
//         marker.openInfoWindowHtml(htmlString);
      }
   );
   
   
   // place new marker in the map   
   map.addOverlay(marker); 
   
   
   
   // add to array
   var markerClass = new Marker(marker,description);
   markersArray.push(markerClass);
   
   // render markers list
//   renderMarkerList();
}





function renderMarkerList()
{
   var divContents = "";
   var currDescription;
   var currMarker;
   
   
   for (var i = 0; i < markersArray.length; i++) 
   {
      currMarker = markersArray[i].marker;
      currDescription = markersArray[i].description;
      
      divContents += currDescription + " (" + currMarker.getLatLng().lat() + "," + currMarker.getLatLng().lng() + ") ";
      divContents += "<a href=\"#\" onClick=\"goToMarker("+ i +");\">Go there</a> <a href=\"#\" onClick=\"removeMarker("+ i +");\">Remove</a><br/>";
   }
   
   
   
   document.getElementById('currentMarkersDiv').innerHTML = divContents;
}



function goToMarker(idx)
{
   if( idx >= 0 && idx < markersArray.length )
   {
      var location = markersArray[idx].marker.getLatLng();
//      markersArray[idx].marker.openInfoWindowHtml('dfdf');
//      markersArray[idx].marker.click();
      map.panTo(location, map.getZoom());
      GEvent.trigger(markersArray[idx].marker, "click");
   }
}



function removeMarker(idx)
{
   if( idx >= 0 && idx < markersArray.length )
   {
      // remove marker from map and array
      map.removeOverlay(markersArray[idx].marker);
      markersArray.splice(idx,1);
      
      renderMarkerList();
   }
   
}




function removeAllMarkers()
{
   for (var i = 0; i < markersArray.length; i++) 
   {
      map.removeOverlay(markersArray[i].marker);
   }
   
   markersArray = new Array();
//   renderMarkerList();
}











// function to trim string
function trim(word)
{
   while (word.substring(0,1) == ' ')
      word = word.substring(1, word.length);

   while (word.substring(word.length-1, word.length) == ' ')
      word = word.substring(0,word.length-1);
      
   return word;
}




