Event.observe(window, 'load', function() {
	// home slideshow

    if($('frontpage')) {	
		//if (!getCookie('disable_frontpage_slideshow')) {
			initSlideShow(false);
		//}
	}
	if ($('gallery_content')) {
		// gallery carousel variables
		galleryLength	= $('gallery_content').getHeight();
		slideGallery	= false;
		
	}

});




/////////////////////////
//      FUNCTIONS      //
/////////////////////////


// COOKIES
function setCookie(c_name,value,expireseconde)
{
	var expdate = new Date();
	expdate.setTime (expdate.getTime() + ( expireseconde * 1000));
	document.cookie = c_name+ "=" +escape(value)+";path=/"+((expireseconde==null) ? "" : ";expires="+expdate.toGMTString());
}

function getCookie(c_name)
{
	if (document.cookie.length>0)
	{
		c_start=document.cookie.indexOf(c_name + "=");
		if (c_start!=-1)
		{
			c_start=c_start + c_name.length+1;
			c_end=document.cookie.indexOf(";",c_start);
		    if (c_end==-1) c_end=document.cookie.length;
		    	return unescape(document.cookie.substring(c_start,c_end));
		}
	}
	return "";
}


// FRONTPAGE - SLIDESHOW
function initSlideShow(killCookie) {
	/*if (killCookie) setCookie('disable_frontpage_slideshow', 'true', 0);
	
	$('slideshow_off').hide();
	$('slideshow').show();*/
	
	// slideshow variables
	selectedIndex	= 1;
	slideShowOn		= 1;
	period			= 10000;
	
	// slideshow start
	setTimeout("slideShow()", period);
}
function setSlideShow(value) {
	slideShowOn = value;
}
function slideShow() {
	if (slideShowOn) {
		slide((selectedIndex%5) +1);
	}
	setTimeout("slideShow()", period);
}
function slide(index) {
	if ( (index != selectedIndex) && ($('number'+index).className.match('clickable')) ) {
	
		$$('.number').each(function(i) {
			i.className = "number";
		});
		
		$('desc'+selectedIndex).fade({ duration: 1.0 });
		$('desc'+index).appear({ duration: 1.0 });
		
		xMove = (selectedIndex - index) * 665;
		new Effect.Move(
			'steps',
			{
				x: xMove,
				y: 0,
				mode: 'relative',
				afterFinish: function(){
					$$('.number').each(function(i) {
						i.className = 'number clickable';
					});
					$('number'+index).className ='number on';
					selectedIndex = index;
				}
			}
		);
		
	}
}
function disableFrontpageSlideshow() {
	setCookie('disable_frontpage_slideshow', 'true', 100000000);
	$('slideshow').hide();
	$('slideshow_off').show();
}


// FRONTPAGE - GALLERY CAROUSEL
function setSlideGallery(value) {
	slideGallery = value;
}
function slideGalleryDown(start) {
	if (start) setSlideGallery(1);

	if ( slideGallery && (630 - $('gallery_content').style.marginTop.replace('px','') < galleryLength) )
	{
		new Effect.Tween (
			'gallery_content',
			0,
			3,
			{
			duration: 0.01,
			afterFinish: function()
				{
				slideGalleryDown(0);
				}
			},
			function(p)
			{
				$('gallery_content').style.marginTop = ($('gallery_content').style.marginTop.replace('px','') - p) + 'px';
			}
		);
	}
}
function slideGalleryUp(start) {
	if (start) setSlideGallery(1);

	if ( slideGallery && ($('gallery_content').style.marginTop.replace('px','') < 0) )
	{
		new Effect.Tween
		(
			'gallery_content',
			0,
			3,
			{
			duration: 0.01,
			afterFinish: function()
				{
				slideGalleryUp(0);
				}
			},
			function(p)
			{
				$('gallery_content').style.marginTop = (parseInt($('gallery_content').style.marginTop.replace('px','')) + p) + 'px';
			}
		);
	}
}


