﻿//DropDown
Type.registerNamespace("PH.CMS.AutoCompleteTable");
Type.registerNamespace("PH.CMS.AutoComplete");
Type.registerNamespace("PH.CMS.Global");

PH.CMS.Global.Rectangle = function() {
  this._top = 0;
  this._left = 0;
  this._width = 0;
  this._height = 0;
}

PH.CMS.Global.GetPosition = function (obj) {
  var oRect = new PH.CMS.Global.Rectangle();
  var oControl = obj;

  oRect._width = oControl.offsetWidth;
  oRect._height = oControl.offsetHeight;
  oRect._left = oControl.offsetLeft;
  oRect._top = oControl.offsetTop;

  while(oControl = oControl.offsetParent) {
    oRect._left += oControl.offsetLeft;
    oRect._top  += oControl.offsetTop;
  }
    
  oControl = obj;

  while(oControl = oControl.parentNode) {
    if(oControl.scrollTop && oControl.tagName.toLowerCase() != 'html') {
      oRect._top -= oControl.scrollTop;
      oRect._left -= oControl.scrollLeft;
    }
  }

  return oRect;
}

/* 
 * CLASS : AutoCompleteControl
 */
PH.CMS.AutoComplete.Control = function(element) {
  PH.CMS.AutoComplete.Control.initializeBase(this, [element]);
  
  element._autoComplete = this;
  
  this._autoPostBack = false;
  this._multiSelect = false;
  this._commandName = null;
  this._commandArgument = null;
}

PH.CMS.AutoComplete.Control.prototype = {  
  //# initialiseDropDown
  initialiseDropDown: function(flags, commandName, commandArgument) {  
    this._multiSelect  = ((1 & flags) == 1);
    this._autoPostBack = ((2 & flags) == 2);
    this._commandName = commandName;
    this._commandArgument = commandArgument;
  }
}
PH.CMS.AutoComplete.Control.registerClass('PH.CMS.AutoComplete.Control', Sys.UI.Control);

/* 
 * CLASS : AutoCompleteTableControl
 */
PH.CMS.AutoCompleteTable.Control = function(element) {
  PH.CMS.AutoCompleteTable.Control.initializeBase(this, [element]);
  
  element._autoCompleteTable = this;
  
  this._col1Width = null;
  this._col2Width = null;
  this._hidden = null;
  this._table = element;
  this._tbody = element;
  this._rowCount = 0;
  
}

PH.CMS.AutoCompleteTable.Control.prototype = {  
  //# initialiseTable
  initialiseTable: function(id, col1Width, col2Width) {  
    this.col1Width = col1Width;
    this.col2Width = col2Width;
    this._hidden = document.getElementById(id + '_hidden');
    
    for(var i=0;i<this._table.childNodes.length;i++) {
        var sTagName = this._table.childNodes[i].tagName;
        if(sTagName) sTagName = sTagName.toLowerCase();
        if(sTagName == "tbody") {
            this._tbody = this._table.childNodes[i];
            break;
        } else if(sTagName == "tr") {
            this._tbody = this._table;
            break;
        }
    }
    
    if(!this._tbody) return;
    
    if(this._hidden.value.length > 0) {
      var oRows = this._hidden.value.split("]|[");
      for(var i=0;i<oRows.length;i++) {
        var oCols = oRows[i].split("/|\\");
        this._createRow(oCols[0], oCols[1]);
      }
    }
    
    this._createRow('','');
  },
  
  _createRow: function(col1, col2) {
    this._rowCount++;
    
    oRowNode = document.createElement('tr');
    this._tbody.appendChild(oRowNode);

    oCellNode = document.createElement('td');
    oRowNode.appendChild(oCellNode);
    oCellNode.style.paddingBottom = "1px";
    oCellNode.style.paddingRight = "1px";
    
    oTextNode = document.createElement('input');
    oTextNode.id = this._table.id + "_" + this._rowCount + 'A';
    oTextNode.type = "text";
    oTextNode.className = "textbox";
    oTextNode.value = col1;
    oTextNode.onchange = PH.CMS.AutoCompleteTable.Control_Change;
    oCellNode.appendChild(oTextNode);
    if(this.col1Width) oTextNode.style.width = this.col1Width;
    PH.CMS.AutoComplete.Initialise(0, 'role', '', oTextNode.id);
    oRowNode._inputA = oTextNode;

    oCellNode = document.createElement('td');
    oRowNode.appendChild(oCellNode);
    oCellNode.style.paddingBottom = "1px";
    
    oTextNode = document.createElement('input');
    oTextNode.id = this._table.id + "_" + this._rowCount + 'B';
    oTextNode.type = "text";  
    oTextNode.className = "textbox";
    oTextNode.value = col2;
    oTextNode.onchange = PH.CMS.AutoCompleteTable.Control_Change;
    oCellNode.appendChild(oTextNode);
    if(this.col2Width) oTextNode.style.width = this.col2Width;
    PH.CMS.AutoComplete.Initialise(1, 'page', '3', oTextNode.id);
    oRowNode._inputB = oTextNode;
  },
  
  save: function() {
    var sValue = '';
    var oRemove = new Array();
    var oLast = null;
    
    for(var i=0;i<this._tbody.childNodes.length;i++) {
      var oRow = this._tbody.childNodes[i];
      if(oRow._inputA) {
        if(oRow._inputA.value.length == 0 && oRow._inputB.value.length == 0) {
          oRemove.push(oRow);
          oLast = oRow;
        } else {
          if(sValue.length > 0) sValue += "]|[";
          sValue += oRow._inputA.value
          sValue += "/|\\";
          sValue += oRow._inputB.value
          oLast = null;
        }
      }
    }
    
    for(var i=0;i<oRemove.length;i++) {
      if(oRemove[i] != oLast) {
        this._tbody.removeChild(oRemove[i]);
      }
    }
    
    if(!oLast) {
      this._createRow('','');
    }
    
    this._hidden.value = sValue;
  }
}
PH.CMS.AutoCompleteTable.Control.registerClass('PH.CMS.AutoCompleteTable.Control', Sys.UI.Control);

