/**
 * Rivet Software Inc.
 *
 * @copyright Copyright � 2006-2007 Rivet Software, Inc. All rights reserved.
 *
 */

AuthRef = {

    hideAuthRef: function(){
        var div = document.getElementById('AR');
        if (div) { div.parentNode.removeChild( div ); }
    },

    showAuthRef:function( obj, win, label, authRefId ){
        var divParent = obj.parentNode;
        var style = '';

        if( top.frames.length > 0 && win.name != '' ){
            var iframe = win.name;
            var n = win.name.lastIndexOf( '_' );
            iframe = win.name.substr( 0, n )+'_i';
            divParent = top.window.document.getElementById( iframe ).parentNode;

            style = 'left: 350px; top: 250px;';
            win = top.window;
        }else{
            this.autoScroll( divParent );
        }

        var doc = win.document;

        if( doc.getElementById( 'AuthRef' ) ) {
            this.hideAuthRef( doc );
        }

        var div1 = win.document.createElement( 'div' );
        div1.id = 'AR';

        if( style != '' ){
            div1.style.cssText = style;
        }

        divParent.appendChild( div1 );

        var div2 = doc.createElement( 'div' );
        div2.id = 'AuthRef';

        var loading = document.createTextNode( '...Loading...' );
        div2.appendChild( loading );

        div1.appendChild( div2 );

        var accessionNum = this.ARGetAccession();

	 	mypath = "../index.php";
        AuthRef_Callback.argument.win = win;
        AuthRef_Callback.argument.label = label;
        YAHOO.util.Connect.asyncRequest('GET', mypath + '/authref/display/?a=' + accessionNum + '&r=' + authRefId, AuthRef_Callback, null);
    },

    toggleAuthRef: function( id, doc ){
        if (id == 'AuthRefD' || id == 'AuthRefR'){
            var on = doc.getElementById(id);
            var off = doc.getElementById(id == 'AuthRefD' ? 'AuthRefR' : 'AuthRefD');
            var label = doc.getElementById('AuthRef_Label');
            label.innerHTML = id == 'AuthRefD' ? 'Definition' : 'Reference';

            on.style.display = 'block';

            if (off) { off.style.display = 'none'; }
            return;
        }

        var el = doc.getElementById( id );

        if (el.style.display == 'block'){
            el.style.display = 'none';
        } else {
            el.style.display = 'block';
        }
    },

    ARGetAccession: function() {
        //var div = Tabs.getActiveTabDiv();
        //div = Tabs.getChildWithClass( div, 'ReportWrapper' );

        //return div.getAttribute( 'accessionnum' );
    },
    
    autoScroll:function( el ){
        //_inspect( el );
        var ARLocation = el.offsetTop;
        var ARHeight = 300;

        var tagName = '';
        while( tagName != 'TABLE' ){
            el = el.parentNode;
            tagName = el.tagName;
        }

        ARLocation += el.offsetTop;

        var container = document.getElementById( 'yui-content' );
        var tabHeight = container.clientHeight;
        var tabLocation = container.scrollTop;

        if( tabLocation == null ){
            tabLocation = 0;
        }

        var scrollDown = ( ARLocation + ARHeight ) - ( tabLocation + tabHeight );

        if( scrollDown > 0 ){
            container.scrollTop += scrollDown;
        }
    },

    asyncSuccess:function(o) {
        win = o.argument.win;
        doc = win.document;

        var idx = Math.min( o.argument.label.length, 30 );

        var html = '' +
            "\n"+'   <div id="AuthRefLabel">' +
            "\n"+'        <a id="close" href="javascript:top.AuthRef.hideAuthRef( document );">x</a>' +
            "\n"+'        <p>' + o.argument.label.substr( 0, idx ) + ( idx == 30 ? '...' : '' ) + ' - <span id="AuthRef_Label">Definition</span></p>' +
            "\n"+'    </div>';

        html += o.responseText;

        doc.getElementById('AuthRef').innerHTML = html;
        var dd = new win.YAHOO.util.DD("AR", "paneldrag", {scroll:false});
        dd.setHandleElId("AuthRefLabel");
    },

    asyncFailure:function(o) {
        alert("There was a problem with your request.");
    }
};

// Defines the callback functions for async
var AuthRef_Callback = {
    success:AuthRef.asyncSuccess,
    failure:AuthRef.asyncFailure,
    argument:[]
};
/** $Author: dev1 $ * $Date: 2007-02-06 13:27:37 -0700 (Tue, 06 Feb 2007) $ * $Rev: 900 $ **/