﻿(function() {   
	var bigIndex = 0; 
	function slidePic(bool) {  
		var imgs = $('#thumb_list img');

		if (bigIndex + 1 < imgs.length) {
			showBig(imgs[bigIndex + 1]); 
		} else { 
			bigIndex = 0;
			showBig(imgs[bigIndex]);
		}
	}
	function showBig(img) {
		$('#big p').html($(img).attr('alt'));  
		$('#big img').attr('src', img.src);
		var theImg = $('#big img').get(0);    
		theImg.onload = function() {
			$('#big').css({width: ($(theImg).width() + 2) + 'px'});			
		}
 		 
		bigIndex = $('#thumb_list img').index(img);
	}       
    
	$(document).ready(function() {   
		var imgs = $('#thumb_list img');
		showBig(imgs[0]);
		
		$('#big img').click(slidePic);
		$('#big .previous').click(function() {
		  	if (bigIndex - 2 < 0) {
			   showBig(imgs[imgs.length - 1]);
			} else {
               bigIndex -= 2;
			   slidePic();
			}
		});  
		$('#big .next').click(slidePic); 
		
		$('#thumb_list img').click(function() {
			showBig(this);
			return false;
		});
	});
})();