//automatically begin slideshow
var autoplay = true;
//number of seconds between each slide transition
var slidesec = 14;

//no need to edit
var fading = false;
var totalSlides = 1;
var curSlide = 1;
var paused = 0;
var sliding = false;
var dnFading = false;
var dnImg = 1;

function daynightFade()
{
	if (!dnFading)
	{
		dnFading = true;
		Effect.Fade("antiage_slide_"+dnImg, { duration:1.0, from:1.0, to:0.0 }); 
		dnImg++;
		if (dnImg > 4) dnImg = 1; 
		Effect.Appear("antiage_slide_"+dnImg, { duration:1.0, from:0.0, to:1.0, afterFinish: function() {
			dnFading = false;			
		} });
	}
}

function fadeSlide(which)
{
	if (which == 'next')
	{
		if (!fading)
		{
			fading = true;
			Effect.Fade("hp_slide_large_"+curSlide, { duration:0.5, from:1.0, to:0.0 }); 
			curSlide++;
			if (curSlide > totalSlides) curSlide = 1; 
			Effect.Appear("hp_slide_large_"+curSlide, { duration:0.5, from:0.0, to:1.0, afterFinish: function() {
				fading = false;
				$$('.controlcolor').each(function (el) {
					el.setStyle({  backgroundColor: $('slide_control_color_'+curSlide).innerHTML });
				});
			} });
		}
	}
	else if (which == 'prev')
	{
		if (!fading)
		{
			fading = true;
			Effect.Fade("hp_slide_large_"+curSlide, { duration:0.5, from:1.0, to:0.0 }); 
			curSlide--;
			if (curSlide < 1) curSlide = totalSlides; 
			Effect.Appear("hp_slide_large_"+curSlide, { duration:0.5, from:0.0, to:1.0, afterFinish: function() {
				fading = false;
				$$('.controlcolor').each(function (el) {
					el.setStyle({  backgroundColor: $('slide_control_color_'+curSlide).innerHTML });
				});
			} });
		}
	}
}
function nextSlide(e)
{
	Event.stop(e);
	if (paused == 1)
	{
		fadeSlide('next');
	}
	else
	{
		fs.stop();
		fadeSlide('next');
		fs = new PeriodicalExecuter(function(fs) {
			  fadeSlide('next');
		}, slidesec);
	}
}
function prevSlide(e)
{
	Event.stop(e);
	if (paused == 1)
	{
		fadeSlide('prev');
	}
	else
	{
		fs.stop();
		fadeSlide('prev');
		fs = new PeriodicalExecuter(function(fs) {
			  fadeSlide('next');
		}, slidesec);
	}
}
function pauseSlide(e)
{
	Event.stop(e);
	if (paused == 0)
	{
		fs.stop();
		paused = 1;
	}
	else
	{
		fs = new PeriodicalExecuter(function(fs) {
		  fadeSlide('next');
		}, slidesec);
		paused = 0;
	}
}

function scrollLeft(e)
{
	if (!sliding)
	{
		sliding = true;
		var scroller_pos = $('hp_scroller_list').positionedOffset();
		if (scroller_pos[0] == 0)
		{
			$('hp_scroller_list').setStyle({ 'left': (-(229*(totalVingettes)))+'px' });
		}
		
		new Effect.Move('hp_scroller_list', { x: 229, y: 0, duration: 0.4, afterFinish: function() {	sliding = false; } });
	}
}

function scrollRight(e)
{
	if (!sliding)
	{
		sliding = true;
		var scroller_pos = $('hp_scroller_list').positionedOffset();
		if (scroller_pos[0] == -(229*(totalVingettes)))
		{
			$('hp_scroller_list').setStyle({ 'left': '0px' });
		}
		
		new Effect.Move('hp_scroller_list', { x: -229, y: 0, duration: 0.4, afterFinish: function() { 	sliding = false; } });
	}
}

function initialize()
{	
	//day and night sub animation
	if ($('antiage_ss') != null)
	{
		fs = new PeriodicalExecuter(function(fs) {
			daynightFade();
		}, 3.05);
	}
	//end day and night sub animation

	totalSlides = $$('.hp_slide_large').length;
	if (autoplay == true)
	{
		if (totalSlides > 1)
		{
			fs = new PeriodicalExecuter(function(fs) {
			  fadeSlide('next');
			}, slidesec);
			$$('.controlcolor').each(function (el) {
				el.setStyle({  backgroundColor: $('slide_control_color_1').innerHTML });
			});
		}
		else
		{
			$('ss_controls').hide();
		}
	}
	else
	{
		paused = 1;
		if (totalSlides > 1)
		{
			$$('.controlcolor').each(function (el) {
				el.setStyle({  backgroundColor: $('slide_control_color_1').innerHTML });
			});
		}
		else
		{
			$('ss_controls').hide();
		}
	}
	if ($('prev_button') != null){Event.observe('prev_button', 'click', prevSlide, false)};
	if ($('next_button') != null){Event.observe('next_button', 'click', nextSlide, false)};
	if ($('pause_button') != null){Event.observe('pause_button', 'click', pauseSlide, false)};
	
	totalVingettes = $$('.hp_scroller_item').length;
	if (totalVingettes > 3)
	{
		$('hp_scroller_item_flow1').innerHTML = $('hp_scroller_item_1').innerHTML;
		$('hp_scroller_item_flow2').innerHTML = $('hp_scroller_item_2').innerHTML;
		$('hp_scroller_item_flow3').innerHTML = $('hp_scroller_item_3').innerHTML;
		
		if ($('prev_arrow') != null){Event.observe('prev_arrow', 'click', scrollLeft, false)};
		if ($('next_arrow') != null){Event.observe('next_arrow', 'click', scrollRight, false)};		
	}
}

Event.observe(window, 'load', initialize, false);

