// optimize browser size and aspect ratio for full image experience
var optimizedAspectRatio = .65;
var ieAssumedMenuHeight = 120;
var maxHeight = window.screen.availHeight;
var maxWidth = window.screen.availWidth;
var browserHeight = window.outerHeight ? window.outerHeight : document.documentElement.clientHeight;
var browserWidth = window.outerWidth ? window.outerWidth : document.documentElement.clientWidth;
var siteHeight = window.innerHeight ? window.innerHeight : document.documentElement.clientHeight;
var siteWidth = window.innerWidth ? window.innerWidth : document.documentElement.clientWidth;
var maxSiteHeight = browserHeight != siteHeight ? maxHeight - (browserHeight - siteHeight) : maxHeight - ieAssumedMenuHeight;
var maxSiteWidth = maxSiteHeight / optimizedAspectRatio;
if(maxSiteWidth > maxWidth){maxSiteHeight = maxWidth * optimizedAspectRatio; maxSiteWidth = maxWidth;}
var newBrowserHeight = browserHeight != siteHeight ? maxSiteHeight + (browserHeight - siteHeight) : maxSiteHeight + ieAssumedMenuHeight;
var newBrowserWidth = maxSiteWidth + (browserWidth - siteWidth);

function optimizeThisScreen(newBrowserWidth,newBrowserHeight) {
	var centerBrowser = (maxWidth - newBrowserWidth) / 2;
	window.moveTo(centerBrowser,0);
	window.resizeTo(newBrowserWidth,newBrowserHeight);
}


// run background slide show...
/* Original from: http://brainerror.net/scripts/javascript/blendtrans/demo.html, customized by SiennaCreative.com  */
function runShow() {
	document.documentElement.style.overflowY = "hidden"; //documentElement required for cross browser compatability on the html/body style 
    carouselRun('slideDeck');
}

//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 = 10;  // original value was 50
    }    
    if(!displayDuration ) {
        displayDuration = 9000;
    }
    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 screenHeight = window.innerHeight ? window.innerHeight : document.documentElement.clientHeight;
	var screenWidth = window.innerWidth ? window.innerWidth : document.documentElement.clientWidth;
	var imageAspectRatio = image.offsetHeight / image.offsetWidth;  // calculate screen/image width slop, to optimize for a full screen experience
	var newImageHeight = screenWidth * imageAspectRatio;
	var heightWaste = newImageHeight - screenHeight;
	var lostPXtolerance = (imageAspectRatio >= .70 && imageAspectRatio <= .80) ? 240 : 100;	// allow upto 240 pix overflow for slightly more boxed format, else 100
	if (heightWaste >=  0 && heightWaste <= lostPXtolerance) {
		image.style.width=screenWidth+'px'; 
		image.style.height='auto'; 
	} else {
		image.style.width='auto'; 
		image.style.height=screenHeight+'px'; 
	}
    rememberImage(image.src);
	image.style.display = 'block';
	if (!image.alt.length) document.getElementById('captionPanel').style.display='none' 
	else document.getElementById('captionPanel').style.display='block',document.getElementById('caption').innerHTML = image.alt;
    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(function(){ optimizeThisScreen(newBrowserWidth,newBrowserHeight) });
addOnloadEvent(runShow);
