﻿var xmldoc;
var seekMD;
var markers = [];
var iwHtml = [];
function cProperties(map, shoppingList, seekM, state, city, landmark, latitude, longitude, maxD, provider, sOption, sDir, fList){
  //alert('now');
  cid = '178200';
  domain = 'baltimore-maryland.org';
  cname = 'hotels';
  if(landmark == ''){
    landmark = city;
  }
  var http_request = false;
  var id = 'compare';
  seekMD = seekM;
  if (window.XMLHttpRequest) { // Mozilla, Safari, ...
	http_request = new XMLHttpRequest();
	if (http_request.overrideMimeType) {
	  http_request.overrideMimeType('text/xml');
	                // See note below about this line
	}
  } 
  else if (window.ActiveXObject) { // IE
	try {
	  http_request = new ActiveXObject("Msxml2.XMLHTTP");
	} 
	catch (e) {
	  try {
	    http_request = new ActiveXObject("Microsoft.XMLHTTP");
	  }
	  catch (e) {}
	}
  }
  if (!http_request) {
	return false;
  }
  //var oFs = 'cProperties(\'' + shoppingList + '\', \'' + seekM + '\', \'' + state + '\', \'' + city + '\', \'' + landmark + '\', \'' + latitude + '\', \'' + longitude + '\', \'' + maxD + '\', \'' + provider + '\''; // + \'' + sDir + '\', 'rating');';
  http_request.onreadystatechange = function() { setSelect(map, http_request, id, sOption, sDir, fList); };
  
  http_request.open('GET', 'mapData.php?seekM=' + seekM + '&shoppingList=' + shoppingList + '&sOption=' 
    + sOption + '&sDir=' + sDir + '&table=hotelsnew' + '&latitude=' + latitude + '&longitude=' + longitude + '&landmark=' + landmark + '&city=' + city + '&state=' + state + '&provider=' + provider, true);
  http_request.send(null);
//document.getElementById("debug").innerHTML = 'mapData.php?seekM=' + seekM + '&shoppingList=' + shoppingList + '&sOption=' + sOption + '&sDir=' + sDir + '&table=hotelsnew' + '&latitude=' + latitude + '&longitude=' + longitude + '&landmark=' + landmark + '&city=' + city + '&state=' + state + '&provider=' + provider; // alert("cProperties sDir = " + sDir);
}
          
