﻿/// <reference name="MicrosoftAjax.js"/>

Type.registerNamespace("ProData.Controls");

ProData.Controls.DropDownComplex = function (element) {
  ProData.Controls.DropDownComplex.initializeBase(this, [element]);
  this._DropListControlClientID = null;
  this._ParentDivClientID = null;
  this._NothingSelectedText = "";
  this._AllowComboInput = true;
  this._ClearInputOnEmptyValue = false;
  this._RetainListValueOnSelect = false;
  this._IsHoveringPop = false;
  this._IsTabbing = false;
  this._OnClientDynFill = "";
  this._OnClientSelect = "";
  this._OnClientChanged = "";
  this._BoxBaseCSS = "ddcBox";
}

// Create the prototype for the control.

ProData.Controls.DropDownComplex.prototype = {

  initialize: function () {
    ProData.Controls.DropDownComplex.callBaseMethod(this, 'initialize');

    // custom initialization
    this._DropListControlClientID = this.get_element().id + "_lbPu";
    this._ParentDivClientID = this.get_element().id + "_dPa";

    this._onClickKeydownHandler = Function.createDelegate(this, this._onClickKeydown);
    this._onClickKeyupHandler = Function.createDelegate(this, this._onClickKeyup);
    this._onClickBlurHandler = Function.createDelegate(this, this._onClickBlur);
    this._onClickClickHandler = Function.createDelegate(this, this._onClickClick);
    this._onListMouseOverHandler = Function.createDelegate(this, this._onListMouseOver);
    this._onListMouseOutHandler = Function.createDelegate(this, this._onListMouseOut);
    $addHandlers(this.get_element(),
                 { 'keydown': this._onClickKeydownHandler,
                   'keyup': this._onClickKeyupHandler,
                   'focus': this._onClickClickHandler,
                   'blur': this._onClickBlurHandler,
                   'mouseover': this._onListMouseOverHandler,
                   'mouseout': this._onListMouseOutHandler
                 },
                 this);

    var curPar = document.getElementById(this._ParentDivClientID);

    curPar._onArrowClickHandler = Function.createDelegate(this, this._onArrowClick);
    curPar._onListMouseOverHandler = Function.createDelegate(this, this._onListMouseOver);
    curPar._onListMouseOutHandler = Function.createDelegate(this, this._onListMouseOut);
    $addHandlers(curPar,
                 { 'click': curPar._onArrowClickHandler,
                   'mouseover': curPar._onListMouseOverHandler,
                   'mouseout': curPar._onListMouseOutHandler
                 },
                 this);

    var curList = document.getElementById(this._DropListControlClientID);

    curList._onListKeydownHandler = Function.createDelegate(this, this._onListKeydown);
    curList._onListClickHandler = Function.createDelegate(this, this._onListClick);
    curList._onListBlurHandler = Function.createDelegate(this, this._onListBlur);
    curList._onListMouseOverHandler = Function.createDelegate(this, this._onListMouseOver);
    curList._onListMouseOutHandler = Function.createDelegate(this, this._onListMouseOut);
    $addHandlers(curList,
                 { 'keydown': curList._onListKeydownHandler,
                   'click': curList._onListClickHandler,
                   'blur': curList._onListBlurHandler,
                   'mouseover': curList._onListMouseOverHandler,
                   'mouseout': curList._onListMouseOutHandler
                 },
                 this);

  },
  dispose: function () {
    //Add custom dispose actions here
    var curList = document.getElementById(this._DropListControlClientID);
    $clearHandlers(curList);
    var curPar = document.getElementById(this._ParentDivClientID);
    $clearHandlers(curPar);
    $clearHandlers(this.get_element());

    ProData.Controls.DropDownComplex.callBaseMethod(this, 'dispose');
  },

  // Control properties

  get_NothingSelectedText: function () {
    return this._NothingSelectedText;
  },
  set_NothingSelectedText: function (value) {
    if (this._NothingSelectedText !== value) {
      this._NothingSelectedText = value;
      //this.raisePropertyChanged('SelectedControlClientID');
    }
  },

  get_AllowComboInput: function () {
    return this._AllowComboInput;
  },
  set_AllowComboInput: function (value) {
    if (this._AllowComboInput !== value) {
      this._AllowComboInput = value;
      //this.raisePropertyChanged('SelectedControlClientID');
    }
  },

  get_ClearInputOnEmptyValue: function () {
    return this._ClearInputOnEmptyValue;
  },
  set_ClearInputOnEmptyValue: function (value) {
    if (this._ClearInputOnEmptyValue !== value) {
      this._ClearInputOnEmptyValue = value;
      //this.raisePropertyChanged('SelectedControlClientID');
    }
  },

  get_RetainListValueOnSelect: function () {
    return this._RetainListValueOnSelect;
  },
  set_RetainListValueOnSelect: function (value) {
    if (this._RetainListValueOnSelect !== value) {
      this._RetainListValueOnSelect = value;
      //this.raisePropertyChanged('SelectedControlClientID');
    }
  },

  get_OnClientDynFill: function () {
    return this._OnClientDynFill;
  },
  set_OnClientDynFill: function (value) {
    if (this._OnClientDynFill !== value) {
      this._OnClientDynFill = value;
      //this.raisePropertyChanged('SelectedControlClientID');
    }
  },

  get_OnClientSelect: function () {
    return this._OnClientSelect;
  },
  set_OnClientSelect: function (value) {
    if (this._OnClientSelect !== value) {
      this._OnClientSelect = value;
      //this.raisePropertyChanged('SelectedControlClientID');
    }
  },

  get_OnClientChanged: function () {
    return this._OnClientChanged;
  },
  set_OnClientChanged: function (value) {
    if (this._OnClientChanged !== value) {
      this._OnClientChanged = value;
      //this.raisePropertyChanged('SelectedControlClientID');
    }
  },

  get_BoxBaseCSS: function () {
    return this._BoxBaseCSS;
  },
  set_BoxBaseCSS: function (value) {
    this._BoxBaseCSS = value;
  },

  // Event delegates - input tag

  _onClickKeydown: function (e) {
    if (this.get_element() && !this.get_element().disabled) {
      if (e.keyCode == 9 && !e.shiftKey) {
        this._IsTabbing = true;
        var curList = document.getElementById(this._DropListControlClientID);
        this.get_element().blur();
        this._onClickClick(e);
        curList.focus();
        e.preventDefault();
        e.stopPropagation();
      }
    }
  },
  _onClickKeyup: function (e) {
    if (this.get_element() && !this.get_element().disabled) {
      if (this._OnClientDynFill != "") {
        eval(this._OnClientDynFill)(this.get_element().id, e);
      }
    }
  },
  _onArrowClick: function (e) {
    if (this.get_element() && !this.get_element().disabled) {
      if (document.getElementById(this._ParentDivClientID).className != this._BoxBaseCSS + "Open") {
        this.get_element().focus();
        e.preventDefault();
        e.stopPropagation();
      }
      else {
        if (this._AllowComboInput)
          this.get_element().focus();
        else {
          var curList = document.getElementById(this._DropListControlClientID);
          curList.focus();
        }
        e.preventDefault();
        e.stopPropagation();
      }
    }
  },
  _onClickClick: function (e) {
    if (this.get_element() && !this.get_element().disabled) {
      document.getElementById(this._ParentDivClientID).className = this._BoxBaseCSS + "Open";
      var curList = document.getElementById(this._DropListControlClientID);
      curList.style.visibility = "visible";
      curList.style.display = "block";
      if (this._OnClientDynFill != "") {
        eval(this._OnClientDynFill)(this.get_element().id, e);
      }
      if (!this._AllowComboInput)
        curList.focus();
    }
  },
  _onClickBlur: function (e) {
    if (this._IsTabbing == true) {
      this._IsTabbing = false;
      e.preventDefault();
      e.stopPropagation();
      return;
    }
    if (this.get_element() && !this.get_element().disabled) {
      var curList = document.getElementById(this._DropListControlClientID);
      if (this._AllowComboInput && !this._IsHoveringPop) {
        var curList = document.getElementById(this._DropListControlClientID);
        curList.style.visibility = "hidden";
        curList.style.display = "none";
        document.getElementById(this._ParentDivClientID).className = this._BoxBaseCSS;

        if (this._OnClientChanged != "") {
          eval(this._OnClientChanged)(this.get_element().id, e);
        }
      }
    }
  },

  // Event delegates - lbDropList

  _onListKeydown: function (e) {
    if (this.get_element() && !this.get_element().disabled) {
      if (e.keyCode == 9 && !e.shiftKey) {
        var curList = document.getElementById(this._DropListControlClientID);
        e.preventDefault();
        e.stopPropagation();
        this._onListBlur(e); // auto action

        var i, j;
        var curForm = this.get_element().form;
        for (i = 0; i < curForm.elements.length; i++) {
          if (curForm.elements[i].id == this.get_element().id) {
            i++;
            if (i < curForm.elements.length) {
              for (j = i; j < curForm.elements.length; j++) {
                if (!curForm.elements[j].disabled && curForm.elements[j].style.display != "none" && curForm.elements[j].type != "hidden") {
                  curForm.elements[j].focus();
                  return;
                }
              }
            }
          }
        }
      }
      if (e.keyCode == 9 && e.shiftKey && !this._AllowComboInput) {
        var curList = document.getElementById(this._DropListControlClientID);
        e.preventDefault();
        e.stopPropagation();
        this._onListBlur(e); // auto action

        var i, j;
        var curForm = this.get_element().form;
        for (i = 0; i < curForm.elements.length; i++) {
          if (curForm.elements[i].id == this.get_element().id) {
            i--; // skip input that does not allow focus
            if (i > 0) {
              for (j = i; j > 0; j--) {
                if (!curForm.elements[j].disabled && curForm.elements[j].style.display != "none" && curForm.elements[j].type != "hidden") {
                  curForm.elements[j].focus();
                  return;
                }
              }
            }
          }
        }
      }
    }
  },

  _onListClick: function (e) {
    if (this.get_element() && !this.get_element().disabled) {
      var curList = document.getElementById(this._DropListControlClientID);
      //e.preventDefault();
      e.stopPropagation();
      curList.blur();
    }
  },

  _onListBlur: function (e) {
    if (this.get_element() && !this.get_element().disabled) {
      var oldValue = this.get_element().value;
      var curList = document.getElementById(this._DropListControlClientID);
      curList.style.visibility = "hidden";
      curList.style.display = "none";
      document.getElementById(this._ParentDivClientID).className = this._BoxBaseCSS;
      var y = curList.selectedIndex;
      if (y >= 0) {
        if (this._ClearInputOnEmptyValue && curList.options[y].value == "") {
          this.get_element().value = "";
          curList.selectedIndex = -1;
          this.get_element().focus();
        }
        else {
          this.get_element().value = curList.options[y].text;
        }
        if (this._AllowComboInput && !this._RetainListValueOnSelect) {
          curList.selectedIndex = -1;
        }
      }
      else {
        if (!this._AllowComboInput)
          this.get_element().value = this._NothingSelectedText;
      }
      if (this._OnClientSelect != "" && y >= 0) { /*&& oldValue != this.get_element().value*/
        eval(this._OnClientSelect)(this.get_element().id, null);
      }
      if (this._OnClientChanged != "" && oldValue != this.get_element().value) {
        eval(this._OnClientChanged)(this.get_element().id, e);
      }
    }
  },
  _onListMouseOver: function (e) {
    if (this.get_element() && !this.get_element().disabled) {
      this._IsHoveringPop = true;
    }
  },
  _onListMouseOut: function (e) {
    if (this.get_element() && !this.get_element().disabled) {
      this._IsHoveringPop = false;
    }
  },

  // Java client utility functions

  itemsClear: function () {
    if (this.get_element() && !this.get_element().disabled) {
      var curList = document.getElementById(this._DropListControlClientID);
      curList.innerHTML = "";
      if (!this._AllowComboInput)
        this.get_element().value = this._NothingSelectedText;
    }
  },

  itemsAdd: function (itemText, itemValue) {
    if (this.get_element() && !this.get_element().disabled) {
      var curList = document.getElementById(this._DropListControlClientID);
      curList.options[curList.options.length] = new Option(itemText, itemValue); ;
    }
  },

  items: function (i) {
    if (this.get_element() && !this.get_element().disabled) {
      var curList = document.getElementById(this._DropListControlClientID);
      return curList.options[i];
    }
    return null;
  },

  itemsLength: function () {
    if (this.get_element() && !this.get_element().disabled) {
      var curList = document.getElementById(this._DropListControlClientID);
      return curList.options.length;
    }
    return null;
  },

  selectedIndex: function () {
    if (this.get_element() && !this.get_element().disabled) {
      var curList = document.getElementById(this._DropListControlClientID);
      return curList.selectedIndex;
    }
    return null;
  },
  selectedValue: function () {
    if (this.get_element() && !this.get_element().disabled) {
      var curList = document.getElementById(this._DropListControlClientID);
      return curList.value;
    }
    return null;
  },
  selectedText: function () {
    if (this.get_element() && !this.get_element().disabled) {
      return this.get_element().value;
    }
    return null;
  }

}

// descriptor for JSON serialization.
ProData.Controls.DropDownComplex.descriptor = {
  properties: [{ name: 'NothingSelectedText', type: String },
  { name: 'AllowComboInput', type: Boolean },
  { name: 'ClearInputOnEmptyValue', type: Boolean },
  { name: 'RetainListValueOnSelect', type: Boolean },
  { name: 'OnClientDynFill', type: String },
  { name: 'OnClientSelect', type: String },
  { name: 'OnClientChanged', type: String },
  { name: 'BoxBaseCSS', type: String}]
}

ProData.Controls.DropDownComplex.registerClass('ProData.Controls.DropDownComplex', Sys.UI.Control);

if (typeof (Sys) !== 'undefined') Sys.Application.notifyScriptLoaded();

// if (Sys && Sys.Application) { Sys.Application.notifyScriptLoaded(); } 

