/**
 * Label容器
 * 继承自tom.dnd.Droppable
 */
tom.space.LabelContainer = tom.Class.create();
tom.extend(tom.space.LabelContainer.prototype, tom.dnd.Droppable.prototype, {
  initialize: function(id, params) {
    var fields = {
      mode: "horizontal"
    };
    tom.extend(fields, params || {});
    tom.dnd.Droppable.prototype.initialize.apply(this, [id, fields])
  },
  getLabels: function() {
    return this.draggables;
  },
  addLabel: function(label) {
    this.add(label);
    label.labelContainer = this;
  },
  removeLabel: function(label) {
    this.remove(label);
    label.labelContainer = null;
  }
});

/**
 * class Label
 */
tom.space.Label = tom.Class.create();
tom.extend(tom.space.Label.prototype, tom.dnd.Draggable.prototype, {
  initialize: function(id, params) {
    var fields = {
      id: id,
      typeID: 1,
      title: 'title',
      description: 'description',
      icon: 'http://tom.com/logo.jpg',
      index: 1,
      isPrivate: 0,
      layoutID: 1,
      themeID: 1,
      ext: '',
      
      revert: tom.fnTrue,
      droppableFilter: function(dp) { return (dp.mode == "horizontal"); },
      
      labelContainer: null,
      user: null,
      menu: null,
      columns: new tom.util.Map(),
      onComplete: function() {
        //计算label drop位置
        var _this = this;
        var index = 1;
        this.labelContainer.getLabels().sort(
            function(a, b) {
              return a.element.offsetLeft - b.element.offsetLeft;
            }
          ).each(function(o) {
              if (o == _this) {
                return false;
              }
              index++;
            }
          );
        
        if (this.index == index) {
          return;
        }
        
        var url = "/xhr/editLabel.php";
        var params = new tom.XHConn.Parameter();
        params.$("action", "sortLabel")
              .$("labelID", _this.id)
              .$("end", index)
              .$("rdm", new Date().getTime());
      
        new tom.XHConn().connectAsync(
            url, 
            "get", 
            params.stringValue(), 
            function(xhr) {
              var result = tom.XHConn.parseResult(xhr, {json: true});
              if (result.json.code != 0) {
                tom.alert(result.json.data);
              } else {
                _this.index = index;
              }
            }
          );
      }
    };
    tom.extend(fields, params);
    tom.dnd.Draggable.prototype.initialize.apply(this, ["lbl_" + id, fields]);
    //绑定dom对象
    this.elTitle = $("lbl_title_" + id);
    //
    this.element.style.cursor = "move";
    //绑定事件
    var _this = this;
    this.elTitle.onmousedown = tom.fnEventStop;
    /*
    this.elTitle.onclick = function(e) {
      document.location = "/?user="+_this.user.id+"&label="+_this.id;
      tom.Event.stop(e||window.event);
      return false;
    }
    */
    if (this.id == tom.space.currentLabel.id && !tom.space.judge.isTomIndex()) {
        this.elArrow = $("lbl_arrow_" + id);
        this.elArrow.onmousedown = tom.fnEventStop;
        this.elArrow.onclick = function(e) {
          _this.menu.show();
          tom.Event.stop(e||window.event);
          return false;
        }
        //初始化menu
        this.menu = new tom.space.LabelMenu("", {label: _this});
    }
    //初始化labelDrop
    this.labelDrop = new tom.space.LabelDrop(this.id, {label: _this});
  },
  addColumnObject: function(col) {
    this.columns.put(col.id, col);
    col.label = this;
  },
  removeColumnObject: function(id) {
    this.columns.remove(id);
     col.label = null;
  },
  addColumnElement: function(el) {
    this.element.appendChild(el);
  },
  removeColumnElement: function(el) {
    this.element.removeChild(el);
  },
  getColumns: function() {
    return this.columns;
  },
  setTitle: function(title) {
    var _this = this;    
      var url = "/xhr/editLabel.php";
      var params = new tom.XHConn.Parameter();
      params.$("action", "editTitle")
//            .$("userID", _this.user.id)
            .$("labelID", _this.id)
            .$("title", title)
            .$("rdm", new Date().getTime());
            
      new tom.XHConn().connectAsync(
          url, 
          "get", 
          params.stringValue(), 
          function(xhr) {
            var result = tom.XHConn.parseResult(xhr, {json: true});
            if (result.json.code == 0) {
              tom.alert("修改成功", "OK", function() {
                    _this.title = title;
                    _this.elTitle.innerHTML = title;
                }, 1500);
            } else {
              tom.alert(result.json.data);
            }
          }
        );
  },
  removeThis: function() {
    var _this = this;   
    tom.confirm(
        "确定删除此标签吗？",
        function() {
          var url = "/xhr/editLabel.php";
          var params = new tom.XHConn.Parameter();
          params.$("action", "delLabel")
                .$("labelID", _this.id)
                .$("rdm", new Date().getTime());
                
          new tom.XHConn().connectAsync(
              url, 
              "get", 
              params.stringValue(), 
              function(xhr) {
                var result = tom.XHConn.parseResult(xhr, {json: true});
                if (result.json.code == 0) {
                  tom.alert("删除成功", "OK", function() {
                      document.location = "?user="+_this.user.id;
                    }, 1500);
                } else if (result.json.code == 2) {
                  tom.alert(result.json.data, "INFO");
                } else {
                  tom.alert(result.json.data, "ERROR");
                }
              }
            );
        },
        tom.fnEmpty,
        ""
      );
  },
  changeTheme: function(themeID) {
      var _this = this;
      var url = "/xhr/editLabel.php";
      var params = new tom.XHConn.Parameter();
      params.$("action", "theme")
//            .$("userID", _this.user.id)
            .$("labelID", _this.id)
            .$("themeID", themeID)
            .$("rdm", new Date().getTime());
            
      new tom.XHConn().connectAsync(
          url, 
          "get", 
          params.stringValue(), 
          function(xhr) {
            var result = tom.XHConn.parseResult(xhr, {json: true});
            if (result.json.code == 0) {
              tom.alert("主题修改成功", "OK", function() {
                  _this.themeID = themeID;
//                  $("linkTheme").href = "/style/" + _this.themeID + ".css";
                  try {
                    refreshThemeButtons(themeID);
                  } catch (e) {}
                }, 1500);
            } else {
              tom.alert(result.json.data);
            }
          }
        );
  },
  changeLayout: function(layoutID) {
      var _this = this;
      var url = "/xhr/editLabel.php";
      var params = new tom.XHConn.Parameter();
      params.$("action", "layout")
//            .$("userID", _this.user.id)
            .$("labelID", _this.id)
            .$("layoutID", layoutID)
            .$("rdm", new Date().getTime());
      
      new tom.XHConn().connectAsync(
          url, 
          "get", 
          params.stringValue(), 
          function(xhr) {
            var result = tom.XHConn.parseResult(xhr, {json: true});
            if (result.json.code == 0) {
              tom.alert("版式修改成功", "OK", function() {
                  _this.layoutID = layoutID;
                  $("linkLayout").href = "/style/layout" + _this.layoutID + ".css?rdm=" + new Date().getTime();
                }, 1500);
            } else {
              tom.alert(result.json.data);
            }
          }
        );
  }
});
tom.extend(tom.space.Label, {
  themeMap: tom.util.Arrays.asMap(
      [
        [1, 'gray'],
        [2, 'green'],
        [3, 'yellow'],
        [4, 'blue'],
        [5, 'pink'],
        [6, 'purple']
      ],
      0)
});
/**
 * class LabelMenu
 */