function setSelect(map, http_request, id, sOption, sDir, fList) {
  if (http_request.readyState == 4) {
    if (http_request.status == 200) {
      xmldoc = http_request.responseXML;
      
      
      drawCompare( sOption, sDir, id, fList);
	}
    else {
      alert('There was a problem with the request.');
    }
  }
}
function drawCompare( sOption, sDir, ID, fList){
 // alert("dCompare sDir = " + sDir + " sOption = " + sOption);
    rDir = 'DESC';
    nDir = 'ASC';
    dDir = 'ASC';
    pDir = 'ASC';
      if(sOption == 'rating'){
       
        if(sDir == 'ASC'){
          rDir = 'DESC';
        }
        else{
          rDir = 'ASC';
        }
      }
      else if(sOption == 'name'){
        if(sDir == 'ASC'){
          nDir = 'DESC';
        }
        else{
          nDir = 'ASC';
        }
      }
      else if(sOption == 'distance'){
        if(sDir == 'ASC'){
          dDir = 'DESC';
        }
        else{
          dDir = 'ASC';
        }
      }
      if(sOption == 'price'){
        if(sDir == 'ASC'){
          pDir = 'DESC';
        }
        else{
          pDir = 'ASC';
        }
      }
     
      
      var hotels = xmldoc.getElementsByTagName('hotel');
      var fPoint = xmldoc.getElementsByTagName('focus');
      var lName;
      for(j = 0; j < fPoint.length; j++){
        lName = fPoint[j].getAttribute("nm");
      }
      var htmlStr = '<table border="0" cellpadding="0" cellspacing="3" align="right">';
      if(hotels.length > 0){
        htmlStr += '<tr><td width="80" class="hcCell2" colspan="2">Zoom To</td>'
        + '<td width="160" class="hcCell2"><a href="javascript:wtf(\'cp\', \'rating\', \'' + rDir + '\');" >Rating <img src="images/' + rDir + '.gif" border="0"/></a> / <a href="javascript:wtf(\'cp\', \'name\', \'' + nDir + '\');" >Name <img src="images/' + nDir + '.gif" border="0"/></a></td>'
        + '<td class="hcCell2"><a href="javascript:wtf(\'cp\', \'distance\', \'' + dDir + '\');" >Dist <img src="images/' + dDir + '.gif" border="0"/></a></td>'
        + '<td width="70" class="hcCell2">Discount Rate</td>'
        + '<td width="70" class="hcCell2">Handicapped Accessible</td>'
        + '<td width="70" class="hcCell2">Allows Pets</td>'
        + '<td width="130" class="hcCell2"><a href="javascript:wtf(\'cp\', \'price\', \'' + pDir + '\');" >Price <img src="images/' + pDir + '.gif" border="0"/></a></td>'
        +'</tr>'; 
      }
      var rc = 0;
      var ic = 0;
      for(i = 0; i < hotels.length; i++){
      
      if(filter(hotels[i], fList) == "y"){
        ic++;
        var img = hotels[i].getAttribute("im");
        var isrc = img.substring(0 , img.indexOf("|"));
        htmlStr += '<tr id="'+ i + '" align="center" onmouseover="changeColor(this.id)" onmouseout="restoreColor(this.id)">'
          + '<td class="hcCell' + (i % 2) + '"><input type="radio" onchange="myclick(' + i + ', map)" name="chName" value="'+ hotels[i].getAttribute("nm") + '" /></td>'
	      + '<td class="hcCell' + (rc % 2) + '" width="55"><a href="javascript:openphotos(\'http:\/\/' + cname + '.' + domain + '/hotels/photos.jsp?locale=en_us_US&room-0-adult-total=2&currencyCode=USD&numberOfRooms=1&photo=1&hotelID='+ hotels[i].getAttribute("bi") + '&cid=' + cid + '&additionalDataString=vrBookingSource%7ChotInfo\', \'thor\', \'500\', \'458\');">'
	      + '<img border="0" src="' + isrc + '" width="55" height="55" /></a></td>'
	      + '<td class="hcCell' + (rc % 2) + '" width="148"><img align="top" src="';
        if(hotels[i].getAttribute("ra") == '' || hotels[i].getAttribute("ra") == '0' ){
          htmlStr += 'space';
        }
        else {
          var starStr;
          starStr = hotels[i].getAttribute("ra").substring(0, 1);
          if (hotels[i].getAttribute("ra").indexOf(.5) != -1){
            starStr += 'star';
          }
          else {
            starStr += 'andahalfstar';
          }
          if(hotels[i].getAttribute("ra") != '1'){
            starStr += 's';
          }
          htmlStr += starStr;
        }
        htmlStr += '.gif" /><br/>'
          + '<a class="spBlue" href="http://' + cname + '.' + domain + '/index.jsp?pageName=hotInfo&cid=' + cid + '&hotelID=' + hotels[i].getAttribute("bi") + '">' + hotels[i].getAttribute("na")+ '</a></td>'
	      + '<td class="hcCell' + (rc % 2) + '" width="50"><span class="hbSmText" style="font-size: 7pt;">' + hotels[i].getAttribute("di") + ' mi ' + hotels[i].getAttribute("co") + '<br/>' + lName  + '</span></td>'
          + '<td class="hcCell' + (rc % 2) + '">'+ dispAmenity(hotels[i].getAttribute("ml"), 0, 0) + '</td>'
          + '<td class="hcCell' + (rc % 2) + '">'+ dispAmenity(hotels[i].getAttribute("amha"), 0, 0) + '</td>'
          + '<td class="hcCell' + (rc % 2) + '">'+ dispAmenity(hotels[i].getAttribute("ampf"), 0, 0) + '</td>'
          + '<td class="hcCell' + (rc % 2) + '"><span class="hbPrice">' + hotels[i].getAttribute("pr")+ '</span><br/>'
          + '<img align="top" src="images/greencorner-l.gif" /><span class="hbButton"><a href="http://' + cname + '.' + domain + '/index.jsp?pageName=hotInfo&cid=' + cid + '&hotelID=' + hotels[i].getAttribute("bi") + '">Check Dates</a></span>'
          + '<img align="top" src="images/greencorner.gif" /></td>'
          + '</tr>';
        rc++;
      }
      
	  }
	  if(ic == 0){
	    htmlStr += '<tr><td colspan="7"><img src="images/alert.gif"/> There are no hotels ';
	    if(seekMD == "byID"){
	      htmlStr += 'that you have selected with those amenities.';
	    }
	    if(seekMD == "distance"){
	      htmlStr += 'in the area with those amenities.';
	    }
	    htmlStr += '<br/><br/>Please try again with less restrictions</td></tr>';
	  }
	  htmlStr += '</table>';
	 getData( map, fList);
	  document.getElementById("compare").innerHTML = htmlStr;
	  
}
function filter(hotel, amArray){
  if(amArray.length != 0){
    for(yC = 0; yC < amArray.length; yC++){  //var aStr = hotel.getAttribute("na") + ' ' + hotel.getAttribute(amArray[0]);
      if(hotel.getAttribute(amArray[yC]) == "0"){
        yC = amArray.length;
        return "n";
        //aStr += ' Does Not Have a ' + amArray[0];
      }
    }
    //debug alert(aStr + ': ' + amArray.length + ' amenities to check');
  }
 
 
  return "y";
}

