// JavaScript Document

$(document).ready(function() {
  // put all your jQuery goodness in here.
  var countThumbs = $('.thumbs-1 > *').length +1;
  var thumbWidth = 0;
  var maxThumbLeft = 0;
  var thumbPos;
  var count=1;
  var imageWidth;
  var imageHeight;
  var topPosition =0;
  var imagePosition = 0;
  var thumbArray = new Array(countThumbs);
  var prevlargeName = 'Large-1';
  var prevX;
  var leftPos;
  var x;
  var y;
  var distance;
  var xExp;
  var blobPos;
  
	imageHeight = $(".Large-1").children('img').attr('height');
	topPosition = (500 - imageHeight) / 2;
		
	$(".Large-1").css({
	'top' : topPosition + 'px'});
  
  for (var i=0; i<countThumbs; i++) {
	  thumbArray[i] = $('#thumb-'+count).width() + 9;
	  thumbWidth = thumbWidth + thumbArray[i];
	  count++;
  }
  
  $('.Large-1').addClass('rolled');
  
	maxThumbLeft = thumbWidth - 930;
	maxThumbLeft = -maxThumbLeft;  
  
	$('#move-thumbs').animate({ 
        width: thumbWidth + 'px'
      });
	
	showDescription('Large-1');
	
	function showDescription(e) {
			imageWidth = $('.'+e).children('img').attr('width');
			$('.'+e).children('.image-description').css({
			'width' : imageWidth + 'px',
			'margin' : '0 auto'});
			$('.'+e).children('.image-description').stop().animate({ 
			top: '-48px',
			height: '38px'
			});
	};
	
	function removeDescription(e) {
		$('.'+e).children('.image-description').stop().animate({ 
			top: '0px',
			height: '0px'
			});
	}
	
	$('div.thumb').click(function () {
		removeDescription(prevlargeName);
		var largeName = $(this).children().attr("alt");
		largeName = largeName.replace("Thumb ", "Large-");		
		if($("." + largeName).hasClass('rolled')){
		} else {
			$('.image-holder').removeClass('rolled');
			$('.image-holder').fadeOut('slow');
			$("." + largeName).fadeIn('slow');
			$("." + largeName).addClass('rolled');
		}
		
		imageHeight = $("." + largeName).children('img').attr('height');
		topPosition = (500 - imageHeight) / 2;
		
		$("." + largeName).css({
			'top' : topPosition + 'px'});
		prevlargeName = largeName;
		showDescription(largeName);
    });
	
	$("#property-thumbs").mousemove(function(e){
		x = e.pageX - this.offsetLeft;
		y = e.pageY - this.offsetTop;
		
		if(x > 565) {
			xExp = (x-565) / 150;
			distance = Math.exp(xExp);
			distance = Math.round(distance);
		} else if(x <365) {
			xExp = (0-x) / 150;
			xExp = xExp + 2.5;
			distance = Math.exp(xExp);
			distance = Math.round(distance);
		}
   });
	
	setInterval(movePosition,40);

	function movePosition() {
		leftPos = $('#move-thumbs').css('left').replace('px','');
		if (x < 365 || x > 565) {
			if (x <365 && leftPos <0) {
					$('#move-thumbs').css({
								  left: (leftPos - -distance) + 'px'
								  });
			}
			if (x >565 && leftPos > maxThumbLeft) {
					$('#move-thumbs').css({
										  left: (leftPos - distance) + 'px'
										  });
			}
		}
		prevX = x;
		blobPos = 930 * ((-leftPos) / (-maxThumbLeft));
		$('#blob').css({'left':(blobPos+15)+'px'});
	}

});


