DBGallery = {

	images : new Array(),
	url : '',
	title : '',
	loading : false,
	chevronTimeout : null,

	init : function()
	{	
		Event.observe(window,'keydown', DBGallery.handleKeypress);
		
		Event.observe($('hotspotLeft'), 'mouseover', DBGallery.handleImageOver);
		Event.observe($('hotspotRight'), 'mouseover', DBGallery.handleImageOver);
		Event.observe($('hotspotLeft'), 'mouseout', DBGallery.handleImageOut);
		Event.observe($('hotspotRight'), 'mouseout', DBGallery.handleImageOut);
		Event.observe($('hotspotLeft'), 'click', DBGallery.handleImageClick);
		Event.observe($('hotspotRight'), 'click', DBGallery.handleImageClick);
		
		DBGallery.setupImageLinks();
		
		window.setTimeout (function(){ Effect.Fade('galleryHeaderName') }, 4000);
	},
	
	setupImageLinks : function()
	{
		$$('#imageNav li a').each( function(o){
			Event.observe(o, 'click', DBGallery.selectImage);		
		});
	},
	
	handleImageOver : function(e)
	{
		var el = Event.element(e);
		
		if (DBGallery.chevronTimeout != null)
		{
			window.clearTimeout(DBGallery.chevronTimeout);
			DBGallery.chevronTimeout = null;
		}
		
		if (el.id == 'hotspotLeft')
		{
			DBGallery.chevronTimeout = window.setTimeout(function(){ Effect.Appear($('chevron_left'), { from: 0, to: 0.6, duration: 0.3}); }, 700);
		}
		else
		{
			DBGallery.chevronTimeout = window.setTimeout(function(){ Effect.Appear($('chevron_right'), { from: 0, to: 0.6, duration: 0.3}); }, 700);
		}
	},
	
	handleImageOut : function(e)
	{
		var el = Event.element(e);
		
		window.clearTimeout(DBGallery.chevronTimeout);
		DBGallery.chevronTimeout = null;
		
		if (el.id == 'hotspotLeft')
		{
			if ($('chevron_left').visible())
			{
				Effect.Fade($('chevron_left'), { from: 0.6, to: 0, duration: 0.1});
			}
		}
		else
		{
			if ($('chevron_right').visible())
			{
				Effect.Fade($('chevron_right'), { from: 0.6, to: 0, duration: 0.1});
			}
		}
	},
	
	handleImageClick: function(e)
	{
		var el = Event.element(e);
		
		if (el.id == 'hotspotLeft')
		{
			DBGallery.selectImage(null, $('prevImage').down('img'));
		}
		else
		{
			DBGallery.selectImage(null, $('nextImage').down('img'));
		}
	},
	
	loadImage : function(el)
	{			
		var file = el.src.split('/');
	
		if (file[file.length-1] == 'blank.gif' && el.id != 'mainImageProtection')
		{		
			el.src = DBGallery.images[parseInt(el.id.split('_')[1])-1].imgsrc;
			
			Event.observe(el, 'load', function()
				{
					var prevSibling = el.up('div').previous('div');
					var nextSibling = el.up('div').next('div');
					
					if (prevSibling != null)
					{
						DBGallery.loadImage(prevSibling.down('img'));
					}
					
					if (nextSibling != null)
					{
						DBGallery.loadImage(nextSibling.down('img'));
					}
				}
			);
		}
	},
	
	pushImage : function(url, title)
	{
		var img = $(document.createElement('img'));
		img.imgsrc = url;
		img.index = DBGallery.images.length + 1;
		img.title = title;
	
		DBGallery.images[DBGallery.images.length] = img;
	},
	
	selectImage : function(e, element)
	{
		var el;
	
		if (e == null)
		{
			el = element;
		}
		else
		{	
			// stop the event
			Event.stop(e);
		
			if (DBGallery.loading)
			{
				return;
			}
		
			el = Event.element(e);
		}
		
		var curEl = $$('#imageNav li.selected')[0];
				
		if (el.tagName == 'IMG')
		{						
			if (el.up('a').id == 'prevImage')
			{
				if ( parseInt(curEl.down('a').innerHTML) == 1 )
				{
					return;
				}
				else
				{
					var id = parseInt(curEl.down('a').innerHTML) - 2;
					curEl.previous('li').addClassName('selected');
				}
			}
			else
			{
				if ( parseInt(curEl.down('a').innerHTML) == DBGallery.images.length )
				{
					return;
				}
				else
				{
					var id = parseInt(curEl.down('a').innerHTML);
					curEl.next('li').addClassName('selected');
				}
			}
		}
		else
		{
			if (el.up('li') == curEl)
			{
				return;
			}
			el.up('li').addClassName('selected');
			var id = parseInt(el.innerHTML) - 1;
		}	
		
		curEl.removeClassName('selected');
		
		var img = DBGallery.images[id];
		
		var mainImage = $$('.mainImageSelected')[0];
		var nextImage = $('mainImage_' + parseInt(id+1));
		
		if (mainImage == null)
		{
			return;
		}
		
		mainImage.removeClassName('mainImageSelected');
		
		DBGallery.loading = true;
		
		
		new Effect.Parallel ([
			new Effect.Fade(
				mainImage,
				{
					afterFinish : function()
					{					
						nextImage.addClassName('mainImageSelected');	
						DBGallery.loading = false;
					}
				}
			),		
			new Effect.Appear(
				nextImage
			)
		],
		{
			duration : 0.5
		});	
	},
	
	handleKeypress : function(e)
	{
		switch (parseInt(e.keyCode))
		{
			case 37:
				DBGallery.selectImage(null, $('prevImage').down('img'));
				break;
			case 39:
				DBGallery.selectImage(null, $('nextImage').down('img'));
				break;
			default:
				break;		
		}
	}
}

DBGallery.init();
