﻿
// File = Farsi/Includes/BidsTM/BidsTM.js

/// <reference name="MicrosoftAjax.js" />"


// Node icon text
var NodeIconText = { LeftArrow : "&#9668;", DownArrow : "&#9660;", Circle : "&#9679;" };

// Node icon css class
var NodeIconCssClass = { ExpCol : "ExpColIcon", Bullet : "BulletIcon"};                                

// Register the namespace for the controls
Type.registerNamespace("BidsTM");
    
// Define NodeICon control

//
// Define control properties.
//

BidsTM.NodeIcon = function(element){
    BidsTM.NodeIcon.initializeBase(this, [element]);
    this._text = null;
    this._cssClass = null; 
}

//
// Create the prototype for the control.
//

BidsTM.NodeIcon.prototype = {
    initialize : function(){
        BidsTM.NodeIcon.callBaseMethod(this, "initialize");
   },
  
  dispose : function(){
    BidsTM.NodeIcon.callBaseMethod(this, "dispose");
  },
  
  //
  // Control properties
  //
  
  get_text : function(){
    return this._text;
  },
  
  set_text : function(value){
    this.get_element().innerHTML  = value;
  },
  
  get_cssClass : function(value){
    return this._cssClass;
  },
  
  set_cssClass : function(value){
    this.addCssClass(value);
  }               
}

// Register the NodeIcon class as a type that inherits from Sys.UI.Control.
BidsTM.NodeIcon.registerClass("BidsTM.NodeIcon", Sys.UI.Control);

// Define TMRoot control

//
// Define control properties.
//

BidsTM.TMRoot = function(element){
    BidsTM.TMRoot.initializeBase(this, [element]);
    this._nodeLevel = null;
    this._childNodes = { };
}

//
// Create the prototype for the control.
//

BidsTM.TMRoot.prototype = {
    initialize : function(){
        BidsTM.TMRoot.callBaseMethod(this, "initialize");
        // Set Css class
        this.addCssClass("Level" + this._nodeLevel + "Node");
   },
  
  dispose : function(){
        BidsTM.TMRoot.callBaseMethod(this, "dispose");
    },
    
   //
   // Control properties
   //
   
  // nodeLevel 
   get_nodeLevel : function(){
    return this._nodeLevel; 
  },
  
  set_nodeLevel : function(value){
    this._nodeLevel = value;
  },
  
  // childNodes
  addChildNode : function(childNode){
    this._childNodes[childNode.get_id()] = childNode; 
  },
  
  getChildNodes : function(){
    var childNodesArr = [];
    for(var childNodeName in this._childNodes){
       childNodesArr[childNodesArr.length] = this._childNodes[childNodeName];     
    }
    
    return childNodesArr;
  }
}

// Register TMRoot as a control
BidsTM.TMRoot.registerClass("BidsTM.TMRoot", Sys.UI.Control);

// Define TMNode control

//
// Define control properties 
//

BidsTM.TMNode = function(element){
    BidsTM.TMNode.initializeBase(this, [element]);
    this._parentId = null;
    this._nodeLevel = null;
    this._pageUrl = null;
    this._expanded = null;
    this._childNodes = { };
    this._nodeIcon = null;
}

//
// Create the prototype for the control.
//

