if (location.host.indexOf("www.") != 0) {
  location.replace(location.protocol + "//www." + location.host + location.pathname + location.search);
}

var house_icon = new GIcon();
house_icon.image = "http://www.imapmls.com/icons/house_icon.png";
house_icon.iconSize = new GSize(24, 23);
house_icon.iconAnchor = new GPoint(6, 20);
house_icon.infoWindowAnchor = new GPoint(5, 1);

var condo_icon = new GIcon();
condo_icon.image = "http://www.imapmls.com/icons/condo_icon.png";
condo_icon.iconSize = new GSize(22, 24);
condo_icon.iconAnchor = new GPoint(6, 20);
condo_icon.infoWindowAnchor = new GPoint(5, 1);

var markerArray = {};
var infoArray = {};

function printDebug(text) {
  var debugDiv = document.getElementById("debug");

  if (debugDiv) {
    debugDiv.innerHTML += text;
  }
}

// Creates a marker whose info window displays the given number
function createMarker(point, info) {
  var marker;
  
  if (info["type"] == "house") {
    marker = new GMarker(point, house_icon);
  } else {
    marker = new GMarker(point, condo_icon);
  }

  // Show this marker's index in the info window when it is clicked
  GEvent.addListener(marker, "click", function() {
    marker.openInfoWindowHtml(info["html"]);
  });

  markerArray["MLS" + info["mlsnumber"]] = marker;
  infoArray["MLS" + info["mlsnumber"]] = info["html"];

  return marker;
}

function addMapMarkers() {
  for (var i = 0; i < results.length; i++) {
    var point = new GLatLng(parseFloat(results[i]["lat"]),
      parseFloat(results[i]["lng"]));
    map.addOverlay(createMarker(point, results[i]));
  }
}

function generateTableResultRow(table, location, cellStyle) {
  var price = location["price"];
  var bed = location["bed"];
  var bath = location["bath"];
  var city = location["city"];
  var sqFt = location["sqft"];
  var mlsNum = location["mlsnumber"];

  var rowEl = document.createElement("tr");

  // Add the MLS ID
  var cellEl = document.createElement("td");
  cellEl.className = cellStyle;
  var mlsLink = document.createElement("a");
  mlsLink.setAttribute("href", "javascript: centerAndZoomToMarker(" + "'MLS" + mlsNum + "');");
  var textNode = document.createTextNode(mlsNum);
  mlsLink.appendChild(textNode);
  cellEl.appendChild(mlsLink);
  rowEl.appendChild(cellEl);

  // Add the Price
  var cellEl = document.createElement("td");
  cellEl.className = cellStyle;
  var textNode = document.createTextNode(price);
  cellEl.appendChild(textNode);
  rowEl.appendChild(cellEl);

  // Add the Beds
  cellEl = document.createElement("td");
  cellEl.className = cellStyle;
  var textNode = document.createTextNode(bed);
  cellEl.appendChild(textNode);
  rowEl.appendChild(cellEl);

  // Add the Baths
  cellEl = document.createElement("td");
  cellEl.className = cellStyle;
  var textNode = document.createTextNode(bath);
  cellEl.appendChild(textNode);
  rowEl.appendChild(cellEl);

  // Add the Square Footage
  cellEl = document.createElement("td");
  cellEl.className = cellStyle;
  var textNode = document.createTextNode(sqFt);
  cellEl.appendChild(textNode);
  rowEl.appendChild(cellEl);

  // Add the City
  cellEl = document.createElement("td");
  cellEl.className = cellStyle;
  var textNode = document.createTextNode(city);
  cellEl.appendChild(textNode);
  rowEl.appendChild(cellEl);

  // Append the row
  table.appendChild(rowEl);
}

function generatePageControls(table) {
  var queryLink = document.location.search;
  
  var rowEl = document.createElement("tr");

  // Add the back control cell
  var cellEl = document.createElement("td");
  cellEl.setAttribute("colspan", "3");
  cellEl.setAttribute("align", "center");
  if (currentPage != 0) {
    var prevLink = document.createElement("a");
    prevLink.setAttribute("href", queryLink.replace("page=" + currentPage, "page=" + (currentPage - 1)));
    var textNode = document.createTextNode("<< Prev <<");
    prevLink.appendChild(textNode);
    cellEl.appendChild(prevLink);
  }
  rowEl.appendChild(cellEl);

  // Add the forward control cell
  var cellEl = document.createElement("td");
  cellEl.setAttribute("colspan", "3");
  cellEl.setAttribute("align", "center");
  if (lastPage != 1) {
    var nextLink = document.createElement("a");
    nextLink.setAttribute("href", queryLink.replace("page=" + currentPage, "page=" + (currentPage + 1)));
    var textNode = document.createTextNode(">> Next >>");
    nextLink.appendChild(textNode);
    cellEl.appendChild(nextLink);
  }
  rowEl.appendChild(cellEl);

  // Append the row
  table.appendChild(rowEl);
}

