var pathToImages = "pix/";
var pathToQTVR = "media/"; // relative to this script
var defaultMainImage = "";
var storedHTML = "";
var QTVRloaded = false;

function startUp(functionToCallOnLoad) {

  // store any existing onLoad event:
  var oldFunction = window.onload;
  
  // if there's not already an onLoad event function, set the new one:
  if (typeof window.onload != 'function') {
    window.onload = functionToCallOnLoad;
  } 
  
  // otherwise create a new function to trigger the old event and the new one:
  else {
    window.onload = function() {
      oldFunction();
      functionToCallOnLoad();
    }
  }
}

function parseLinks() {

	// parse the document's <a> tags looking for any with rel="external" //
	// then set the target to _blank so they open in a new window        //
	
	// abort if the browser has insufficient DOM support;
  	if (!document.getElementsByTagName) return false;
  	
  	// get a list of the <a> tags in the document:
	var links = document.getElementsByTagName("a");
	
	// loop through the list:
	for (var i = 0; i < links.length; i++) {
		var thisLink = links[i];
		if (thisLink.getAttribute("rel") == "external" && thisLink.getAttribute("href")) {
			thisLink.target = "_blank";
		}
	}
}

function swapImage(image) {
  
  // abort of the start_main element isn't present:
  if (!document.getElementById("start_main")) return false;
  
  // get the div containing the image:
  var wrapper = document.getElementById("start_main");  
    
  // if storedHTML is set (i.e. a QTVR has been played) restore the image:
  if (storedHTML || storedHTML != "") {
    wrapper.innerHTML = storedHTML;
    storedHTML = "";
  }

  // if start_main contains a flash movie, replace it with an img tag:
  if (document.getElementById("main_flash")) {
  	wrapper.innerHTML = '<img src="" alt="" id="main_image" />';
  }
  
  // unset QTVR loaded flag:
  QTVRloaded = false;
  
  // abort of the main_image element isn't present:
  if (!document.getElementById("main_image")) return false;
  
  // add the images path:
  image = pathToImages + image;
  
  // get the main_image element:
  var main_image = document.getElementById("main_image");
  
  // set its image src attribute to the thumbnail's title:
  main_image.setAttribute("src",image);
  
  // ensure image is visible:  
  main_image.style.visibility = 'visible';  
  
  // exit:
  return false;
}

function showQTVR(filename) {    
  
  // abort if QTVR is already loaded:
  if (QTVRloaded) {
  	return;
  }
 
  // abort of the small_main_image element isn't present:
  if (!document.getElementById("start_main")) return false;
  
  // add the qtvr path:
  qtvr = pathToQTVR + filename;
  
  // get the main_image element:
  var wrapper = document.getElementById("start_main");    
    
  // get and store html for image element, if it's not already populated:
  if (!storedHTML || storedHTML == "") {
    storedHTML = wrapper.innerHTML;
  }

  // if start_main contains a flash movie, replace it with an img tag:
  if (document.getElementById("main_flash")) {
  	wrapper.innerHTML = '<img src="" alt="" id="main_image" />';
  }
  
  // get the main_image element:
  var main_image = document.getElementById("main_image");
  
  // hide image is visible:  
  main_image.style.visibility = 'hidden';
  
  // construct HTML to insert applet element:
    wrapper.innerHTML = '<applet code="PurePlayerPro" width="591" height="208" archive="' + pathToQTVR + 'PurePlayerPro.jar"><param name="panorama" value="' + qtvr + '"><param name="BackgroundColor" value="#ffffff"></applet>';
  
  // set flag to indicate QTVR is loaded:
  QTVRloaded = true;   
    
  // exit:
  return false;
}

function setUpThumbnails() {

  // abort if the browser has insufficient DOM support;
  if (!document.getElementsByTagName) return false;
  if (!document.getElementById) return false;
  
  // abort if the thumbnails div isn't present:
  if (!document.getElementById("thumbnails")) return false;

  // abort of the main_image element isn't present:
  if (!document.getElementById("main_image") && !document.getElementById("main_flash")) return false;
  
  // store the default main image:
  //defaultMainImage = document.getElementById("main_image").getAttribute("src");
  
  // get the 'thumbnails' div:
  var thumbnails = document.getElementById("thumbnails");

  // get all the divs within the thumbnails div:
  var buttons = thumbnails.getElementsByTagName("div");
  
  // loop through the divs and add event handlers to each:
  for (var i=0; i < buttons.length; i++) {
    
    // get the thumbnail's title attribute (used to give the image filename):
    var title = buttons[i].getAttribute("title");
    
    // do not proceed if the title is empty:
    if (title == "" || !title) {
    	continue;
    }
    
    // set the cursor to act like a link:
    buttons[i].style.cursor = "pointer";
    
    // extract the first word of the title (used to indicate the file type):
    var type = title.substring(0, title.indexOf("_"));
    
    // store the title with the button object:
    buttons[i].src = title;
    
    
    // add onclick event handler, depending on image file type:
    switch (type) {
      case "qtvr":
      
      	// set the thumbnail's title to something more meaningful:
    	buttons[i].setAttribute("title", "Click to see virtual tour");
    
    	// add the event handler
        buttons[i].onclick = function() {
          return showQTVR(this.src);
        }
        break;
      case "image":     
      
      	// set the thumbnail's title to something more meaningful:
		buttons[i].setAttribute("title", "Click to enlarge image");	 
		
		// add the event handler:
		buttons[i].onclick = function() {
		  return swapImage(this.src);
		}
		break;
    }
  }
}

function getDatesForForm() {

	var myOptions = "";
	
	// get today's date:
	var myDate = new Date();
	
	// get day:
	var myDay = myDate.getDate();
	
	// set the date to tomorrow:
	myDate.setDate(myDay + 1);
	
	// now extract the day/month/year parts:
	myDay = myDate.getDate();
	var myMonth = 1 + myDate.getMonth();
	var myYear  = myDate.getFullYear();
		
	parseSelectField(document.getElementById("arrival_day"), myDay);
	parseSelectField(document.getElementById("arrival_month"), myMonth);
	parseSelectField(document.getElementById("arrival_year"), myYear);
	
	// set the departure date to the next day:
	//myDate.setDate(myDay + 1);
	//myDay   = myDate.getDate();
	//myMonth = 1 + myDate.getMonth();
	//myYear  = myDate.getFullYear();
		
	//parseSelectField(document.getElementById("departure_day"), myDay);
	//parseSelectField(document.getElementById("departure_month"), myMonth);
	//parseSelectField(document.getElementById("departure_year"), myYear);
}

function parseSelectField(myField, myValue) {
	
	// loop through passed field options and set the one with value=myValue to selected:
	for (var i = 0; i < myField.options.length; i++) {
		var thisOption = myField.options[i];
		if (thisOption.value == myValue) {
			myField.selectedIndex = i;
		}
	}
}

startUp(setUpThumbnails);
startUp(getDatesForForm);
startUp(parseLinks);