/* Home page featured activites slide show */

// Set the id of the container that holds the property slides
var parentContainerId = 'featured';

// Current slide index
var featureIndex = 0;

// Array which we will populate with the elements we want to show or hide
var featureSlides = [];

// Control links call nextFeature() or previousFeature() to move slides forward or back.
function nextFeature() {
 moveFeaturedSlide(1);
}

function previousFeature() {
 moveFeaturedSlide(-1);
}

function moveFeaturedSlide(direction) {
 var newIndex = featureIndex + direction;
 //if (document.getElementById('debug')) {
  //alert("move:" + direction + ":" + featureIndex + ":" + newIndex);
 //}

 // Ignore a previous call if we're already at the first frame
 if (newIndex < 0) {
  return;
 }

 // Initialize our feature slides array if necessary
 if (featureSlides.length == 0) {
  // Start by targeting a wrapper div - we're keying in on a div with ID featured
  var content = document.getElementById(parentContainerId);

  // Get an array of all the divs within our target and iterate through each one.
  var divs = content.getElementsByTagName('div');
  for (var a = 0; a < divs.length; a++) {
   var div = divs[a];
   
   // If our div has a classname of fp (visible) or fpHidden(hidden) add them to our array of slides
   if (div.className == 'fp' || div.className == 'fpHidden') {
    featureSlides[featureSlides.length] = div;
   }
  }
 }

 // If we are already at the last slide ignore a next slide call.
 if (newIndex >= featureSlides.length) {
  return;
 }

 // Hide the slide that was previously visible
 var lastDiv = featureSlides[featureIndex];
 lastDiv.className = "fpHidden";

 // Show our newly selected slide
 var nextDiv = featureSlides[newIndex];
 nextDiv.className = "fp";
 
 // Update featureIndex so next time the function is called we'll know what frame we're on.
 featureIndex = newIndex;

 setFeatureIndexCookie();

 // Call in the next previous links and alter their class names - we can change their appearance
 // from disabled to enabled by giving these styles different colors, behavior or background images.
 // All the heavy lifting of changing appearance is done in the style sheet.
 var nextLink = document.getElementById('nextFeature');
 var prevLink = document.getElementById('previousFeature');
 nextLink.className = "";
 prevLink.className = "enabledLinkLeft";

 if (newIndex == featureSlides.length - 1) {
  nextLink.className = "disabledLinkRight";
 } else if (newIndex == 0) {  
  prevLink.className = "disabledLinkLeft";  
 }
}

function setFeatureIndexCookie() {
 var cookie = "featureIndex=" + featureIndex + "; path=/";
 document.cookie = cookie;
}

// Show previously shown slide to returning visitors
function setFeaturedSlide() {
 var cookies = document.cookie;
 var cookie = getCookie("featureIndex");
 if (cookie != null) {
  var lastIndex = parseInt(cookie);
  moveFeaturedSlide(lastIndex);
 } else {
  moveFeaturedSlide(0);
 }
}

function getCookie(name) {
  var cookieName = name + "=";
  var cookies = document.cookie.split(';');
  for(var i = 0; i < cookies.length; i++) {
    var cookie = cookies[i];                
    while (cookie.charAt(0)==' ') {
                 cookie = cookie.substring(1, cookie.length);
                }

    if (cookie.indexOf(cookieName) == 0) {
                 return cookie.substring(cookieName.length, cookie.length);
                }
  }
  return null;
}

/*
 Add rollovers to nav:
 1. add a call to this javascript file to the page head
 2. place thumbnails within a div with id 'quickNav' 
 
 <div id="quickNav"> <a href=""><img src="nav.gif"></a><a href=""><img src="nav2.gif"></a></div>
*/

var preloaded = [];

function initRollovers() {
 var container = document.getElementById('homePageNav');
 if (!container) {
  return;
 }
 
 var navImages = container.getElementsByTagName('img');
 addRollovers(navImages);
}

function addRollovers(images) {
 for (var a = 0; a < images.length; a++) {
  var img = images[a];
  
  // Calculate rollover src and preload image
  var src = img.src;
  var extIndex = src.lastIndexOf('.');
  var ext = src.substring(extIndex);
  var root = src.substring(0, extIndex);
  var rolloverSrc = root + '_over' + ext;
  
  var image = new Image();
  image.src = rolloverSrc;
  preloaded[a] = image;
  
  // Reset mouseover/mouseout to perform rollover
  var mouseover = 'this.src = "' + rolloverSrc + '";';
  img.onmouseover = new Function(mouseover);
  
  var mouseout = 'this.src = "' + src + '"';
  img.onmouseout = new Function(mouseout);

 }
}

// Initialize rollovers and image preload on page load
if (window.addEventListener){
  window.addEventListener("load", initRollovers, false);
} else if (window.attachEvent){
  window.attachEvent("onload", initRollovers);
}