// JavaScript Document

$(document).ready(function() {
  // put all your jQuery goodness in here.
  var countThumbs = $('.thumbs-1 > *').length;
  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 counter =2;
  var prevlargeName = 'Large-1';
  var largeName;
  var prevX;
  var leftPos;
  var x;
  var y;
  var distance;
  var xExp;
  var blobPos;
  
   
  function imageMove() {
	  var largeName = $('#thumb-'+counter).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'});
		counter ++;
		if (counter > countThumbs) {
		counter = 1;}
	};
	
	window.setInterval(function(){imageMove();},5000);
  
  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'
      });
  
	$('.image-holder').mouseenter(function () { 
			imageWidth = $(this).children('img').attr('width');
			$(this).children('.image-description').css({
			'width' : imageWidth + 'px',
			'margin' : '0 auto'});
			$(this).children('.image-description').stop().animate({ 
			top: '-48px',
			height: '38px'
			});
	  });
	
	$('.image-holder').mouseleave(function () { 
		$(this).children('.image-description').stop().animate({ 
			top: '0px',
			height: '0px'
			});
	 });
	
	$('div.thumb').click(function () {
		largeName = $(this).children().attr("alt");
		counter = largeName.replace("Thumb ", "");
		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'});
    	});
	$("#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'});
	}
	});


