// run background slide show...
/* Original from: http://brainerror.net/scripts/javascript/blendtrans/demo.html, customized by SiennaCreative.com  */
var slideThing = 0;
var currentImage = 0;
var currentSlide = 0;
var showDuration = 14000;
var showSpeed = 10;

function runShow() {
	carouselRun('mySlideDeck'); //css div tag name around images
}

//find first image inside image deck
function firstChildImage(o) {    
    o = o.firstChild;        
    while(o && o.tagName != 'IMG') {
        o = o.nextSibling;
    }    
    return o;
}

//get next image in the deck
function nextSlide(o) {
    do o = o.nextSibling;
    while(o && o.tagName != 'IMG');
    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(deckName, transitionSpeed, displayDuration, img, slideNumber, overrun) {
    if(!overrun) overrun = false;
	if(!transitionSpeed) {
        transitionSpeed = showSpeed;  // original value was 50
    }    
    if(!displayDuration ) {
        displayDuration = showDuration;
    }
	if (img) {
		var image =img;	
		var slideNum = slideNumber;
	}
	else {
		var image = firstChildImage(document.getElementById(deckName));	
		var slideNum = 0;
	}
    startFade(image, transitionSpeed, displayDuration, slideNum, overrun);
}

//make image a block-element and set the caption
function startFade(image, transitionSpeed, displayDuration, slideNum, override) {
	slideNum += 1;	
	if(slideNum > 1 && ! override) {
		setOpacity(image,0);
		image = getnextSlide(image);
		if(slideNum != 1) document.getElementById('indicate'+(slideNum-1)).src = 'images/icon_slideIndicator-Off.gif';
		if(image == firstChildImage(image.parentNode)) slideNum = 1;
	}
	document.getElementById('indicate'+slideNum).src = 'images/icon_slideIndicator-On.gif';
	if(navigator.userAgent.match(/iPhone/i)) document.getElementById('homeWallpaper').style.width = '1280px';  // for proper iphone centering of body content
	if(navigator.userAgent.match(/iPad/i)) document.getElementById('homeWallpaper').style.width = '1400px';  // for proper ipad centering of body content
	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';
	//use image id for caption storage, use alt for seo phrasing, and keep title="" to keep mouseover quiet
	if (!image.id.length) document.getElementById('storyPanel').style.display='none'  
	else document.getElementById('storyPanel').style.display='block',document.getElementById('caption').innerHTML = image.id;
	currentSlide = slideNum;
	currentImage = image;
    continueFade(image, 0, transitionSpeed, displayDuration, slideNum);
}

//set an increased opacity and check if the image is done blending
function continueFade(image, opacity, transitionSpeed, displayDuration, slideNum) {
    opacity += 10;
	if (opacity < 106) {
		slideThing = setTimeout(function() {fadeThis(image, opacity, transitionSpeed, displayDuration, slideNum)}, transitionSpeed);
	} else {
		//get the next image and start fade again
		slideThing = setTimeout(function() {startFade(image, transitionSpeed, displayDuration, slideNum)}, displayDuration);		
    }
}

//set the opacity to a new value and continue fading
function fadeThis(image, opacity, transitionSpeed, displayDuration, slideNum) {
    setOpacity(image,opacity);
    continueFade(image, opacity, transitionSpeed, displayDuration, slideNum);
}

function overRide(image,slideNum) {
	clearTimeout (slideThing);
	document.getElementById('indicate'+(currentSlide)).src = 'images/icon_slideIndicator-Off.gif';
	currentImage.style.display='none';
	document.getElementById('storyPanel').style.display='none'
	image = document.getElementById(image);
	carouselRun('mySlideDeck', showSpeed, showDuration, image, slideNum, true);
}


// 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);
