outwitImages.UIControls = {

  load: function() {
  //bind the select all attribute...
  var urlbar = document.getElementById("urlbar");
  var prefService = Components.classes["@mozilla.org/preferences-service;1"]
                              .getService(Components.interfaces.nsIPrefBranch);

  urlbar.setAttribute("clickSelectsAll", prefService.getBoolPref("browser.urlbar.clickSelectsAll",false));
 

    optionsCtrl.init();
    scrollbarCtrl.load();

    document.getElementById('mediacatch').init();
 
    //Check if the user have done an update and/or it's an firstrun of the current version
    var lastVersion = ow.app.kernel.prefs.getString("oWImages.firstrun","");
    
    var container = document.getElementById("owImagesContainer");
    container.init();

    var currentVersion = ow.app.module.version;
    if (!lastVersion) {
      ow.app.kernel.prefs.setString("oWImages.firstrun",currentVersion);
      container.mode = container.firstrunMode;
      window.addEventListener("mouseup", function(event){
        if(container.mode == container.firstrunMode){
          container.mode = container.thumbnailsMode;
        }
      },false);
    }
    else{
      container.mode = container.thumbnailsMode;
    }

    
    window.addEventListener("keypress", this.onWindowKeyPress, false);
  },

  unload: function() {
    scrollbarCtrl.unload();
    window.removeEventListener("keypress", this.onWindowKeyPress, false);
  },

  onWindowKeyPress: function(event) {
    var isEscapeKey = (event.keyCode==27);
    if (isEscapeKey || (event.metaKey && event.charCode == 119)) {
      var container=document.getElementById("owImagesContainer");
      if (container.mode == container.slideshowMode) {
        container.thumbnails.imageViewer.zoomStatus = container.thumbnails.imageViewer.NOZOOM;
        container.slideshow.stop();
      }
      else if(container.mode == container.optionsMode){
        container.hideOptions();
      }
      else if (container.thumbnails.imageViewer.zoomStatus != container.thumbnails.imageViewer.NOZOOM) {
        container.thumbnails.imageViewer.zoomStatus = container.thumbnails.imageViewer.NOZOOM;
        container.thumbnails.imageViewer.hide();
      }
      else if (!isEscapeKey) {
        ow.app.quit();
      }
    }
    else{
      if( ! document.commandDispatcher.focusedElement ){
        var stopChar = [13, 32, 37, 38, 39, 40];
        if(stopChar.indexOf(event.charCode) != -1  || stopChar.indexOf(event.keyCode) != -1){
          event.preventDefault();
          event.stopPropagation();
        }
        else{
        }
      }
    }

  },
  MODE_URL:'url',
  MODE_CATCH:'catch',
  MODE_DISCOVER:'discovering',

  _container:null,
  get container(){
    if(this._container == null)
      this._container = document.getElementById("owImagesContainer");
    return this._container;
  },
  
  _mode:'url',
  get mode(){
    return this._mode;
  },
  set mode( aValue ){
    //XXX Find a cleaner way instead of this global
    this._mode = aValue;

    switch(this.mode){
      case this.MODE_CATCH:
        this.backgroundItems = [];
        if(!this.saveItems.length)
          this.saveItems = [].concat(this.container.thumbs.items);//clone it
        if (this.clearOnNew) this.container.clear();
        document.getElementById('owImagesTopPanelSourceSelector').value = 'Catch';
        document.getElementById('owImagesTopPanelToolbarUrl').style.visibility = 'hidden';
        document.getElementById('search-container').style.visibility = 'hidden';
        document.getElementById('menu-url-source').setAttribute('checked', 'false');
        document.getElementById('menu-url-source').removeAttribute('disabled');
        document.getElementById('popup-url-source').setAttribute('checked', 'false');
        document.getElementById('popup-url-source').removeAttribute('disabled');
        document.getElementById('menu-catch-source').setAttribute('checked', 'true');
        document.getElementById('menu-catch-source').setAttribute('disabled', 'true');
        document.getElementById('owImagesSourceSelectorMenuitemCatch').setAttribute('checked', 'true');
        document.getElementById('owImagesSourceSelectorMenuitemCatch').setAttribute('disabled', 'true');
        this.container.slideshow.extractorManager = null;
      break;

      case this.MODE_DISCOVER:
      break;

      case this.MODE_URL:
        if (this.clearOnNew) this.container.clear();
        if(this.saveItems.length){
          this.container.thumbs.items = [].concat(this.saveItems);
          this.container.thumbs.refresh();
          this.container.thumbs.recompute();
          for(let i =0;i<this.saveItems.length;i++){
            let aBean = this.saveItems[i];
            this.container.slideshow.appendImage(outwitKit.URLHistoryService.getBeanFromString(aBean.img.mediaUrl, true));
          }
          this.saveItems = [];
        }
        // enqueue items that came when we were in catch mode
        if (this.backgroundItems.length) {
          for (let i = 0; i < this.backgroundItems.length; ++i) {
            this.container.appendItem(this.backgroundItems[i]);
          }
          this.backgroundItems = [];
        }
        this.container.slideshow.extractorManager = widgets.page.extractorManager;
        document.getElementById('owImagesTopPanelSourceSelector').value = 'URL';
        document.getElementById('owImagesTopPanelToolbarUrl').style.visibility = 'visible';
        document.getElementById('search-container').style.visibility = 'visible';
        document.getElementById('menu-catch-source').setAttribute('checked', 'false');
        document.getElementById('owImagesSourceSelectorMenuitemCatch').setAttribute('checked', 'false');
        document.getElementById('menu-url-source').setAttribute('checked', 'true');
        document.getElementById('popup-url-source').setAttribute('checked', 'true');

        document.getElementById('menu-catch-source').removeAttribute('disabled');
        document.getElementById('owImagesSourceSelectorMenuitemCatch').setAttribute('checked', 'false');
        document.getElementById('owImagesSourceSelectorMenuitemCatch').removeAttribute('disabled');
        document.getElementById('menu-url-source').setAttribute('checked', 'true');
        document.getElementById('menu-url-source').setAttribute('disabled', 'true');
        document.getElementById('popup-url-source').setAttribute('checked', 'true');
        document.getElementById('popup-url-source').setAttribute('disabled', 'true');
        //document.getElementById('urlbar').value = '';
      break;
    }
  },

  checkCatch: function(){
    var catchItems = document.getElementById('mediacatch').length == 0;
    var currentMode = this.mode == this.MODE_CATCH;
    var rv = (catchItems || currentMode);
    document.getElementById('owImagesSourceSelectorMenuitemCatch').setAttribute("disabled", rv);
    document.getElementById('owImagesSourceSelectorMenuitemCatch').disabled = rv;
    document.getElementById('menu-catch-source').setAttribute("disabled", rv);
  },

  checkThumbsLimit: function() {
    return this.container.thumbs._itemsDisplayed.length < ow.app.kernel.prefs.getInt('oWImages.thumbnails.limit', 1500);
  },

  saveItems: [],
  backgroundItems: [],
//  urlbarEditing: false,
  clearOnNew: true,

  newPage: function(url) {
//    if (!this.urlbarEditing)
//      document.getElementById('urlbar').value = url;
    },

  showFromCatch: function(){
    var ccatch = document.getElementById("mediacatch");
    if (ccatch.length != 0) {
      this.mode = this.MODE_CATCH;
      this.container.appendItems(ccatch.exportBeans());
    }
  },
 
  showFromUrl: function() {
    if(this.mode != this.MODE_URL){
      this.mode = 'url';
    }
  }
};

