var photos,current,currentIndex,currentImage,previewImages,previewInfo,playing,page,lastIndex,pageRange,timeOut,timeOut2,paginationLinks;

document.observe('dom:loaded', init);

function init()
{
	photos = $$('ul#photoResult li');
	current = 0;
	page = 1;
	photoPerPage = 20;
	lastIndex = photos.indexOf(photos.last());
	currentImage = 1;
	updateImages();
	previewImages = $$('div#photoView img');
	previewInfo = $$('div#photoView p');
	observePaginationControl();	
	loadPhoto(current); 
	observePhotoControl();
	playing = false;
}

function updateImages()
{
	if(currentImage == 0)
	{
		currentImage = 1;
		newImage = 0;
	}
	else
	{
		currentImage = 0;
		newImage = 1;
	}
}

function observePaginationControl()
{
	$$('#paginationControl a').invoke('observe', 'click', updatePaginationControl); 
	$$('#photoOverview img').invoke('observe', 'click', loadClickedPhoto);
	paginationLinks = $$('ul#pageRange li');
}

function loadClickedPhoto()
{
	if(current != Math.round((this.alt - 1)))
	{
		current = Math.round((this.alt - 1));
		loadPhoto(current);
	}
}

function updatePaginationControl(ev)
{			
	ev.stop();
	page = this.title;
	new Ajax.Updater({success: 'overview'}, this.href, {method: 'get', onComplete: observePaginationControl});
}

function checkPaginationControl(photo)
{
	var pagePhoto = Math.ceil((photo+1) / photoPerPage);
	if(pagePhoto != page)
	{
		page = pagePhoto
		new Ajax.Updater({success: 'overview'}, paginationLinks[((page-1))].innerHTML, {method: 'get', onComplete: observePaginationControl});
	}
}

function observePhotoControl()
{
	$('next').observe('click',nextPhoto);
	$('prev').observe('click',prevPhoto);
	$('play').observe('click',playPhoto);
}

function loadPhoto(photo)
{
	var info = photos[photo].childElements();
	var name = '';
	var desc = '';
	var elements = '';
	
	
	previewImages[newImage].src = info[0].innerHTML;
	
	elements = info[1].childElements();
	elements.each(function(s, index){
		name = name + s.innerHTML;
	})
	
	previewInfo[newImage].update('');
	previewInfo[newImage].update(name);
	timeOut2 = setTimeout(showPhoto, 200);
	checkPaginationControl(photo);
}

function showPhoto()
{
	clearTimeout(timeOut2);
	previewImages[currentImage].fade({duration: 0.5});
	previewInfo[currentImage].fade({duration: 0.5});
	setTimeout('previewImages[newImage].appear({duration: 1.0});previewInfo[newImage].appear({duration: 1.0}); updateImages();', 500);
}


function nextPhoto(ev)
{
	ev.stop();
	current = current + 1 <= lastIndex ? current + 1 : 0;
	loadPhoto(current);
}

function prevPhoto(ev)
{
	ev.stop();
	current = current - 1 < 0 ? current = lastIndex : current - 1;
	loadPhoto(current);	
}

function playPhoto(ev)
{
	ev.stop();
	if(playing)
	{
		$('play').removeClassName('pauze');
		clearTimeout(timeout);
		playing = false;
	}
	else
	{
		$('play').addClassName('pauze');
		timeout = setTimeout(rotatePhoto, 3000);
		playing = true;
	}
}

function rotatePhoto()
{
	photos[current].fade({duration: 0.5});
	current = current + 1 <= lastIndex ? current + 1 : 0;
	loadPhoto(current);
	timeout = setTimeout(rotatePhoto, 4000);	
}