var currentImage;
var currenthref;
var thumbIndex;

$(function()
{
	if (0!=$('#productgallery').length) 
	{
		currentImage = $('#productgallery-thumbs a:eq(0)').get(0);
		
		//once image is preloaded, resize image container
		imgPreloader = new Image();
		imgPreloader.onload=function(){
			document.getElementById('productgallery-mainimage').src = imgPreloader.src;
			$('#productgallery-fullsize').css('height', document.getElementById('productgallery-mainimage').offsetHeight);
		};
		imgPreloader.src = $('#productgallery-mainimage').attr('src');
		
	
		$("#productgallery-thumbs a").click(function(){
			var t = this.title;
		
			currentImage = this;

			showImage(t,this.href);
			
      if ($(this).hasClass('prodvideo'))
      {
        if($("#productgallery-mainimage").parent('a').length == 0)
        {
          $("#productgallery-mainimage").wrap('<a href="'+prodvideopath+'" rel="shadowbox;width=980;height=630;" class="shadowbox" />'); // prodvideopath path declared in productpage.xsl
          Shadowbox.setup("a.shadowbox");
        }
      }
      else
      {
        if($("#productgallery-mainimage").parent('a').length > 0)
        {
          $("#productgallery-mainimage").unwrap('a');
        }
      }
			
			this.blur();
		
			return false;
	
		});
		
	}

});


function selectImage(i)
{
	if (i >= 0 && i < $('#productgallery-thumbs a').length)
	{
		var thisimage = $('#productgallery-thumbs a:eq('+i+')').get(0);
		currentImage = thisimage;
	
		showImage(thisimage.title, thisimage.href);
	}
}

function showImage(title, href)
{
	imgPreloader = new Image();

	$("#productgallery-mainimage").hide(); // hide the image
	
	// once image is preloaded, resize image container
	imgPreloader.onload=function(){
		document.getElementById('productgallery-mainimage').src = imgPreloader.src;

		doThumbs();

		resizeImageContainer(imgPreloader.height);
	}
	imgPreloader.src = href;
	
}

function resizeImageContainer(imgHeight) {
		// get curren width and height
		var heightCurrent = $('#productgallery-fullsize').css('height');

		// get new width and height
		var heightNew = imgHeight;

		// scalars based on change from old to new
		yScale = ( heightNew / heightCurrent) * 100;

		// calculate size difference between new and old image, and resize if necessary
		hDiff = heightCurrent - heightNew;

		$("#productgallery-fullsize").animate({height: heightNew},400,'linear',function(){
			$("#productgallery-mainimage").fadeIn();
		});

}

function doThumbs()
{
	// we have the current thumb, so if it's not first, or last, we need to make sure it's
	// in the middle.  This is about counting how far in it is basically.
	
	// count how far in it is!
	if (currentImage != null)
	{
		thumbIndex = 0;
		var numThumbs = $('#productgallery-thumbs a').each(function (i) {
			if (this.href == currentImage.href)
			{
				thumbIndex = i;
			}
		}).length;
		
		//move active flag
		$('#productgallery-thumbs a').removeClass('active');
		$('#productgallery-thumbs a:eq('+thumbIndex+')').addClass('active');
	
		if (numThumbs>5)
		{
			if (thumbIndex <= 1) 
			{
				var intMove = 0;
			}
			else if (thumbIndex > (numThumbs - 3))  
			{
				var intMove = ((numThumbs-3)*55);
			}
			else 
			{
				var intMove = ((thumbIndex-1)*55);
			}
			$('#productgallery-thumbs-slider').animate({left: -intMove}, "fast");
			
		}
	}
	
}

