var currentClosingWidth = 0;
var currentOpeningWidth = 0;
var closingDiv = 0;
var openingDiv = 0;
var isMove = 0;
var moveDirection = 1;
var galleryTimer = 0;

function initMovement(direction)
{
        if(isMove && direction != moveDirection)
        {
		closingDiv = closingDiv ^ openingDiv;
		openingDiv = closingDiv ^ openingDiv; 
		closingDiv = closingDiv ^ openingDiv; 
		currentClosingWidth = currentClosingWidth ^ currentOpeningWidth;
		currentOpeningWidth = currentClosingWidth ^ currentOpeningWidth;
		currentClosingWidth = currentClosingWidth ^ currentOpeningWidth;
		moveDirection = direction;
		return;
        }
        else if(isMove)
        {
        	return;
        }
        isMove = 1;
        if(direction == -1)
        {
                openingDiv = currentMiddleDiv - showNum;
                closingDiv = currentMiddleDiv + (showNum - 1);
	}
	else
	{
                openingDiv = currentMiddleDiv + showNum;
                closingDiv = currentMiddleDiv - (showNum - 1);
	}
	moveDirection = direction;
	currentClosingWidth = images[closingDiv] + 6;
	currentOpeningWidth = 0;
	moveImage();
}

function HandleArray()
{
// get new image and add it into html or just delete div
	if(currentMiddleDiv == 1)
	{
		main = $('gallerydiv');
		// take last one and bring it to the beginning;
		movedImageID = imagesIDs[totalNumber - 1];
		movedImageWidth = images[totalNumber - 1];
		// need to rebuild arrays
		for(i = (totalNumber - 2); i >= 0; i--)
		{
			images[i + 1] = images[i];
			imagesIDs[i + 1] = imagesIDs[i];
		}
		images[0] = movedImageWidth;
		imagesIDs[0] = movedImageID;
		// now move last div to first position
		//alert(main.lastChild.id);
		first = main.lastChild.cloneNode(true);
		main.removeChild(main.lastChild);
		first.style.width="0px";
		main.insertBefore(first,main.firstChild);
		currentMiddleDiv ++;
	}
	else 
	if(currentMiddleDiv == (totalNumber - 2))
	{
		main = $('gallerydiv');
		// take the first one and bring it to the end
		movedImageID = imagesIDs[0];
		movedImageWidth = images[0];
		for(i = 1; i < totalNumber; i++)
		{
			images[i - 1] = images[i];
			imagesIDs[i - 1] = imagesIDs[i];
		}
		images[totalNumber - 1] = movedImageWidth;
		imagesIDs[totalNumber - 1] = movedImageID;
		// now move the first div to last position
		//alert(main.firstChild.id);
		last = main.firstChild.cloneNode(true);
		main.removeChild(main.firstChild);
		last.style.width="0px";
		main.appendChild(last);
		currentMiddleDiv --;
	}
}
function getClosingDiv()
{
	return $("img" + imagesIDs[closingDiv]);
}
function getOpeningDiv()
{
	return $("img" + imagesIDs[openingDiv]);
}
function moveImage()
{
	divC = getClosingDiv();
	divO = getOpeningDiv();
	// now should perfome moveing
	// if moveDirection = -1 right image is closing, left image is opening
	lres = closeDiv(divC);
	rres = openDiv(divO);
	if(lres || rres)
	{
		setTimeout("moveImage()",delay);
	}
	else
	{
		isMove = 0;
		if(moveDirection == 1)
		{
			currentMiddleDiv = openingDiv - 1;
		}
		else
		{
			currentMiddleDiv = openingDiv + 1;
		}
		HandleArray();
		clearTimeout(galleryTimer);
		galleryTimer = setTimeout("initMovement('"+moveDirection+"');",4000);
	}
}

function closeDiv(divC)
{
	if(currentClosingWidth > 0)
	{
	        if((currentClosingWidth - step) < 0)
	        {
		        SetGalleryWidth(-currentClosingWidth);
	        	currentClosingWidth = 0;	
	        }
	        else
	        {
	        	currentClosingWidth -= step;
		        SetGalleryWidth(-step);
		}
		divC.style.width = currentClosingWidth + "px";
		return true;
	}
	return false;
}

function openDiv(divO)
{
	//opening
	if(currentOpeningWidth < (images[openingDiv] + 6))
	{
	        if((currentOpeningWidth + step) > (images[openingDiv] + 6))
	        {
			SetGalleryWidth(images[openingDiv] + 6 - currentOpeningWidth);
        	currentOpeningWidth = (images[openingDiv] + 6);
	        }
	        else
	        {
	        	currentOpeningWidth += step;
		        SetGalleryWidth(+step);
		}
		divO.style.width = currentOpeningWidth + "px";
		return true;
	}
	return false;
}

function SetGalleryWidth(delta)
{
	currentGalleryWidth += delta;
	$('gallery_container').style.width= currentGalleryWidth + "px";
}
