var index_id = 0;

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 view_image( direction ) {
    
    if( !img_array[(index_id + direction)] ) { return false; }
    
    index_id = (index_id + direction);
    
    // get current information about image
    var cur_width  = $( '#image_file' ).width();
    var cur_height = $( '#image_file' ).height();
    
    // update the image
    $( '#image_panel_left' ).css( 'display', 'none' );
    $( '#image_panel_right' ).css( 'display', 'none' );
    $( '#image_file' ).css( 'display', 'none' ); // hide it while we resize
    $( '#image_file' ).attr( 'src', img_array[index_id]['src'] );
    
    var __width = img_array[index_id]['width'];
    var __height = img_array[index_id]['height'];
    
    // split size
    var __split_width = ( __width / 2 ) - 2;
    var __split_height = ( __height ) - 70;
    
    // update clickable area
    $( '#image_panel_left' ).css( {
        'width': __split_width + 'px',
        'height': __height + 'px',
        'cursor': 'pointer'
    } );
    
    $( '#image_nav_left' ).css( {
        'width': __split_width + 'px',
        'height': __split_height + 'px'
    } );
    
    $( '#image_panel_right' ).css( {
        'width': __split_width + 'px',
        'height': __height + 'px',
        'cursor': 'pointer'
    } );
    
    $( '#image_nav_right' ).css( {
        'width': __split_width + 'px',
        'height': __split_height + 'px'
    } );

    // assign image size
    $( '#image_file' ).attr( 'width', img_array[index_id]['width'] );
    $( '#image_file' ).attr( 'height', img_array[index_id]['height'] );

    // set the holder's height
    $( '#file_holder' ).attr( 'height', ( cur_height ) + 'px' );
    $( '#file_holder' ).attr( 'width', ( cur_width ) + 'px' );
    
    // id counter
    $( '#image_id_text' ).text( ( index_id + 1 ) );
    
    var x_dir = '+';
    var y_dir = '+';
    
    var adj_x = ( ( cur_width - img_array[index_id]['width'] ) / 2 );
    if( adj_x < 0 ) {
        x_dir = '-';
        adj_x = Math.abs( adj_x );
    }
    
    var adj_y = ( ( cur_height - img_array[index_id]['height'] ) / 2 );
    if( adj_y < 0 ) {
        y_dir = '-';
        adj_y = Math.abs( adj_y );
    }
    
    if( adj_x == 0 && adj_y == 0 ) {
        $( '#image_file' ).css( 'display', 'block' );
        $( '#image_panel_left' ).css( 'display', 'block' );
        $( '#image_panel_right' ).css( 'display', 'block' );
    }
    else {
        $( '#file_holder' ).animate(
            { 
                width: ( Math.abs( img_array[index_id]['width'] ) ) + 'px',
                height: ( Math.abs( img_array[index_id]['height'] ) + 10 ) + 'px'
            },
            {
                duration: 800
            }
        );
        
        $( '#simplemodal-container' ).animate(
            {
                left: x_dir + '=' + adj_x + 'px',
                top: y_dir + '=' + adj_y + 'px'
            },
            {
                duration: 800,
                complete: function() {
                    $( '#image_file' ).css( 'display', 'block' );
                    $( '#image_panel_left' ).css( 'display', 'block' );
                    $( '#image_panel_right' ).css( 'display', 'block' );
                }
            }
        );
    }
        
    if( !img_array[(index_id+1)] ) {
        $( '#image_next' ).attr( 'src', HTML_ROOT + '/images/temp/transparent.gif' );
        $( '#text_next' ).addClass( 'gray' ).unbind();
        $( '#image_panel_right' ).unbind().css( 'cursor', 'auto' );
    }
    else {
        $( '#image_next' ).attr( 'src', HTML_ROOT + '/images/temp/viewer_next.gif' );
        $( '#text_next' ).removeClass( 'gray' ).unbind().bind( 'click', function(e) { return view_image( +1 ); } );
        $( '#image_panel_right' ).unbind().bind( 'click', function(e) { return view_image( +1 ); } );
    }
    
    if( !img_array[(index_id-1)] ) {
        $( '#image_prev' ).attr( 'src', HTML_ROOT + '/images/temp/transparent.gif' );
        $( '#text_prev' ).addClass( 'gray' ).unbind();
        $( '#image_panel_left' ).unbind().css( 'cursor', 'auto' );
    }
    else {
        $( '#image_prev' ).attr( 'src', HTML_ROOT + '/images/temp/viewer_prev.gif' );
        $( '#text_prev' ).removeClass( 'gray' ).unbind().bind( 'click', function(e) { return view_image( -1 ); } );
        $( '#image_panel_left' ).unbind().bind( 'click', function(e) { return view_image( -1 ); } );
    }

    return false;
}

$( document ).ready ( function() {
    init_position();
} );

function init_position()
{
    // frame size
    var _maxheight = 265;
    var _maxwidth = 400;
    
    var _height = $( '#main_logo' ).attr( 'mheight' );
    var _width = $( '#main_logo' ).attr( 'mwidth' );
    
    // force image size based off getimagesize()
    $( '#main_logo' ).hide();
    $( '#main_logo' ).attr( 'height', _height );
    $( '#main_logo' ).attr( 'width', _width );
    
    // center image
    var _bufferwidth = -( ( _width - _maxwidth ) / 2 );
    var _bufferheight = -( ( _height - _maxheight ) / 2 );
    $( '#main_logo' ).css( {
        'margin-top': _bufferheight,
        'margin-left': _bufferwidth
    } );
    
    $( '#main_logo' ).show();
}