﻿var xmldoc;
var baseIcon = new GIcon();
var mapFocus = {};
var siteInfo = {cid: '178200', cname: 'hotels', domain: 'baltimore-maryland.org'};
var dataMenu = {hotels: 100};
var bMap;
  baseIcon.shadow = "/images/shadowB.png";
  baseIcon.iconSize = new GSize(21, 31);
  baseIcon.shadowSize = new GSize(52, 29);
  baseIcon.iconAnchor = new GPoint(7, 31);
  baseIcon.infoWindowAnchor = new GPoint(9, 2);
  baseIcon.infoShadowAnchor = new GPoint(18, 25);
/*  
function mapAnchor(){
  
 // drawWindow();
  var clickZone = document.getElementById("mapWindow");
  clickZone.addEventListener("click", openMap, false);
  var clickZone = document.getElementById("pClose");
  clickZone.addEventListener("click", closeMap, false);
}
*/
function openMap(){
  
  mapFocus = mainFocus;
  drawWindow();
  var clickZone = document.getElementById("popup");
  var map = document.getElementById("mainWin");
  var cClose = document.getElementById("cClose");
  clickZone.style.visibility = "visible";
  map.className = "transOn";
  
  bMap = new GMap2(document.getElementById("pFrame"));
  bMap.addControl(new GLargeMapControl());
  bMap.addControl(new GMapTypeControl());
  bMap.addControl(new GScaleControl());
  bMap.enableContinuousZoom();
  bMap.enableDoubleClickZoom();
  dataAcquire();
  openBox("controlBox");
  hideOpenLink();
  // ie bugs cClose.addEventListener("click", closeBox, false);
}

function closeMap(){
  var clickZone = document.getElementById("popup");
  var map = document.getElementById("mainWin");
  clickZone.style.visibility = "hidden";
  closeBox();
  hideOpenLink();
  map.className = "transOff";
  GUnload();
}

function dataAcquire(){
  var point = new GLatLng(mapFocus.latitude, mapFocus.longitude);
  bMap.setCenter(point);
  bMap.setZoom(mapFocus.zoom);
  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;
  }
  
  http_request.onreadystatechange = function() { setSelect(http_request); };
 // window.alert('getInteractiveMapData.php?focusId=' + mapFocus.tableId +  '&focusTable=' + mapFocus.table);
  http_request.open('GET', 'getInteractiveMapData.php?focusId=' + mapFocus.tableId +  '&focusTable=' + mapFocus.table, true);
  http_request.send(null);
}

function setSelect(http_request) {
  /*var statusAlert = document.getElementById('ajaxAlert');
  if (http_request.readyState == 3) {
    statusAlert.setAttribute("class", "ajaxLoad");
  } */
  if (http_request.readyState == 4) {
   // statusAlert.setAttribute("class", "ajaxLoaded");
    if (http_request.status == 200) {
      xmldoc = http_request.responseXML;
      drawMap();
      
	}
    else {
      alert('There was a problem with the request.');
    }
  }
}

function inArray(haystack, needle){
 for(var p = 0; p < haystack.length; p++){
    if(haystack[p] == needle){
      return true;
    }
  }
  return false;
}


function drawMap(){
  
  bMap.clearOverlays();
  focusMarker = createMarker(new GLatLng(mapFocus.latitude, mapFocus.longitude), mapFocus.name, 'star', mapFocus.name);
  //window.alert('sdfsdf' + mapFocus.name);
  focusMarker.infowindow = mapFocus.name;
  bMap.addOverlay(focusMarker);
  var dataTypeParCol = xmldoc.getElementsByTagName('data');
  var dataTypePar = dataTypeParCol[0];
  var dataType = dataTypePar.childNodes;
  for(var i = 0; i < dataType.length; i++){
   // if(inArray(dataMenu, dataType[i].tagName)){
    if(dataType[i].tagName in dataMenu){
      if(dataType[i].tagName == 'hotels'){
        parseHotels();
      }
      else{
        parseOther(dataType[i].tagName.substring(0, (dataType[i].tagName.length - 1)));
      }
    }
  }
}

