/*
 * OutWit Images
 * Copyright (c) OutWit Technologies 2007.
 * All rights reserved.
 *
 */


/**
 * @name scrollbarCtrl
 * @static
 *
 * @todo this contains everything but scrollabar controls... move ti UICOntrols.
 *
 * Static scrollbar method to control
 * the one in the ui
 */
var scrollbarCtrl = {};



/**
 * @name scrollbarCtrl.init
 * @static
 * Start the scrollar and attach it to the gallery.
 * In fact we did more here for convenience devlopment
 * but this have to be clean of all these "link" at the runtime
 * XXX In fact we need a "load" method somewhere that is ran when everything is load
 */
scrollbarCtrl.load = function() {
  var container = document.getElementById("owImagesContainer");
  var scrollbar = document.getElementById('thumbScrollbar');

  if (container && container.thumbWindow && container.screenManager && container.gallery && container.gallery._element) {
    container.screenManager.scrollbar = scrollbar;

    container.gallery.popupMenuCtrl = popupMenuCtrl;
    container.screenManager.refresh();
    container.gallery.catchFunc = document.getElementsByTagName('mediacatch')[0];
    container.thumbWindow.coreKit = coreKit;
    var myXulFocus = document.commandDispatcher;
    container.thumbWindow.stealFocus = function(){
/*dump("MYXUL : " + (myXulFocus.focusedElement ? myXulFocus.focusedElement.nodeName : "aucun")
+ " thumbfoc ? " + (container.thumbWindow.focused)
+ " globfoc ? " + (window.focused)
+ " eqwinthumb ? " + (document.commandDispatcher.focusedWindow == container.thumbWindow)
+ " eqwinglob ? " + (document.commandDispatcher.focusedWindow == window) + "\n");*/
/*      var isOWIWindowFocus = (document.commandDispatcher.focusedWindow == container.thumbWindow
                         || document.commandDispatcher.focusedWindow == window);*/
      if (!myXulFocus.focusedElement || myXulFocus.focusedElement.nodeName != "html:input") {
        container.thumbWindow.focus();
      }
    }
    //container.gallery.catchFunc.autoload();
    infoBoxCtrl.load();

    coreKit.coreMessager.subscribe(container.gallery.catchFunc, "CATCH", 1);

    scrollbar.onchange = (function() {
      var scrollbar = document.getElementById('thumbScrollbar');
      return function() {
        var curpos = parseFloat(scrollbar.curpos);
        var maxpos = Math.floor(parseFloat(scrollbar.maxpos));
        if (maxpos == curpos) {
          container.screenManager.changePositionTo(container.screenManager.maxPosition);
        } else {
          container.screenManager.changePositionTo(curpos*100);
        }
      };
    })();

  } else {
    coreKit.timerService.setTimer(50, null, scrollbarCtrl.load);
  }
};

scrollbarCtrl.unload = function() {
  var container = document.getElementById("owImagesContainer");
  infoBoxCtrl.unload();
  coreKit.coreMessager.unsubscribe(container.gallery.catchFunc, "CATCH", 1);
};
