var bodyWidth = 0;
var imageContainer = null;
var currentImageIndex = 0;
var ctrWidth = 0;
var imgs = [0];
var last = 0;

var lnth = 0;
var st = 0;
var flowDirection = 'r';
var alts = [];
var links = 0;
var st_height = 0;
var imagesW = [];
var imgobj = [];



function setCaption() 
{
	$('#caption').html(alts[currentImageIndex]);
}

function prevImage()
{
	if (currentImageIndex > 0)
	{
		currentImageIndex--;
		changeImage();
	}
}

function nextImage()
{
	if (currentImageIndex == lnth-1)
	{
		currentImageIndex = 0;
	}
	else
	{
		currentImageIndex++;
	}
	
	changeImage();
}

function getPartOfImageClicked(e)
{
	var offsetLeft = parseInt( e.pageX - ((bodyWidth - imagesW[currentImageIndex]) / 2) );

	if (offsetLeft > (imagesW[currentImageIndex]/2))
	{
		nextImage();
	}
	else
	{
		prevImage();
	}
}


function initSettings()
{
	bodyWidth = $(document).width();

	imageContainer = $($('#images'));

	tcnt = 0;
	imageContainer.find('img').each(function(){
		
		var temp = $(this).width();
		var temph = $(this).height();
		if (st_height < temph) 
		{
			st_height = temph;
		}
		
		$(this).attr('rel',lnth);
		ctrWidth += temp+10;
		lnth++;
		imagesW.push(temp);
		imgs.push(ctrWidth);
		imgobj.push($(this));
		if (tcnt != 0) 
		{
			$(this).fadeTo("fast", 0.15);
		}
		tcnt++;

		alts.push($(this).attr('alt'));
		
		$(this).click(getPartOfImageClicked);
		
	});
	
	imageContainer.css('height', st_height+'px');
	imageContainer.css('width', ctrWidth+500+'px');
	st = parseInt((bodyWidth - imagesW[currentImageIndex])/2)
	imageContainer.css('margin-left', st+'px');
	//$($('#container')).css('margin-left', st+'px');
	setCaption();	
	
	$(window).resize(function()
	{
		//bodyWidth = $(document).width();
		//var tempp = parseInt((bodyWidth - imagesW[currentImageIndex])/2)-(imgs[currentImageIndex]);
		//imageContainer.css('margin-left',tempp+'px');	
		//$($('#container')).css('margin-left',parseInt((bodyWidth - imagesW[currentImageIndex])/2)+'px');	
	});	
}

function changeImage() 
{
	if (last != currentImageIndex) 
	{
		imgobj[last].fadeTo("fast", 0.20);
	}
	
	var tempp = parseInt( (bodyWidth - imagesW[currentImageIndex])/2) - (imgs[currentImageIndex] );
	var tempc = currentImageIndex;
	var templ = last

	setCaption();
	
	imageContainer.animate({ marginLeft: tempp}, 500, 'easeInOutQuad', function()
	{
		imageContainer.css('margin-left',tempp+'px');	
		imgobj[tempc].fadeTo("fast", 1.0);
	});	
	
	last = currentImageIndex;
}

