$j = typeof $j == "undefined" ? jQuery : $j;
deprecated = typeof deprecated == "undefined" ? function(f) { return f; } : deprecated;

function displayObjectDetails(index, caller)
{
    caller = $j(caller);
    var targets = $j("div#sr" + index + " div.objectDetails");
    
    if (caller.text() == "+") {
        caller.html("&ndash;");
        targets.slideDown();

    } else {
        caller.html("+");
        targets.slideUp();
    }
}

var goDefDialog = null;

function showGoDefs()
{
    try
    {
        
        if (goDefDialog == null)
        {
            goDefDialog = $j("#goEvidenceDefs").dialog(
                    {
                        resizable : false,
                        height : 300,
                        width : 400,

                        title : 'Go Evidences Fetched/Queried',
                        autoOpen: false,


                    });

        }

        goDefDialog.dialog('open');
    }
    catch (e)
    {
        alert("Error creating go def dialog: " + e);
    }
}

function showObjectDetails()
{
    $j('.objectDetails').slideDown();
}

// I can't find where this is actually used, so I am deprecating it.

showObjectDetails = deprecated(showObjectDetails);


function displayAllObjectDetails(caller)
{
	if ($j(".srResult .objectDetails").is(":hidden")) {
		$j(".srResult a.srBullet").html("&ndash;");
		$j(".srResult .objectDetails").show();
		$j(caller).html("&ndash;");
	} else {
		$j(".srResult a.srBullet").html("+");
		$j(".srResult .objectDetails").hide();
		$j(caller).html("+");
	}
}


function spinner(caller, on)
{
    $j('#spinner' + caller).toggle();
}

var SPIN_ON = true;
var SPIN_OFF = false;

function displayGoResults(goResults, caller)
{
    var goResultsTable = $j("<table />");
    if (goResults.length == 0)
    {
    	goResultsTable.append("<tr><td>No GO terms found</td></tr>");
    }
    else
    {
    	$j.each(goResults, function() {
    		goResultsTable.append("<tr><td class='goAccession'>" + this.accession + "<br />" + this.name + "</td></tr>")
    					  .append("<tr><td>" + this.definition + "</td></tr>")
    					  .append("<tr><td>&nbsp;</td></tr>");
    	});
    }
    
    var goDiv = $j("<div />").append($j("<div class='goResults' />").append(goResultsTable));
    $j("#goData" + caller).append(goDiv).toggle();
    $j("#fetchGo" + caller).toggle();
}

function fetchGo(p, caller)
{
    spinner(caller, SPIN_ON);
    
    $j.ajax({
    	//url: ajaxPath + "/go",
    	url: "/dancer/ajax/go",
    	dataType: 'json',
    	data: { "p": p },
    	success: function(goResults) {
    		displayGoResults(goResults, caller);
    	},
    	failure: function() {
    		alert("Failed to fetch Go Annotations.");
    	},
    	complete: function() {
    		spinner(caller, SPIN_OFF);
    	}
    });
}

function toggleDivs()
{
	// first argument must be the index(unique identifier) of the div
	var index = arguments[0];
	for(var i=1; i<arguments.length; i++) {
		$j("#" + arguments[i] + index).toggle()
	}
}

// I do not think this is being used anywhere.

toggleDivs = deprecated(toggleDivs);




var actGraphs = {};

function showActorGraph(geneId, actId, geneName, actName)
{
    try
    {
        var divId = "actorGraph" + geneId + "_" + actId;
        
        var dialog = actGraphs[divId];
        if (typeof dialog == "undefined")
        {
            $j("#" + divId).html("<iframe border='0' width='101%' height='101%' src='http://wodaklab.org/netility/iref/nw?actId="
                + actId + "' ></iframe>");
            
            dialog = $j('#' +divId).dialog(
                    {
                        resizable : true,
                        height : 600,
                        width : 580,
                        dialogClass: 'actDialog',
                        title : 'Interactions: Gene ' + geneName + ', Protein ' + actName,
                        autoOpen: false
                    });

            actGraphs[divId] = dialog;
        }

        dialog.dialog('open');
    }
    catch (e)
    {
        alert("Error creating go actor's dialog: " + e);
    }
}



var mAlerts = new Array();

function malert(text)
{
    if (mAlerts[text] == null)
    {
        try
        {
        	var d = $j("<div><p>" + text + "</p></div>")
        				.attr("id", "mAlert" + (mAlerts.length + 1))
        				.addClass("iDialog")
        				.hide();
        	
            mAlerts[text] = d.dialog({
                height : 200,
                width : 400,
                modal : true,
                resizable: false,
                buttons: {
                    Ok: function() {
                        $j(this).dialog('close');
                    }
                },
                title : 'Alert'
            });
        }
        catch (mae)
        {
            alert("Error creating dialog: " + mae);
        }

    }
    else
    {
        mAlerts[text].dialog('open');
    }

}