function filterMarkers(){
  var fa = document.forms['filterForm'].elements;
  for(var i = 0; i < fa.length; i++){
    if(fa[i].checked == true){
     //does it exist? if so do nothing, if not add it with default of 25
      if(fa[i].name in dataMenu ){
      
      }
      else{
        dataMenu[fa[i].name] = 25;
      }
    }
    else{
    //does it exist? if so delete the property, else do nothing
     if(fa[i].name in dataMenu ){
       delete dataMenu[fa[i].name]; 
     }
     else{
       
      }
    }
  }
 
  drawMap();
}

function changeMarkerCount(operand, markerCategory){
  if(markerCategory in dataMenu ){
    if(operand == 'add') dataMenu[markerCategory] = dataMenu[markerCategory] + 5;
    else if(operand == 'subtract' && dataMenu[markerCategory] > 5) dataMenu[markerCategory] = dataMenu[markerCategory] - 5;
    else {
 /*     var changeCheck = document.getElementById(markerCategory);
      changeCheck.setAttribute('checked', false);
      dataMenu[markerCategory] = dataMenu[markerCategory] - 5;
      delete dataMenu[markerCategory]; */
    }  
  }
  else if(operand == 'add'){
   /*   var changeCheck = document.getElementById(markerCategory);
      changeCheck.setAttribute('checked', true);
    dataMenu[markerCategory] = 25; */
  }
  
  drawMap();
}


function parseOther(oType){
  var dataT = xmldoc.getElementsByTagName(oType);
  var markers = [];
  var iwHtml = [];
  var lName;
 
  for(var j = 0; j < dataT.length; j++){
    if(j < dataMenu[oType + "s"]){
      lName = dataT[j].getAttribute("na");
       var html = '<div style="width: 350px; height: 228px;"><table cellpadding="0" cellspacing="3" border="0">'
       + '<tr><td colspan="2"><h3 class="blueDot">' + lName + '</h3></td></tr>'
       + '<tr valign="top"><td width="145">' + dataT[j].getAttribute("ad") + '<br/>' + dataT[j].getAttribute("ci") + ', ' + dataT[j].getAttribute("st") + ' ' + dataT[j].getAttribute("zi") + '<br />' + dataT[j].getAttribute("pn") + '</td>'
       + '<td>';
      if(dataT[j].getAttribute("ph") != ''){
        html += '<img src="' + dataT[j].getAttribute("ph") + '" width="200"/>';
      }
      else {
        html += '<img src="images/space.gif" width="200" height="120" />';
      }
      html += '</td></tr>'
       + '<tr><td colspan="2">';
      if(dataT[j].getAttribute("tx")!= ''){
        html += '<p>' +  dataT[j].getAttribute("tx")+ ' ... <a class="spBlueU" href="/' + dataT[j].getAttribute("li") + '">MORE</a></p>';
      }
      html += '</td></tr></div>';
      markers[j] = createMarker(new GLatLng(dataT[j].getAttribute("la"), dataT[j].getAttribute("lo")), lName, oType, html); //new GMarker(new GLatLng(locations[i].getAttribute("la"),locations[i].getAttribute("lo")), icon, locations[i].getAttribute("na"));
      bMap.addOverlay(markers[j]);
    }
  }
}

