ST.namespace('ST');


ST.Grid = (function() {
   
  /**
   * @constructor
   * @param {type} data
   */
  var component = function(data)
  {
    this._data = data;
    
    ST.defSettings(this._data,{
      hierachicalFlag: false,
      toolbar: {}
    });
    var self = this;
    //Нужно место под тулбар?
    //var hasButtons = ST.isArray(this._data['buttons']['items']) && this._data['buttons']['items'].length > 0;
    
    this.layout = new ST.grid.GridLayout({
      'wrapper': this._data['wrapper'],
      'handler': {
        'scrolledUp': function(scroledAtPixels, scrollTopValue, maxScroll){
          self.items.view.scrolledOneElemetUp(scroledAtPixels, scrollTopValue, maxScroll);
        },
        'scrolledDown': function(scroledAtPixels, scrollTopValue, maxScroll){
          self.items.view.scrolledOneElemetDown(scroledAtPixels, scrollTopValue, maxScroll);
        },
        
      },
    });
    
    this._data['columns'] = this._calcMissingColumnsSettings(this._data['columns']);
    
    this.header = new ST.grid.header.Header({
              headerGroups: this._data['headerGroups'],
              wrapper: this.layout.header.content.div,
              handler:{
                moveHeader: function(cell, moveX) {
//                  var scrolledRightDone = true;// (self.layout.bottomScroll.div.scrollWidth - self.layout.bottomScroll.div.offsetWidth - self.layout.bottomScroll.div.scrollLeft) == 0;
//                   if(scrolledRightDone)
//                   {
//                     self.layout.bottomScroll.div.scrollLeft = self.layout.bottomScroll.div.scrollLeft-15;
//                     //console.log(self.layout.bottomScroll.div.scrollWidth)
//                   }
                   
                  self.items.setColWidth(cell._data['settings']['alias'], self.header.getColWidth(cell._data['settings']['alias']));
                  self.layout.setBodyWidth(self.items.getSumColWidth());

                },
              },
              columns: ST.cloneSettings(this._data['columns'])
        });
    
    this.layout.setHeaderRowCount(this.header.calcHeaderRowNum()) 
    
    this.items = new ST.grid.items.Items({
              wrapper: this.layout.body.content.div,
              hierachicalFlag: this._data['hierachicalFlag'],
              headerColumn: this._data['columns'][0],
              handler:{
                clickItem: function(item, cell){
                  if(ST.isFunction(self._data['handler']['clickItem']))
                  {
                    self._data['handler']['clickItem'].apply(self,[item, cell])
                  }
                },
                mouseEnterItem: function(row, cell) {
                  if(ST.isFunction(self._data['handler']['mouseEnterItem']))
                  {
                    self._data['handler']['mouseEnterItem'].apply(self,[row, cell])
                  }
                },
                dblClickItem: function(item, cell){
                  if(ST.isFunction(self._data['handler']['dblClickItem']))
                  {
                    self._data['handler']['dblClickItem'].apply(self,[item, cell])
                  }

                },
                mouseLeaveItem: function(row, cell) {
                  if(ST.isFunction(self._data['handler']['mouseLeaveItem']))
                  {
                    self._data['handler']['mouseLeaveItem'].apply(self,[row,cell])
                  }
                },
            
                dropDownItem: function(row, cell){
                  if(ST.isFunction(self._data['handler']['dropDownItem']))
                  {
                    self._data['handler']['dropDownItem'].apply(self,[row,cell])
                  }
                },
                dropUpItem: function(row, cell){
                  if(ST.isFunction(self._data['handler']['dropUpItem']))
                  {
                    self._data['handler']['dropUpItem'].apply(self,[row,cell])
                  }
                },
                //cell: this._data['handler']['cell'],
                changeItem: function(row, cell) { 
                  
                  if(ST.isFunction(self._data['handler']['changeItem']))
                  {
                    self._data['handler']['changeItem'].apply(self,[row,cell])
                  }
                },
//                scrollXMove: function(moveX){
//                  self.header.layout.divSized.scrollLeft = moveX;
//                  alert(123)
//                },
                commitDone: function(rowsLength){
                  self.layout.setRightScrollHeight(rowsLength*ST.grid.items.ItemsLayout.CELL_HEIGHT);
                }
              },
              columns: ST.cloneSettings(this._data['columns'])
            });
         
    
    ST.defSettings(this._data['toolbar'],{
        wrapper: this.layout.toolbar.div
      });
      
    this.toolbar = new ST.Toolbar(this._data['toolbar']);   
    
   // this.toolbar = new ST.ImgButtonGroup(this._data['buttons']);
    this.layout.setBodyWidth(this.items.getSumColWidth());
  };
    
  /**
  * добавить на страницу
  */
  component.prototype.add = function()
  {  
    
    this.layout.append();
    this.toolbar.add();
    this.header.add();
    this.items.add();
    this.width(600);
    this.height(300);
  };
  
    /**
   * Расчёт недостающих настроек столбцов для использования в header и items
   */
  component.prototype._calcMissingColumnsSettings = function(columnsSettings)
  {      
    for(var i = 0; i <  columnsSettings.length; i++)
    {
      if(ST.isNumber(columnsSettings[i]['widthPix']) == false)
      {
        columnsSettings[i]['widthPix'] = 100;
      }
      
      if(ST.isBoolean(columnsSettings[i]['visible']) && columnsSettings[i]['visible'] == false)
      {
        columnsSettings[i]['widthPix'] = 0;
      }
      
      if(ST.isBoolean(columnsSettings[i]['editable']) == false)
      {
        columnsSettings[i]['editable'] = true;
      }
      
      //записываем номер столбца
      columnsSettings[i]['num'] = i; 
    }
    return columnsSettings;
  }
   /**
   * set/get table width (px)
   */
  component.prototype.width = function(width)
  {
    this.layout.width(width);
    this.items.updateWidth();
    this.header.updateWidth();
  };
  

  
   /**
   * set/get table height (px)
   */
  component.prototype.height = function(height)
  {   
    this.layout.height(height);
    this.items.height(height - this.layout.SCROLL - this.layout.TOOLBAR_HEIGHT - this.layout.HEADER_HEIGHT);
    //this.content.height(height - this.layout.TOOLBAR_HEIGHT);
  };
  
  return component;
  
})();

