// load a bunch of functions that will execute when the page is done loading
function addLoadEvent(func) {
  var oldonload = window.onload;
  if (typeof window.onload != 'function') {
    window.onload = func;
  } else {
    window.onload = function() {
      if (oldonload) {
        oldonload();
      }
      func();
    }
  }
}

// get gallery links
// addLoadEvent(preload_gallery);
addLoadEvent(prepareGallery);

// liberally adapted from an ALA article
// http://alistapart.com/articles/behavioralseparation

// change the image named "big_photo" with whichever thumbnail is clicked.
// use the thumbnail links href for the replacement src
function prepareGallery(){
	// check to make sure the browser supports DOM
	if(document.getElementById && document.getElementsByTagName){
		if(document.getElementById('photo_thumbs')){
			var thumbs = document.getElementById('photo_thumbs');
			var links = thumbs.getElementsByTagName('a');
			for(var i = 0; i < links.length; i++){
				links[i].onclick = function(){
					return showPic(this);
				};
			}
		}
	}
}

function showPic (whichpic) {
	if (document.getElementById) {
		document.getElementById('big_photo').src = whichpic.href;
		// insert titles, or this won't work
		if (whichpic.title) {
			document.getElementById('photo_caption').childNodes[0].nodeValue = whichpic.title;
		} else {
			document.getElementById('photo_caption').childNodes[0].nodeValue = whichpic.childNodes[0].nodeValue;
		}
		return false;
	} else {
		return true;
	}
}

// CMX form for Reservations
if( document.addEventListener ) document.addEventListener( 'DOMContentLoaded', cmxform, false);

function cmxform(){
  // Hide forms
  $('form').hide().end();

  // Processing
  $('form').find('label').not('.nocmx') .each( 
  function( i ){
    var labelContent = this.innerHTML;
    var labelWidth = document.defaultView.getComputedStyle(this, '').getPropertyValue('width');
    var labelSpan = document.createElement('span');
        labelSpan.style.display = 'block';
        labelSpan.style.width = labelWidth;
        labelSpan.innerHTML = labelContent;
    this.style.display = '-moz-inline-box';
    this.innerHTML = null;
    this.appendChild( labelSpan );
  } 
  ).end();

  // Show forms
  $('form').show().end();
}