$(document).ready(function() {
	
	
	// FUNCTIONS
	
	
	jQuery.fn.exists = function() {
		return jQuery(this).length > 0;
	}
	
	
	// GALLERY	
	
	
	var gallery_speed = 375;
	var project_speed = 375;
	var thumbnail_speed = 250;
	var thumbnail_opacity = 0.6;
	var current_image = 1;
	var transitioning = false;
	
	if ($('#gallery').exists()) {
		$('#gallery').cycle({
			fx		: 'fade',
			speed 	: project_speed,
			timeout	: 0,
			sync	: 0,
			nowrap	: false,
			before	: function () { transitioning = true; },
			after	: function () { transitioning = false; }
		});
	}
	
	$('#image-controls #next').click(function() {
		if (!transitioning) {
			$('#gallery').cycle('next');
		}
	});
	
	$('#image-controls #previous').click(function() {
		if (!transitioning) {
			$('#gallery').cycle('prev');
		}
	});
	
	
	// THUMBNAILS
	
	
	$('#thumbnail-gallery').jCarouselLite({
    	start		: thumbnail,
		scroll		: 1,
		visible		: 15,
    	circular	: false,	
        btnNext		: '#thumbnail-controls #next',
        btnPrev		: '#thumbnail-controls #previous'
	});
	
	$('#thumbnail-controls #next').click(function() {
		var total_thumbnails = $('#thumbnail-gallery li').length;
		if (thumbnail < (total_thumbnails - 15)) {
			thumbnail++;
			$.post(base_url + '/session.php?thumbnail=' + thumbnail);
		}
	});
	
	$('#thumbnail-controls #previous').click(function() {
		if (thumbnail > 0) {
			thumbnail--;
			$.post(base_url + '/session.php?thumbnail=' + thumbnail);
		}
	});
	
	$('#thumbnail-gallery').find('img').css('opacity', thumbnail_opacity);
	$('#' + project).css('opacity', 1.0);
	
	$('#thumbnail-gallery').find('img').hover(function() {
		$(this).fadeTo(thumbnail_speed, 1.0);
	}, function () {
		if ($(this).attr('id') != project) {
			$(this).fadeTo(thumbnail_speed, thumbnail_opacity);
		}
	});
	
	
	// MENU
	
	
	$('#' + category + ' a').addClass('selected');
	
	
});


$(window).bind('load', function() { 
	
	
	// SCROLL
	
	
	$('.scroll').jScrollPane({
		scrollbarWidth	: 10,
		scrollbarMargin	: 18,
		wheelSpeed		: 5,
		arrowSize		: 10,
		showArrows		: true,
		topCapHeight	: 5,
		bottomCapHeight	: 1,
		dragMaxHeight	: 150
	});
	
	
	// UNHIDE
	
	
	$('#container').removeClass('hidden');
	
});
