
// Global variables
var slide_array = new Array();
var bottom_array = new Array();
var current_index = 0;
var slide_milliseconds = 800;
var rotate_milliseconds = 6800;
var rotate_timer = null;
var is_rotating = false;

// Setup Google Map after the page is loaded
jQuery.noConflict();
jQuery(document).ready(function($) {
  
  // Homepage Stay in Touch form
  // Add/remove default value when appropriate
  if (jQuery('input.stay-input').attr('value') == '') {
    jQuery('input.stay-input').attr('value', 'email');
  }
  jQuery('input.stay-input').focus(function() {
    if (jQuery(this).attr('value') == 'email') {
      jQuery(this).attr('value', '');
    }
  });
  jQuery('input.stay-input').blur(function() {
    if (jQuery(this).attr('value') == '') {
      jQuery(this).attr('value', 'email');
    }
  });
  
  
  // Populate an array of images to rotate
  jQuery('div.slideshow-images img').each(function(index) {
    slide_array.push(this);
  });
  jQuery('div.bottom-content div.bottom-slide').each(function(index) {
    bottom_array.push(this);
  });
  
  // IF there are multiple images, then rotate them
  if (slide_array.length > 1) {
    is_rotating = true;
    
    // Initial css setup
    jQuery('div.slideshow-btn-left').css('display','block');
    jQuery('div.slideshow-btn-right').css('display','block');
    
    // Start the timer
    rotate_timer = setTimeout('rotateFromRight()', rotate_milliseconds);
    
    
    //===========================================================================
    // LEFT arrow click
    //===========================================================================
    jQuery('div.slideshow-btn-left').click(function(e) {
      e.stopImmediatePropagation();
      rotateFromLeft();
    });
    
    
    //===========================================================================
    // RIGHT arrow click
    //===========================================================================
    jQuery('div.slideshow-btn-right').click(function(e) {
      e.stopImmediatePropagation();
      rotateFromRight();
    });
    
    
    //===========================================================================
    // HIDE button click
    //===========================================================================
    jQuery('div.slideshow-btn-hide').click(function(e) {
      e.stopImmediatePropagation();
      jQuery('div.slideshow-btn-hide').css('display','none');
      jQuery('div.slideshow-btn-show').css('display','block');
      jQuery('div.slideshow-bottom').animate({height: '18px'}, 'fast');
      jQuery('div.bottom-content').fadeOut('fast');
    });
    
    
    //===========================================================================
    // SHOW button click
    //===========================================================================
    jQuery('div.slideshow-btn-show').click(function(e) {
      e.stopImmediatePropagation();
      jQuery('div.slideshow-btn-show').css('display','none');
      jQuery('div.slideshow-btn-hide').css('display','block');
      jQuery('div.slideshow-bottom').animate({height: '132px'}, 'fast');
      jQuery('div.bottom-content').fadeIn('fast');
    });
    
  } // end if there are multiple images
  
});


//===========================================================================
// rotateFromLeft()
//===========================================================================
function rotateFromLeft() {
  
  // Cancel current timer, and restart
  clearTimeout(rotate_timer);
  rotate_timer = setTimeout('rotateFromLeft()', rotate_milliseconds);
  
  // Determine previous slide
  var previous_index = (current_index > 0) ? (current_index - 1) : (slide_array.length - 1) ;
  
  // Determine appropriate position of previous slide, and set it
  var current_left = parseFloat(jQuery(slide_array[current_index]).css('left'));
  
  // Only slide from the left if it is not already in the process of sliding from the left. :)
  if (current_left >= 0) {
    var previous_left = (current_left - 1020) + 'px';
    jQuery(slide_array[current_index]).stop();
    jQuery(slide_array[previous_index]).stop().css('left', previous_left);
    
    // Start the animation
    jQuery(slide_array[current_index]).animate({left: (1020 + 'px')}, slide_milliseconds);
    jQuery(slide_array[previous_index]).animate({left: '0px'}, slide_milliseconds);
    
    // Fade In/Out the bottom bar
    jQuery(bottom_array[current_index]).stop().css('z-index', '30').fadeOut(slide_milliseconds);
    jQuery(bottom_array[previous_index]).stop().css('z-index', '31').fadeIn(slide_milliseconds);
    
    current_index = previous_index;
  }
} // end rotateFromLeft()


//===========================================================================
// rotateFromRight()
//===========================================================================
function rotateFromRight() {
  
  // Cancel current timer, and restart
  clearTimeout(rotate_timer);
  rotate_timer = setTimeout('rotateFromRight()', rotate_milliseconds);
  
  // Determine the next slide
  var next_index = current_index + 1;
  if (next_index >= slide_array.length) {
    next_index = 0;
  }
  
  // Determine the current position of the current image
  var current_left = parseFloat(jQuery(slide_array[current_index]).css('left'));
  
  // Only slide from the right if it is not already in the process of sliding from the right. :)
  if (current_left <= 0) {
    var next_left = (current_left + 1020) + 'px';
    jQuery(slide_array[current_index]).stop();
    jQuery(slide_array[next_index]).stop().css('left', next_left);
    
    // Start the animation
    jQuery(slide_array[current_index]).animate({left: ('-' + 1020 + 'px')}, slide_milliseconds);
    jQuery(slide_array[next_index]).animate({left: '0px'}, slide_milliseconds);
    
    // Fade In/Out the bottom bar
    jQuery(bottom_array[current_index]).stop().css('z-index', '30').fadeOut(slide_milliseconds);
    jQuery(bottom_array[next_index]).stop().css('z-index', '31').fadeIn(slide_milliseconds);
    
    current_index = next_index;
  }
} // end rotateFromRight()



