$(document).ready(function() {

	// Shop tour
	var currentMaincontentWidth = $('#mainContent').width();
	var totalItems			    = $('#shopTour #scrollContainer .panel').length;
	var itemsPerPanel		    = 1;
	var wItem				    = currentMaincontentWidth;
	var curItem                 = 1;

	$('#shopTour').data("currentlyMoving", false);

	$('#shopTour #scrollContainer .panel').each(function() {
		$(this).css({"width" : currentMaincontentWidth});
	});
	
	function change(direction) {
		if( (direction && !(curItem + itemsPerPanel - 1 < totalItems) ) || (!direction && (curItem <= 1)) ) { return false; }	        
        if ( ($('#shopTour').data("currentlyMoving") == false) ) {            
			$('#shopTour').data("currentlyMoving", true);			
			var next = direction ? curItem + itemsPerPanel : curItem - itemsPerPanel;
			if ( next < 1 ) { next = 1; }
			if ( next > totalItems ) { next = totalItems; }
			var movingDistance = (next - curItem) * wItem; 
			var leftValue = $('#shopTour #scrollContainer').css("left");
			var movement = parseFloat(leftValue, 10) - movingDistance;
			animateTo(movement);
			curItem = next;
			if (curItem == totalItems ) { $('#shopTour a.right').hide(); } else { $('#shopTour a.right').show(); }
			if (curItem == 1 ) { $('#shopTour a.left').hide(); } else { $('#shopTour a.left').show(); }
		}
	}
	
	function animateTo(left) {
		$('#shopTour #scrollContainer').stop().animate({"left" : left}, 200, function() {
			$('#shopTour').data("currentlyMoving", false);
		});
	}

	$('#shopTour a.right').click(function() { 
		change(true);
		$('#shopTour .tooltip').hide();
	});	
	$('#shopTour a.left').click(function() { 
		change(false);
	});
	
	
	// Slideshow
	var i = 0;
	$('#slideshow #images img').each(function() {
		i++;
		$(this).addClass('image' + i);
	});
	
	$('#slideshow #images img:first').show();
	var currentSlideshowImage = 1;
	var maxSlideshowImage = $('#slideshow img').length;
	
	function changeSlideshowImage(nextSlideshowImage) {
		$('#slideshow #images img').hide();
		currentSlideshowImage = nextSlideshowImage;
		if ( currentSlideshowImage > maxSlideshowImage ) { currentSlideshowImage = 1; }
		// Find caption
		var caption = $('#slideshow #images img.image' + currentSlideshowImage).attr("title");
		//Insert caption
		$('#slideshow #images p.slideshowCaption').html(caption);
		$('#slideshow #images img.image' + currentSlideshowImage).fadeIn('fast');
	}
	
	$('#slideshow a.left').click(function() {
		if ( currentSlideshowImage == 1 ) { changeSlideshowImage(maxSlideshowImage); } else {
			changeSlideshowImage(parseInt(currentSlideshowImage - 1));
		}
	});
	
	$('#slideshow a.right').click(function() {
		changeSlideshowImage(parseInt(currentSlideshowImage + 1));
	});
	
	var caption = $('#slideshow #images img.image' + currentSlideshowImage).attr("title");
	//Insert caption
	$('#slideshow #images p.slideshowCaption').html(caption);
	
	
	// Comments form show / hide.
	$('p.commentFormShow').click(function() {
		$('#commentForm').fadeIn('fast');
		$('p.commentFormHide').show();
		$(this).hide();
	});
	$('p.commentFormHide').click(function() {
		$('#commentForm').fadeOut('fast');
		$('p.commentFormShow').show();
		$(this).hide();
	});
	
	//Scaling wordpress images to fit on shitty small resolutions.
	// Plain old img src images
	$('.storycontent img').each(function() {
		var maxImageWidth = currentMaincontentWidth - 90; // -90 to take into account the padding of the gray box.
		var imgWidth = $(this).width();
		var imgHeight = $(this).height();
		var ratio = imgWidth / imgHeight;
		if(imgWidth > maxImageWidth) {
			imgWidth = maxImageWidth;
			imgHeight = imgWidth / ratio;
			$(this).attr('width', imgWidth);
			$(this).attr('height', imgHeight);	
		}
	});
	// Images within a caption div. Need to resize the caption div too.	
	$('.storycontent .wp-caption').each(function() {
		var maxImageWidth = currentMaincontentWidth - 90; // -90 to take into account the padding of the gray box.
		var imgWidthCaption = $(this).outerWidth();
		if(imgWidthCaption > maxImageWidth) {
			$(this).css({"width" : maxImageWidth+6});
		}
	});
	
});