function parseHotels(){
  var dataT = xmldoc.getElementsByTagName('hotel');
  var markers = [];
  var iwHtml = [];
  var lName;
  //window.alert(mainFocus.amenityFilter);
  for(var j = 0; j < dataT.length; j++){
    if(j < dataMenu.hotels){
     if(mainFocus.amenityFilter != 'ampf' || dataT[j].getAttribute(mainFocus.amenityFilter) == '1'){
      lName = dataT[j].getAttribute("na");
      
      var html = '<div style="width: 350px; height: 250px;"><table cellpadding="0" cellspacing="3" border="0">'
       + '<tr><td colspan="2"><h3 class="blueDot">' + lName + '</h3></td></tr>'
       + '<tr valign="top"><td width="145"><img align="top" src="';
      if(dataT[j].getAttribute("ra") == ''){
        html += 'space';
      }
      else {
        var starStr;
        starStr = dataT[j].getAttribute("ra").substring(0, 1);
        if (dataT[j].getAttribute("ra").indexOf(.5) == -1){
          starStr += 'star';
        }
        else {
          starStr += 'andahalfstar';
        }
        if(dataT[j].getAttribute("ra") != '1'){
          starStr += 's';
        }
        html += starStr;
        
      }
      
      html += '.gif" /><br/>' + dataT[j].getAttribute("ad") + '<br/>' + dataT[j].getAttribute("ci") + ', ' + dataT[j].getAttribute("st") + ' ' + dataT[j].getAttribute("zi") + '</td>'
       + '<td>';
      if(dataT[j].getAttribute("imlg") != ''){
        html += '<img align="top" src="' + dataT[j].getAttribute("imlg") + '" width="200" />';
      }
      else {
        html += '<img src="images/space.gif" width="200" height="120" />';
      }
      html += '</td></tr>'
       + '<tr><td colspan="2">';
      if(dataT[j].getAttribute("ot")!= ''){
        html += '<p>' +  dataT[j].getAttribute("ot").substring(0, 200) + ' ... <a class="spBlueU" href="http://' + siteInfo.cname + '.' + siteInfo.domain + '/index.jsp?pageName=hotInfo&cid=' + siteInfo.cid + '&hotelID=' + dataT[j].getAttribute("bi") + '">MORE</a></p>';
      }
      else {
        html += '<a class="spBlueU" href="http://' + siteInfo.cname + '.' + siteInfo.domain + '/index.jsp?pageName=hotInfo&cid=' + siteInfo.cid + '&hotelID=' + dataT[j].getAttribute("bi") + '">Full Profile</a></p>';
      
      }
      html += '</td></tr></div>';
      markers[j] = createMarker(new GLatLng(dataT[j].getAttribute("la"), dataT[j].getAttribute("lo")), lName, 'hotel', html); //new GMarker(new GLatLng(locations[i].getAttribute("la"),locations[i].getAttribute("lo")), icon, locations[i].getAttribute("na"));
      
      
      bMap.addOverlay(markers[j]);
	  
	}
	}
  }  
} 
 /* 
  for(i = 0; i < dataType.length; i++){
    var dataPoints = xmldoc.getElementsByTagName('datum');
    var markers = [];
    var iwHtml = [];
    var lName;
    for(j = 0; j < dataPoints.length; j++){
      lName = dataPoints[j].getAttribute("name");
      markers[j] = createMarker(new GLatLng(dataPoints[j].getAttribute("latitude"), dataPoints[j].getAttribute("longitude")), lName, dataPoints[j].getAttribute("type")); //new GMarker(new GLatLng(locations[i].getAttribute("la"),locations[i].getAttribute("lo")), icon, locations[i].getAttribute("na"));
      iwHtml[j] = lName;
	  markers[j].infowindow = iwHtml[j];
	  bMap.addOverlay(markers[j]);
    }
  } */
         

function createMarker(point, name, icon, html){
  var tIcon = baseIcon;
  tIcon.image = "/images/" + icon + ".png";
  var marker = new GMarker(point, {icon:tIcon, title:name} );
  GEvent.addListener(marker, 'click', function() {
	//var text = html;
	marker.openInfoWindowHtml(html);
	
  });

  return marker;
}

function refocusMap(name, latitude, longitude, table, tableId, zoom){
  mapFocus.name = name;
  mapFocus.latitude = latitude;
  mapFocus.longitude = longitude;
  mapFocus.table = table;
  mapFocus.tableId = tableId;
  mapFocus.zoom = zoom;
  
}

function resetFocusMap(){
  mapFocus = mainFocus;
}

function openBox(eid){
  var elem = document.getElementById(eid);
  elem.style.visibility = "visible";
}

function closeBox(){
  var eid = "controlBox";
  var elem = document.getElementById(eid);
  elem.style.visibility = "hidden";
  showOpenLink();

}
function hideOpenLink(){
  var elem = document.getElementById("contBoxOpen");
  elem.style.visibility = "hidden";

}

function showOpenLink(){
  var elem = document.getElementById("contBoxOpen");
  elem.style.visibility = "visible";
 //ie bugs elem.addEventListener("click", openHide, false);

}
function openHide(){
  openBox("controlBox");
  hideOpenLink();
}