tom.space.LabelMenu = tom.Class.create();
tom.extend(tom.space.LabelMenu.prototype, tom.widget.Menu.prototype, {
  initialize: function(id, params) {
    var fields = {
      label: null,
      fnCreateElement: function() {
        var el = document.createElement("DIV");
        el.className = "setting";
        el.style.position = "absolute";
        var el2 = document.createElement("DIV");
        el2.className = "setcont";
        el.appendChild(el2);
        var elUl = document.createElement("UL");
        el2.appendChild(elUl);
        this.container = elUl;
        document.body.appendChild(el);
        return el;
      }
    }
    tom.extend(fields, params);
    tom.widget.Menu.prototype.initialize.apply(this, [id, fields]);
    
    var _this = this;
    this.itemChangeTitle = this.addItem(new tom.space.LabelMenuItem("", {text: "修改标题", onClick: function(e) { tom.space.ChangeLabelTitleWindow.show(_this.label); }}));
    this.itemAddModule = this.addItem(new tom.space.LabelMenuItem("", {text: "添加模块", onClick: function(e) { document.location = "/add_module.php?labelID="+_this.label.id }}));
    this.itemSetupLayout = this.addItem(new tom.space.LabelMenuItem("", {text: "设置版式", onClick: function(e) { tom.space.LayoutSetupWindow.show(_this.label); }}));
    this.itemSetupTheme= this.addItem(new tom.space.LabelMenuItem("", {text: "设置主题", onClick: function(e) { tom.space.ThemeSetupWindow.show(_this.label) }}));
    this.itemRemoveLabel = this.addItem(new tom.space.LabelMenuItem("", {text: "删除标签", onClick: function() { _this.label.removeThis() }}));
  },
  show: function() {
    tom.widget.Menu.prototype.show.apply(this, []);
    var p = tom.Position.cumulativeOffset(this.label.element);
    this.setLeft(p.left);
    this.setTop(p.top + this.label.element.offsetHeight + 2);
  }
});

