/* CONTENTS
MODIFIED 2/6/7 CMW

this depends on jsfunctions.js and prototype.js

openLigand - open new window
        used for ligand thumbnails
        
callAjax - called on load to populate the content div




*/


function openLigand(ligandID,height,width) {
	window.open('ligandView.cfm?ligandID='+ligandID, 'ligandWin', 'height='+height+',width='+width+',directories=0,menus=0');


}

function callAjax() {

//declare local variables
    var theDiv,     //the content div
        subsection, //the subsection being displayed
        ajaxInfo,   //the info for the ajax call
        theGetter,  //the getter document, dependent on the detail level
        detail,     //the detail being displayed
        moltype,    //the moltype being displayed
        myContEl;   //instance of the content class for the AJAX call defined in jsfunctions.js

        
/* only do this function if the following conditions are met:
    
    molType is coregulator or receptor AND detail is ortholog
    or
    molType is ligand and detail is empty or glance
    or
    molType is precipitate
    or
    molType is coregulator or receptor or ligand AND detail is literature
    or 
    molType is gene
    or
    molType is coregulator,receptor AND detail is interactions
    or
    molType is corergulator,receptor,gene,precipitate, and detail is p and p
    

*/

    moltype = location.search.toLowerCase().parseQuery().moltype ? location.search.toLowerCase().parseQuery().moltype : "";
    detail = location.search.toLowerCase().parseQuery().moldetail ? location.search.toLowerCase().parseQuery().moldetail : "";

    if (moltype !== "receptor" && moltype !== "coregulator" && moltype !== "ligand" && moltype !== "precipitate" && moltype !== "gene") {
        return false;
    }

    if ((moltype === "receptor" || moltype === "coregulator") && (detail !== "ortholog" && detail !== "literature" && detail !== "interactions" && detail != "pandp")) {
        return false;
    }

    if (moltype === "ligand" && (detail !== "glance" && detail !== "" && detail !== "literature")) {
        return false;
    }



//we are making the assumption that the div ID for this is "tabContent1"
//I hate making this assumption, but I can't figure out any other way
//to do this dynamically. If you can, then you are smarter than me and should
//get a raise

    theDiv = $('tabContent1');
    
    //if that is undefined, we're not displaying a molecule, so return false and exit
    if (!theDiv) {
        return false;
    }
    
//get the subsection by parsing the URL for a variable called "molSubsection"

    subsection = location.search.toLowerCase().parseQuery().molsubsection ? location.search.toLowerCase().parseQuery().molsubsection : 'default';


//decode the subsection if the value is 'default'
    if (subsection === 'default') {
        subsection = defaultSubsection(moltype, detail, "molSubsection");
        }

//if false is returned, it's either an invalid molType or we don't have data, so call a function to check this and write as needed
    if (!subsection) {
    	defaultContent(moltype, detail, theDiv);
        return false;
    } 
    
//now verify that the tab exists on the page and set the default if not

    if (!tabExists(subsection)) {
        subsection = defaultSubsection(moltype, detail, "molSubsection");
    }
    
//fire the onclick event of the tab to get the AJAX. This resolves a lot of problems,
//especially when linking coregs from the precip list of the corip pages

    clickTab(subsection);


/*
//set the getter page depending on the detail level
    switch (detail) {
        case "ortholog" :
            theGetter = "/getters/molinfo.cfm";
            break;
        case "literature" :
            theGetter = "/getters/litInfo.cfm";
            break;
        //default is molInfo because no detail should be a precipitate
        default :
            theGetter = "/getters/molinfo.cfm";
            break;
    }


//set the ajax info - check jsfunctions.js for more info
    ajaxInfo = {
        server : location.hostname,
        path : theGetter,
        paramtype : "passThru",
        params: ""
    };

    myContEl = new ContentEl(theDiv, ajaxInfo, subsection);
    
    myContEl.getHTML();
*/
}

//function to get the default of the url variable molsubsection
//theQueryparam is what needs to be cleared in any links on the page
function defaultSubsection(theMolType,theDetail,theQueryparam) {

    //vars
    var subsection; //return value
 
   //decode the number to the text representation - this is dependent on the moltype and detail
    switch (theMolType) {
    
    case "receptor" :
            
        switch (theDetail) {
        
        case "ortholog" :
            subsection = "gene";
            break;
        case "literature" :
            subsection = getDefaultTab(theQueryparam);
            break;
        case "interactions" :
        	subsection = getDefaultTab(theQueryparam);
        	break;
		case "pandp" :
        	subsection = getDefaultTab(theQueryparam);
        	break;        	
        }
        
        break;
        
    case "coregulator" :
            
        switch (theDetail) {
        
        case "ortholog" :
            subsection = "gene";
            break;
        case "literature" :
            subsection = getDefaultTab(theQueryparam);
            break;
        case "interactions" :
        	subsection = getDefaultTab(theQueryparam);
        	break;
        case "pandp" :
        	subsection = getDefaultTab(theQueryparam);
        	break;        	
        
        }
        
        break;
        
        
    case "precipitate" :

        switch (theDetail) {
        
        case "literature" :
            subsection = getDefaultTab(theQueryparam);
            break;
            
        case "interactions" :
        	subsection = getDefaultTab(theQueryparam);
        	break;
        	
		case "pandp" :
        	subsection = getDefaultTab(theQueryparam);
        	break;        	
            
        //default is for the information aka main section
        default :
            subsection = "gene";
            break;
        
        }
        break;
            
    case "ligand" :

        switch (theDetail) {
        
        case "literature" :

            subsection = getDefaultTab(theQueryparam);
            break;
            
        //default is also for glance
        default :
            subsection = "nomenclature";
            break;
        
        }
        
        break;
        
	case "gene" :

        switch (theDetail) {
        
        case "literature" :
            subsection = getDefaultTab(theQueryparam);
            break;
            
        case "interactions" :
        	subsection = getDefaultTab(theQueryparam);
        	break;
        	
        case "pandp" :
        	subsection = getDefaultTab(theQueryparam);
        	break;        	

        //default is for the information aka main section
        default :
            subsection = "gene";
            break;
        
        }
        break;
            
        
     default : //exit
        return false;
    
    }
    
    return subsection;
}

//puts in any default content if there are no tabs, e.g. a molecule is displaying that has no interactants
function defaultContent(theMolType, theDetail, contentDiv) {

	//as of now (Oct 2008), the only time this will be called is for a molecule displaying interactions with no interactants, so
	//let's keep things really simple
	if(theDetail === 'interactions') {
		contentDiv.innerHTML = "No interactions are available for this molecule.";
	}

}

//call callAjax on the window load
Event.observe(window, 'load', callAjax, false);