dojo.require("dojo.lang.array");
dojo.require("bl.console");

dojo.declare("ReadController", bl.tree.BasicTreeController,
  function(model, view, cfg) {
    dojo.event.connect(this.model, "onSelectionChanged", this, "handleSelectionChanged");
    this.playlist = null;
  },
  {
    handleSelectionChanged: function() {
      //console.log("currently selected: %o", this.model.selected);
      if(this.model.selected.length != 1) {
        return;
      }
      var n = this.model.selected[0];
      if(n.isRoot()) {
        var shouldOpenRoot = window.confirm(bl.translate.get("confirmreadall"));
        if(!shouldOpenRoot) return;
      }
      //console.log("reading %s", n.ss_id());
      this.read(n);
    },

    handleViewClick: function(id, evt) {
      if(this.ignoreNextClick) {
        $bl.log("ignoring click in ReadController");
        this.ignoreNextClick = false;
        return;
      }

      $bl.log("calling super handleViewClick from ReadController");

      bl.tree.BasicTreeController.prototype.handleViewClick.apply(this, arguments);

      $bl.log("trying to open what was clicked on");

      if(this.model.selected.length != 1) return;

      var n = this.model.selected[0];

      if(n.id() == id) {
        $bl.log("opening %o", n);
        this.read(n);
      }
    },

    read: function(node) {
      node.readAll();
      this.showArticles(node);
    },
    
    showArticles: function(node) {
      if(!parent.basefrm) return;
      var href = "/myblogs_display?";
      if(node.isSub()) { 
        href += "sub=" + node.sub_id() + "&site=" + node.site_id();
      } else if(node.isFolder()) {
        href += "folder=" + node.folder_id();
      } else if(node.isRoot()) {
        href += "all=1";
      }

      if(this.playlist) href += "&playlistid=" + this.playlist;

      parent.basefrm.location.href = href;
    }
  });

dojo.declare("PlaylistController", ReadController, function() {
      this.moveToCell = true;
    },
    {
      read: function(node) {
        if(this.moveToCell) { 
          if(parent.basefrm && parent.basefrm.highlightCell) {
            if(node.isSub()) { 
              parent.basefrm.highlightCell(node.sub_id());
            }
          }
        } else {
          ReadController.prototype.read.apply(this, arguments);
        }
      }
    });

dojo.declare("ClipController", bl.tree.BasicTreeController,
  function(model, view, cfg) {
    dojo.event.connect(this.model, "onSelectionChanged", this, "handleSelectionChanged");
  },
  {
    handleSelectionChanged: function() {
      if(this.model.selected.length != 1) return;

      var n = this.model.selected[0];
      if(!parent.basefrm) return;
      this.read(n);
    },

    read: function(node) {
      if(node.isRoot()) {
        parent.basefrm.location.href = "/myblogs_display?allbooks=1";
      } else if(node.isFolder()) {
        parent.basefrm.location.href = "/myblogs_display?bookid=" + node.folder_id();
      } else if(node.isSub()) {
        parent.basefrm.location.href = "/myblogs_display?bookid=" + node.sub_id();
      }

    }
  });

dojo.declare("BlogController", bl.tree.BasicTreeController,
    function(model, view, cfg) {
      dojo.event.connect(this.model, "onSelectionChanged", this, "handleSelectionChanged");
      dojo.event.connect(document, "onclick", this, "genericClick");
    },
    {
      genericClick: function(evt) {
        if(evt.ctrlKey || evt.shiftKey || evt.metaKey || bl.alg.isInputElement(evt.target)) return;
        this.model.clearSelection();
      },
      handleSelectionChanged: function() {
        if(this.model.selected.length != 1) return;
        var n = this.model.selected[0];
        if(!n.isSub()) return;
        window.open('/saveitem?mode=3&itemid='+n.sub_id(),'ModifySubscription','toolbar=no,status=no,scrollbars=yes,location=no,menubar=no,directories=no,width=520,height=600');
      },
 
      isAllowablePick: function(node) {
        return node.isSub();
      },

      read: function(node) { return; }
      
    });


