//ALTER TABLE `navigation` ADD `nIconPath` VARCHAR( 255 ) NULL DEFAULT NULL 
      
      Ext.override(Ext.tree.TreeNode, {
        clone: function() {
          var atts = this.attributes;
          atts.id = Ext.id(null, "ynode-");
          var clone = new Ext.tree.TreeNode(Ext.apply({}, atts));
          clone.text = this.text;
          for(var i=0; i<this.childNodes.length; i++){
            clone.appendChild(this.childNodes[i].clone());
          }
          return clone;
        }
      });
      
var treeNavMenu = new Ext.menu.Menu({
            items: [{
              id: 'mnu_navigation-show',
              text: 'Show Item',
              hidden: true
            },{
              id: 'mnu_navigation-hide',
              text: 'Hide Item'
            },{
              id: 'mnu_navigation-intranet',
              text: 'Staff Intranet Page (Private)',
              checked: false
            }/*,{
              text: 'Menu Icon',
              hidden: true,
              id: 'navMenuHolder',
              menu: { items: ['<div class="menu-title">Choose a Theme</div>',
                              new Ext.form.ComboBox({
                                id: 'navMenu',
                                store: new Ext.data.ArrayStore({
                                  fields: ['key','name'],
                                  data : [
                                            ['iconServices', 'No Icon'],
                                            ['iconNewsView', 'iconNewsView'],
                                            ['iconBlogView', 'iconBlogView'],
                                            ['iconPodcastView', 'iconPodcastView'],
                                            ['iconChannelsView', 'iconChannelsView'],
                                            ['iconArchiveView', 'iconArchiveView'],
                                            ['iconMonthView', 'iconMonthView'],
                                            ['iconWeekView', 'iconWeekView'],
                                            ['iconDayView', 'iconDayView'],
                                            ['iconYearView', 'iconYearView'],
                                            ['iconNew', 'iconNew'],
                                            ['iconRSS', 'iconRSS'],
                                            ['iconExport', 'iconExport'],
                                            ['iconLink', 'iconLink'],
                                            ['iconSearch', 'iconSearch'],
                                            ['iconStaff', 'iconStaff'],
                                            ['iconEmail', 'iconEmail'],
                                            ['iconForm', 'iconForm'],
                                            ['iconFile', 'iconFile'],
                                            ['iconNewPage', 'iconNewPage'],
                                            ['iconGallery', 'iconGallery'],
                                            ['iconForum', 'iconForum'],
                                            ['iconStats', 'iconStats'],
                                            ['iconFileManager', 'iconFileManager'],
                                            ['iconGradebook', 'iconGradebook'],
                                            ['iconRSVP', 'iconRSVP'],
                                            ['iconSchools', 'iconSchools'],
                                            ['iconMap', 'iconMap'],
                                            ['iconSort', 'iconSort']
                                          ]
                                }),
                                displayField: 'name',
                                typeAhead: true,
                                mode: 'local',
                                checked: true,
                                triggerAction: 'all',
                                emptyText: 'Select an icon...',
                                selectOnFocus: true,
                                tpl: new Ext.XTemplate(
                                    '<tpl for=".">',
                                      '<div ext:qtip="{name}" class="x-combo-list-item">',
                                        '<span class="{key}" style="display: block; padding: 3px; float: left; height: 16px; width: 16px;"></span>',
                                        '<span class="x-menu-item-text" style="display: block; padding: 3px; float: left;">{name}</span>',
                                        '<br class="clear" />',
                                      '</div>',
                                    '</tpl>'
                                ),
                                //itemSelector: 'div.menu-item',
                                width: 250,
                                listeners: {
                                  select: function(c,r,i){
                                    Ext.getCmp('navMenuHolder').parentMenu.contextNode.getUI().getIconEl().className = r.data.name;
                                  }
                                },
                                getListParent: function() {
                                    return this.el.up('.x-menu');
                                },
                                iconCls: 'no-icon'
                              })]
                            }
            }*/,'-',{id: 'mnu_navigation-expand_collapse', text: 'Expand/Collapse', menu: { items: [
                    {
                      id: 'mnu_navigation-expand',
                      text: 'Expand'
                    },{
                      id: 'mnu_navigation-collapse',
                      text: 'Collapse'
                    },'-',{
                      id: 'mnu_navigation-expand_all',
                      text: 'Expand All'
                    },{
                      id: 'mnu_navigation-collapse_all',
                      text: 'Collapse All'
                    }],
                    listeners: {
                      itemclick: function(item) {
                        var n = item.parentMenu.parentMenu.contextNode;
                        switch (item.id) {
                          case 'mnu_navigation-expand':
                            if (n.parentNode) {
                              n.expand();
                            }
                            break;
                          case 'mnu_navigation-expand_all':
                            if (n.parentNode) {
                              n.expand(true);
                            }
                            break;

                          case 'mnu_navigation-collapse':
                            if (n.parentNode) {
                              n.collapse();
                            }
                            break;                        
                          case 'mnu_navigation-collapse_all':
                            if (n.parentNode) {
                              n.collapse(true);
                            }
                            break;
                        }
                      }
                    }
                  }
            },'-',{
              id: 'mnu_navigation-rename',
              text: 'Rename Item'
            },{
              id: 'mnu_navigation-editlink',
              text: 'Edit Link',
              hidden:true
            },{
              id: 'mnu_navigation-editpage',
              text: 'Edit Page',
              hidden:true
            },{
              id: 'mnu_navigation-editgcd',
              text: 'Edit Info',
              hidden:true
            },{
              id: 'mnu_navigation-delete',
              text: 'Delete Item',
              hidden: true
            }],
            listeners: {
              itemclick: function(item) {
                var n = item.parentMenu.contextNode;
                switch (item.id) {
                  case 'mnu_navigation-expand_collapse':
                    if (n.parentNode) {
                      n.toggle();
                    }
                    break;
                    
                  case 'mnu_navigation-intranet':
                    n.attributes.private = !item.checked;
                    if (!item.checked)
                      n.getUI().addClass('boldTreeNavDealie');
                    else
                      n.getUI().removeClass('boldTreeNavDealie');
                    break;
                    
                  case 'mnu_navigation-editpage':
                    callEditor({navigationContextNode: n, editorDataID: n.attributes.data_id, editorType: 'page', editorModal: true});
                    break;
                    

                  case 'mnu_navigation-editgcd':
                    callEditor({navigationContextNode: n, editorDataID: n.attributes.gcd_id, editorType: n.attributes.gcd_type, editorModal: true});
                    break;

                  case 'mnu_navigation-editlink':
                    var tehForm = new Ext.FormPanel({
                            region: 'center',
                            labelWidth: 75, // label settings here cascade unless overridden
                            bodyStyle:'padding:10px 10px 0',
                            defaults: {width: 230},
                            defaultType: 'textfield',
                            items: [{
                                    fieldLabel: 'Link Title',
                                    name: 'linkName',
                                    value:n.attributes.linkName,
                                    allowBlank:false
                                },{
                                    fieldLabel: 'Link URL',
                                    name: 'linkURL',
                                    value:n.attributes.linkURL,
                                    allowBlank:false
                                }, new Ext.form.ComboBox({
                                    name: 'linkTarget',
                                    hiddenName: 'linkTargetValue',
                                    fieldLabel: 'Link Target',
                                    store: [['', 'Same Window'], ['_blank', 'New Tab/Window']],
                                    typeAhead: true,
                                    value:n.attributes.linkTarget,
                                    forceSelection: true,
                                    triggerAction: 'all',
                                    selectOnFocus:true
                                })

                            ]
                          });
                      var formHolder = new Ext.Panel({
                        border: false,
                        region: 'center',
                        layout: 'border',
                        items: [
                          {
                            region: 'north', 
                            margins:'10 10 10 10',
                            cmargins:'3 3 3 3', 
                            baseCls: 'x-plain',
                            border: true,
                            html: '&bull; <strong>Title & URL</strong> specifies where the link goes.<br />&bull; <strong>Target</strong> lets you load in a new window.'
                          },tehForm]
                      });
                    var winNewLink = new Ext.Window({
                          width:350,
                          height:220,
                          title: 'Edit Link',
                          layout:'fit',
                          closeAction:'close',
                          modal:true,
                          closable:false,
                          border:false,
                          resizable:false,
                          items: formHolder,
                          buttons: [{
                              text: 'Save',
                              handler: function(f){
                                var myForm = tehForm.getForm();
                                var myFormVals = myForm.getValues();
                                n.setText(myFormVals.linkName);
                                n.attributes.linkName = myFormVals.linkName;
                                n.attributes.linkURL = myFormVals.linkURL;
                                n.attributes.linkTarget = myFormVals.linkTargetValue;
                                winNewLink.close();
                              }
                            },{
                              text: 'Cancel',
                              handler: function(){
                                  winNewLink.close();
                                }
                            }]
                        });
                    winNewLink.show(this);
                    break;
                  case 'mnu_navigation-rename':
                    if (n.parentNode) {
                      Ext.Msg.prompt('Rename Menu Item', 'Please enter a new name for this item:', function(btn, text){
                        if (btn == 'ok' && text.trim()){
                          n.setText(text);
                        }
                      }, this, false, n.text);
                    }
                    break;

                  case 'mnu_navigation-show':
                    if (n.parentNode) {
                      n.getUI().toggleCheck(true);
                    }
                    break;
                    
                  case 'mnu_navigation-hide':
                    if (n.parentNode) {
                      n.getUI().toggleCheck(false);
                    }
                    break;
                    
                  case 'mnu_navigation-delete':
                    var msgTitle = 'Delete Item?';
                    var msgStr = 'You are about to delete a menu. Are you sure you want to do this?';
                    if (n.attributes.data_type=='page'){
                      msgTitle = 'Delete Page?';
                      msgStr = 'You are about to delete a page. This will also delete the content. Are you sure you want to do this?';
                    }else if (n.attributes.data_type=='header'){
                      msgTitle = 'Delete Header?';
                      msgStr = 'You are about to delete a header. This will also remove sub-menus. Are you sure you want to do this?';
                    }
                    if (n.parentNode) {
                      Ext.Msg.show({
                        title: msgTitle,
                        msg: msgStr,
                        buttons: Ext.Msg.YESNO,
                        fn: function(btn){
                          if (btn == 'yes'){
                            n.attributes.deleted=true;
                            n.getUI().hide();
                          }
                        },
                        icon: Ext.MessageBox.QUESTION
                      });
                    }
                    break;
                }
              }
            }
          });
      
      
      function windowNavEditor(loadMode, gcdID, gcdType){
        if (!gcdID)
          gcdID = 0;
        if (!gcdType)
          gcdType = '';
        if (!canEdit)
          return false;
        var tree2 = new Ext.tree.TreePanel({
              id : 'mainModuleList_adder',
              useArrows:true,
              autoScroll:true,
              animate:true,
              region: 'center',
              enableDD:true,
              //tbar: [{text: 'Add'},{text: 'Rename'},{text: 'Delete'}],
              containerScroll: true,
              rootVisible: false,
              root: { nodeType: 'async', allowDrop: false},
              dataUrl : '/ajax/siteconfig/moduleList.cfm?mode='+(gcdType=='Class'?'Classes':gcdType)+'&gcd='+gcdID+'&title='+encodeURIComponent(currentPageTitle)+'&ref='+encodeURIComponent(window.location.href),
              listeners: {
                checkchange: function(node, checked){
                  if(checked){
                    node.getUI().addClass('complete');
                  }else{
                    node.getUI().removeClass('complete');
                  }
                }
              }
          });
        
                   
        var tree = new Ext.tree.TreePanel({                    
              useArrows:true,
              id: 'mainNavEditor',
              autoScroll:true,
              animate:true,
              region: 'center',
              enableDD:true,
              containerScroll: true,
              rootVisible: false,
              root: { nodeType: 'async'},
              dataUrl : '/ajax/siteconfig/nav/navNodes.cfm?mode='+gcdType+'&gcd='+gcdID,
              listeners: {
                beforenodedrop: function(e){
                  //alert();
                  //alert(tree2.identify());

                  if(e.dropNode.attributes.data_type=="header" && e.target.getDepth() > 1){
                    e.cancel=true;
                    return false;
                  }else if(e.dropNode.attributes.data_type=="header" && e.target.getDepth() == 1 && e.point=="append"){
                    e.cancel=true;
                    return false;
                  }else if(e.dropNode.attributes.data_type!="header" && e.target.getDepth() == 1 && e.point!="append"){
                    e.cancel=true;
                    return false;
                  }
                  if (e.source.tree.id!=e.tree.id){
                    var newEl = e.dropNode.clone();
                    newEl.leaf=false;
                    newEl.getUI().toggleCheck(true);                    
                    e.dropNode = newEl;
                    if (newEl.attributes.data_type=='module'){
                      setTimeout(function(){
                        //windowModuleConfig(newEl);
                      }, 100);
                    }else if (newEl.attributes.data_type=='header'){
                        setTimeout(function(){
                          Ext.Msg.prompt('Rename Menu Item', 'Please enter a new name for this item:', function(btn, text){
                            if (btn == 'ok' && text.trim()){
                              e.dropNode.setText(text);
                            }else{
                              e.dropNode.remove();
                              //e.target.removeChild(e.dropNode);
                              //if (e.dropNode) // make sure it's gone!
                              //  e.target.removeChild(e.dropNode);
                            }
                          }, this, false, e.dropNode.text);
                        }, 100);
                    }else if (newEl.attributes.data_type=='page' && newEl.attributes.browse==true){
                        setTimeout(function(){
                          browseForPage({navigationContextNode: newEl});
                        }, 100);
                    }else if (newEl.attributes.data_type=='page'){
                        setTimeout(function(){
                          callEditor({navigationContextNode: newEl, editorDataID: newEl.attributes.data_id, editorType: 'page', editorModal: true});
                        }, 100);
                    }else if (newEl.attributes.newLink){
                        setTimeout(function(){
                          var tehForm = new Ext.FormPanel({
                                  region: 'center',
                                  labelWidth: 75, // label settings here cascade unless overridden
                                  bodyStyle:'padding:10px 10px 0',
                                  defaults: {width: 230},
                                  defaultType: 'textfield',
                                  items: [{
                                          fieldLabel: 'Link Title',
                                          name: 'linkName',
                                          value:newEl.attributes.linkName,
                                          allowBlank:false
                                      },{
                                          fieldLabel: 'Link URL',
                                          name: 'linkURL',
                                          value:newEl.attributes.linkURL,
                                          allowBlank:false
                                      }, new Ext.form.ComboBox({
                                          name: 'linkTarget',
                                          hiddenName: 'linkTargetValue',
                                          fieldLabel: 'Link Target',
                                          store: [['', 'Same Window'], ['_blank', 'New Tab/Window']],
                                          typeAhead: true,
                                          value:newEl.attributes.linkTarget,
                                          forceSelection: true,
                                          triggerAction: 'all',
                                          selectOnFocus:true
                                      })

                                  ]
                                });
                          var formHolder = new Ext.Panel({
                            border: false,
                            region: 'center',
                            layout: 'border',
                            items: [
                              {
                                region: 'north', 
                                margins:'10 10 10 10',
                                cmargins:'3 3 3 3', 
                                baseCls: 'x-plain',
                                border: true,
                                html: '&bull; <strong>Title & URL</strong> specifies where the link goes.<br />&bull; <strong>Target</strong> lets you load in a new window.<br /><br /><strong>Click \'Save\' below when you are finished.</strong>'
                              },tehForm]
                          });
                          var winNewLink = new Ext.Window({
                                width:350,
                                height:240,
                                title: 'New Link',
                                layout:'fit',
                                closeAction:'close',
                                modal:true,
                                closable:false,
                                border:false,
                                resizable:false,
                                items: formHolder,
                                buttons: [{
                                    text: 'Save',
                                    handler: function(f){
                                      var myForm = tehForm.getForm();
                                      var myFormVals = myForm.getValues();
                                      e.dropNode.setText(myFormVals.linkName)
                                      e.dropNode.attributes.linkName = myFormVals.linkName;
                                      e.dropNode.attributes.linkURL = myFormVals.linkURL;
                                      e.dropNode.attributes.linkTarget = myFormVals.linkTargetValue;
                                      if (myFormVals.linkName.trim() == "" || myFormVals.linkURL.trim() == "" || myFormVals.linkURL == "http://"){
                                        e.target.removeChild(e.dropNode);
                                        if (e.dropNode) // make sure it's gone!
                                          e.target.removeChild(e.dropNode);
                                      }
                                      winNewLink.close();
                                    }
                                  },{
                                    text: 'Cancel',
                                    handler: function(){
                                        e.target.removeChild(e.dropNode);
                                        if (e.dropNode) // make sure it's gone!
                                          e.target.removeChild(e.dropNode);
                                        winNewLink.close();
                                      }
                                  }]
                              });
                          winNewLink.show(this);
                        }, 100);
                    }
                  }
                },
                contextmenu: function(node, e) {
                  node.select();
                  var c = treeNavMenu;
                  c.contextNode = node;
                  c.findById('mnu_navigation-hide').setVisible(node.getUI().isChecked());
                  c.findById('mnu_navigation-show').setVisible(!node.getUI().isChecked());
                  c.findById('mnu_navigation-intranet').setChecked(node.attributes.private);
                  c.findById('mnu_navigation-rename').setDisabled(node.attributes.topbar);
                  c.findById('mnu_navigation-rename').setVisible(node.attributes.data_type!="link" ? true : false);
                  c.findById('mnu_navigation-editlink').setVisible(node.attributes.data_type=="link" ? true : false);
                  c.findById('mnu_navigation-editpage').setVisible(node.attributes.data_type=="page" ? true : false);
                  c.findById('mnu_navigation-editgcd').setVisible(gcdID && node.getDepth()==1 ? true : false);
                  c.findById('mnu_navigation-delete').setVisible(!node.attributes.topbar);
                  if (gcdID && node.getDepth()==1){
                    c.findById('mnu_navigation-editgcd').setText('Edit '+gcdType+' Info');
                    c.findById('mnu_navigation-hide').setDisabled(true);
                    c.findById('mnu_navigation-show').setDisabled(true);
                    c.findById('mnu_navigation-rename').setDisabled(true);
                    c.findById('mnu_navigation-delete').setDisabled(true);
                  }else{
                    c.findById('mnu_navigation-hide').setDisabled(false);
                    c.findById('mnu_navigation-show').setDisabled(false);
                    c.findById('mnu_navigation-rename').setDisabled(false);
                    c.findById('mnu_navigation-delete').setDisabled(false);
                  }
                  c.showAt(e.getXY());
                  Ext.getCmp('navMenu').setValue(node.attributes.iconCls);
                  //Ext.getCmp('navMenuHolder').parentMenu.contextNode.getUI().getIconEl().className = node.attributes.iconCls;
                },
                checkchange: function(node, checked){
                  if(checked){
                    node.getUI().addClass('complete');
                  }else{
                    node.getUI().removeClass('complete');
                  }
                  if (gcdID && node.getDepth()==1){
                    node.getUI().toggleCheck(true);
                  }
                }
              }
          });
        var treeLeft = new Ext.Panel({
          border: false,
          region: 'center',
          margins:'5 0 0 0',
          layout: 'border',
          items: [
            {
              region: 'north', 
              margins:'0 0 -1 0',
              baseCls: 'siteConfigHeaderTab',
              cmargins:'3 3 3 3',
              html: '<strong>Current '+(gcdType?gcdType+' ':'')+'Navigation</strong>'
            },tree]
        });
        var treeRight = new Ext.Panel({
          border: false,
          region: 'east',
          layout: 'border',
          margins:'5 0 0 5',
          cmargins:'3 3 3 3',
          width: 230,
          items: [
            {
              region: 'north', 
              margins:'0 0 -1 0',
              baseCls: 'siteConfigHeaderTab',
              cmargins:'3 3 3 3', 
              border: true, 
              html: '<strong>Available Modules</strong>'
            },{
              region: 'south', 
              margins:'10 10 10 10',
              cmargins:'3 3 3 3',
              baseCls: 'x-plain', 
              html: '<strong>Drag & Drop above modules</strong> into your navigation on the left to activate them.'
            },tree2]
        });
        var navInfo = '&bull; <strong>Drag & Drop</strong> to add or re-order menus.<br />&bull; <strong>Check & uncheck</strong> to show & hide menus.<br />&bull; <strong>Right-click</strong> for options such as rename and delete.<br /><br />Changes will be applied when you click \'Save\' below.';
        var treeHolder = new Ext.Panel({
          id: 'tabNavigationEditor',
          bodyStyle: 'background: #DFE8F6',
          layout: 'fit',
          items: new Ext.Panel({
              border: false,          
              layout: 'border',
              items: [
                {
                  region: 'north',
                  border: true, 
                  html: '<div class="siteConfigInfo"><div class="siteConfigHeader">Here is where you edit your navigation!</div><div class="siteConfigText">'+navInfo+'</div></div>'
                },
                treeLeft,
                treeRight]
            })
        });
        
        if (loadMode){
          treeHolder.title = (gcdID && gcdType ? gcdType+' ' : '') + 'Navigation';
          return treeHolder;
        }
          
        var winNavEdit = new Ext.Window({
              minWidth:600,
              minHeight:400,
              width:600,
              height:500,
              title: (gcdID && gcdType ? gcdType+' ' : '') + 'Navigation Editor',
              layout:'fit',
              closeAction:'close',
              modal:true,
              closable:false,
              border:false,
              maximizable:true,
              defaults: {autoScroll:true, padding: 5},
              items: treeHolder,
              bodyStyle: 'background: #ddd;',
              buttons: [{
                text: 'Save',
                handler: function(){
                  var tmpSerial = new Ext.tree.JsonTreeSerializer(tree, { nodeFilter: function(node) {
                      //node.attributes.linkURL = encodeURIComponent(decodeURI(decodeURI(decodeURI(decodeURI(decodeURI(node.attributes.linkURL))))));
                      return true;
                    }});
                    //alert(tmpSerial.toString());
                    //return false;
                  Ext.Msg.wait('Saving your navigation configuration. This may take a few seconds.', 'Please wait...');
                  new Ajax.Request('/ajax/siteconfig/nav/updateNav.cfm?gcdType='+gcdType+'&gcd='+gcdID, {
                    postBody: 'navData=' + Base64.encode(tmpSerial.toString()),
                    onSuccess: function(transport){
                      Ext.Msg.updateText('Reloading the page...');
                      winNavEdit.close();
                      window.location.reload(true);
                    }
                  });
                }
              },{ text: 'Cancel', handler: function(){ winNavEdit.close(); } }]
          });
        tree.getRootNode().expand(true);
        tree2.getRootNode().expand(true);
        winNavEdit.show(this);
      }
      
      function checkMenus(){
        $$('.horizontalChild div').each(function(e){
          var child = e;
          var viewPort = document.viewport.getDimensions();
          var thePos = child.viewportOffset();
          var theSize = child.getDimensions();
          
          if (thePos.left + theSize.width >= viewPort.width){
            var offLeft = theSize.width;
            child.style.left = -offLeft + 'px';
          }
        });
        setTimeout(checkMenus, 500);
      }

reportingIn('navTab');