// Enhanced ABQ Relators Script
// version 0.5 BETA
// 2007-05-22
//
// Copyright (c) 2007 Skismo, Charlie Snider, and Matthew Bohnsack
// http://skismo.com/
// http://pullmonkey.com/
// http://bohnsack.com/
//
// Released under the GPL license
// http://www.gnu.org/copyleft/gpl.html
//
// --------------------------------------------------------------------
// This is a Greasemonkey user script: http://www.greasespot.net/
// --------------------------------------------------------------------
//
// ==UserScript==
// @name          Enhanced ABQ Realtors
// @namespace     http://skismo.com/
// @description   Script to enhance the ABQ Realtors website
// @include       http://www.abqrealtors.com/*
// @include       http://abqrealtors.com/*
// ==/UserScript==

window.helloworld = function () {
  alert('Hello World!');
}

var href = window.location.href;

if(href.match("homefinder/detail.asp")) {
  detail();
} else if(href.match("homefinder/list.asp")) {
  list(); 
}

return;

function detail() {

  var imgsize = 64;

  // find all pictures for page
  select_box = document.getElementsByName('s')[0];
  images     = select_box.options;

  var mydiv  = document.createElement("div");
  var myhtml = '';
  for(var i=0; i < images.length; i++) {
    myhtml = myhtml + 
    '<img src="' + images[i].value + '" ' +
    'width="'  + imgsize + '" ' + 'height="' + imgsize + '" ' +
    'onclick="document.getElementsByName(\'im\')[0].src = \'' + 
              images[i].value + '\';" ' +
    'onmouseover="document.getElementsByName(\'im\')[0].src = \'' + 
              images[i].value + '\';" ' +
    'style="padding: 5px;" '
    '/>\n';
  }
  mydiv.innerHTML = myhtml;
  document.getElementsByName('f')[0].appendChild(mydiv);

  // hide the select box and in its place display thumbnails
  parent_of_select_box = select_box.parentNode;
  parent_of_select_box.removeChild(select_box);

  // Price and price per square foot
  var tds = document.getElementsByTagName('td');

  if(tds) {
    formatted_price = tds[6].getElementsByTagName('font')[0].innerHTML;
    price = formatted_price.replace(/,/g,'');  // remove commas
    price = price.replace(/\$/g,'');           // remove dollar signs

    area  = tds[14].getElementsByTagName('font')[0].innerHTML;

    price_per_square_foot = Math.ceil(100*(price / area))/100;

    font_element = tds[6].getElementsByTagName('font')[0];
    font_element.innerHTML = 
    formatted_price + " ($" + price_per_square_foot + "  / ft^2)";
  }

  // change mapquest link to google maps
  // google maps link looks like this:
  // http://maps.google.com/?q=9909%20PINOT%20NOIR%20AVE%20albuquerque%20nm%2087121
  // where q is the address, city, state, zip
  map_quest_node = tds[22];
  map_quest_node = map_quest_node.getElementsByTagName('div')[0];
  map_quest_node = map_quest_node.getElementsByTagName('b')[0];
  map_quest_link = map_quest_node.getElementsByTagName('font')[0];
  // use regex to capture parentheses
  if(map_quest_link.innerHTML.match(/address=(.*)&amp;city=(.*)&amp;state=(.*)&amp;zipcode=(.*)&amp;country=(.*)&amp;/)){
	  var re = /^.*address=(.*)&amp;city=(.*)&amp;state=(.*)&amp;zipcode=(.*)&amp;country=(.*)&amp;.*$/;
	  var address = map_quest_link.innerHTML.replace(re,"$1");
	  var city = map_quest_link.innerHTML.replace(re,"$2");
	  var state = map_quest_link.innerHTML.replace(re,"$3");
	  var zip = map_quest_link.innerHTML.replace(re,"$4");
	  var country = map_quest_link.innerHTML.replace(re,"$5");
	  map_quest_link.innerHTML  = "<a href=\"http://maps.google.com?q=" + 
	 	address + ", " + city + ", " + state + ", " + zip +
	    "\">Google Map</a>";
  }
}

function list() {
  // nothing yet, but it would be cool to show the price per square foot in
  // every row of the search results
  trs = document.getElementsByTagName('tr');
  for(var i=2; i < trs.length; i++) {
  	tr = trs[i];
	tds = tr.getElementsByTagName('td');
	
	price_node = tds[0];
	area_node  = tds[5];

	// wanted to traverse the tree with childNodes but read it was not a 
	// very good crossbrowser solution
	price_node        = price_node.getElementsByTagName('div')[0];
	price_node        = price_node.getElementsByTagName('font')[0];
	formatted_element = price_node.getElementsByTagName('b')[0];
	formatted_price   = formatted_element.innerHTML;
	
	price = formatted_price.replace(/,/g,'');  // remove commas
	price = price.replace(/\$/g,'');           // remove dollar signs

	area_node = area_node.getElementsByTagName('div')[0];
	area      = area_node.getElementsByTagName('font')[0].innerHTML;

	price_per_square_foot = Math.ceil(100*(price / area))/100;

	// print result to first column
	formatted_element.innerHTML = formatted_element.innerHTML + 
		"<br/> ($" + 
		price_per_square_foot + 
		" / ft^2)";
  }
}
