/* initialisation */
var slideCount = slides.length;
var currentSlide = 1;
var queue = [2];
var isStopped = 0;

var initPage = function()
{
	preloadImages();
	initLinks();
	setActiveLink();
	prepareImages();
	initSlideShow();
	initAccordion();
}

/* preload all slides */
var preloadImages = function()
{
	var preloaded = [];
	for (var index = 1; index <= slideCount; index++){
		preloaded[index] = new Element('img', {
		    'src':composeSrc(index)
		});
	}
}

/* generate slideshow controls */
var initLinks = function()
{
	var controlsContainer = $('show-controls');
	if (controlsContainer){
		for (var index = 1; index <= slideCount; index++){
			var control = new Element('a', {
			    'href':'#',
			    'rel':'slide-' + index,
				'class':'slide',
				'events':{
					'click':function(e){
						setNext(this.getProperty('rel').replace('slide-',''))
						var event = new Event(e);
						event.stop();
					}
				}
			});
			control.setHTML(index);
			control.injectBefore('play-button');
		}
	}
	
	var playButton = $('play-button');
	if (playButton){
		playButton.addEvent('click',function(e){
			if (playButton.hasClass('play')){
				continueShow();
			} else if (playButton.hasClass('pause')) {
				stopShow();
			}
			
			var event = new Event(e);
			event.stop();
		});
	}
}

var initSlideShow = function()
{
	var slideShowContainer = $('slideshow');
	if (slideShowContainer)
	{
		startShow();
	}
}

var prepareImages = function()
{
	var hiddenImage = $('slideshow').getElement('.hidden');
	var visibleImage = $('slideshow').getElement('.visible');
	
	hiddenImage.setStyle('opacity','0');
	hiddenImage.setStyle('display','block');
	visibleImage.setStyle('opacity','1');
	visibleImage.setStyle('display','block');
}

var startShow = function()
{
	stopFlag = 0;
	showNext.delay(pauseDuration);
}

var stopShow = function()
{
	isStopped = 1;
	
	var playButton = $('play-button');
	playButton.setProperty('class','play');
}

var continueShow = function()
{
	isStopped = 0;
	
	var playButton = $('play-button');
	playButton.setProperty('class','pause');
}

var setNext = function(index)
{
	queue = [index.toInt()];
	setActiveLink();
	stopShow();
}

var showNext = function()
{
	var hiddenImage = $('slideshow').getElement('.hidden');
	var visibleImage = $('slideshow').getElement('.visible');

	if (queue.length == 0 && !isStopped){
		queue.push(getNext());
	}
	
	if (queue.length > 0)
	{
		currentSlide = queue.shift();
		hiddenImage.setProperty('src',composeSrc(currentSlide));
		switchImages({onComplete:function(){
				setActiveLink(currentSlide);
				showNext.delay(pauseDuration);
			}
		});
	}
	else
	{
		showNext.delay(pauseDuration);
	}
}

var switchImages = function(params)
{
	var hiddenImage = $('slideshow').getElement('.hidden');
	var visibleImage = $('slideshow').getElement('.visible');
	
	hideImage(visibleImage,function(){
		setLinks(currentSlide);
		showImage(hiddenImage,params.onComplete);
	});
}

var showImage = function(image,onComplete)
{
	var opacityChange = new Fx.Style(image, 'opacity',{duration:fadeDuration,onComplete:function(){
			image.setProperty('class','visible');
			onComplete.apply();
		}
	});
	opacityChange.start(0,1);
}

var hideImage = function(image,onComplete)
{
	var opacityChange = new Fx.Style(image, 'opacity',{duration:fadeDuration,onComplete:function(){
			image.setProperty('class','hidden');
			onComplete.apply();
		}
	});
	opacityChange.start(1,0);
}

var setActiveLink = function()
{
	$$('#show-controls a.slide').each(function(item,index){
		item.removeClass('active');
		if (index + 1 == currentSlide){
			item.addClass('active');
		}
	});
}

var getNext = function()
{
	var nextSlide = currentSlide + 1;
	if (currentSlide < slideCount){
		return nextSlide;
	} else {
		return 1;
	}
}

var setLinks = function(index)
{
	var slideLink = $('slide-link');
	var slideTextLink = $('slide-text-link')
	if (slideTextLink){
		slideTextLink.setHTML(slides[index - 1].text); 
		slideTextLink.setProperty('href',slides[index - 1].url);
	}
	if (slideLink){
		slideLink.setProperty('href',slides[index - 1].url);
	}
}

var composeSrc = function(index)
{
	return pathToImages + slides[index - 1].imageName;
}

var initAccordion = function()
{
	var headers = $$('.nav-header');
	var contents = $$('.nav-content');
	var myEffect = new Fx.Accordion(headers, contents, {
		onActive:function(el){
			el.addClass('active');
		},
		onBackground:function(el){
			el.removeClass('active');
		},
		display:defaultItem - 1
	});
}

function createCookie(name,value,days) {
  if (days) {
    var date = new Date();
    date.setTime(date.getTime()+(days*24*60*60*1000));
    var expires = "; expires="+date.toGMTString();
  }
  else expires = "";
  document.cookie = name+"="+value+expires+"; path=/";
}

function readCookie(name) {
  var nameEQ = name + "=";
  var ca = document.cookie.split(';');
  for(var i=0;i < ca.length;i++) {
    var c = ca[i];
    while (c.charAt(0)==' ') c = c.substring(1,c.length);
    if (c.indexOf(nameEQ) == 0) return c.substring(nameEQ.length,c.length);
  }
  return null;
}


window.addEvent('domready',initPage);