var splashLoopIndex = 1;
var splashIntervalId = 0;
var splashTimeoutId = 0;
var splashFadeInSpeed = 2500;
var splashFadeOutSpeed = 2500;
var splashIntervalSpeed = 5000;
var splashHoverFlag = false;

function startSlides()
{
    // show background loader
	$('#slideshow').toggleClass('slideshow-loader', true);
	$('#slideshow div.splash-image').hide();

    // for handling overlaybar hover state before first image loads.
    $("#slideshow").hover(
        function() {
            splashHoverFlag = true;
        },
        function() {
            splashHoverFlag = false;
        }
    );
    	
    $("#slideshow div.splash-image").hover(
        function() {
            splashHoverFlag = true;
            if (splashIntervalId) {
                window.clearInterval(splashIntervalId);
                splashIntervalId = 0;
            }
                        
            $(this).find('.overlaybar').show();
        },
        function() {
            splashHoverFlag = false;
            if (!splashIntervalId) {
            	splashIntervalId = window.setInterval("changeSlide()", splashIntervalSpeed);
            }
            
            $(this).find('.overlaybar').hide();
        }
    );
    	
    // double check to make sure it's loaded. already preloading from scripts loaded from ajax call.
    loadImg = new Image();
    loadImg.onload = function() {
        if (splashHoverFlag) {
            $('#slideshow div.splash-image:nth-child(1)').find('.overlaybar').show();
        } else {
            $('#slideshow div.splash-image').find('.overlaybar').hide();
        }
            	
        $('#slideshow div.splash-image:nth-child(1)').fadeIn(splashFadeInSpeed, function(){
        	$('#slideshow').toggleClass('slideshow-loader', false);
        	
            if (!splashIntervalId && !splashHoverFlag) {
                splashIntervalId = window.setInterval("changeSlide()", splashIntervalSpeed);
            }
        });
    }
    loadImg.onerror = function() {
        if (splashHoverFlag) {
            $('#slideshow div.splash-image:nth-child(1)').find('.overlaybar').show();
        } else {
            $('#slideshow div.splash-image').find('.overlaybar').hide();
        }
            	
        $('#slideshow div.splash-image:nth-child(1)').fadeIn(splashFadeInSpeed, function(){
            if (!splashIntervalId && !splashHoverFlag) {
                splashIntervalId = window.setInterval("changeSlide()", splashIntervalSpeed);
            }
        });    	
    }    
    
    src = $('#slideshow div.splash-image:nth-child(1) img').attr('src');
    loadImg.src = src;
}

function changeSlide()
{
	// clear current interval
	window.clearInterval(splashIntervalId);
	splashIntervalId = 0;
	
	// update current splash index
    if ( splashLoopIndex >= $("#slideshow div.splash-image").size() ) {
        splashLoopIndex = 1;
    } else {
        splashLoopIndex++;
    }

    loadImg = new Image();
    loadImg.onload = function() {
        if (splashHoverFlag) {
          $('#slideshow div.splash-image:nth-child(' + splashLoopIndex + ')').find('.overlaybar').show();
        } else {
        	$('#slideshow div.splash-image').find('.overlaybar').hide();
        }
    	
        $('#slideshow div.splash-image:visible').fadeOut(splashFadeOutSpeed);
        $('#slideshow div.splash-image:nth-child(' + splashLoopIndex + ')').fadeIn(splashFadeInSpeed, function(){
        	  if (!splashIntervalId) {
        		  splashIntervalId = window.setInterval("changeSlide()", splashIntervalSpeed);
        	  }
        });
    }
    loadImg.onerror = function() {
    	if (splashHoverFlag) {
    		$('#slideshow div.splash-image:nth-child(' + splashLoopIndex + ')').find('.overlaybar').show();
    	} else {
    		$('#slideshow div.splash-image').find('.overlaybar').hide();
    	}

    	$('#slideshow div.splash-image:visible').fadeOut(splashFadeOutSpeed);
    	$('#slideshow div.splash-image:nth-child(' + splashLoopIndex + ')').fadeIn(splashFadeInSpeed, function(){
    		if (!splashIntervalId) {
    			splashIntervalId = window.setInterval("changeSlide()", splashIntervalSpeed);
    		}
    	});
    }
    
    src = $('#slideshow div.splash-image:nth-child(' + splashLoopIndex + ') img').attr('src');
    loadImg.src = src;
}
