//automatically begin slideshow
var autoplay = false;
//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 sliding2 = false;
var sliding3 = false;
var panelopen = 0;

function fadeSlide(which)
{
	if (panelopen == 1)
	{
		var rolluppos = $('rollup').positionedOffset();
		if (rolluppos[0] == 0){
			slideDown(null);
			if (paused == 1)
			{
				pauseSlide(null);
			}
		}
		else
		{
			slideDownRight(null);
			if (paused == 1)
			{
				pauseSlide(null);
			}
		}
	}
	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)
{
	if (e != null)
	{
		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 slideUp(e)
{
	Event.stop(e);
	if ((!sliding2)&&(!fading))
	{
		if(paused == 0)
		{
			pauseSlide(null);
		}
		sliding2 = true;
		new Effect.Move('rollup', { x: 0, y: -276, duration: 0.4, afterFinish: function() { sliding2 = false; panelopen = 1; $('unreveal_offer').show(); $('reveal_offer').hide(); } });
	}
}

function slideDown(e)
{
	if (e != null)
	{
		Event.stop(e);
	}
	if ((!sliding2)&&(!fading))
	{
		$('unreveal_offer').hide();
		$('reveal_offer').show();
		if(paused == 1)
		{
			pauseSlide(null);
		}
		sliding2 = true;
		new Effect.Move('rollup', { x: 0, y: 276, duration: 0.4, afterFinish: function() { sliding2 = false; panelopen = 0; } });
	}
}
function slideUpLeft(e)
{
	Event.stop(e);
	if ((!sliding3)&&(!fading))
	{
		if(paused == 0)
		{
			pauseSlide(null);
		}
		sliding3 = true;
		new Effect.Move('rollup', { x: -630, y: -180, duration: 0.4, afterFinish: function() { sliding3 = false; panelopen = 1; $('men_unreveal_offer').show(); $('men_reveal_offer').hide(); } });
	}
}

function slideDownRight(e)
{
	if (e != null)
	{
		Event.stop(e);
	}
	if ((!sliding3)&&(!fading))
	{
		$('men_unreveal_offer').hide();
		$('men_reveal_offer').show();
		if(paused == 1)
		{
			pauseSlide(null);
		}
		sliding3 = true;
		new Effect.Move('rollup', { x: 630, y: 180, duration: 0.4, afterFinish: function() { sliding3 = false; panelopen = 0; } });
	}
}
function initialize()
{	
	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)};
		if ($('reveal_offer') != null){Event.observe('reveal_offer', 'click', slideUp, false)};
		if ($('unreveal_offer') != null){Event.observe('unreveal_offer', 'click', slideDown, false)};
		if ($('men_reveal_offer') != null){Event.observe('men_reveal_offer', 'click', slideUpLeft, false)};
		if ($('men_unreveal_offer') != null){Event.observe('men_unreveal_offer', 'click', slideDownRight, false)};
	}
	pauseSlide();
}

Event.observe(window, 'load', initialize, false);