function dispAmenity(val1, val2, val3){
  htmlStr = '<img src="images/';
  var combo = Math.max(Math.max(parseInt(val1), parseInt(val2)), parseInt(val3)); 
  if(combo == 0){
    htmlStr +=  'doesnothave';
  }
  else {
    htmlStr += 'has'; 
  }
  htmlStr += 'amenity.gif" />';
  return htmlStr;
}

//*****************************************************************************************************************8
function load(map) {
 
  map.addControl(new GLargeMapControl());
  map.addControl(new GMapTypeControl());
  map.addControl(new GScaleControl());
  map.addControl(new GOverviewMapControl());
  map.enableContinuousZoom();
  map.enableDoubleClickZoom();
  map.setCenter(new GLatLng(0, 0));

  GEvent.addListener(map, "click", function(overlay, point) {
      if (overlay){	// marker clicked
        overlay.openInfoWindowHtml(overlay.infowindow);	// open InfoWindow
	    map.panTo(overlay.getPoint());
	    if(map.getZoom() < 14){
	      map.setZoom(14);
	    }
	  } 
   });
}

function myclick(p, map) {
        markers[p].openInfoWindowHtml(iwHtml[p]);
         map.panTo(markers[p].getPoint());
	    if(map.getZoom() < 14){
	     map.setZoom(14);
	    }
}

