// basepath: the base directory for AJAX calls
var basepath = "";
function base(dir) { basepath = dir; }

// Document setup

$(document).ready(function() {
	
	// Featured scroller
	if ($('#featured #browser').length>0) {
		featured_init();
	}
	
	// Portfolio navigator
	var time = 500;
	$('#portfolio #photo .info').css('opacity', 0);
	$('#portfolio #photo img').mouseenter(function() {
		$('#portfolio .info')
			.stop()
			.animate( { 'opacity' : 1 }, time);
	}).mouseleave(function() {
		$('#portfolio .info')
			.stop()
			.animate( { 'opacity' : 0 }, time);
	});
	
});

// Featured

featured_time = 10;
featured_speed = 1;

function featured_init() {
	
	/*$('#featured #browser').mouseenter(function(e){
		var x = (e.pageX / $(this).width()) * ($('#featured #browser ul').width() - $(this).width());
		$(this).animate( { scrollLeft : x } , time, function(){
			$(this).mousemove(function(e){
				var x = (e.pageX / $(this).width()) * ($('#featured #browser ul').width() - $(this).width());
				$(this).scrollLeft(x);
			});
		});
	}).mouseleave(function(e){
		$(this).unbind('mousemove');
	});*/
	
	window.setTimeout('featured_animate()', featured_time);
}

function featured_animate() {
	// Scroll left
	var left = $('#featured #browser').scrollLeft() + featured_speed;
	$('#featured #browser').scrollLeft(left);
	
	// Wrap content
	var child = $('#featured #browser ul li:first-child');
	if (left > child.outerWidth(true)) {
		$('#featured #browser').scrollLeft(left - child.outerWidth(true));
		$('#featured #browser ul').append(child);
	}
	
	// Continue animation
	window.setTimeout('featured_animate()', featured_time);
}

// Portfolio

var portfolio_current = null;
var portfolio_photos = null;
var portfolio_category = null;

function portfolio_init(photos, index, cat) {
	portfolio_photos = photos;
	portfolio_current = index;
	portfolio_category = cat;
}

function portfolio_next() {
	if (portfolio_current + 1 == portfolio_photos.length)
		return portfolio_selectImage(0);
	else
		return portfolio_selectImage(portfolio_current + 1);
}

function portfolio_previous() {
	if (portfolio_current == 0)
		return portfolio_selectImage(portfolio_photos.length - 1);
	else
		return portfolio_selectImage(portfolio_current - 1);
}

function portfolio_selectImage(i) {
	var img = portfolio_photos[i];
	var time = 500;
	
	// Scroll the page
	var scrollat = $('html, body').scrollTop();
	var scrollto = $("#body").offset().top;
	if (scrollat > scrollto)
		$('html, body').scrollTo( scrollto, { duration : time * 2 } );
	
	if ($('#photo .container img').attr('id') == "photo" + img) return false;
	
	// Fade current image
	$('#photo').animate( { 'opacity' : 0 } , time, function() {
		// Insert new image
		$.ajax({
			type	: "GET",
			url		: basepath + "ajax/photo/view",
			data	: {
				photo : img,
				category : portfolio_category
			},
			success : function (data) {
				$('#photo').html(data);
				$('#photo').animate( { 'opacity' : 1 } , time );
				$('#portfolio #photo .info').css('opacity', 0);
				$('#portfolio #photo img').mouseenter(function() {
					$('#portfolio .info')
						.stop()
						.animate( { 'opacity' : 1 }, time);
				}).mouseleave(function() {
					$('#portfolio .info')
						.stop()
						.animate( { 'opacity' : 0 }, time);
				});
			},
			error	: function (XMLHttpRequest, textStatus, errorThrown) {
				alert("AJAX failure @ portfolio_selectImage");
			}
		});
	});
	
	// Set the current image
	portfolio_current = i;
	
	// Scroll the photo browser
	photos_goto(Math.floor(i / photos_pp) + 1);
		
	
	return false;
}

// Photos

photos_time = 500;
photos_page = 1;
photos_count = 0;
photos_pp = 7;

function photos_init(count) {
	photos_count = count;
	photos_page = ($('#photos #browser').scrollLeft() / $('#photos').width()) + 1;
	
	if (photos_count > photos_pp)
		$('#photos .navigation a').show();
}

function photos_goto(page) {
	$('#photos #browser').scrollTo({top: 0 , left: $('#photos').width() * (page-1) }, {  duration : photos_time });
	photos_page = page;
	if (photos_count > photos_pp) {
		/*
		if (photos_page * photos_pp >= photos_count)
			$('#photos .navigation a.next').hide();
		else
			$('#photos .navigation a.next').show();
			
		if (photos_page == 1)
			$('#photos .navigation a.previous').hide();
		else
			$('#photos .navigation a.previous').show();
		*/
	}
	return false;
}

function photos_next() {
	if (photos_page != Math.ceil(photos_count / photos_pp))
		return photos_goto(photos_page + 1);
	else
		return photos_goto(1);
	/*$('#photos #browser').scrollTo({top: 0 , left: '+=' + $('#photos').width() }, {  duration : photos_time });
	photos_page++;
	$('#photos .navigation a.previous').show();
	if (photos_page * photos_pp >= photos_count)
		$('#photos .navigation a.next').hide();
	return false;*/
}

function photos_previous() {
	if (photos_page != 1)
		return photos_goto(photos_page - 1);
	else
		return photos_goto(Math.ceil(photos_count / photos_pp));
	/*$('#photos #browser').scrollTo({top: 0 , left: '-=' + $('#photos').width() }, {  duration : photos_time });
	photos_page--;
	$('#photos .navigation a.next').show();
	if (photos_page == 1)
		$('#photos .navigation a.previous').hide();
	return false;*/
}