/** coded by 
Shawn Hill
Bernstein Rein
12.10.2009
requires jQuery 1.2.6 **/
/** edited by 
Benjamin Myers
Clayton Homes, Inc.
10.25.2010
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 = '';
// number of divs holding images
var slideCount = $("#view-area").children('div.home-view').length;
// this is the slide show variable
var slideshowlio = '';
var isShowPaused = "no";
var selectedNum = "";
var newSelected = "";
var cntrlImgPath = "/display/sehomes/images/gallery/imageView/"

// 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 ){
  // 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.id.substr(1) ); 
	  	clearTimeout( slideshowlio );
		if(isShowPaused == 'no'){
			clearInterval(slideshowlio);
			$("#playPause").attr('src', cntrlImgPath+'play-control-play.png');
			isShowPaused = 'yes';
		}
		else if(isShowPaused == 'yes'){
			$("#playPause").attr('src', cntrlImgPath+'play-control-pause.png');
			slideshowlio = setInterval( "goTo( new String((parseInt(selected)%"+slideCount+") + 1) )", 8000 );
			isShowPaused = 'no';
		}
		else{}
  });
  // 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(1);
  // start slide show
  slideshowlio = setInterval( "goTo( new String((parseInt(selected)%"+slideCount+") + 1) )", 8000 );
});

$("#playPause").click(function(){
	if(isShowPaused == 'no'){
		clearInterval(slideshowlio);
		$("#playPause").attr('src', cntrlImgPath+'play-control-play.png');
		isShowPaused = 'yes';
	}
	else if(isShowPaused == 'yes'){
		$("#playPause").attr('src', cntrlImgPath+'play-control-pause.png');
		slideshowlio = setInterval( "goTo( new String((parseInt(selected)%"+slideCount+") + 1) )", 8000 );
		isShowPaused = 'no';
	}
	else{}
});

$("#playPause").mouseover(function(){
	if(isShowPaused == 'no'){
		$("#playPause").attr('src', cntrlImgPath+'play-control-pause-ovr.png');
	}
	else{
		$("#playPause").attr('src', cntrlImgPath+'play-control-play-ovr.png');
	}
}).mouseout(function(){
	if(isShowPaused == 'no'){
		$("#playPause").attr('src', cntrlImgPath+'play-control-pause.png');
	}
	else{
		$("#playPause").attr('src', cntrlImgPath+'play-control-play.png');
	}
});

$("#back").mouseover(function(){
	$("#back").attr('src', cntrlImgPath+'play-control-back-ovr.png');
}).mouseout(function(){
	$("#back").attr('src', cntrlImgPath+'play-control-back.png');
});

$("#forward").mouseover(function(){
	$("#forward").attr('src', cntrlImgPath+'play-control-forward-ovr.png');
}).mouseout(function(){
	$("#forward").attr('src', cntrlImgPath+'play-control-forward.png');
});

$("#back").click(function(){
	if(selected != 1){
		selectedNum = parseInt(selected);
		selectedNum--;
		newSelected = String(selectedNum);
		goTo(newSelected);
	}
	else{}
});

$("#forward").click(function(){
	if(selected != slideCount ){
		selectedNum = parseInt(selected);
		selectedNum++;
		newSelected = String(selectedNum);
		goTo(newSelected);
	}
	else{}
});