BidsTM.TMNode.prototype = {
    initialize : function(){
        BidsTM.TMNode.callBaseMethod(this, "initialize");
        
        // Define event delegates
        this._onclickDelegate = Function.createDelegate(this, this._onClick);
        this._onmouseoverDelegate = Function.createDelegate(this, this._onMouseover);
        this._onmouseoutDelegate = Function.createDelegate(this, this._onMouseout); 
        
        // Add event handlers
        $addHandlers(this.get_element(),
            { 'click' : this._onClick, 'mouseover' : this._onMouseover, 'mouseout' : this._onMouseout},
                this);
   },
  
  dispose : function(){
        // Remove event handlers
        $clearHandlers(this.get_element());
        
        BidsTM.TMNode.callBaseMethod(this, "dispose");
    },
    
    //
    // Event delegates
    //
    
    _onClick : function(e){
        e.stopPropagation();
        var pageFullUrl = this._getPageFullUrl();
        if ( pageFullUrl != null){
			location.href = pageFullUrl;
        }
        else if (this.get_expanded())
            this.collapseNode();
        else
            this.expandNode();    
  },
  
  _onMouseover : function(e){
        var elem = this.get_element();
         elem.style.cursor = "pointer";
        
        e.stopPropagation();
  },
  
  _onMouseout : function(e){
        e.stopPropagation();
        this.get_element().style.cursor = "default";
  },   
    
   //
   // Control properties
   //
   
   // parentId
   get_parentId : function(){
    return this._parentId; 
  },
  
  set_parentId : function(value){
    this._parentId = value;
  },
  
  // nodeLevel 
   get_nodeLevel : function(){
    return this._nodeLevel; 
  },
  
  set_nodeLevel : function(value){
    this._nodeLevel = value;
  },
  
  // pageUrl
  get_pageUrl : function(){
    return this._pageUrl;
  },
  
  set_pageUrl : function(value){
    this._pageUrl = value;
  },
  
  // pageId
  get_pageId : function(){
    return this._pageId;
  },
  
  set_pageId : function(value){
    this._pageId = value;
  },
  
  // expanded
  get_expanded : function(){
    return this._expanded;
  },
  
  set_expanded : function(value){
    this._expanded = value;
  },
  
  // nodeIcon
  get_nodeIcon : function(){
    return this._nodeIcon;
  },
  
  set_nodeIcon : function(value){
    this._nodeIcon = value;
  },
  
  //
  // General methods
  //
  
  // childNodes
  addChildNode : function(childNode){
    this._childNodes[childNode.get_id()] = childNode; 
  },
  
  getChildNodes : function(){
    var childNodesArr = [];
    for(var childNodeName in this._childNodes){
        childNodesArr[childNodesArr.length] = this._childNodes[childNodeName];     
    }
    
    return childNodesArr;
  },
  
  
  collapseNode : function(){
    // If this node has children and is already expanded then collapse it.
  var childNodes = this.getChildNodes();
    var childNode;
    if ((childNodes.length > 0) && (this.get_expanded())){
        this.set_expanded(false);
        this.get_nodeIcon().set_text(NodeIconText.LeftArrow);
        for(var i=0;i<childNodes.length;i++){
            childNode = childNodes[i];
            if (childNode.getChildNodes().length > 0)
                childNode.collapseNode();
            childNode.get_element().style.display = "none";    
        }
    }
  },
  
  expandNode : function(){
    // If this node has children and is already
    // collapsed then expand it.
    var childNodes = this.getChildNodes();
    if ((childNodes.length > 0) && (!this.get_expanded())){
      // if (this.get_nodeLevel() == 1)
        //   this._collapseAllLevel1NodesExceptThis();
        this.set_expanded(true);
        this.get_nodeIcon().set_text(NodeIconText.DownArrow);
        for(var i=0;i<childNodes.length;i++){
            childNodes[i].get_element().style.display = "block";
        }
    }
  },
  
  _collapseAllLevel1NodesExceptThis : function(){
    // Get all tmRoot children (Leve1Nodes)
    var childNodes = $find("0").getChildNodes();
    var childNode;
    for(var i=0;i<childNodes.length;i++){
        childNode = childNodes[i];
        if ((childNode.get_expanded()) && (this.get_id() != childNode.get_id()))
            childNode.collapseNode();
    }
  },
  
  _getPageFullUrl : function(){
        var pageUrl = this.get_pageUrl();
        if (pageUrl == null)
            return null;
        
        var pageDir;
        if (UserRoleName == "ConfirmedMembers")
            pageDir = ConfirmedMembersPagesDir;
        else
            pageDir = MainPagesDir;     
        
        if (AppDir == null)
            return null;
        
        return AppDir + pageDir + pageUrl;
    }
}

// Register TMNode as a control
BidsTM.TMNode.registerClass("BidsTM.TMNode", Sys.UI.Control);

// Setup tree menu nodes
Sys.Application.add_init(
    function(){
        // Get all div elements on the page
        var oDivs = document.getElementsByTagName("div");
        var divNode, nodeId, nodeIconId, tmNode,
            parentId, nodeLevel, pageUrl;
        for(var i=0;i<oDivs.length;i++){
            divNode = oDivs[i];
            nodeLevel = divNode.getAttribute("nodeLevel");
            if ((nodeLevel != null) && (!isNaN(nodeLevel))){
                nodeLevel = Number(nodeLevel);
                nodeId = Number(divNode.getAttribute("id"));
                if (nodeId == 0){
                    tmNode = $create(BidsTM.TMRoot,
                    { nodeLevel : nodeLevel }, null, null, divNode);
                }
                else{
                    nodeIconId = nodeId + "_icon";
                    parentId = divNode.getAttribute("parentId");
                    pageUrl = divNode.getAttribute("bidsTMPageUrl");
                    pageUrl = (pageUrl == "--") ? null : pageUrl; 
                    tmNode = $create(BidsTM.TMNode,
                    { parentId : parentId, nodeLevel : nodeLevel, pageUrl : pageUrl, expanded : true},  
                     null, { nodeIcon : nodeIconId}, divNode);
                }
                
                // Add tmNode to parent childNodes
                if (nodeLevel > 0){
                    $find(parentId).addChildNode(tmNode);                                
                }
            }
        }
    }
);

// Setup icon nodes
Sys.Application.add_init(
    function(){
        // Get all span elements on the page
        var oSpans = document.getElementsByTagName("span");
        var spanNode, spanId, ownerNode, removeIndex, 
            tmNodeIcon, iconCssClass, iconText;
        for(var i=0;i<oSpans.length;i++){
            spanNode = oSpans[i];
            spanId = spanNode.getAttribute("id");
            if ((spanId !=null) && (spanId.endsWith("icon"))){
                // Get owner id
                removeIndex = spanId.indexOf("_icon");
                ownerNode = $find(spanId.substring(0, removeIndex));
                if (ownerNode.getChildNodes().length > 0){
                    iconText = NodeIconText.DownArrow;
                    iconCssClass = NodeIconCssClass.ExpCol;
                }
                else{
                   iconText = NodeIconText.Circle;
                   iconCssClass = NodeIconCssClass.Bullet;
               }
                
                var iconNode = $create(BidsTM.NodeIcon,
                    { text : iconText, cssClass : iconCssClass}, null, null, spanNode);
            }
        }
    }
); 
                
// End of script
if (typeof(Sys) != 'undefined')
    Sys.Application.notifyScriptLoaded();
    
    										
		