	var photoInterval = 8000;
	var popupDuration = 500;
	var scrolling = false;
	var timer;
	
	window.addEvent('domready',initPopups);
	
	function initPopups()
	{
	}

	function showPopup(id)
	{
		$('homepagePopup1').set('opacity',0);
		$('homepagePopup2').set('opacity',0);
		//$('homepagePopup3').set('opacity',0);
		
		$('homepageOverlay').addEvent('click',function(){$$('.homepagePopup').set('opacity',0); $('homepageOverlay').set('opacity',0);});
		$('homepageOverlay').set('opacity',0.5);
		$('homepageOverlay').setStyles({ 'top': -$(window).getScroll().y,'height':$(window).getScrollSize().y+$(window).getScroll().y });

		var el = $(id);
		el.setStyle('left',395);
		el.set('morph',{duration:(popupDuration), transition:Fx.Transitions.Quad.easeInOut, link:'cancel'});
		el.addEvent('click',function(){el.set('opacity',0), $('homepageOverlay').set('opacity',0);});
		el.morph({'left':495, 'opacity':1});
	}
	
	function initGallery(fileName)
	{
		var newImg = new Asset.image(fileName, {onload:function()
		{
			newImg.inject($('galleryViewer'));
		}});
		
		$$('.galleryThumbnail')[0].addClass('active');
		
		timer=(function(){showNextPhoto()}).periodical(photoInterval);
	}

	function scrollRight()
	{
		var minScroll = ($$('.galleryThumbnailsColumn').length-3)*-82;
		if(!scrolling && ($('galleryThumbnails').getCoordinates($('galleryThumbnailsContainer')).left>minScroll))
		{
			scrolling=true;
			$('galleryThumbnails').set('morph',{duration:500, onComplete:function(){scrolling=false;}});
			$('galleryThumbnails').morph({left:$('galleryThumbnails').getCoordinates($('galleryThumbnailsContainer')).left-82});		
		}
	}

	function scrollLeft()
	{
		if(!scrolling && ($('galleryThumbnails').getCoordinates($('galleryThumbnailsContainer')).left<0))
		{
			scrolling=true;
			$('galleryThumbnails').set('morph',{duration:500, onComplete:function(){scrolling=false;}});
			$('galleryThumbnails').morph({left:$('galleryThumbnails').getCoordinates($('galleryThumbnailsContainer')).left+82});		
		}
	}

	function scrollReset()
	{
		if(!scrolling)
		{
			scrolling=true;
			$('galleryThumbnails').set('morph',{duration:500, onComplete:function(){scrolling=false;}});
			$('galleryThumbnails').morph({left:0});		
		}
	}
	
	function showNextPhoto()
	{
		var thumbnails = $$('.galleryThumbnail');
		var next;
		for(i=0; i<thumbnails.length; i++)
		{
			if(thumbnails[i].hasClass('active'))
			{
				next=i+1;
				if(next>=(thumbnails.length)) next=0;
			}
		}
		
		if(thumbnails[next].getCoordinates($('galleryThumbnailsContainer')).left>=246) scrollRight();
		if(next==0) scrollReset();
		showPhoto(thumbnails[next]);
	}

	function showPhoto(link)
	{
		$clear(timer);
		timer=(function(){showNextPhoto()}).periodical(photoInterval);

		if($(link).className!='active')
		{
			
			$('galleryThumbnails').getChildren().each(function(el)
			{
				el.getChildren('.active').each(function(el)
				{
					el.removeClass('active');
				})
			});
	
	
			$(link).addClass('active');
			
			var newImg = new Asset.image($(link).getChildren()[0].src.replace('thumbnails','gallery'),
			{
				onload: function() 
				{
					newImg.setOpacity(0);
					newImg.setStyles({'position':'relative', 'left':-450, 'top':-300});
					newImg.inject($('galleryViewer'));
					newImg.set('morph',{duration:1000, onComplete:function()
					{
						$('galleryViewer').getChildren()[0].src=newImg.src;
						newImg.destroy();				
					}});
					newImg.morph({left:0, opacity:1});
	
				}
			}
			);
		}
	} 
	
	
	

