$(document).ready(function(){
	pictureBrowser.init();
	moreInfo.init();
	about.init();
});

var resizeBody = function(){
	$w = $(window);
	$c = $('#container');
	var p = (parseInt($c.css('paddingLeft')) || 0) + (parseInt($c.css('paddingRight')) || 0);
	$('body>.jScrollPaneContainer').css({'height': $w.height() + 'px', 'width': $w.width() + 'px'});
	$c.css({'height': ($w.height()-p) + 'px', 'width': ($w.width() - p) + 'px', 'overflow':'auto'});
	$c.jScrollPane();
}


pictureBrowser = {
	options : {
		'animationSpeed' : 'fast'
	},
	init : function() {
		jQuery.easing.def = "easeOutCubic";
		
		// Go through all the image browsers
		$('ul.pictures').each(function(){
			// Set the total ammount of pictures in the counter
			$(this).prevAll('div.counter:first').find('span.total').text($(this).find('li').size());
			
			// Hide all the pictures but the first
			$(this).find('li:gt(0)').css('left',$(this).find('li img').width());
		});
	},
	previous : function(caller) {
		currentPicture = parseFloat($(caller).parent().find('.current').text()) - 1;
		maxPicture = parseFloat($(caller).parent().find('.total').text());
		prevPicture = currentPicture - 1;
		pictureWidth = $(caller).parent().nextAll('ul.pictures').width();
		
		// Take the current picture and slide it
		$(caller).parent().nextAll('ul.pictures').find('li:eq('+ currentPicture +')').animate({
			'left':pictureWidth
		},function(){
			// Once the anim is done, hide it back where it was
			$(this).css('left',-pictureWidth);
		});
		
		// Check if we're at the first picture
		if(prevPicture < 0) {
			currentPicture = maxPicture;
			prevPicture = currentPicture - 1;
		};
		
		// Position the previous picutre before the slide
		$(caller).parent().nextAll('ul.pictures').find('li:eq('+ prevPicture +')').css('left',-pictureWidth);
		
		// Take the next picture and slide it
		$(caller).parent().nextAll('ul.pictures').find('li:eq('+ prevPicture +')').animate({
			'left':0
		});
		
		$(caller).parent().find('.current').text(currentPicture);
		// Change the current text
		currentPicture = currentPicture - 1;
	},
	next : function(caller) {
		currentPicture = parseFloat($(caller).parent().find('.current').text()) - 1;
		maxPicture = parseFloat($(caller).parent().find('.total').text());
		nextPicture = currentPicture + 1;
		pictureWidth = $(caller).parent().nextAll('ul.pictures').width();
		
		// Check if we're at the last picture
		if(nextPicture > (maxPicture - 1)) {
			nextPicture = 0;
			currentPicture = maxPicture - 1;
		};
		
		// Take the current picture and slide it
		$(caller).parent().nextAll('ul.pictures').find('li:eq('+ currentPicture +')').animate({
			'left':-pictureWidth
		},function(){
			// Once the anim is done, hide it back where it was
			$(this).css('left',pictureWidth);
		});
		
		// Position the next picture before the slide
		$(caller).parent().nextAll('ul.pictures').find('li:eq('+ nextPicture +')').css('left',pictureWidth);
		
		// Take the next picture and slide it
		$(caller).parent().nextAll('ul.pictures').find('li:eq('+ nextPicture +')').animate({
			'left':0
		});
		
		// Change the current text
		currentPicture = nextPicture + 1;
		$(caller).parent().find('.current').text(currentPicture);
	}
}

moreInfo = {
	init : function() {
		$('.moreInfo').hide() // hide all the more info boxes
		
		// Modify the css to display properly when you have JS enabled
		.css({
			'position':'absolute',
			'top': 35,
			'left':0,
			'z-index':2
		});
	},
	open : function(caller){
		if($(caller).parent().nextAll('.moreInfo').is(':hidden')){
			$(caller).parent().nextAll('.moreInfo').slideDown(function(){
				if(!$.browser.msie && $.browser.version != 6) // Animate the button for browsers other than IE6
				$(caller).parent().nextAll('.moreInfo').find('.closeButton a').animate({'height':28});
			
			});
		}else{
			$(caller).parent().nextAll('.moreInfo').find('.closeButton a').trigger('click');
		}
	},
	close : function(caller){
		if(!$.browser.msie && $.browser.version != 6) { // Animate the button for browsers other than IE6
			$(caller).animate({'height':0},function(){
				$(this).parent().parent().parent().slideUp();
			});
		}else{
			$(caller).parent().parent().parent().slideUp();
		}
	}
}

about = {
	init : function() {
		$('.about .content').hide();
	},
	toggle : function() {
		if($('.about .content').is(':visible')){
			$('.about .content').slideUp();
		}else{
			$('.about .content').slideDown();
		}
	}
}