$(document).ready(function() {
	$('.projects div.roll a').each(function() {
		var n = $(this).attr('id').substr(1);
		$(this).css('background', 'url(images/projects/p'+n+'.jpg) no-repeat');
	});
	$('.projects div.roll a:last').css('margin', '0px');
	$('.projects div.roll a:first').css('background-position', 'bottom');
	$('div.pcont:first').css('display', 'block');
	$('.projects div.roll a').hover(function () {
		$('.projects div.roll a').css('background-position', 'top');
		$(this).css('background-position', 'bottom');
		var n = $(this).attr('id').substr(1);
		$('div.pcont').hide();
		$('div#d'+n).show();
	});
	
	var margin = $('.projects div.roll div').css('marginLeft').split('px').slice(0,1);
	var limit = (($('.projects div.roll a').length * 129 - 774) * -1);

	$('.projects a.left').hover(
		function() {
			if(margin == 0) $(this).css('background-position', 'top left');
			else $(this).css('background-position', 'bottom left');
		},
		function() {
			$(this).css('background-position', 'top left');
		}
	);
	
	$('.projects a.left').click(function() {
		if(margin < 0) {
			margin = margin + 774;
		}
		$('.projects div.roll div').animate({
			marginLeft: margin+'px'
		}, 500);
		return false;
	});
	
	$('.projects a.right').click(function() {
		if(margin > limit) {
			margin = margin - 774;
		}
		$('.projects div.roll div').animate({
			marginLeft: margin+'px'
		}, 500);
		return false;
	});
	
	$('.projects a.right').hover(
		function() {
			if(margin <= limit) $(this).css('background-position', 'top right');
			else $(this).css('background-position', 'bottom right');
		},
		function() {
			$(this).css('background-position', 'top right');
		}
	);
});
