Prototype.Browser.IE6 = Prototype.Browser.IE && parseInt(navigator.userAgent.substring(navigator.userAgent.indexOf("MSIE")+5)) == 6;

function externalLinks() {
  if (!document.getElementsByTagName) return;
  var anchors = document.getElementsByTagName("a");
  for (var i = 0; i < anchors.length; i++) {
    var anchor = anchors[i];
    if (anchor.getAttribute("href") && anchor.getAttribute("rel") == "external") anchor.target = "_blank";
  }
}

window.onload = externalLinks;

var quickSearchForm = $("quick_search");
if (quickSearchForm) {
  function updateQuickSearchForm() {
    var value = $("property_type").value.match(/RENTAL/) ? "_blank" : "";
    quickSearchForm.writeAttribute("target", value);
  }
  updateQuickSearchForm();
  $("property_type").observe("change", updateQuickSearchForm);
}

if ($("large") && $("thumbs")) {
  var large = $("large");
  var thumbs = $("thumbs");
  thumbs.observe("click", function(e) {
    var img1 = e.findElement("img");
    var img2 = large.down("img");
    if (img1 && img2) {
      thumbs.insert(img2);
      large.insert(img1);
      if (Prototype.Browser.IE6) {
        var ratio1 = img1.getWidth() / img1.getHeight();
        var ratio2 = img2.getWidth() / img2.getHeight();
        var style1 = "width:" + Math.floor(img1.getWidth() / img1.getHeight() * large.getHeight()) + "px;height:" + large.getHeight() + "px";
        var style2 = "width:" + img1.getWidth() + "px;height:" + Math.floor(img2.getHeight() / img2.getWidth() * img1.getWidth()) + "px";
        img1.setStyle(style1);
        img2.setStyle(style2);
      }
    }
  });
}
