// JavaScript Document

$(function() {
	$('body').removeClass('no_js');
	
	$('#select .thumbnail').click(function(e) {
		e.preventDefault();
		id = $(this).parent().attr('id');
		$('li:not(#'+id+') .details').fadeOut('slow');
		$(this).next().fadeIn('normal');
		
	});
	
	// Create Gallery Preview
	$('.gallery .container').prepend('<div id="preview"><div class="inner"></div></div>');
	$('.gallery #main').prepend('<a id="previous" class="button" href="#" />').append('<a id="next" class="button" href="#" />');
	
	thumbs = $('.gallery #main .thumbnails').children();
	base = $('.gallery #main .thumbnails').wrap('<div class="container" />');
	container = $('.gallery #main .container');
	cell = thumbs.eq(0);
	
	numberofcells = thumbs.size();
	maxwidth = cell.outerWidth(true) * numberofcells;
	
	position = 1;
	beginning = true;
	end = false;
	reset();

	base.width(maxwidth);
	container.width(cell.outerWidth(true) * width);
	
	/*****************/
	/*****BACKWRD*****/
	/*****************/

	previousbutton = $('#previous').click(function(e){
		e.preventDefault();
		if(!beginning) {
			before = position - digress;
			if(before < width) {
				position = 1;
				beginning = true;
			} else {
				position = position - digress;
			}
			move(position);
			end = false;
		}
		reset();
		checkends();
	});
	
	/*****************/
	/*****FORWARD*****/
	/*****************/
	
	nextbutton = $('#next').click(function(e){
		e.preventDefault();
		if(!end) {
			after = numberofcells - position - advance;
			if(advance >= after) {
				advance = after;
				end = true;
			}
			position = position + advance;
			move(position);
			beginning = false;
		}
		reset();
		checkends();
	});
	
	/*****************/
	/*****GENERAL*****/
	/*****************/
	
	function move(framenumber) {
		base.animate({
			marginLeft: (framenumber-1) * (-cell.outerWidth(true))
		});
	}
	
	function reset() {
		width = 6;
		advance = width - 1;
		digress = width - 1;
	}
	
	function checkends() {
		if(end) {
			nextbutton.addClass('disabled');
			previousbutton.removeClass('disabled');
		} else if (beginning) {
			previousbutton.addClass('disabled');
			nextbutton.removeClass('disabled')
		} else {
			previousbutton.removeClass('disabled');
			nextbutton.removeClass('disabled');
		}
	}
	
	checkends();
	
	// Gallery thumbnail functionality
	$('.gallery #preview .inner').css('backgroundImage','url(' + $('.gallery #main li:first-child a').attr('href') +')');
	$('.gallery #preview .inner li:first-child a').addClass('selected');
	$('.gallery #main .thumbnails a').click(function(e) {
			newsrc = $(this).attr('href');
			e.preventDefault();
			$('#preview .inner').animate({ opacity: 0 }, function() {
				img = new Image();
				$(img).load(function() {
					$('#preview .inner').css('backgroundImage','url(' + newsrc +')');
					$('#preview .inner').animate({ opacity: 1 });
				}).attr('src', newsrc);
			});
			$('.gallery #main .thumbnails a').removeClass('selected');
			$(this).addClass('selected');
	});
	
});