function initPage()
{
	$('#carusel').galleryScroll({
		btPrev: 'a.link-prev',
		btNext: 'a.link-next',
		circleSlide: false
	});
	initGal();
}

var slides;
var duration = 5000;
var all_slides = [];
var all_thumbs = []
var autoplay = true;
var current = 0;
var timer;
function initGal()
{
	if(document.getElementById('gal'))
	{
		all_slides = $("#gal .box");
		all_thumbs = $('#pages .page a');
		slides = all_slides.length;
		if(slides>1)
		{
			all_slides.css({"position": "absolute", "top": 0, "left": 0, "opacity": 0, zIndex:0});
			$(all_slides[0]).css({"opacity": 1});
			$(all_slides[0]).css({zIndex:1});
			$(all_thumbs[0]).addClass("active");
		
			$('#playpause').click(function()
			{
				if(autoplay)
					autoplay = false;
				else
				{
					autoplay = true;
					timer = setTimeout('rotate(' + current +')', duration);
				}
			});
			
			$(all_thumbs).each(function(el)
			{
				$(all_thumbs)[el].href = 'javascript:;';
				$(all_thumbs[el]).click(function()
				{
					if(el!=current)
					{
						autoplay = false;
						$(all_thumbs).removeClass("active");
						$(this).addClass("active");
						$(all_slides[current]).fadeTo("slow", 0);
                        $(all_slides[current]).css({zIndex:0});
						$(all_slides[el]).fadeTo("slow", 1);
						$(all_slides[el]).css({zIndex:1});
						current = el;
					}
				});
			});
			timer = setTimeout('rotate(' + current +')', duration);
		}
	}
}
function rotate(_index)
{
	if(autoplay)
	{
		$(all_slides[_index]).fadeTo("slow", 0);
		$(all_slides[_index]).css({zIndex:0});
		if(_index==slides-1)
			_index=-1;
		$(all_slides[++_index]).fadeTo("slow", 1);
		$(all_slides[_index]).css({zIndex:1});
		current = _index;
		
		$(all_thumbs).removeClass("active");
		$(all_thumbs[current]).addClass("active");

		timer = setTimeout('rotate(' + _index + ')', duration);
	}
}


if (window.addEventListener)
{
	window.addEventListener("load", initPage, false);
}
else if (window.attachEvent)
{
	window.attachEvent("onload", initPage);
}