// JavaScript Document

photos = [];
captions = [];
current = [];
scrolls = [];

function handleMouseEnter(e)
{
	id = this.id;
	id = id.replace(/_sub/, '');
	sub_id = id+'_sub';
	if ($(id).className == 'menu_item')
		$(id).src = $(id).src.replace(/\.gif$/, '_color.gif');
	if ($defined($(sub_id)))
		$(sub_id).style.zIndex = 11;
}

function handleMouseLeave(e)
{
	id = this.id;
	id = id.replace(/_sub/, '');
	sub_id = id+'_sub';
	if ($(id).className == 'menu_item')
		$(id).src = $(id).src.replace(/_color\.gif$/, '.gif');	
	if ($defined($(sub_id)))
		$(sub_id).style.zIndex = 1;
}

window.addEvent('domready', function(e){
	$$('.menu_item').each(function(e){
		 e.addEvent('mouseenter', handleMouseEnter.bindWithEvent(e));							   
		 e.addEvent('mouseleave', handleMouseLeave.bindWithEvent(e));							   
	});
	$$('.submenu_block').each(function(e){
		 e.addEvent('mouseenter', handleMouseEnter.bindWithEvent(e));							   
		 e.addEvent('mouseleave', handleMouseLeave.bindWithEvent(e));							   
	})
});

function showPhoto(where, index)
{
	count = photos[where].length;
	if (index == count)
		index = 0;	
	current[where] = index;
	cap = captions[where][index];
	what = photos[where][index];	
	scrollTo(where, index);
	$(where+'-cap').set('html', cap);
	$(where).src = what;
	return false;
}

function toRange(what, range)
{
	return Math.min(range-1, Math.max(0, what)); 
}

function scrollTo(where, to)
{
	l = photos[where].length
	si = toRange(to - 2, l);
	if (scrolls[where])
		scrolls[where].cancel();
	current[where] = toRange(to, l);;
	if (scrolls[where])
		scrolls[where].cancel();
	scrolls[where] = new Fx.Scroll(where+'-scroll').toElement(where+'-tn-'+si);
}

function photosLeft(where)
{
	pwl = photos[where].length;
	if (current[where] > pwl-3)
		scrollTo(where, pwl-3);
	else
		scrollTo(where, current[where] - 1);
	return false;
}

function photosRight(where)
{
	if (current[where] < 2)
		scrollTo(where, 3);
	else
		scrollTo(where, current[where] + 1);
	return false;
}