function centerAndZoomToMarker(id) {
  markerArray[id].openInfoWindowHtml(infoArray[id]);
}

function generateResultsTable() {
  var resultsDiv = document.getElementById("results");
  var tableEl = document.createElement("table");
  tableEl.setAttribute("id", "resultsTable");
  tableEl.className = "resultTable";
  tableEl.setAttribute("cellspacing", "0");
  var theadEl = document.createElement("thead");
  var rowEl = document.createElement("tr");

  // Add the MLS Number Header
  var cellEl = document.createElement("td");
  cellEl.className = "resultHeader";
  var textNode = document.createTextNode("MLS");
  cellEl.appendChild(textNode);
  rowEl.appendChild(cellEl);

  // Add the Price Header
  var cellEl = document.createElement("td");
  cellEl.className = "resultHeader";
  var textNode = document.createTextNode("Price");
  cellEl.appendChild(textNode);
  rowEl.appendChild(cellEl);

  // Add the Beds Header
  cellEl = document.createElement("td");
  cellEl.className = "resultHeader";
  var textNode = document.createTextNode("Beds");
  cellEl.appendChild(textNode);
  rowEl.appendChild(cellEl);

  // Add the Baths Header
  cellEl = document.createElement("td");
  cellEl.className = "resultHeader";
  var textNode = document.createTextNode("Baths");
  cellEl.appendChild(textNode);
  rowEl.appendChild(cellEl);

  // Add the Square Footage Header
  cellEl = document.createElement("td");
  cellEl.className = "resultHeader";
  var textNode = document.createTextNode("Sq. Ft.");
  cellEl.appendChild(textNode);
  rowEl.appendChild(cellEl);

  // Add the City Header
  cellEl = document.createElement("td");
  cellEl.setAttribute("class", "resultHeader");
  cellEl.className = "resultHeader";
  var textNode = document.createTextNode("City");
  cellEl.appendChild(textNode);
  rowEl.appendChild(cellEl);

  // Append the Header row
  theadEl.appendChild(rowEl);

  var tbodyEl = document.createElement("tbody");
  var i;

  if (results.length > 0) {
    // Append the location rows
    for (i=0; i < results.length; i++) {
      if (i % 2 == 0) {
        generateTableResultRow(tbodyEl, results[i], "resultRow");
      } else {
        generateTableResultRow(tbodyEl, results[i], "resultOddRow");
      }
    }
    if (hasPages) {
      generatePageControls(tbodyEl);
    }
  } else {
    var rowEl = document.createElement("tr");
    var cellEl = document.createElement("td");
    cellEl.setAttribute("colspan", "6");
    cellEl.setAttribute("align", "center");
    var textNode = document.createTextNode("No Results Found");
    cellEl.appendChild(textNode);
    rowEl.appendChild(cellEl);
    tbodyEl.appendChild(rowEl);
  }

  // Append the thead element
  tableEl.appendChild(theadEl);
  // Append the tbody element
  tableEl.appendChild(tbodyEl);
  // Append the table element
  resultsDiv.appendChild(tableEl);
}

function loadResults(){
  var resultsDiv= document.getElementById("results");
  resultsDiv.innerHTML = "";
  generateResultsTable();
  addMapMarkers();
}

function toggleSectVis(elId) {
  var togImg = document.getElementById(elId + "_img");
  var elem = document.getElementById(elId);

  if (togImg.src == "http://www.imapmls.com/images/triangle-open.gif") {
    elem.style.display = "none";
    togImg.src = "/images/triangle-closed.gif";
  } else {
    blindDown(elId, {duration: 0.75});
    togImg.src = "/images/triangle-open.gif";
  }
}

function selectTab(tab) {
  var tabs = document.getElementsByTagName("h3");
  var tabEl = document.getElementById(tab + "PanelTab");
  var i;
  var oldTab;
  var oldTabEl;
  var prevCounty;

  for (i=0; i < tabs.length; i++) {
    if (tabs[i].className == "panelTab selected") {
      oldTab = tabs[i].id;
      oldTabEl = tabs[i];
    }
  }

  if (oldTab != "mainPanelTab") {
    prevCounty = oldTab.substring(0, oldTab.indexOf("PanelTab"));
    toggleSectVis(prevCounty);
  }

  oldTabEl.className = "panelTab";
  tabEl.className = "panelTab selected";
  if (tab != "mainPanelTab") {
    toggleSectVis(tab);
  }
}
