var gmarkers = new Array();
var htmls = new Array();
var tot_marks = 0;

function checkEnter(e){ 
//e is event object passed from function invocation var characterCode literal character code will be stored in this variable
if(e && e.which){ //if which property of event object is supported (NN4)
	e = e
	characterCode = e.which //character code is contained in NN4's which property
	}
	else{
	e = event
	characterCode = e.keyCode //character code is contained in IE's keyCode property
	}

	if(characterCode == 13){ //if generated character code is equal to ascii 13 (if enter key)
	return true
	}
	else{
	return false
	}
}

function run_srch() {
	mv_search(document.getElementById('search').value);
}

function hilite(tableRow, highLight) {
  if (highLight) {
    tableRow.style.backgroundImage = 'url("/images/s_depressed.gif")';
  } else {
    tableRow.style.backgroundImage = 'none';
  }
}

function readMap(url) {
   var request = GXmlHttp.create();
   request.open("GET", url, true);
   request.onreadystatechange = function() {
     if (request.readyState == 4) {
       var xmlDoc = request.responseXML;
       // obtain the array of markers and loop through it
       var markers = xmlDoc.documentElement.getElementsByTagName("marker");

       // hide the info window, otherwise it still stays open where the removed marker used to be
       map.getInfoWindow().hide();

       map.clearOverlays();

       // empty the array
       gmarkers = [];

       // reset the side_bar
       side_bar_html="";

       for (var i = 0; i < markers.length; i++) {
         // obtain the attribues of each marker
         var lat = parseFloat(markers[i].getAttribute("lat"));
         var lng = parseFloat(markers[i].getAttribute("lng"));
         var point = new GLatLng(lat,lng);
         var html = markers[i].getAttribute("html");
         var label = markers[i].getAttribute("label");
         // create the marker
         var marker = createMarker(point,label,html);
         map.addOverlay(marker);
       }
       // put the assembled side_bar_html contents into the side_bar div
       document.getElementById("side_bar").innerHTML = side_bar_html;
     }
   }
   request.send(null);
 }

function mv_search (search) {
	var results_table = "";
	map.setCenter(new GLatLng(54.826008,-4.262695), 5)
	GDownloadUrl("/portal/search_xml?search=" + search, 
	function(data, responseCode) {
	  //var info = eval(data);
	  var info = eval('(' + data + ')');
	  if(info && info[0]) {
	    for (var i = 0; i < info.length; i++) {
			//don't plot hotels
			var point = new GLatLng(info[i].latitude,info[i].longitude);
            var html = createResultMarker(point, info[i]);
  			results_table += html;
			//var marker = createMarker(point,info[i].name,info[i].html);
		}
	  
		document.getElementById('search_results').innerHTML = 
		"<table cellpadding=\"0\" cellspacing=\"0\" class=\"results\">" +
		results_table +
		"</table>";
		//document.getElementById('search_results').display = "block";
	 } else {
		alert('Not found');
	}
	});
}

function createResultMarker(point, pnt_nfo) {
	var place = new GIcon();
	place.image = "/images/icons/map_place_red.png";
	place.iconSize = new GSize(15, 15);
	place.iconAnchor = new GPoint(6, 20);
	place.shadow = "/images/icons/map_hotel_shadow.png";
	place.iconSize = new GSize(15, 15);
	place.shadowSize = new GSize(20, 15);
	place.iconAnchor = new GPoint(6, 20);
	place.infoShadowAnchor = new GPoint(11, 11);
	place.infoWindowAnchor = new GPoint(5, 1);


	var rhtml = "";
	if(pnt_nfo.type == 'hotel') {
		rhtml = '<tr onmouseout="hilite(this,false)" onmouseover="hilite(this,true)" onclick="javascript:map.setCenter(new GLatLng(' + pnt_nfo.latitude + ', ' + pnt_nfo.longitude + '), ' + pnt_nfo.zoom + ')">' + pnt_nfo.html + '</tr>';
	} else {
		var marker = new GMarker(point, place);
		map.addOverlay(marker);
	
		// save the info we need to use later for the side_bar
		gmarkers[tot_marks] = marker;
		//check auto zoom level for nearest hotel :-
		//htmls[tot_marks] = pnt_nfo.html;
		// add a line to the side_bar html
		rhtml = '<tr onmouseout="hilite(this,false)" onmouseover="hilite(this,true)" onclick="javascript:myclick(' + tot_marks + ')">' + pnt_nfo.html + '</tr>';
		tot_marks++;
	}
	return rhtml;
}

function myclick(i) {
	map.setCenter(gmarkers[i].getPoint(),6);
}

function createMarker(point, icon, xml) {
  var marker = new GMarker(point, icon);
	var id = xml.getAttribute("id");
	var url = "/portal/hotel_info_xml?id=" + id;
  GEvent.addListener(marker, "click", function() {
		GDownloadUrl(url, function(data, responseCode) {
			var infoTabs = [
			  new GInfoWindowTab("About", data)
			 // new GInfoWindowTab("Picture", "None available")
			];
	    marker.openInfoWindowTabsHtml(infoTabs);
		});
  });
  return marker;
}

