/*
This file is part of pDAF.
Copyright (c) 2005-2008 by Gregorcic & Helger IT systems OEG.
All rights reserved.
http://www.phloc.com
 */


// constructor
//========================================================
function pDAFTreeClass() 
//========================================================
{
  this.sTreeItemPrefix = "pdaf_tree_item_";
  
  this.sAttrTreeStateID = "treestateid";
}

//========================================================
pDAFTreeClass.prototype = 
//========================================================
{
  //------------------------------------------------------
  expand : function(sTreeID, sParentIDPath, sItemID, nLevel, sProgressMsg) 
  //------------------------------------------------------
  {
    pDAFUIExt.startProgress(sProgressMsg);      
    var aTree = jphloc.ge(sTreeID);
    var aItem = jphloc.ge(this.getUniqueItemID(sTreeID, sParentIDPath, sItemID));
    if (aTree && aItem)
    {
      sajax_do_call(null, 
          "pDAFTree.expand", 
          {callback:pDAFTree.expandCallback, extra_data:[sTreeID, sParentIDPath, sItemID]},
          [sTreeID, this.getTreeStateID(aTree), sParentIDPath, sItemID, nLevel]);    
    }
  },
  
  //------------------------------------------------------
  getUniqueItemID : function(sTreeID, sParentIDPath, sItemID) 
  //------------------------------------------------------
  {
    return this.sTreeItemPrefix + sTreeID + "_" + sParentIDPath + sItemID;
  },
  
  //------------------------------------------------------
  getTreeStateID : function(aTreeNode) 
  //------------------------------------------------------
  {
    return aTreeNode.getAttribute(this.sAttrTreeStateID);
  },
  
  //------------------------------------------------------
  expandAll : function(sTreeID, sProgressMsg) 
  //------------------------------------------------------
  {
    pDAFUIExt.startProgress(sProgressMsg);
    var aTree = jphloc.ge(sTreeID);
    if (aTree)
    {
      sajax_do_call(null, 
          "pDAFTree.expandAll", 
          {callback:pDAFTree.expandAllCallback, extra_data:[sTreeID]},
          [sTreeID, this.getTreeStateID(aTree)]);    
    }
  },

  //------------------------------------------------------
  collapse : function(sTreeID, sParentIDPath, sItemID, nLevel, sProgressMsg) 
  //------------------------------------------------------
  {
    pDAFUIExt.startProgress(sProgressMsg);
    var aTree = jphloc.ge(sTreeID);
    var aItem = jphloc.ge(this.getUniqueItemID(sTreeID, sParentIDPath, sItemID));
    if (aTree && aItem)
    {
      sajax_do_call(null, 
          "pDAFTree.collapse", 
          {callback:pDAFTree.collapseCallback, extra_data:[sTreeID, sParentIDPath, sItemID]},
          [sTreeID, this.getTreeStateID(aTree), sParentIDPath, sItemID, nLevel]);
    }
  },

  //------------------------------------------------------
  collapseAll : function(sTreeID, sProgressMsg) 
  //------------------------------------------------------
  {
    pDAFUIExt.startProgress(sProgressMsg);
    var aTree = jphloc.ge(sTreeID);
    if (aTree)
    {
      sajax_do_call(null, 
          "pDAFTree.collapseAll", 
          {callback:pDAFTree.collapseAllCallback, extra_data:[sTreeID]},
          [sTreeID, this.getTreeStateID(aTree)]);    
    }
  },
  
  //------------------------------------------------------
  expandCallback : function (sResultXHTMLSniplet, extra_data)
  //------------------------------------------------------
  {
    pDAFTree.updateItem(sResultXHTMLSniplet, extra_data[0], extra_data[1], extra_data[2]);
    pDAFTree.updateRows(extra_data[0]);
    pDAFUIExt.stopProgress();
  },
  
  //------------------------------------------------------
  collapseCallback : function (sResultXHTMLSniplet, extra_data)
  //------------------------------------------------------
  {
    pDAFTree.updateItem(sResultXHTMLSniplet, extra_data[0], extra_data[1], extra_data[2]);
    pDAFTree.updateRows(extra_data[0]);
    pDAFUIExt.stopProgress();
  },

  //------------------------------------------------------
  expandAllCallback : function (sResultXHTMLSniplet, extra_data)
  //------------------------------------------------------
  {
    pDAFTree.updateContent(sResultXHTMLSniplet, extra_data[0]);
    pDAFTree.updateRows(extra_data[0]);
    pDAFUIExt.stopProgress();
  },
  
  //------------------------------------------------------
  collapseAllCallback : function (sResultXHTMLSniplet, extra_data)
  //------------------------------------------------------
  {
    pDAFTree.updateContent(sResultXHTMLSniplet, extra_data[0]);
    pDAFTree.updateRows(extra_data[0]);
    pDAFUIExt.stopProgress();
  },
  
  //------------------------------------------------------
  updateItem : function (sResultXHTMLSniplet, sTreeID, sParentIDPath, sItemID)
  //------------------------------------------------------
  {
    sResultXHTMLSniplet = sajax_clean_result(sResultXHTMLSniplet);
    var sUniqueItemID = this.getUniqueItemID(sTreeID, sParentIDPath, sItemID);
    var aItem = jphloc.ge(sUniqueItemID);
    var aItemContent = jphloc.ge(sUniqueItemID + "_content");
    var aSniplets = sResultXHTMLSniplet.split(g_pdaf_ajax_sniplet_separator);
    if (aItem)
      jphloc.outerHTML(aItem, aSniplets[0]);
    if (aItemContent)
      jphloc.outerHTML(aItemContent, aSniplets[1]);
  },

  //------------------------------------------------------
  updateContent : function (sResultXHTMLSniplet, sTreeID)
  //------------------------------------------------------
  {
    sResultXHTMLSniplet = sajax_clean_result(sResultXHTMLSniplet);
    var aTreeContent = jphloc.ge(sTreeID + "_tree_content");
    jphloc.outerHTML(aTreeContent, sResultXHTMLSniplet);
  },
  
  //------------------------------------------------------
  updateRows : function (sTreeID)
  //------------------------------------------------------
  {
    var aTree = jphloc.ge(sTreeID);
    var aRows = jphloc.getChildrenByTagName(aTree, "div", true);
    var bOdd = true;
    for (var i = 0; i < aRows.length; i++)
    {
      var aRow = aRows[i];
      if (jphloc.hasClass(aRow, "pdaf_tree_item"))
      {
        if (bOdd)
        {
          jphloc.addClass(aRow, "odd");
          jphloc.removeClass(aRow, "even");
        }
        else
        {
          jphloc.addClass(aRow, "even");
          jphloc.removeClass(aRow, "odd");          
        }
        bOdd = !bOdd;
      }
    }    
  },
  
  //------------------------------------------------------
  filter : function(sTreeID, sProgressMsg) 
  //------------------------------------------------------
  {
    pDAFUIExt.startProgress(sProgressMsg);
    var aTree = jphloc.ge(sTreeID);
    var aFilterField = jphloc.ge(sTreeID + "_filter");
    if (aTree && aFilterField)
    {
      sajax_do_call(null, 
          "pDAFTree.filter", 
          {callback:pDAFTree.filterCallback, extra_data:[sTreeID]},
          [sTreeID, this.getTreeStateID(aTree), aFilterField.value]);
    }
  },
  
  //------------------------------------------------------
  filterCallback : function (sResultXHTMLSniplet, extra_data)
  //------------------------------------------------------
  {
    pDAFTree.updateContent(sResultXHTMLSniplet, extra_data[0]);
    pDAFTree.updateRows(extra_data[0]);
    pDAFUIExt.stopProgress();
  }

}

var pDAFTree = window.pDAFTree = new pDAFTreeClass();
