var homepage = false;

$(document).ready(function() {	
	$('.gallery').hover(function() {
		$('body').addClass('cursor');
	}, function() {
		$('body').removeClass('cursor');
	}).mousemove(function() {
		$('body').addClass('cursor');
	}).data('viewed',1).cycle({
		timeout: ( homepage ? 5000 : 0 ),
		fx: 'fade',
		slideExpr: 'span',
		next: '.gallery, .gallery-next',
		prev: '.gallery-prev',
		before: function() {
			var $this = $(this);
			var $gallery = $this.parents('.gallery');
			var $caption = $('.caption');
			var caption = $this.attr('alt');
			var $thumb = $('.gallery-thumbs span:eq(' + $('.gallery span').index( $(this) ) + ')');
			var $thumbs_div = $('div.gallery-thumbs');
			var gallery_width = $thumbs_div.width();
			
			if( $caption.css('opacity') != 1 && caption != '' ) {
				$caption.html( caption ).animate({'opacity':1});
			} else {
				if( caption == '' ) {
					$caption.animate({'opacity':0}, function() {
						$caption.html('&nbsp;');
					});
				} else {
					$caption.html( caption );
				}				
			}
			
			if( $thumb.size() > 0 ) {
				carousel_page( Math.floor( $thumb.get(0).offsetLeft / gallery_width ) );	
			}
			
			$(this).timbarber(function() {
				if( !homepage ) {
					$gallery.viewed( 1, function() {
						$('ul.gallery-thumbs li a span').timbarber();
					});	
				}
				
				$this.next().timbarber();
				$this.prev().timbarber();
			});
		},
		pager: 'ul.gallery-thumbs',
		pagerAnchorBuilder: function(idx, slide) {
			return 'ul.gallery-thumbs li:eq(' + idx + ') a';
		}
	}).each(function() {
		var $gallery = $(this);
		var $thumbs_ul = $('ul.gallery-thumbs');
		var $thumbs_div = $('div.gallery-thumbs');
		var gallery_width = $thumbs_div.width();
		var $controls = $('.controls');
		
		$thumbs_ul.width( $thumbs_ul.calculate_width('span') );
		
		$thumbs_ul.find('li').hover(function() {
			$(this).animate({'opacity':1});
		}, function() {
			$(this).animate({'opacity':0.65});
		});
		
		$gallery.data('current_page', 0);
		$gallery.data('total_pages', Math.ceil( $thumbs_ul.width() / gallery_width ) );
		
		if( $thumbs_ul.width() < gallery_width ) {
			$thumbs_ul.css('margin', 'auto');
			$controls.hide();
		}
	});
	// end $('.gallery').cycle()
	
	$('ul.navigation li a').click(function() {
		$('.gallery, ul.gallery-thumbs li, .caption').animate({'opacity':0});
	});
	
	$('.controls .next').click(carousel_page_next);
	$('.controls .prev').click(carousel_page_prev);
}).bind('keydown', 'left', function() {
	$('.gallery-prev').trigger('click');
}).bind('keydown', 'right', function() {
	$('.gallery-next').trigger('click');
});

// oops, missed the DRY philosphy.
function carousel_page( page ) {
	var $thumbs_div = $('div.gallery-thumbs');
	var $thumbs_ul = $('ul.gallery-thumbs');
	var gallery_width = $thumbs_div.width();
	var $gallery = $('.gallery');
	
	$gallery.data('current_page', page);
	$thumbs_ul.animate({'left': -1 * ( gallery_width * page) });
	
	return false;	
}

function carousel_page_next() {
	var $gallery = $('.gallery');
	var current_page = $gallery.data('current_page');
	var total_pages = $gallery.data('total_pages');
	var next_page = current_page + 1;
	
	return carousel_page( ( next_page >= total_pages ) ? 0 : next_page );
}

function carousel_page_prev() {
	var $gallery = $('.gallery');
	var current_page = $gallery.data('current_page');
	var total_pages = $gallery.data('total_pages');
	var prev_page = current_page - 1;
	
	return carousel_page( ( prev_page < 0 ) ? (total_pages - 1) : prev_page );
}