/*
 *  CLASS : DropDown Manager
 */
PH.CMS.AutoComplete.Manager = function() {
  this._panel = null;
  this._control = null;
  this._selectedItem = null;
  this._currentIndex = null;
  this._unselectTimeout = 0;
  this._lastTerm = null;
  this._items = new Array();
}

PH.CMS.AutoComplete.Manager.prototype = {

  _initialiseRenderPanel: function() {
    if(this._panel != null) {
      document.forms[0].removeChild(this._panel);
    }

    //Create Panel
    this._panel = document.createElement("div");
    this._panel.className = 'autocomplete';
    this._panel.style.display = 'none';
    this._panel.style.position = 'absolute';
    this._panel.style.top = '10px';
    this._panel.style.left = '10px';
    document.forms[0].appendChild(this._panel);
  },
  
  selectControl: function(control) {
    if(this._unselectTimeout != 0) {
      clearTimeout(this._unselectTimeout);
      this._unselectTimeout = 0;
    }

    if(this._control != control) {
        this._unselectControl();
        
        this._control = control;
        this._control.onblur = PH.CMS.AutoComplete.Control_Blur;
        this._control.onkeydown = PH.CMS.AutoComplete.Control_KeyDown;
        this._control.onkeyup = PH.CMS.AutoComplete.Control_KeyUp;
    }
    
    this.hideItems();
  },
  
  unselectControl: function() {
    this._unselectTimeout = setTimeout('PH.CMS.AutoComplete.oManager._unselectControl();', 200);
  },

  nextItem: function() {
    if(this._currentIndex == this._items.length - 1) return;
    this._currentIndex += 1;
    this._highlightSelected();
  },

  previousItem: function() {
    if(this._currentIndex == 0) return;
    this._currentIndex -= 1;
    this._highlightSelected();
  },
  
  selectItem: function(link) {
    if(this._panel == null) return;
    if(this._panel.style.display == 'none') return;    
    if(!link && this._currentIndex >= this._items.length) return;

    var oItem = (link) ? link._item : this._items[this._currentIndex];
    var iIndex = this._control.value.lastIndexOf(",");
    
    if(this._control._autoComplete._multiSelect && iIndex > 0) {
      this._control.value = this._control.value.substr(0, iIndex) + ", " + oItem[0];
    } else {
      this._control.value = oItem[0];
    }
    
    this._control.onchange();
    this._control.focus();
    this.hideItems();
  },
  
  hideItems: function() {
    if(this._panel != null) {
      this._panel.style.display = 'none';
    }
  },
    
  lookupItems: function(term) {
    term = term.replace(/^\s+|\s+$/g,""); 

    if(term.length > 0) {
      if(this._lastTerm == term) {
        this.showItems();
      } else {
        this._items = new Array();
        this._currentIndex = 0;
        this._lastTerm = term;
        this.hideItems();

        AutoCompleteService.GetItems(term, this._control._autoComplete._commandName, this._control._autoComplete._commandArgument, PH.CMS.AutoComplete.Lookup_Success, PH.CMS.AutoComplete.Lookup_Failed, null);
      }
    } else {
        this._items = new Array();
        this._currentIndex = 0;
        this.hideItems();
    }
  },
  
  _highlightSelected: function() {
    for(var i=0;i<this._panel.childNodes.length;i++) {
      if(i == this._currentIndex) {
        this._panel.childNodes[i].className = 'selected';
        this._panel.scrollTop = (i * 22);
      } else {
        this._panel.childNodes[i].className = null;
      }
    }
  },
  
  _unselectControl: function() {
    this._unselectTimeout = 0;
    if(this._control != null) {
      this._control.onblur = null;
      this._control.onkeypress = null;
      this._control.onkeyup = null;
      this._control = null;
      this.hideItems();
    }
  },

  _loadItems: function() {
    this._currentIndex = 0;

    if(!this._items || this._items.length == 0) {
       this.hideItems();
       return;
    }
    
    this._initialiseRenderPanel();

    while (this._panel.hasChildNodes()) {
      this._panel.removeChild(this._panel.firstChild);       
    }
        
    for(var i=0;i<this._items.length;i++) {
      var oLink = document.createElement("a");
      oLink._item = this._items[i];
      oLink.href = 'javascript:void(0);';
      oLink.onclick = PH.CMS.AutoComplete.Item_Click;
      oLink.innerHTML = (this._items[i][1]) ? this._items[i][1] : this._items[i][0];
      this._panel.appendChild(oLink);
    }
    
    var oDiv = document.createElement("div");
    oDiv.style.height = '1px';
    this._panel.appendChild(oDiv);
    
    this.showItems();
    this._highlightSelected();
  },
  
  showItems: function() {
    if(!this._items || this._items.length == 0) {
       this.hideItems();
       return;
    }

    var oPos = PH.CMS.Global.GetPosition(this._control);
    
    this._panel.ScrollTop = 0 + 'px';
    this._panel.style.top = (oPos._top + oPos._height) + 'px';
    this._panel.style.left = oPos._left + 'px';
    this._panel.style.width = (oPos._width - 4) + 'px';
    this._panel.style.height = (this._items.length >= 5) ? '111px' : ((this._items.length * 22) + 1) + 'px';
    this._panel.style.display = 'block';
  }
}