tom.space.LabelMenuItem = tom.Class.create();
tom.extend(tom.space.LabelMenuItem.prototype, tom.widget.MenuItem.prototype, {
  initialize: function(id, params) {
    var fields = {
      onMouseOver: tom.fnEmpty,
      onMouseOut: tom.fnEmpty,
      fnCreateElement: function() {
        var el = document.createElement("LI");
        return el;
      }
    }
    tom.extend(fields, params);
    tom.widget.MenuItem.prototype.initialize.apply(this, [id, fields]);
    this.element.innerHTML = this.text;
  }
});


tom.space.LabelDrop = tom.Class.create();
tom.extend(tom.space.LabelDrop.prototype, tom.dnd.Droppable.prototype, {
  initialize: function(id, params) {
    var fields = {
      mode: "normal",
      label: null,
      onDrag: function() {
        var _this = this;
        if (tom.space.LabelDrop.lastFocus != this) {
          tom.space.LabelDrop.lastFocus = this;
          var p = tom.Position.cumulativeOffset(this.element);
          tom.extend(tom.dnd.Droppable.getDropTmpDiv().style, {
              position: "absolute",
              display: "block",
              left: p.left + "px",
              top: p.top + "px",
              width: _this.element.offsetWidth + "px",
              height: _this.element.offsetHeight + "px",
              border: "1px solid red"              
            });
        }
      },
      onComplete: function(e, dg) {
        if (this.label.id == dg.column.label.id) {
          return;
        }
        var _this = this;
        var url = "/xhr/change_module_set.php";
          var params = new tom.XHConn.Parameter();
          params.$("action", "change_module_label_column")
//                .$("userID", this.label.user.id)
                .$("mid", dg.id)
                .$("label", this.label.id)
                .$("cid", 1)
                .$("index", 1)
                .$("rdm", new Date().getTime());
         new tom.XHConn().connectAsync(
              url, 
              "get", 
              params.stringValue(), 
              function(xhr) {
                var result = tom.XHConn.parseResult(xhr, {json: true});
                if (result.json.code != 0) {
                  tom.alert(result.json.data);
                } else {
                  dg.revert = tom.fnFalse;
                  dg.column.removeModule(dg);
                  dg.element.parentNode.removeChild(dg.element);
                  tom.space.modules.remove(dg.id);
                }
              }
            );
      }
    };
    tom.extend(fields, params || {});
    tom.dnd.Droppable.prototype.initialize.apply(this, ["lbl_" + id, fields]);
  }
});
tom.space.LabelDrop.lastFocus = null;

