
window.addEvent('load',function(){Gallery.setup();});


var Gallery =
	{
		Count : 0,
		Current : 0,
		Animating : false,
		Cycle: true,
		CurrentPage: 0,
		Loading: true,
		ImageList: ['theworld4.jpg','theworld5.jpg','theworld6.jpg','theworld4.jpg','theworld5.jpg','theworld6.jpg','theworld4.jpg','theworld5.jpg','theworld6.jpg','theworld4.jpg','theworld5.jpg','theworld6.jpg','theworld4.jpg','theworld5.jpg','theworld6.jpg'],
		setup: function()
		{
			if ($('gallery') != null && $('fullgal') != null)
			{
			
				$('fullgal').addEvent('click',function(e){Gallery.show(); e = new Event(e); e.preventDefault()});
			
				Gallery.Count = Gallery.ImageList.length;
				
					var galNav = $$('#gallerynav ul');
					
					$('galClose').addEvent('click',function(e){e = new Event(e);Gallery.hide();e.preventDefault()});
					
					for(var i=0;i<Gallery.Count;i++)
					{
						img = new Element('img');
						img.setProperty('width','930');
						img.setProperty('height','440');
						img.setStyles({'width':'930px','height':'440px'});
						img.setProperty('src','images/gallery/' + Gallery.ImageList[i]);
						img.injectInside('gallery');
					}
					
					Gallery.Images = $$('#gallery img');
					
					Gallery.Images.setOpacity(0);
					Gallery.Images.removeClass('hidden');	
				
					$('gallerynav').setOpacity(0);
				if (Gallery.Count > 1) // multi-image view
				{	
					if (Gallery.Count > 12)
					{
						$('gallerynav').setStyle('width','600px');
					}
					else
					{
						$('gallerynav').setStyle('width',(Gallery.Count * 51)-13 + "px");
						$('gallerynav').setStyle('right','20px');
					}
					
					galNav.setStyle('width',(Gallery.Count * 51)+'px');
					
					// add the thumbnails
					for (var j=0;j<Gallery.Count;j++)
					{
						li = new Element('li');
						li.injectInside(galNav[0]);
						li.setStyle('left',j*51 + 'px');
						
						img = new Element('img');
						img.injectInside(li);
						img.setProperty('src','images/gallery/thumb_' + Gallery.ImageList[j]);
						img.setProperty('width','38');
						img.setProperty('height','38');
						
						img.addEvent('click',new Function('Gallery.Show(' + j + ')'));
					}
					
					left = new Element('img');
					left.injectInside('galScrollNav');
					left.setProperty('id','galLeftNav');
					left.setProperty('src','images/nav_left.png');
					left.setProperty('width','10');
					left.setProperty('height','38');
					
					left.addEvent('click',function(){Gallery.ScrollLeft()});
					
					right = new Element('img');
					right.injectInside('galScrollNav');
					right.setProperty('id','galRightNav');
					right.setProperty('src','images/nav_right.png');
					right.setProperty('width','10');
					right.setProperty('height','38');
					right.addEvent('click',function(){Gallery.ScrollRight()});
					
				}
			}
		},
		show: function()
		{
			$('mainbody').addClass('hidden');
			$('gallerywrapper').removeClass('hidden');
			
			if (Gallery.Count > 1)
			{
			$('galLeftNav').setStyle('left',($('gallerynav').getLeft()-$('gallerywrapper').getLeft()-18) + 'px');
			Gallery.updateNav();
			
			var fxd = new Fx.Style('gallerynav','opacity',{duration:1000,transition:Fx.Transitions.Quad.easeIn,'onComplete':function(){Gallery.Loading=false;}});
			fxd.start(1);
			}
			var fx2 = new Fx.Style(Gallery.Images[Gallery.Current],'opacity',{duration:1000,transition:Fx.Transitions.Quad.easeIn,'onComplete':function(){Gallery.Loading=false;}});
			fx2.start(1);
			if (Gallery.Count > 1)
			{
			Gallery.Cycle = true;
			Gallery.NextAnim.delay(6000);
			}
		},
		hide: function()
		{
			Gallery.doHide();
			$('gallerywrapper').addClass('hidden');
			$('mainbody').removeClass('hidden');
			Gallery.Current=0;
			Gallery.CurrentPage=0;
			Gallery.doShow(0);
			if (Gallery.Count > 1)
			Gallery.updateNav();
			Gallery.Cycle =false;
		},
		Prev: function()
		{
			if (!Gallery.Animating)
			{
				Gallery.Cycle = false;
				Gallery.doHide();
				
				Gallery.Current--;
				if (Gallery.Current <= 0)
					Gallery.Current = Gallery.Count-1;
					
				Gallery.doShow();
				Gallery.updateNav();
				
			}
			
		},
		NextAnim : function()
		{
			if (Gallery.Cycle)
			{
				Gallery.doNext();
				Gallery.NextAnim.delay(6000);
			}
		},
		Next: function()
		{
			if (!Gallery.Animating)
			{
				Gallery.Cycle = false;
				Gallery.doNext();
			}
		},
		Show: function(i)
		{
			if (!Gallery.Animating)
			{
				Gallery.Cycle = false;
				Gallery.doHide();
				Gallery.Current = i;
				Gallery.doShow();
				Gallery.updateNav();
			}
		},
		doNext: function()
		{
			Gallery.doHide();
			Gallery.Current++;
			if (Gallery.Current >= Gallery.Count)
				Gallery.Current = 0;
			Gallery.doShow();
			Gallery.updateNav();
		},
		doHide : function()
		{
			var hide = new Fx.Style(Gallery.Images[Gallery.Current],'opacity',{
							duration:1000,
							transition: Fx.Transitions.Quad.easeIn,
							'onStart':function(){Images.Animating=true},
							'onComplete':function(){Images.Animating=false}
							});
			hide.start(0);
		},
		doShow: function()
		{
			var show = new Fx.Style(Gallery.Images[Gallery.Current],'opacity', {duration:1000,transition: Fx.Transitions.Quad.easeIn});
			show.start(1);
		},
		updateNav: function()
		{

			var img = $$('#gallerynav img');		
			if (Gallery.Current % 12 == 0)
			{
			
			var scroll = new Fx.Scroll('gallerynav', { duration:1000, transition: Fx.Transitions.Quad.easeInOut});
			scroll.toElement(img[Gallery.Current]);
			Gallery.CurrentPage = (Gallery.Current / 12);
			}
			for (var i=0;i<img.length;i++)
				if (i==Gallery.Current)
					img[i].setOpacity(1);
				else
					img[i].setOpacity(0.6);
		


			if (Gallery.CurrentPage == (Gallery.Count - Gallery.Count % 12)/12 || (Gallery.Count % 12 == 0 && (Gallery.Count / 12-1) == Gallery.CurrentPage))
				$('galRightNav').setStyle('visibility','hidden');
			else
				$('galRightNav').setStyle('visibility','');
	

			if (Gallery.CurrentPage == 0)
				$('galLeftNav').setStyle('visibility','hidden');
			else
				$('galLeftNav').setStyle('visibility','');

		},
		ScrollLeft: function()
		{
			Gallery.Cycle = false;
			Gallery.CurrentPage--;
			
			nextPage = Gallery.CurrentPage*12;
			
			Gallery.doHide();
			Gallery.Current = nextPage;
			Gallery.doShow();
			Gallery.updateNav();
		},
		ScrollRight: function()
		{
			Gallery.Cycle = false;
			Gallery.CurrentPage++;
			
			nextPage = Gallery.CurrentPage*12;
			Gallery.doHide();
			Gallery.Current = nextPage;
			Gallery.doShow();
			Gallery.updateNav();
		}
	}