var infoBoxCtrl = {
  load: function() {
    this.catchCurrentRef    = document.getElementById("owImagesBoxInfoCatchCurrent");
    this.catchSeparatorRef  = document.getElementById("owImagesBoxInfoCatchSeparator");
    this.catchTotalRef      = document.getElementById("owImagesBoxInfoCatchTotal");
    this.resultCurrentRef   = document.getElementById("owImagesBoxInfoResultCurrent");
    this.resultSeparatorRef = document.getElementById("owImagesBoxInfoResultSeparator");
    this.resultTotalRef     = document.getElementById("owImagesBoxInfoResultTotal");
    this.resultSkippedRef   = document.getElementById("owImagesBoxInfoResultSkipped");
    this.saveInProgressRef  = document.getElementById("owImagesBoxInfoSaveInProgress");
    this.saveDoneRef        = document.getElementById("owImagesBoxInfoSaveDone");

    this.catchTotalRef.value = document.getElementById("mediacatch").childNodes.length;

    this.hideCatchCurrent();
    this.hideResultCurrent();

    this.container = document.getElementById("owImagesContainer");

    coreKit.coreMessager.subscribe(this, 'CATCH_overChanged', 1);
    coreKit.coreMessager.subscribe(this, 'CATCH_remove', 1);
    coreKit.coreMessager.subscribe(this, 'CATCH_add', 1);
    coreKit.coreMessager.subscribe(this, 'THUMB_clear', 1);
    coreKit.coreMessager.subscribe(this, 'THUMB_add', 1);
    coreKit.coreMessager.subscribe(this, 'THUMB_skip', 1);
    coreKit.coreMessager.subscribe(this, 'THUMB_overChanged', 1);

    outwitImages.downloadManager.addProgressListener(this.downloadProgressListener);
  },
 
  unload:function() {
    outwitImages.downloadManager.removeProgressListener(this.downloadProgressListener);
    coreKit.coreMessager.unsubscribe(this, 'CATCH_overChanged', 1);
    coreKit.coreMessager.unsubscribe(this, 'CATCH_remove', 1);
    coreKit.coreMessager.unsubscribe(this, 'CATCH_add', 1);
    coreKit.coreMessager.unsubscribe(this, 'THUMB_clear', 1);
    coreKit.coreMessager.unsubscribe(this, 'THUMB_add', 1);
    coreKit.coreMessager.unsubscribe(this, 'THUMB_skip', 1);
    coreKit.coreMessager.unsubscribe(this, 'THUMB_overChanged', 1);
  },

  downloadProgressListener: {
    onEnqueue: function() {
      infoBoxCtrl.incrementSaveInProgress();
    },
    onStart: function() {
      
    },
    onSuccess: function() {
      infoBoxCtrl.incrementSaveDone();
      infoBoxCtrl.decrementSaveInProgress();
    },
    onFail: function() {
      infoBoxCtrl.decrementSaveInProgress();
    }
  },

  observe:function(topic, type, code, textMessage, newData) {
    switch (topic) {
      case 'CATCH_overChanged':
        if (newData) {
          this.container.thumbWindow.InfoManager.setInfo(
                { filename:newData.filename,
                  size:newData.size,
                  mediaUrl:newData.mediaUrl,
                  sourceUrl:newData.sourceUrl,
                  alt:newData.alt });
          this.setCatchCurrent(newData.uid);
        }
        else {
          this.container.thumbWindow.InfoManager.clean();
          this.hideCatchCurrent();
        }
        break;
      case 'CATCH_remove':
        this.container.thumbWindow.InfoManager.clean();
        if (textMessage == "all") {
          this.setCatchTotal(0);
        }
        else {
          this.decrementCatchTotal();
        }
        this.hideCatchCurrent();
        break;
      case 'CATCH_add':
        this.incrementCatchTotal();
        break;
      case 'THUMB_clear':
        this.setResultTotal(0);
        this.setResultSkipped(0);
        this.hideResultCurrent();
        break;
      case 'THUMB_add':
        this.incrementResultTotal();
        break;
      case 'THUMB_skip':
        this.incrementResultSkipped();
        break;
      case 'THUMB_overChanged':
        if (newData) {
          this.setResultCurrent(newData._realIndex + 1);
        }
        else {
          this.hideResultCurrent();
        }
        break;
    }
  },

  setCatchCurrent: function(catchCurrentIndex) {
    if (this.catchCurrentHidden) this.showCatchCurrent();
    this.catchCurrentRef.value = catchCurrentIndex;
  },

  showCatchCurrent: function() {
    this.catchCurrentRef.style.display = "-moz-box";
    this.catchSeparatorRef.style.display = "-moz-box";
    this.catchCurrentHidden = false;
  },

  hideCatchCurrent: function() {
    this.catchCurrentRef.style.display = "none";
    this.catchSeparatorRef.style.display = "none";
    this.catchCurrentHidden = true;
  },

  incrementCatchTotal: function() {
    this.setCatchTotal(parseInt(this.catchTotalRef.value) + 1);
  },

  decrementCatchTotal: function() {
    this.setCatchTotal(parseInt(this.catchTotalRef.value) - 1);
  },

  setCatchTotal: function(catchTotal) {
    this.catchTotalRef.value = catchTotal;
  },

  setResultCurrent: function(thumbCurrentIndex) {
    if (this.resultCurrentHidden) this.showResultCurrent();
    this.resultCurrentRef.value = thumbCurrentIndex;
  },

  showResultCurrent: function() {
    this.resultCurrentRef.style.display = "-moz-box";
    this.resultSeparatorRef.style.display = "-moz-box";
    this.resultCurrentHidden = false;
  },

  hideResultCurrent: function() {
    this.resultCurrentRef.style.display = "none";
    this.resultSeparatorRef.style.display = "none";
    this.resultCurrentHidden = true;
  },

  incrementResultTotal: function() {
    this.setResultTotal(parseInt(this.resultTotalRef.value) + 1);
  },

  setResultTotal: function(resultTotal) {
    this.resultTotalRef.value = resultTotal;
  },

  incrementResultSkipped: function() {
    this.setResultSkipped(parseInt(this.resultSkippedRef.value) + 1);
  },

  setResultSkipped: function(resultSkipped) {
    this.resultSkippedRef.value = resultSkipped;
  },

  setSaveInProgress: function(nbSaveInProgress) {
    this.saveInProgressRef.value = nbSaveInProgress;
  },

  incrementSaveInProgress: function() {
    this.setSaveInProgress(parseInt(this.saveInProgressRef.value) + 1);
  },

  decrementSaveInProgress: function() {
    this.setSaveInProgress(parseInt(this.saveInProgressRef.value) - 1);
  },

  setSaveDone: function(nbSaveDone) {
    this.saveDoneRef.value = nbSaveDone;
  },

  incrementSaveDone: function() {
    this.setSaveDone(parseInt(this.saveDoneRef.value) + 1);
  }
};
