/** coded by 
Shawn Hill
Bernstein Rein
12.10.2009
requires jQuery 1.2.6 **/

var scrollTime = 700;// millis
var thumbWidth = 82;
var slideIncrement = 5 * thumbWidth;// number of tiles to slide per click
var padding = 5;
var sliderWidth = 0;//calculated in initializer
var width = 0;//calculated in initializer
var totalThumbs = 0;//calculated in initializer
var offSet = 0;//calculated in initializer
var currentPosition = 0;//calculated in initializer
// this variable keeps track of the selected home.
var selected = '';
// this is the slide show variable
var slideshowlio = '';

// scrolls thumbs either forward or backwards (space permitting) global slideIncrement dictates how far to slide 
function scrollThumbs( movingForward ){
  currentPosition = ( movingForward ? (currentPosition + slideIncrement) : (currentPosition - slideIncrement) );
  slideThumbs();
  return false;
}

// scrolls to the featured thumbnail
function centerThumb( thumbIndex ){
  currentPosition = offSet + ((width - thumbWidth)/2) - (thumbWidth * (thumbIndex -1));
  slideThumbs();
}

// slides the thumbnails ensures boundaries are not crossed
function slideThumbs(){
  if((currentPosition + sliderWidth) < (offSet + width )){currentPosition = offSet + width - sliderWidth - padding;}
  if(currentPosition > offSet){currentPosition = offSet;}
  $('#jump-links').animate({marginLeft: currentPosition + 'px'}, scrollTime, 'swing' );
}

// overrides the page jump action
function goTo( newSection ){
  // get index number for home
  if (newSection.indexOf('#') > -1){ newSection = newSection.substr( newSection.indexOf('#')+2 ); }
  // only change if this is a different home
  if(selected != newSection){
    // show other home
    $('#home'+selected).fadeOut("slow");
    $('#home'+newSection).fadeIn("slow");
    // switch selected thumb
    $('#t'+selected).removeClass("selected");
    $('#t'+newSection).addClass("selected");
    // set the current home to selected variable
    selected = newSection;
				// recenter the thumb navigation 
    centerThumb( parseInt(selected) );
  }
}

// page initialization function
$(document).ready( function(){
  // set the width of the scroll pane and number of thumbs, offSet and slider width
  width = $('#jump-links').width();
  totalThumbs = $('#jump-links').children().length;
  offSet = currentPosition = parseInt($('#jump-links').css('margin-left'));
  sliderWidth = thumbWidth * totalThumbs;
  // hide home views
  $('#view-area > div').hide();
  // set up thumb links to change view
  $('#jump-links a').click( function(){ goTo( this.href.substr(2) ); clearTimeout( slideshowlio );});
  // activate scroll buttons
  $('#scroll-left > a').click( function(){ return scrollThumbs( true );} );
  $('#scroll-right > a').click( function(){ return scrollThumbs( false );} );
  // show first home or linked home
  goTo( ((location.href.indexOf('#') > -1) ? location.href : '1' ) );
		// start slide show
		slideshowlio = setInterval( "goTo( new String((parseInt(selected)%15) + 1) )", 8000 );
});


