/* Home page featured activites slide show */

// Set the id of the container that holds the property slides
var parentContainerId = 'client_views';

// 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;
 
 // 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;
}

// Detail page thumbnail rollovers

var preloadedDetailImages;

function setMainImageCaptions(src, newActiveImage, width, height, caption) {
 var imageContainer = document.getElementById("mainImage");
 var imageCode = getImageCodeCaptions(src, width, height, caption);
 imageContainer.innerHTML = imageCode;

 var container = document.getElementById("detailPhotos");

 var images = container.getElementsByTagName('img');
 for (var a = 0; a < images.length; a++) {
  var image = images[a];
  if (image.className == 'detailThumbActive') {
    image.className = 'detailThumb';
  }

  if (image.className == 'detailThumb') {
   if (image == newActiveImage) {
    image.className = 'detailThumbActive';
   } 
  }
 }
}

function preloadImages(imagePaths) {
 preloadedDetailImages = new Array();
 for (var a = 0; a < imagePaths.length; a++) {
  var img;
  img = new Image();
  img.src = imagePaths[a];
  preloadedDetailImages[preloadedDetailImages.length] = img;
 }
}

var shouldScaleUp = false;
var borderTop = "#fff";
var borderRight = "#fff";
var borderBottom = "#fff";
var borderLeft = "#fff";

function getImageCodeCaptions(src, imageWidth, imageHeight, caption) {

/*
 var width = 432;
 var height = 288;
 var minHeight = 288;

  if (shouldScaleUp && imageWidth < width && imageHeight < minHeight) {
   var verticalScale = minHeight / imageHeight;
   var scale = width / imageWidth;
   
   if (verticalScale < scale) {
    scale = verticalScale;
   }
   
   imageWidth *= scale;
   imageHeight *= scale;     
  }  

  if (imageWidth > width) {   
   var scale = width / imageWidth;
   imageWidth *= scale;
   imageHeight *= scale;
  }

  if (imageHeight > minHeight) {
   //var scale = minHeight / imageHeight;
   //imageWidth *= scale;
   //imageHeight *= scale;
  }

  var style = '';


  if (imageWidth < width) {
   var diff = width - imageWidth;
   var marginLeft = diff / 2;
   var marginRight = width - imageWidth - marginLeft;
   style = "border-left: " + marginLeft + "px solid " + borderLeft + "; border-right: "
       + marginRight + "px solid " + borderRight + "; ";
  }

  if (imageHeight < height) {
   var diff = height - imageHeight;
   var marginTop = diff / 2;
   var marginBottom = height - imageHeight - marginTop;
   style += "border-top: " + marginTop + "px solid " + borderTop + "; border-bottom: "
       + marginBottom + "px solid " + borderBottom + "; ";

  }

 caption = caption.replace('"', '&ldquo');
 caption = caption.replace('"', '&rdquo');

 var imgCode = '<img src="' + src + '" width="' + imageWidth + '" height="' + imageHeight + '" style="' + style + '" title="' + caption + '" alt="' + caption + '" />';
*/

var imgCode = '<img src="' + src + '" width="' + imageWidth + '" height="' + imageHeight + '" />';
 return imgCode;
}

// Reset frame position and scroll to top on iframe pages on reload to make sure browser returns to top of page.
function resetFramePos() {
 var iframe = document.getElementById('outerFrame');
 if (iframe) {
  iframe.scrollTop = 0; 
 }
 window.scroll(0, 0);
}