/*
 * Functions
 */
PH.CMS.AutoComplete.Control_KeyDown = function(e) {
  var code=null;
  if(window.event){code=window.event.keyCode;}
  else if(e.which==0){code=e.keyCode;}
  else if(e.which){code=e.which;}

  switch(code) {
    case 40     : PH.CMS.AutoComplete.oManager.nextItem(); return false;
    case 38     : PH.CMS.AutoComplete.oManager.previousItem(); return false;
    case 13     : PH.CMS.AutoComplete.oManager.selectItem(); return false;
    case 9      : PH.CMS.AutoComplete.oManager.selectItem(); return true;
    case 27     : PH.CMS.AutoComplete.oManager.hideItems(); return false;
    default     :   
      if(code == 188 && PH.CMS.AutoComplete.oManager._control._autoComplete._multiSelect) {
        PH.CMS.AutoComplete.oManager.hideItems();
      }
      return true;
  }
}

PH.CMS.AutoComplete.Control_KeyUp = function(e) {
  var code=null;
  if(window.event)   {code=window.event.keyCode;}
  else if(e.which==0){code=e.keyCode;}
  else if(e.which)   {code=e.which;}
  
  switch(code) {
    case 40 :
    case 38 :
    case 13 :
    case 27 : return false;
  }
  
  var sTerm = null
  if(this._autoComplete._multiSelect) {
    var iIndex = this.value.lastIndexOf(",");
    if(iIndex < 0) {
      sTerm = this.value
    } else {
      sTerm = this.value.substr(iIndex + 1);
    }
  } else {
    sTerm = this.value;
  }

  PH.CMS.AutoComplete.oManager.lookupItems(sTerm);
}

PH.CMS.AutoCompleteTable.Control_Change = function(e) {
  var oParentNode = this.parentNode;
  while(oParentNode.tagName.toLowerCase() != "table") oParentNode = oParentNode.parentNode;
  oParentNode._autoCompleteTable.save();
}

PH.CMS.AutoComplete.Control_Focus = function(e) {
  PH.CMS.AutoComplete.oManager.selectControl(this);
  if(this.previousfocus) this.previousfocus(e);
}

PH.CMS.AutoComplete.Control_Blur = function(e) {
  PH.CMS.AutoComplete.oManager.unselectControl();
}

PH.CMS.AutoComplete.Item_Click = function(e) {
  PH.CMS.AutoComplete.oManager.selectItem(this);
}

PH.CMS.AutoComplete.Lookup_Success = function(result, eventArgs) {
  eval('PH.CMS.AutoComplete.oManager._items = ' + result);
  PH.CMS.AutoComplete.oManager._loadItems();
}

PH.CMS.AutoComplete.Lookup_Failed = function(error) {
    alert(error.get_message());
}

/*
 * Initialisation
 */
PH.CMS.AutoComplete.Initialise = function(flags,commandName,commandArgument,ClientID) {
  var oControl = $find(ClientID);
  var oElement = $get(ClientID);
  oElement.previousfocus = oElement.onfocus;
  oElement.onfocus = PH.CMS.AutoComplete.Control_Focus;
  
  if(oControl == null) {
    oControl = $create(PH.CMS.AutoComplete.Control,null,null,null,oElement)
  }
  oControl.initialiseDropDown(flags,commandName,commandArgument);
}

PH.CMS.AutoComplete.oManager = new PH.CMS.AutoComplete.Manager();

/*
 * Table Initialisation
 */
PH.CMS.AutoCompleteTable.Initialise = function(ClientID, col1Width, col2Width) {
  var oControl = $find(ClientID);
  var oElement = $get(ClientID);
  if(oControl == null) {
    oControl = $create(PH.CMS.AutoCompleteTable.Control,null,null,null,oElement)
  }
  oControl.initialiseTable(ClientID, col1Width, col2Width);
}

//.NET AJAX
if (typeof(Sys) !== 'undefined') Sys.Application.notifyScriptLoaded(); 