var nn = !!document.layers;
var ie = !!document.all;
/** check if XPCOM is supported */
var mozilla = !!window.Components;

window.onload = function()
{
    window.clipboard = new Clipboard();
}

/**
 * A clipboard that can write and read from the system clipboard.
 */
function Clipboard()
{
    if ( nn )
    {
        netscape.security.PrivilegeManager.enablePrivilege( 'UniversalSystemClipboardAccess' );
        var tmp = new java.awt.Frame();
        this.clipboard = fr.getToolkit().getSystemClipboard();
    }
    else if ( ie )
    {
        this.clipboard = document.createElement( 'INPUT' );
        with ( this.clipboard.style )
        {
            position = 'absolute';
            left = '0px';
            top = '0px';
            visibility = 'hidden';
        }
        document.body.appendChild( this.clipboard );
    }
    else if ( mozilla )
    {
        netscape.security.PrivilegeManager.enablePrivilege( 'UniversalXPConnect' );
        this.clipboardid = Components.interfaces.nsIClipboard;
        this.clipboard = Components.classes['@mozilla.org/widget/clipboard;1'].getService( this.clipboardid );
        this.clipboardstring = Components.classes['@mozilla.org/supports-string;1'].createInstance( Components.interfaces.nsISupportsString );
    }

    this.copy = Clipboard_copy;
    this.paste = Clipboard_paste;
}

/**
 * Copies a text to the system clipboard. Priveleges are necessary.
 * @param text the text
 */
function Clipboard_copy( text )
{
    if ( nn )
    {
        field.select();
        this.clipboard.setContents( new java.awt.datatransfer.StringSelection( text ), null );
    }
    else if ( ie )
    {
        this.clipboard.value = text;
        this.clipboard.select();
        var textRange = this.clipboard.createTextRange();
        textRange.execCommand( 'copy' );
    }
    else if ( mozilla )
    {
        netscape.security.PrivilegeManager.enablePrivilege( 'UniversalXPConnect' );
        this.clipboardstring.data = text;
        var transfer = Components.classes['@mozilla.org/widget/transferable;1'].createInstance( Components.interfaces.nsITransferable );
        transfer.setTransferData( 'text/unicode', this.clipboardstring, text.length*2 );
        this.clipboard.setData( transfer, null, this.clipboardid.kGlobalClipboard );
    }
}

/**
 * Gets the current text in the system clipboard.
 * @return the text
 */
function Clipboard_paste()
{
    if ( nn )
    {
        var content = this.clipboard.getContents( null );

        if ( content != null )
        {
            return content.getTransferData( java.awt.datatransfer.DataFlavor.stringFlavor );
        }
    }
    else if ( ie )
    {
        this.clipboard.value = '';
        var textRange = this.clipboard.createTextRange();
        textRange.execCommand( 'paste' );

        return this.clipboard.value;
    }
    else if ( mozilla )
    {
        netscape.security.PrivilegeManager.enablePrivilege( 'UniversalXPConnect' );
        var transfer = Components.classes['@mozilla.org/widget/transferable;1'].createInstance( Components.interfaces.nsITransferable );
        transfer.addDataFlavor( 'text/unicode' );
        this.clipboard.getData( transfer, this.clipboardid.kGlobalClipboard );
        var str = new Object();
        var strLength = new Object();
        transfer.getTransferData( 'text/unicode', str, strLength );
        str = str.value.QueryInterface( Components.interfaces.nsISupportsString );
        return str.data.substring( 0, strLength.value / 2 );
    }
}