/**
  * Application Layout
  */

// reference local blank image
Ext.BLANK_IMAGE_URL = '/images/s.gif';

// create namespace
Ext.namespace('Application');


// create application
Application.app = function() {
  // do NOT access DOM from here; elements don't exist yet

  // private variables
  var tools;
  var items;
  var viewport;
  var widget_list;

  // private functions
  var onDrop = function(event){
    var portlet = event.data.panel;
    Ext.Ajax.request({
      url: '/widget/move_widget',
      params: 'id='+portlet.dbId+'&col='+event.columnIndex+'&row='+event.position
    })
  }

  var onValidateDrop = function(event){
    var portlet = event.data.panel;
    if(event.columnIndex == 1) {
      return (portlet.defCol == 1);
    } else return (portlet.defCol == 0 || portlet.defCol == 2);
  }

  var head = {
    region: 'north',
    contentEl: 'top_block',
    width: '100%'
  }

  var bottom = {
    region: 'south',
    contentEl: 'bottom_block',
    width: '100%'
  }

  var clean = {
    title:'ddd',
    region: 'center'
  }

  var portal = null;

  var fill = function(){
    if(widgets_url == undefined) alert('widgets_url undefined');
    Ext.Ajax.request({
      url: widgets_url,
      timeout: 300000,
      scope: this,
      success: function(ret, json){
        var retObj = Ext.decode(ret.responseText);
        portal = new Ext.ux.Portal(retObj.portal);
        portal.on('drop', onDrop, this);
        portal.on('validatedrop', onValidateDrop, this);

        var portal_top = {
          contentEl: 'page_header',
          border: false
        }
        
        var win;
        var button = Ext.get('show-btn');
        widget_list = portal.getCheckboxes();
        for (i = 0; i < widget_list.length; i++) {
          widget_list[i].handler = function(cb, val){
            cb.widget.setVisible(val);
            cb.widget.notifyVisible(val);
          };
        }
        if(widget_list.length == 0)widget_list=null;
        win = new Ext.Window({
          layout:'fit',
          closeAction:'hide',
          modal: true,
          renderTo: 'settings',
          plain: true,

          items: {
            title: 'Widgets',
            items: widget_list
          }

        });


        button.on('click', function(){
          win.setSize(300, 450);
          win.center();
          win.y = 100;
          win.show(this);
        })

        var mainContainer = new Ext.Panel({
          applyTo: 'mainContainer',
          autoShow: true,
          autoHeight: true,
          monitorResize: true,
          style: 'overflow-x:hidden; overflow:auto; position:relative;"',
          items: [head, portal, bottom]
        })

      },
      failure: function(){
        alert('Failure while saving params')
      }
    });
    
  };

  // public space
  return {
    // public properties, e.g. strings to translate

    // public methods
    init: function() {
      Ext.state.Manager.setProvider(new Ext.state.CookieProvider());

      fill();
    }
  }
}(); // end of app

Ext.onReady(Application.app.init, Application.app);
