var moving = false, img_w = 943, speed = 500, image_current = 1, min_x = 0, autoplay = 1, homeMainPhoto_interval = 0, image_count = 0;

$(document).ready(function() {
	
	// load facebook fan count
	$('div#foo-facebook span').load('fb.php');
	
	image_count =  $('div#photo-content').children().size();
	min_x = - (image_count - 1) * img_w;
	homeMainPhoto_interval = setInterval("showPhoto()", 7000);
	
	$('a#prev').click(function() {
		pauseautoplay();	
		if (image_current > 1)
		{
			var i = parseInt(image_current) - 1;
			var newx = - (i - 1) * img_w;
			var newimagex = -newx;
			if (moving == false)
			{		
				image_current = i;
				moving = true;
				$('div#photo-mask').append('<div id="newimage" style="background: #f00; position: absolute; top: 0; left: 0; height: 419px; width: 943px; display: none;"></div>');
				$('div#newimage').css({ left: '0'});
				$('div#newimage').html($('div#photo-content div:nth-child('+image_current+')').html()).fadeIn('slow', function() {
					$("div#photo-content").css({ left: newx + 'px' });
					$('div#newimage').remove();																											
				});
				
				moving = false;
			}
		}
		else
		{
			if (moving == false)
			{
				image_current = image_count;
				newx = - (image_current - 1) * img_w;
				$('div#photo-mask').append('<div id="newimage" style="background: #f00; position: absolute; top: 0; left: 0; height: 419px; width: 943px; display: none;"></div>');
				$('div#newimage').css({ left: '0'});
				$('div#newimage').html($('div#photo-content div:nth-child('+image_current+')').html()).fadeIn('slow', function() {
																																
					$("div#photo-content").css({ left: newx+'px' });
					$('div#newimage').remove();		
				});
			}
		}
	});
	
	$('a#next').click(function() {
		pauseautoplay();				   
		showPhoto();
	});
	
});

function showPhoto() {
	if (image_current < image_count)
	{
		var i = parseInt(image_current) + 1;
		var newx = - (i - 1) * img_w;
		var newimagex = -newx;
		if (moving == false)
		{		
			image_current = i;
			moving = true;
			$('div#photo-mask').append('<div id="newimage" style="position: absolute; top: 0; left: 0; height: 419px; width: 943px; display: none;">&nbsp;</div>');
			$('div#newimage').css({ left: '0'});
			$('div#newimage').html($('div#photo-content div:nth-child('+image_current+')').html()).fadeIn('slow', function() {
				$("div#photo-content").css({ left: newx + 'px' });
				$('div#newimage').remove();																											
			});
			moving = false;
		}
	}
	else
	{
		if (moving == false)
		{
			image_current = 1;
			$('div#photo-mask').append('<div id="newimage" style="position: absolute; top: 0; left: 0; height: 419px; width: 943px; display: none;">&nbsp;</div>');
			$('div#newimage').css({ left: '0'});
			$('div#newimage').html($('div#photo-content div:nth-child('+image_current+')').html()).fadeIn('slow', function() {
				$("div#photo-content").css({ left: '0' });
				$('div#newimage').remove();		
			});
		}
	}
}

function pauseautoplay() {
	clearInterval(homeMainPhoto_interval);		
	autoplay = 0;
}