function drawWindow(){
  var focName = document.getElementById("mfName");
  var focImg  = document.getElementById("mfImage"); 
//  window.alert(mapFocus.name);
  focName.innerHTML = mapFocus.name;
  focImg.setAttribute("src", "/images/gallery/" + mapFocus.pic);
/*
  var baseEle = document.getElementById('popup');
  var span1 = document.createElement("span"); var span2 = document.createElement("span"); var span3 = document.createElement("span"); var span4 = document.createElement("span"); var span5 = document.createElement("span"); var span6 = document.createElement("span");
  var img1 = document.createElement("img"); var img2 = document.createElement("img"); var img3 = document.createElement("img"); var img4 = document.createElement("img"); var img5 = document.createElement("img"); var img6 = document.createElement("img");
  var img7 = document.createElement("img"); var img8 = document.createElement("img"); var img9 = document.createElement("img"); var img10 = document.createElement("img"); var img11 = document.createElement("img");
  var div1 = document.createElement("div"); var div2 = document.createElement("div"); var div3 = document.createElement("div");
  var tab1 = document.createElement("table");
  var label1 = document.createElement("label");
  var form1 = document.createElement("form");
  var input1= document.createElement("input"); var input2= document.createElement("input"); var input3= document.createElement("input"); var input4= document.createElement("input");
  var alert1 = document.createElement("div");
  
  span1.setAttribute("id", "pClose");
  img1.setAttribute("src", "/images/close.gif");
  var text1 = document.createTextNode("close ");
  baseEle.appendChild(span1);
  span1.appendChild(text1);
  span1.appendChild(img1);
  
  span2.setAttribute("class", "pTitle");
  var text2 = document.createTextNode("Interactive Map");
  baseEle.appendChild(span2);
  span2.appendChild(text2);
  
  
  div1.setAttribute("id", "contBoxOpen");
  div1.setAttribute("class", "cpFlash");
  var text3 = document.createTextNode("Open Control Panel");
  baseEle.appendChild(div1);
  div1.appendChild(text3);
  
  div2.setAttribute("id", "pFrame");
  baseEle.appendChild(div2);
  
  div3.setAttribute("id", "controlBox");
  baseEle.appendChild(div3);
    form1.setAttribute("id", "filterForm");
    form1.setAttribute("name", "filterForm");
    form1.setAttribute("action", "javascript:filterMarkers();");
      form1.onload = "reset";
      div3.appendChild(form1);
      form1.appendChild(tab1);
      tab1.setAttribute("style", "margin: 5px;");
      tab1.border = "0";
      tab1.cellPadding = "0";
      tab1.cellSpacing = "0";
      tab1.width = "218";
      var row0 = tab1.insertRow(0);
        var td00 = row0.insertCell(0);
        td00.setAttribute("colspan", "3");
        td00.appendChild(span3);
            var text4 = document.createTextNode("Map Control Panel");
            span3.appendChild(text4);
        var td01 = row0.insertCell(1);
          label1.setAttribute("id", "cClose");
          td01.appendChild(label1);
            img2.setAttribute("src", "/images/close.gif");
            label1.appendChild(img2);
      var row1 = tab1.insertRow(1);
        var td10 = row1.insertCell(0);
        td10.setAttribute("colspan", "4");
          td10.appendChild(span4);
            var text5 = document.createTextNode("Focused On:");
            span4.appendChild(text5);
            
  
            
        var td11 = row1.insertCell(1);
      var row2 = tab1.insertRow(2);
        var td20 = row2.insertCell(0);
        var td21 = row2.insertCell(1);
          td21.setAttribute("colspan", "3");
            td21.appendChild(span5);
            var text6 = document.createTextNode(mapFocus.name);
            span5.appendChild(text6);
        var td22 = row2.insertCell(2);
      var row3 = tab1.insertRow(3);
      row3.setAttribute("height", "75");
        var td30 = row3.insertCell(0);
        td30.setAttribute("colspan", "5");
          img3.setAttribute("src", "/images/gallery/" + mapFocus.pic);
          img3.setAttribute("width", "218");
          td30.appendChild(img3);
      var row4 = tab1.insertRow(4);
        var td40 = row4.insertCell(0);
        td40.setAttribute("colspan", "5");
          td40.appendChild(span6);
          var text7 = document.createTextNode("Display");
      var row5 = tab1.insertRow(5);
        var td50 = row5.insertCell(0);
        var td51 = row5.insertCell(1);
          input1.setAttribute("type", "checkbox");
          input1.setAttribute("checked", "true");
          input1.setAttribute("name", "hotels");
          input1.setAttribute("id", "hotels");
          if(input1.addEventListener){
            input1.addEventListener("change", filterMarkers, false); 
          }
          else if(input1.attachEvent){
            input1.attachEvent('change', filterMarkers);
          }
          td51.appendChild(input1);
            var text8 = document.createTextNode("Hotels");
            td51.appendChild(text8);
        var td52 = row5.insertCell(2);
          img4.setAttribute("src", "/images/minus.png");
          img4.setAttribute("height", "12");
          img4.onclick = function(){ changeMarkerCount('subtract', 'hotels');}
          td52.appendChild(img4);
          img5.setAttribute("src", "/images/plus.png");
          img5.setAttribute("height", "12");
          img5.onclick = function(){ changeMarkerCount('add', 'hotels');}
          td52.appendChild(img5);
        var td53 = row5.insertCell(3);
          td53.setAttribute("colspan", "2");
      var row6 = tab1.insertRow(6);
        var td60 = row6.insertCell(0);
        var td61 = row6.insertCell(1);
          input2.setAttribute("type", "checkbox");
          input2.setAttribute("name", "attractions");
          input2.setAttribute("id", "attractions");
          if(input2.addEventListener){
            input2.addEventListener("change", filterMarkers, false); 
          }
          else if(input2.attachEvent){
            input2.attachEvent('change', filterMarkers);
          }
          td61.appendChild(input2);
            var text9 = document.createTextNode("Attractions");
            td61.appendChild(text9);
        var td62 = row6.insertCell(2);
          img6.setAttribute("src", "/images/minus.png");
          img6.setAttribute("height", "12");
          img6.onclick = function(){ changeMarkerCount('subtract', 'attractions');}
          td62.appendChild(img6);
          img7.setAttribute("src", "/images/plus.png");
          img7.setAttribute("height", "12");
          img7.onclick = function(){ changeMarkerCount('add', 'attractions');}
          td62.appendChild(img7);
        var td63 = row6.insertCell(3);
          td63.setAttribute("colspan", "2");//
      var row7 = tab1.insertRow(7);
        var td70 = row7.insertCell(0);
        var td71 = row7.insertCell(1);
          input3.setAttribute("type", "checkbox");
          input3.setAttribute("name", "restaurants");
          input3.setAttribute("id", "restaurants");
          if(input3.addEventListener){
            input3.addEventListener("change", filterMarkers, false); 
          }
          else if(input3.attachEvent){
            input3.attachEvent('change', filterMarkers);
          }
          td71.appendChild(input3);
            var text10 = document.createTextNode("Restaurants");
            td71.appendChild(text10);
        var td72 = row7.insertCell(2);
          img8.setAttribute("src", "/images/minus.png");
          img8.setAttribute("height", "12");
          img8.onclick = function(){ changeMarkerCount('subtract', 'restaurants');}
          td72.appendChild(img8);
          img9.setAttribute("src", "/images/plus.png");
          img9.setAttribute("height", "12");
          img9.onclick = function(){ changeMarkerCount('add', 'restaurants');}
          td72.appendChild(img9);
        var td73 = row7.insertCell(3);
          td73.setAttribute("colspan", "2");//
      var row8 = tab1.insertRow(8);
        var td80 = row8.insertCell(0);
        var td81 = row8.insertCell(1);
          input4.setAttribute("type", "checkbox");
          input4.setAttribute("name", "golfcourses");
          input4.setAttribute("id", "golfcourses");
          if(input4.addEventListener){
            input4.addEventListener("change", filterMarkers, false); 
          }
          else if(input4.attachEvent){
            input4.attachEvent('change', filterMarkers);
          }
          td81.appendChild(input4);
            var text11 = document.createTextNode("Golf Courses");
            td81.appendChild(text11);
        var td82 = row8.insertCell(2);
          img10.setAttribute("src", "/images/minus.png");
          img10.setAttribute("height", "12");
          img10.onclick = function(){ changeMarkerCount('subtract', 'golfcourses');}
          td82.appendChild(img10);
          img11.setAttribute("src", "/images/plus.png");
          img11.setAttribute("height", "12");
          img11.onclick = function(){ changeMarkerCount('add', 'golfcourses');}
          td82.appendChild(img11);
        var td83 = row8.insertCell(3);
          td83.setAttribute("colspan", "2");
          
  var alertText1 = document.createTextNode("Loading Data");
  alert1.appendChild(alertText1);
  alert1.setAttribute("id", "ajaxAlert");
  alert1.setAttribute("class", "ajaxLoad");
  div3.appendChild(alert1);
  
  */
} 


