var m_nAssociationType;
var m_sAssociationAction;
var m_sAssociationTab;

function setClearCartAction(p_sClearChartAction){
  if(p_sClearChartAction != null){
    createCookie('clearCartAction',p_sClearChartAction,0);
  }
}

/**
 * DojoTabWidget.ftl sets the variables above until, 
 * the page (RHS) completely loads.  The divs on the 
 * RHS are undefined until the page has finished loading. 
 *
 * @see dojoTabSelection::publishTab() 
 */
function setAssociationPanel(p_nWidgetType, p_sAction, p_sInnerTabTube) { 
  m_nAssociationType = p_nWidgetType;
  m_sAssociationAction = p_sAction;
  m_sAssociationTab = p_sInnerTabTube;
} 

/**
 * Updates the new search panel.
 * Removed the current data cart.
 */
function newSearchPanel(){
  if(m_nAssociationType==1){  
    dojoRequest(m_sAssociationAction, m_sAssociationTab, null);
    dojo.byId('statsTab').style.display='';
    
    var sClearChartAction = readCookie('clearCartAction');
    dojoRequestNoResults(sClearChartAction);
  }else{
    dojo.byId('statsTabTube').innerHTML='';
    dojo.byId('statsTab').style.display='none';
  }
}

/**
 * Updates the dataset and piims view panel.
 * See:  dojo.byId('experimentsContent').parentNode.style.height='262px';
 *       After leaving the accordion, the size goes to 0px.
 *		 Grab the parent node of 'experimentsContent' and 
 * 		 set the height accordingly.
 */
function datasetPanel(){
  if(m_nAssociationType==3 || m_nAssociationType==4 || m_nAssociationType==6 || m_nAssociationType==7){
    dojo.byId('experimentsContent').innerHTML='';
    if(dojo.byId('experimentsContent').parentNode.style.height == "0px"){
      dojo.byId('experimentsContent').parentNode.style.height='262px';
    }
    loadAccordionPiimsRef(1, 'experimentsContent');
    dojo.byId('piimsTab').style.display='';
  }else{
    dojo.byId('piimsTab').style.display='none';
  }
}

/**
 * Updates the dataset and piims view panel.
 */
function piimsAvailableDatasetPanel(){
  if(m_nAssociationType==8){
    dojoRequest(m_sAssociationAction, m_sAssociationTab, null);
    dojo.byId('piimsSearchTab').style.display='';    
  }else{
    dojo.byId('piimsSearchTabTube').innerHTML='';
    dojo.byId('piimsSearchTab').style.display='none';
  }
}

/**
 * Replaces the data cart panel.  Show datasets that are not 
 * in the gold standard.
 */
function otherDatasetsPanel(){
  if(m_nAssociationType==2){
    dojoRequest(m_sAssociationAction, m_sAssociationTab, null);
    dojo.byId('otherDatasetTab').style.display='';    
  }else{
    dojo.byId('otherDatasetTabTube').innerHTML='';
    dojo.byId('otherDatasetTab').style.display='none';
  }
}

/**
 * Topic for handling tabs.<b>
 * When a new tab is added, set the data variables 
 * at the top of the page. 
 */
topics = [];
topics[0] = dojo.subscribe("tabSelection", setAssociationPanel);
topics[1] = dojo.subscribe("setRightPanel", newSearchPanel);
topics[2] = dojo.subscribe("setRightPanel", datasetPanel);
topics[3] = dojo.subscribe("setRightPanel", piimsAvailableDatasetPanel);
topics[4] = dojo.subscribe("setRightPanel", otherDatasetsPanel);

/**
 * Upon adding a tab, create 4 cookings.  
 * a) tabId
 * b) tab's widget type
 * c) tab's action
 * d) tab's result div
 */
function addTab(p_sTabId, p_nTabWidgetType, p_sTabAction, p_sTabDiv){
  var value = p_sTabId+","+p_nTabWidgetType+","+p_sTabAction+","+p_sTabDiv;
  createCookie(p_sTabId+"-id",value,0);
}

/**
 *  When tab changes, get the appropriate panel 
 *  on the RHS.
 */
dojo.subscribe("datasetTabContainer-selectChild", function(child){
  var nPreviousAssociationType = m_nAssociationType;
  
  var sCookieTabValues = readCookie(child.id+"-id");
  var cookieTabArray = sCookieTabValues.split(",");
    
  m_nAssociationType = cookieTabArray[1];
  m_sAssociationAction = cookieTabArray[2];
  m_sAssociationTab = cookieTabArray[3];
  
  if(m_sAssociationAction != null && m_sAssociationTab != null){
    if(m_sAssociationAction != "" && m_sAssociationTab != ""){
      publishActiveTab("setRightPanel", m_nAssociationType, m_sAssociationAction, m_sAssociationTab);
    }
  }
});

/**
 *  When tab is removed, delete the cookie.
 */
dojo.subscribe("datasetTabContainer-removeChild", function(child){
  eraseCookie(child.id+"-id");
  dojoRequestNoResults("DatasetRemoveTab.action?tabId="+child.id);
});

/**
 * Announces that a new tab has been added once that page 
 * has been loaded.  At the bottom of DatasetSearch.ftl, 
 * it calls the topic setRightPanel to change the display 
 * on the right.
 */
function publishTab(p_sTopic){
  dojo.publish(p_sTopic);
}

/**
 * Sends a message that a new tab has been added.  
 * This gets called at the bottom of the widgets<b>
 *
 * @see <web.dir>/templates/common/TabularSearchFormWidget.ftl
 * @see <web.dir>/templates/common/TabularSearchResultsWidget.ftl
 * @see <web.dir>/templates/common/TabularDatasetViewWidget.ftl
 * @see <web.dir>/templates/common/TabularDatasetPiimsWidget.ftl 
 */
function publishActiveTab(p_sTopic, p_nWidgetType, p_sChildAction, p_sChildDiv){
  //alert(p_sTopic+", "+p_sChildAction+", "+p_sChildDiv);
  dojo.publish(p_sTopic, [p_nWidgetType, p_sChildAction, p_sChildDiv]);
}

/**
 * Determines the display for piims data.  You must 
 * provide the appropriate relation type.
 *
 * [p_sPiimsType = 1] returns "experiments"
 * [p_sPiimsType = 2] returns "runs"
 * [p_sPiimsType = 3] returns "trays"
 *
 * @see org.ionomicshub.datasets.actions.DatasetViewAction::getRelatedPiimsInfo()
 * @see org.ionomicshub.datasets.statics.RelatedInfoInterface 
 */
function loadAccordionPiimsRef(p_sPiimsType, p_sResultsDiv){
  dojoRequest(m_sAssociationAction+"&type="+p_sPiimsType, p_sResultsDiv, null);
}

function loadAccordionDatasetRef(p_sResultsDiv){
  dojoRequest(m_sAssociationAction, p_sResultsDiv, null);
}

