var index_id = 0;

// doc ready for image related events
$( document ).ready ( function() {
    init_position();
    news_image_position();
    $( 'input:text' ).hint();

    if( typeof faderSettings != 'undefined' && faderSettings.numberOfImages > 1 && typeof js_array != 'undefined' )
    {
        $.preloadImages( js_array );
        var imageFade = outer();
        var cycleMe = setInterval( imageFade, faderSettings.timing );
    }
} );

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 );
	}
};

//image Next / Prev image area resizer
function view_image( direction ) {
    
    if( !js_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', js_array[index_id]['src'] );
    $( '#imagedesc' ).css( 'display', 'none' );//lets start by hiding the image
    $( '#imagenav' ).css( 'display', 'none' );//lets start by hiding the imagenav
    $( '#imageclose' ).css( 'display', 'none' );//lets start by hiding the image close tab
    var __width = js_array[index_id]['width'];
    var __height = js_array[index_id]['height'];
    
    // split size
    var __split_width = ( __width / 2 ) - 2;
    var __split_height = ( __height ) - 25;
    
    // 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', js_array[index_id]['width'] );
    $( '#image_file' ).attr( 'height', js_array[index_id]['height'] );

    // set the holder's height
    $( '#file_holder' ).attr( 'height', ( cur_height ) + 'px' );
    $( '#file_holder' ).attr( 'width', ( cur_width ) + 'px' );
    
    var firstname;
    
    firstname = '';
    if( typeof js_array[index_id]['first_name'] != 'undefined' ){
    	firstname = '<b>'+js_array[index_id]['first_name']+'</b><br>';
    }

    //lets put the new title and description in the imagedesc div
    $( '#imagedesc' ).html( firstname+'<b><i>'+js_array[index_id]['title']+ '</i></b><br>' +  js_array[index_id]['description']);
    // id counter
    $( '#image_id_text' ).text( ( index_id + 1 ) );
    
    var x_dir = '+';
    var y_dir = '+';
    
    var adj_x = ( ( cur_width - js_array[index_id]['width'] ) / 2 );
    if( adj_x < 0 ) {
        x_dir = '-';
        adj_x = Math.abs( adj_x );
    }
    
    var adj_y = ( ( cur_height - js_array[index_id]['height'] ) / 2 );
    if( adj_y < 0 ) {
        y_dir = '-';
        adj_y = Math.abs( adj_y );
    }
    
    // determine if by moving our image, we go above the top/left of page
    // if so, we just set it to top/left of page instead
    var offset = $( '#simplemodal-container' ).offset();
    if( ( offset.left - adj_x ) < 0 ) { adj_x = 0; }
    if( ( offset.top - adj_y ) < 0 ) { adj_y = 0; }
    
    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( js_array[index_id]['width'] ) ) + 'px',
                height: ( Math.abs( js_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( !js_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( !js_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;
}

// center image within div layout limit
function init_position( show )
{
    if( typeof show == 'undefined' ) show = true;
    
    // frame size
    var _maxheight = 75;
    var _maxwidth = 100;
    
    $( '#main_logo, .main_logo' ).each( function() {
    	var _height = $( this ).attr( 'sheight' );
        var _width = $( this ).attr( 'swidth' );
        
        // force image size based off getimagesize()
        if( show === true ) {
            $( this ).hide();
        }
        
        $( this ).attr( 'height', _height );
        $( this ).attr( 'width', _width );
        
        // center image
        var _bufferwidth = -( ( _width - _maxwidth ) / 2 );
        var _bufferheight = -( ( _height - _maxheight ) / 2 );
        $( this ).css( {
            'margin-top': _bufferheight,
            'margin-left': _bufferwidth
        } );
        
        if( show === true ) {
            $( this ).show();
        }
    } );
    
}

//center image within div layout limit
function news_image_position( show )
{
    if( typeof show == 'undefined' ) show = true;
    
    // frame size
    var _maxheight = 90;
    var _maxwidth = 120;
    
    $( '.news_image' ).each( function() {
    	var _height = $( this ).attr( 'sheight' );
        var _width = $( this ).attr( 'swidth' );
        
        // force image size based off getimagesize()
        if( show === true ) {
            $( this ).hide();
        }
        
        $( this ).attr( 'height', _height );
        $( this ).attr( 'width', _width );
        
        // center image
        var _bufferwidth = -( ( _width - _maxwidth ) / 2 );
        var _bufferheight = -( ( _height - _maxheight ) / 2 );
        $( this ).css( {
            'margin-top': _bufferheight,
            'margin-left': _bufferwidth
        } );
        
        if( show === true ) {
            $( this ).show();
        }
    } );
    
}

/**
 * Image Cycle
 **/
function displayImage( image )
{
	$( "#main_logo" ).fadeOut( faderSettings.fadeSpeed, function()
	{
	    $( this ).load( function() {
	        
	        $( this ).attr( 'swidth', image.mwidth );
	        $( this ).attr( 'sheight', image.mheight );
    		
    		init_position( false );
    		
	    } ).attr( 'src', image.src );
	    
	    $( this ).fadeIn( faderSettings.fadeSpeed );
		
	} );
};

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