// JavaScript Document

var zoomOptions = {
	zoomWidth: 377,
	zoomHeight: 377,
	zoomType:'reverse',
	xOffset:55,
	position: 'right'
}
var alreadyZoomed = [];
function runZoom(num) {
	if (num>0 && typeof alreadyZoomed[num] =='undefined') {
		$("#largeAnchor-"+num).jqzoom(zoomOptions);
		alreadyZoomed[num] = true;
	}
}
$(document).ready(function() {
// product page, reset the style dropdown to -select size-
	if ($('#product-size').length>0) $('#product-size').attr('value','');
	
  // put all your jQuery goodness in here.
  var countThumbs = $('.thumbs > *').length;
  
  var thumbWidth = 0;
  var maxThumbLeft = 0;
  var thumbPos;
  var count=0;
  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;
  
	imageHeight = $(".Large-1").children('img').attr('height');
	topPosition = (420 - imageHeight) / 2;
	/*$(".Large-1").css({
	'top' : topPosition + 'px'});*/
  
  for (var i=0; i<countThumbs; i++) {
	  thumbArray[i] = $('#thumb-'+count).width();
	  thumbWidth = thumbWidth + thumbArray[i];
	  count++;
  }
  
  $('.Large-1').addClass('rolled');
  
	maxThumbLeft = thumbWidth - 678;
	maxThumbLeft = -maxThumbLeft;  
  
	$('#move-thumbs').animate({ 
        width: thumbWidth + 'px'
      });
	
	function removeDescription(e) {
		$('.'+e).children('.image-description').stop().animate({ 
			top: '0px',
			height: '0px'
			});
	}
	
	$('.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');
			$(".mainImage").hide();
			$("." + largeName).stop(true,true).hide().fadeIn('slow');
			//$("." + largeName).addClass('rolled');
		//}
		
		imageHeight = $("." + largeName).find('img').attr('height');
		topPosition = Math.round(($(".right-product-holder").height() - imageHeight) / 2);
		
		$("." + largeName).css({
			'top' : topPosition});
		prevlargeName = largeName;
		//showDescription(largeName);
		var id = this.id;
		var data = id.split("-");
		var num = data[1]

		runZoom(num);
    });
	
	$("#property-thumbs").mousemove(function(e){
		x = e.pageX - this.offsetLeft;
		y = e.pageY - this.offsetTop;
		
		if(x > 289) {
			xExp = (x-289) / 150;
			distance = Math.exp(xExp);
			distance = Math.round(distance);
		} else if(x <319) {
			xExp = (0-x) / 150;
			xExp = xExp + 2.5;
			distance = Math.exp(xExp);
			distance = Math.round(distance);
		}
   });
	
	setInterval(movePosition,40);

	function movePosition() {
		if ($('#move-thumbs').length>0) {
			leftPos = $('#move-thumbs').css('left').replace('px','');
			if (x < 319 || x > 278) {
				if (x <319 && leftPos <0) {
						$('#move-thumbs').css({
									  left: (leftPos - -distance) + 'px'
									  });
				}
				if (x >289 && leftPos > maxThumbLeft) {
						$('#move-thumbs').css({
											  left: (leftPos - distance) + 'px'
											  });
				}
			}
			prevX = x;
		}
	} 
	var timeout = setTimeout(function() {
		if ($(".zoomfirstimage").length) {
			$(".zoomfirstimage").jqzoom(zoomOptions);
		}
  },500);return;

});