
var FADER = 0;
var CUR = 1;
var PREV = 2;
var NEXT = 3;

function Slideshow_Markup(slideIndex, targetDiv) {
  var captionDiv = '';
  if(slideImages[slideIndex]){
    var imgtag = '<img src="'+imageSitePath+slideImages[slideIndex]+'" width="'+slideImageWidth+'" height="'+slideImageHeight+'" />';
    
    if(showCaptions != 'no_captions'){
      if(slideCaptions[slideIndex]){
          captionDiv = new Element('div', {
            'class': 'caption'
          });
          captionDiv.setOpacity(0.8);
          if (targetDiv == PREV || targetDiv == NEXT) {
            captionDiv.setStyle('visibility', 'hidden');
          }
          captionDiv.setHTML(slideCaptions[slideIndex]);
          if(showCaptions == 'with_credits'){
            if (slideCredits[slideIndex]) {
	            var creditSpan = new Element('span', {
	              'class' : 'credit'
	            });
	            creditSpan.setText(" (" + slideCredits[slideIndex] + ")");
	            captionDiv.adopt(creditSpan);
	        }
          }
      }
    }
    
    switch(targetDiv) {
	    case FADER:
	        $('slide_fader').setHTML(imgtag);
	        if (captionDiv) { captionDiv.injectInside('slide_fader'); }
	        break;
	    case CUR:
	        $('slide_current').setHTML(imgtag);
	        if (captionDiv) { captionDiv.injectInside('slide_current'); }
	        break;
	    case PREV:
	        $('slide_previous').setHTML(imgtag);
	        if (captionDiv) { captionDiv.injectInside('slide_previous'); }
	        break;
	    case NEXT:
	        $('slide_next').setHTML(imgtag);
	        if (captionDiv) { captionDiv.injectInside('slide_next'); }
	        break;
    }
  }
}

function Slideshow_GetPreviousSlideIndex(){
    if(currentSlideIndex == 0){
        return slideImages.length - 1;
    }
    else{
        return currentSlideIndex - 1;
    }
}

function Slideshow_GetNextSlideIndex(){
    if(currentSlideIndex == (slideImages.length - 1)){
        return 0;
    }
    else{
        return currentSlideIndex + 1;
    }
}

function Slideshow_ShowPreviousSlide(){
    $('previousButton').removeEvents('click');
    (function(){$('previousButton').addEvent('click', function () {Slideshow_ShowPreviousSlide()})}).delay(1000);
    Slideshow(Slideshow_GetPreviousSlideIndex());
}

function Slideshow_ShowNextSlide(){
    $('nextButton').removeEvents('click');
    (function(){$('nextButton').addEvent('click', function () {Slideshow_ShowNextSlide()})}).delay(1000);
    Slideshow(Slideshow_GetNextSlideIndex());
}

function Slideshow(newSlideIndex) {
  var fader = $('slide_fader');
  fader.setOpacity(0);
  Slideshow_Markup(newSlideIndex, FADER);
  var fadeEffect = fader.effect('opacity', {duration: 1000,transition: Fx.Transitions.linear});
  fadeEffect.start(1).chain(function () {
      $("slide_current").innerHTML = $("slide_fader").innerHTML;
      $("slide_fader").innerHTML = "";
      currentSlideIndex = newSlideIndex;
      Slideshow_Markup(Slideshow_GetPreviousSlideIndex(), PREV);
      Slideshow_Markup(Slideshow_GetNextSlideIndex(), NEXT);
  });

}

function Slideshow_NavMouseOver(myState){
    if((myState != "next") && (myState != "previous")) myState = "gallery";
    var myElement = $(myState + "Button");
    myElement.className = myElement.className + " " + myState + "_over";
}

function Slideshow_NavMouseOut(myState){
    if((myState != "next") && (myState != "previous")) myState = "gallery";
    var myRe = new RegExp(" " + myState + "_over");
    var myElement = $(myState + "Button");
    myElement.className = myElement.className.replace(myRe,"");
}


window.addEvent('domready', function() {
    Slideshow_Markup(firstSlideIndex, CUR);
    Slideshow_Markup(Slideshow_GetPreviousSlideIndex(), PREV);
    Slideshow_Markup(Slideshow_GetNextSlideIndex(), NEXT);

    $('slide_fader').setOpacity(0);
    $('slide_fader').setStyle('visibility', 'visible');
    $('slide_current').setOpacity(1);

    $('previousButton').addEvents({
        'click': function () {Slideshow_ShowPreviousSlide()},
        'mouseover': function () {Slideshow_NavMouseOver('previous')},
        'mouseout': function () {Slideshow_NavMouseOut('previous')}
    });
    
    $('nextButton').addEvents({
        'click': function () {Slideshow_ShowNextSlide()},
        'mouseover': function () {Slideshow_NavMouseOver('next')},
        'mouseout': function () {Slideshow_NavMouseOut('next')}
    });
    if ($('galleryButton')) {
	    $('galleryButton').addEvents({
	        'click': function () {top.location.href = galleryURL},
	        'mouseover': function () {Slideshow_NavMouseOver('gallery')},
	        'mouseout': function () {Slideshow_NavMouseOut('gallery')}
	    });
    }

});