function getData( map, fList){
   map.clearOverlays();
   var baseIcon = new GIcon();
   baseIcon.shadow = "http://www.google.com/mapfiles/shadow50.png";
   baseIcon.iconSize = new GSize(20, 34);
   baseIcon.shadowSize = new GSize(37, 34);
   baseIcon.iconAnchor = new GPoint(9, 34);
   baseIcon.infoWindowAnchor = new GPoint(9, 2);
   baseIcon.infoShadowAnchor = new GPoint(18, 25);
   var icon = new GIcon(baseIcon);
   var bicon = new GIcon(baseIcon);
   icon.image = "http://www.google.com/mapfiles/marker.png";
   bicon.image = "http://www." + domain + "/images/N.png";
   focused = xmldoc.documentElement.getElementsByTagName("focus");
   if (focused.length){
	 var gzl;
     var focusP;
	 var cpName;
	 for (var i = 0; i < focused.length; i++) { 
	   var lat = parseFloat(focused[i].getAttribute("la"));
	   var lon = parseFloat(focused[i].getAttribute("lo"));
	  //alert(lat + " lon " + lon);
	   gzl = calculateZoom2(parseFloat(focused[i].getAttribute("so")), parseFloat(focused[i].getAttribute("no")), parseFloat(focused[i].getAttribute("we")), parseFloat(focused[i].getAttribute("ea")) );	
	   cpName = focused[i].getAttribute("nm");
	   
	 }
	 
     // document.getElementById("mapMainControl").innerHTML += ' ' +gzl;
	 
	 centerref = map.getCenter();
	 latitude = lat;
	 longitude = lon;
	 map.setCenter(new GLatLng(lat, lon));
	//map.setCenter(new GLatLng(lat, lon));
	 map.setZoom(Math.min((gzl - 1), 16));
	 
   }
   locations = xmldoc.documentElement.getElementsByTagName("hotel");
   
   markers = [];
   if (locations.length){
     for (var i = 0; i < locations.length; i++) { // cycle thru locations
	   if(filter(locations[i], fList) == "y"){
	     markers[i] = new PdMarker(new GLatLng(locations[i].getAttribute("la"),locations[i].getAttribute("lo")), icon, locations[i].getAttribute("na"));
		 iwHtml[i] = iwDraw(locations[i]);
		 markers[i].infowindow = iwHtml[i];
		
		 map.addOverlay(markers[i]);
	   }
	 }
   } 
   //focusP = 
  // alert(latitude + " " + longitude);
	 //map.addOverlay(new GMarker(new GLatLng(latitude, longitude)));
 }

function calculateZoom2( minY, maxY, minX, maxX){
//  document.getElementById("MainControl").innerHTML = minX + ' ' + maxX + ' ' + minY + ' ' + maxY;
  var mapElement = document.getElementById("mapMain");
  var degLonPerPixel = Math.abs(maxX - minX) / 700; //parseInt(mapElement.style.width);
  var degLatPerPixel = Math.abs(maxY - minY) / 450; //parseInt(mapElement.style.height);
  var resolution = Math.max(degLonPerPixel, degLatPerPixel);
  
  return Math.ceil(Math.log(1.46025 / resolution) / Math.log(2) ) ;
}
 
function iwDraw(data){
   var rtnStr;
   var gh = document.getElementById("debug");
   if(data.getAttribute("type") == "hotel"){
     rtnStr = '<div class="ibHb"><div class="ibHbHead"><span class="hbName" style="margin-right: 10px">'
      + data.getAttribute("na") 
      + '</span></div><div class="ibHbLower"><img align="top" src="'
      + data.getAttribute("im").substring(0, data.getAttribute("im").indexOf('|'))
      + '" /><img align="top" src="';
      if(data.getAttribute("ra") == ''){
        rtnStr += 'space';
      }
      else {
        var starStr;
        starStr = data.getAttribute("ra").substring(0, 1);
        if (data.getAttribute("ra").indexOf(.5) != -1){
          starStr += 'star';
        }
        else {
          starStr += 'andahalfstar';
        }
        if(data.getAttribute("ra") != '1'){
          starStr += 's';
        }
        rtnStr += starStr;
        
      }
      
      rtnStr += '.gif" />';
      rtnStr += '<span class="ibHbSmText">' 
      + data.getAttribute("ad") + ' / ' + data.getAttribute("ci") + ', ' + data.getAttribute("st") + ' ' + data.getAttribute("zi") 
      + '</span><p class="ibHbSmText">'
      + data.getAttribute("ot") + '</p>'
      + '&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;  '
      + '&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; '
      + '<img align="top" src="images/greencorner-l.gif" /><span class="hbButton"><a href="http://' + cname + '.' + domain + '/index.jsp?pageName=hotInfo&cid=' + cid + '&hotelID=' + data.getAttribute("bi") + '">CHECK DATES</a></span>'
          + '<img align="top" src="images/greencorner.gif" />';
      rtnStr += '</div></div>';
   }
   return rtnStr;
}