var ext = 'png';
if( jQuery.browser.msie && jQuery.browser.version < 7 ) { ext = 'gif'; }

var bottles = [
    { 'src': HTML_ROOT + '/images/temp/rum-silver.' + ext },
    { 'src': HTML_ROOT + '/images/temp/rum-gold.' + ext },
    { 'src': HTML_ROOT + '/images/temp/rum-platinum.' + ext }
];

//Settings
var faderSettings = {
	timing: 5000,
	fadeSpeed: 900,
	numberOfImages: 3
};

jQuery.preloadImages = function() {
	var a = ( typeof arguments[0] == 'object' ) ? arguments[0] : arguments;
	for( var i = a.length -1; i > 0; i--)
	{
		jQuery( '<img>' ).attr( 'src', a[i].src );
	}
};

function init_table_fix()
{
    var _height = $( '.page_content' ).height();
    _height = ( _height - 360 );
    
    $( '.page_fill_left' ).css( 'height', _height + 'px' );
    $( '.page_fill_right' ).css( 'height', _height + 'px' );
}

$( document ).ready( function() {
    $.preloadCssImages(); // preload images
    $.preloadImages( bottles ); // preload our bottles
    
    if( typeof faderSettings != 'undefined' && faderSettings.numberOfImages > 1 )
    {
        var imageFade = outer();
        var cycleMe = setInterval( imageFade, faderSettings.timing );
    }
    
    init_position();
    
    // gallery image
    $(".default .jCarouselLite").jCarouselLite( {
        btnNext: ".default .next",
        btnPrev: ".default .prev",
        visible: 1,
        circular: false
    } );
    
    // ie6 png fix
    $( document ).pngFix();
} );

$( window ).load ( function() {
    init_table_fix();
} );

function init_position()
{
    // frame size
    var _maxheight = 205;
    var _maxwidth = 460;
    
    $( '#jCarouselLiteDemo .carousel li img' ).each( function() {
        
        var _height = $( this ).attr( 'sheight' );
        var _width = $( this ).attr( 'swidth' );
        
        // force image size based off getimagesize()
        $( this ).attr( 'height', _height );
        $( this ).attr( 'width', _width );
        $( this ).show();
        
        // center image
        var _bufferwidth = -( ( _width - _maxwidth ) / 2 );
        var _bufferheight = -( ( _height - _maxheight ) / 2 );
        $( this ).css( {
            'margin-top': _bufferheight,
            'margin-left': _bufferwidth
        } );
    } );
}

/**
 * Image Cycle
 **/
function displayImage( id, prev, image )
{
    //$( '#bottle' ).attr( 'src', HTML_ROOT + '/images/temp/rum-' + colour + '.' + ext );
	//alert( prev );
	$( '#bottle_' + prev ).fadeOut( faderSettings.fadeSpeed, function() {
	    $( '#bottle_' + id ).fadeIn( faderSettings.fadeSpeed );
	} );
};

function outer()
{
	var counter = 0;
	var previous = faderSettings.numberOfImages;
	function inner()
	{
	    counter++;
	    previous++;
		if( counter >= faderSettings.numberOfImages ) { counter = 0; }
		if( previous >= faderSettings.numberOfImages ) { previous = 0; }
		
		displayImage( counter, previous, bottles[counter] );
	}
	return inner;
}
/**
 * End Image Cycle
 **/