
var start_up = true;

var galleryImages = [];
var gallery_index = 0;

var container;
var target;
var img;

var resizeTimer = null;

function resizeImageAndShow() 
{
    img.style.display = 'none';

    var maxWidth = ($(window).width() - parseInt(container.css('padding-left'), 10) - parseInt(container.css('padding-right'), 10)) - 0;
    var maxHeight = (($(window).height() > window.innerHeight ? window.innerHeight: $(window).height()) - parseInt(container.css('padding-top'), 10) - parseInt(container.css('padding-bottom'), 0)) - 0;
	
    if (img.width > maxWidth || img.height > maxHeight) {
        // One of these is larger than the window //
        var ratio = img.width / img.height;
        if (img.height >= maxHeight) {
            img.height = maxHeight;
            img.width = maxHeight * ratio;
        } else {
            img.width = maxWidth;
            img.height = maxWidth * ratio;
        }
    } else {
		var ratio = img.width / img.height;
		if ( maxWidth >= maxHeight ) {
			img.height = maxHeight;
			img.width = img.height * ratio;
		} else {
			img.width = maxWidth;
			img.height = img.width * ratio;
		}
	}
	
	container.animate({
        'width': maxWidth,
        'height': maxHeight,
		'top': 0 + 'px', 
		'left': 0 + 'px'
    },
    'normal',
    function() {
        target.append(img);
		$(target).css( "margin-top", (maxHeight-img.height)/2 + 'px' ); 
		$(target).css( "margin-left", (maxWidth-img.width)/2 + 'px' );
        
		$(img).fadeIn('fast',
        function() {
            container.removeClass('loading');
        });
    })

}

function loadImage( _url ) 
{
    img.src = _url;
}

function newImage( _url ) 
{
	container.addClass('loading');
	
	img = null;
    img = new Image();
    img.onload = resizeImageAndShow;

	loadImage( _url );
}

function clearImage()
{
	target.children().remove();
}

function nextPrevImage ( _e ) {
	_e.preventDefault();
	
	var target = $( _e.target );
	if ( target.is('a#prev') ) {
		gallery_index--;
	} else if( target.is('a#next') ) {
		gallery_index++;	
	}
	
	if ( gallery_index >= galleryImages.length ) {
		gallery_index = 0;
	} else if (gallery_index < 0 ) {
		gallery_index = galleryImages.length-1;
	}
	
	clearImage();
	newImage( galleryImages[gallery_index] );
	
}

function jj_gallery( _galleryImages )
{
	galleryImages = _galleryImages;
	
	container = $('div#splash_image_container');
	target = $(jQuery('<div class="target"></div>'));
	container.append(target);
	
	container.show().css({
        'top': Math.round((($(window).height() > window.innerHeight ? window.innerHeight: $(window).height()) - container.outerHeight()) / 2) + 'px',
        'left': Math.round(($(window).width() - container.outerWidth()) / 2) + 'px',
        'margin-top': 0,
        'margin-left': 0
    })

	$('a#prev').add( $('a#next') ).click( function( event ) {
		nextPrevImage( event );
	});
	
	// newImage( galleryImages[gallery_index] );
	
	$(window).bind("resize", function(){
		if (resizeTimer) clearTimeout(resizeTimer);
		resizeTimer = setTimeout(resizeImageAndShow, 100);
	 });
	
	// checkForMouseMove();
	
}

/*

// TIMEOUT //

var timeout 		= null;
var timed_out 		= false;
var time_out_time 	= 6000;

function timedOut() 
{
	$("div#gallerynav_maincontainer").fadeOut('slow');
	timed_out = true;
	// console.log( 'timedOut');
}

function resetTimer() 
{
	if ( timed_out ) {
		// console.log("show again");
		$("div#gallerynav_maincontainer").fadeIn();
		timed_out = false;
	} else {
		// console.log("resetTimer");
		timeout = null;
		timeout = setTimeout( timedOut, 4000);
	}
}

function checkForMouseMove( ) {
	
	timeout = setTimeout( timedOut, 4000);
	container.mousemove( function(e) {
		resetTimer();
    });
	
}

*/

