// run background slide show...
/* Original from: http://brainerror.net/scripts/javascript/blendtrans/demo.html, customized by SiennaCreative.com  */
function runShow() {
	carouselRun('slideDeck'); //css div tag around images
}

//get next image
function nextSlide(o) {
    do o = o.nextSibling;
    while(o && o.tagName != 'IMG');
    return o;
}

//find first image inside an element
function firstChildImage(o) {    
    o = o.firstChild;        
    while(o && o.tagName != 'IMG') {
        o = o.nextSibling;
    }    
    return o;
}

//set the opacity of an element to a specified value
function setOpacity(obj, o) {
    obj.style.opacity = (o / 100);
    obj.style.MozOpacity = (o / 100);
    obj.style.KhtmlOpacity = (o / 100);
    obj.style.filter = 'alpha(opacity=' + o + ')';
}

//make image invisible and set next one as current image
function getnextSlide(image) {	
    if (nextImage = nextSlide(image)) {
		image.style.display = 'none';
		nextImage.style.display = 'block';
    } else {
		//if there is not a next image, get the first image again
		nextImage = firstChildImage(image.parentNode);
    }
    return nextImage;
}

//set default values for parameters and starting image
function carouselRun(id, transitionSpeed, displayDuration, caption) {
    if(!transitionSpeed) {
        transitionSpeed = 50;  // original value was 50
    }    
    if(!displayDuration ) {
        displayDuration = 7500;
    }
    var image = firstChildImage(document.getElementById(id));
	var engineStart = true;
    startFade(image, transitionSpeed, displayDuration, caption, engineStart);
}

//make image a block-element and set the caption
function startFade(image, transitionSpeed, displayDuration, caption, engineStart) {
	if(!engineStart) {
		setOpacity(image,0);
		image = getnextSlide(image);
	}
	//var imageWidth = 1680;
	//var screenWidth = window.innerWidth ? window.innerWidth : document.documentElement.clientWidth;
	//var positionImage = ((screenWidth - imageWidth) / 2)+'px';
	//image.style.marginLeft = positionImage;
	image.style.display = 'block';
	if (!image.title.length) document.getElementById('descriptionPanel').style.display='none' 
	else document.getElementById('descriptionPanel').style.display='block',document.getElementById('caption').innerHTML = image.title;
    continueFade(image, 0, transitionSpeed, displayDuration, caption);
}

//set an increased opacity and check if the image is done blending
function continueFade(image, opacity, transitionSpeed, displayDuration, caption) {
    opacity += 10;
	if (opacity < 106) {
		setTimeout(function() {fadeThis(image, opacity, transitionSpeed, displayDuration, caption)}, transitionSpeed);
	} else {
		//get the next image and start fade again
		setTimeout(function() {startFade(image, transitionSpeed, displayDuration, caption)}, displayDuration);		
    }
}

//set the opacity to a new value and continue fading
function fadeThis(image, opacity, transitionSpeed, displayDuration, caption) {
    setOpacity(image,opacity);
    continueFade(image, opacity, transitionSpeed, displayDuration, caption);
}


// add and run mulitple onload events...
function addOnloadEvent(fnc){
  if ( typeof window.addEventListener != "undefined" )
    window.addEventListener( "load", fnc, false );
  else if ( typeof window.attachEvent != "undefined" ) {
    window.attachEvent( "onload", fnc );
  }
  else {
    if ( window.onload != null ) {
      var stackOnLoad = window.onload;
      window.onload = function ( e ) {
        stackOnLoad( e );
        window[fnc]();
      };
    }
    else 
      window.onload = fnc;
  }
}

addOnloadEvent(runShow);