/* base.js */

/**
 * Attaches an onclick event to each link with rel="external" that opens the
 * link in a new window.
 */
function initExternalLinks() {
  var as = document.getElementsByTagName("a");
  for (var i = 0; i < as.length; i++) {
    if (as[i].getAttribute("rel") == "external") {
      as[i].onclick = openNewWindow;
    }
  }
}

/**
 * Display an image in the property detail view.
 */
function showImage(fileSrc, i, captionOn) {
  iw = fileSrc.width;
  ih = fileSrc.height;
  document.getElementById('mainImage').src = fileSrc;
  document.getElementById('mainImage').width = iw;
  document.getElementById('mainImage').height = ih;
  if (captionOn){
    showCaption(i);
  }
  orientImage(document.getElementById('mainImage'), imgList[i]);
  return false;
}

function showCaption(i) {
  var captionDiv = document.getElementById( "caption");
  captionDiv.innerHTML = "<p><strong>" + captionList[i] + "</strong></p>";
}


var timer = null;

/**
 * Recursive function to resize an image once it has loaded.
 */
function orientImage(img, thumb) {
  if (img == null || !img.complete) {
  timer = setTimeout("orientImage()", 500);
  } else {
  resize(img, thumb);
  clearTimeout(timer);
  }
}

/**
 * Resizes an image to ideal dimensions based on aspect ratio.
 */
function resize(myImage, myThumb) {
  tw = myThumb.width;
  th = myThumb.height;

  if (th > tw) { // portrait
    myImage.width = (371 * tw) / th;
    myImage.height = 371;
  } else { // landscape
    myImage.width = 495;
    myImage.height = (495 * th) / tw;
    }
}

  function openNewWindow() {
    window.open(this.href);
    return false;
  }

var LBpageAnchors = new Array();

/* method to link flash onclick call */
function runOnclickEventOn(imgID) {
 LBpageAnchors[imgID].onclick();
}

function scrapeImageData(from) {
 try {
 var iElm = from; // input element (XHTML)
 var rElm = document.createElement("thumbs"); // return element (XML for Flash)

 var tElm, tImg; // tmp

 for (var ii=0; ii<iElm.childNodes.length; ii++) {
 tElm = document.createElement("thumb");

 if (iElm.childNodes[ii].nodeName != 'LI') continue; // TO DO .... use getElementsByTagName in place of this
 tImg = iElm.childNodes[ii].getElementsByTagName("img")[0];
 tElm.setAttribute("imgsrc", tImg.getAttribute("src"));
 LBpageAnchors.push(iElm.childNodes[ii].getElementsByTagName("a")[0]);
 tElm.setAttribute("link", LBpageAnchors.length-1);
 rElm.appendChild(tElm);

 }
 var temp = document.createElement("return");
 temp.appendChild(rElm);
 return temp.innerHTML;
 } catch (e) {
 // data scrape failed, return null !!!
 return null;
 }
}

/* hide reveal collapsible content pane */

function showPane(elm) {
  el = document.getElementById(elm);

  if(el.className == 'ccp closed') {
    el.className = 'ccp open';
  } else {
    el.className = 'ccp closed';
  }

  return false;
}