if( typeof( SOFTADMIN_PREVIEW_WIDTH ) == 'undefined' ) {
    SOFTADMIN_PREVIEW_WIDTH = '900px';
}

function OpenPreview( name, url )
{
	obj = $( '#' + name );
	prv = $( '#' + name + 'tab' );
	cls = $( '#' + name + 'close' );
	lod = $( '#' + name + 'loading' );

	// assign iframe attributes
	$( obj ).css( 'width', SOFTADMIN_PREVIEW_WIDTH ).css( 'height', '100px' ).attr( 'src', url ).css( 'z-index', '10' ).css( 'display', 'block' ).setcenter();
	
	// add loading indicator
	$( lod ).css( 'width', SOFTADMIN_PREVIEW_WIDTH ).css( 'height', '100px' ).css( 'z-index', '20' ).css( 'display', 'block' ).addClass( 'softadmin_preview_loading' ).setcenter();
	
	// add onload binding to iframe
	$( obj ).bind( 'load', function( e ) { disable_elements( $( obj ) ); } );
	
	// assign background attributes
	$( prv ).css( 'z-index', '2' ).css( 'filter', 'Alpha(Opacity=60)' ).css( 'opacity', '.60' ).css( 'background-color', '#000000' ).css( 'position', 'fixed' ).css( 'display', 'block' );
	
	// assign close tab attributes
	$( cls ).css( 'width', SOFTADMIN_PREVIEW_WIDTH ).css( 'height', '30px' ).css( 'z-index', '10' ).css( 'display', 'block' ).setcenter();
}

// custom function to center the preview window
$.fn.setcenter = function() {
	$( this ).css( 'left', find_center( $( this ) ) + 'px' );
};

var setpreviewX;

function find_center( obj ) {
    // cache result
    if( setpreviewX > 0 ) { return setpreviewX; }
    
    // determine how much left padding we need to center the preview
    window_width = $( window ).width();
	obj_width = $( obj ).css( 'width' ).slice( 0, -2 ); // remove 'px'
    setpreviewX = ( window_width - obj_width ) / 2; // calculate the left padding
	setpreviewX = ( setpreviewX < 0 ) ? 0 : setpreviewX; // max left padding of 0
	
	return setpreviewX;
}

function disable_elements( obj ) {
    /* 
     * Nullify all links (except appropriate ones) and
     * Disable all form elements
     */
    
    goSetHeight(); // apply iframe resizing
    
    // Links
    $( obj ).contents().find( "a" ).bind( 'click', function(e) { return false; } ).attr( 'onclick', '' );
    
    // Form input elements
    $( obj ).contents().find( "input" ).attr( 'disabled', 'disabled' );
    
    // Form textarea elements
    $( obj ).contents().find( "textarea" ).attr( 'disabled', 'disabled' );
}

var getFFVersion = navigator.userAgent.substring( navigator.userAgent.indexOf("Firefox")).split("/")[1]
var FFextraHeight = getFFVersion >= 0.1 ? 16 : 0 // extra height in px to add to iframe in FireFox 1.0+ browsers

function ResizePreview( name )
{
	ifr = $( '#' + name );
	prv = $( '#' + name + 'tab' );
	lod = $( '#' + name + 'loading' );
	
	obj = document.getElementById( name ); // used to get iframe content height
	
	if( obj.contentDocument && obj.contentDocument.body.offsetHeight ) // ns6 syntax
	{
	    $( ifr ).animate( { height: ( obj.contentDocument.body.offsetHeight + FFextraHeight ) + 'px' }, 'normal' );
	}
	else if( obj.Document && obj.Document.body.scrollHeight ) // ie5+ syntax
	{
	    $( ifr ).animate( { height: obj.Document.body.scrollHeight + 'px' }, 'normal' );
	}
	
	$( lod ).css( 'height', '0px' ).css( 'z-index', '-1' ).css( 'display', 'none' ).removeClass( 'softadmin_preview_loading' );
	$( prv ).css( 'height', '100%' );
}

function goSetHeight()
{
	if( parent != window )
		return;
	else
		parent.ResizePreview( 'preview' );
}

function ClosePreview()
{
	if( parent == window )
		DestroyPreview( 'preview' );
	else
		parent.ClosePreview( 'preview' );
}

function DestroyPreview( name )
{
	obj = $( '#' + name );
	
	if( obj ) {
		
	    $( obj ).css( 'height', '0px' ).css( 'z-index', '-1' ).css( 'display', 'none' ).attr( 'src', '' );
		
		prv = $( '#' + name + 'tab' );
		
		if( prv ) {
		    $( prv ).css( 'z-index', '-2' ).css( 'filter', '' ).css( 'opacity', '' ).css( 'background-color', '' ).css( 'position', '' ).css( 'display', 'none' );
		}
		
		cls = $( '#' + name + 'close' );
		if( cls ) {
            $( cls ).css( 'height', '0px' ).css( 'z-index', '-1' ).css( 'display', 'none' );
		}
	}
	else
	{
		window.close();
	}
}
