var currentImage;
var currentId;
var imageCount;
var defaultWidth;
var defaultHeight;

var smallerby = 10;
var smallerwidth;
var	smallerheight;
var lastTimer;

/*
function scaleImage() {
	if( currentImage.width() > $('#content_box').width() ) {
		lastTimer = window.setTimeout( "scaleImage()", 100 ); 
		currentImage.width( currentImage.width() - smallerby );
		currentImage.height( currentImage.height() - Math.floor( ( defaultWidth / defaultHeight - 1 ) * 10 ) );
		currentImage.css( 'margin-left', parseInt( currentImage.css( 'margin-left' ) ) + 5 + "px" );
		currentImage.css( 'margin-top', parseInt( currentImage.css( 'margin-top' ) ) + 3 + "px");
	}	
}

function setImage() {
	currentImage.css( 'margin-left', "-300px" );
	currentImage.css( 'margin-top', "-180px" );
	currentImage.width( defaultWidth );
	currentImage.height( defaultHeight );
}
*/

function initImage() {
// 	currentId = 0;
	currentId = Math.floor( Math.random() * imageCount + 1 );
	currentImage = $('.bg_image').eq( currentId );
/*	defaultWidth = currentImage.width();
//	defaultHeight = currentImage.height();
//	smallerwidth =  smallerby / 2;
//	smallerheight = Math.floor( ( defaultWidth / defaultHeight - 1 ) / 2 * 10 );
//	setImage();
//	scaleImage();
*/
	currentImage.fadeIn( fade, 'linear', window.setTimeout( "nextImage()", imageTimeout) );
}

function nextImage() {
	currentImage.fadeOut( fade ) //, function() {
		clearTimeout( lastTimer );
		if( currentId >= imageCount ) {
			currentId = 0;	
		}
		else {
			currentId++;	
		}	
		currentImage = $('.bg_image').eq( currentId );
//		setImage();
//		scaleImage();
		currentImage.fadeIn( fade, 'linear', window.setTimeout( "nextImage()", imageTimeout) );
//	});
}
/*
if( scaling == "scale" ) { }
else {
scaleImage = function() { };
setImage = function() { 
	currentImage.width( 800 );
	currentImage.height( 481 );
	currentImage.css( 'left', "0px" );
	currentImage.css( 'top', "0px" );
};
}
*/
$( document ).ready( function() {
	imageCount = $('.bg_image').length - 1;
});

$( window ).load( function() {
	if (imageCount > 1 ) {
		initImage();
	}
	else {
		$('.bg_image').eq( 0 ).fadeIn( fade );
	}
$( function() { $('.scroll-pane').jScrollPane(); } );
});
