/* *******************************************************************
 * XPRIMA.Scrollers - button
 * 
 * Provides button objects (a clickable area)  for use with scrollers.
 *
 * Created : Wednesday November 11 2009, 9:49
 * Author  : Martin Laplante
 *
 * Date       Author         Description
 * ---------- -------------- -----------------------------------------
 * 2009-11-11 Laplante M.    Creation
 * -------------------------------------------------------------------
   *******************************************************************/
if(typeof(XPRIMA) == "undefined") XPRIMA = {};
if(typeof(XPRIMA.Scrollers) == "undefined") XPRIMA.Scrollers = {};
/*
 * ButtonAreaWithHover
 * -------------------
 * 
 * A button area with a hovering border of 1px.
 */

// Prototype
XPRIMA.Scrollers.ButtonAreaWithHover = function(cfg) {
    this.cfg          = null;  // Received at construction time
    this.enabled      = false; // State.  The area stays present, but become invisible.
    this.container    = null;  // Root element, to set visibility on or off.
    this.handler      = null;  // Mouse click handling
    this.handlerScope = null;  // Mouse click handling context
    this.initDone     = false; // Set to true when Init is completed
    this.init(cfg);
  };


// Definition
XPRIMA.Scrollers.ButtonAreaWithHover.prototype = {
  getW : function() {
    return this.cfg.w+2; // 2 pixels added because of 1px border 
  },
  getH : function() {
    return this.cfg.h+2; // 2 pixels added because of 1px border 
  },
  disable     : function() {
    var scope = this;
    if(scope.initDone==false){
      return;
    }
    YUI().use('node','node-base',function(Y) {
       scope.container.setStyle('visibility','hidden');
       scope.enabled=false;
    });
  },
  enable      : function() {
    var scope = this;
    if(scope.initDone==false) {
      return;
    }
    YUI().use('node','node-base',function(Y) {
       scope.container.setStyle('visibility','visible');
       scope.enabled=true;
    });
  },
  doMouseOver : function() {
    var scope = this;
    if(scope.enabled) {
      YUI().use('node','node-base',function(Y) {
        scope.container.setStyle('border','dotted 1px #aaa');
      });
    }
  },
  doMouseOut  : function() {
    var scope = this;
    if(scope.enabled) {
      YUI().use('node','node-base',function(Y) {
        scope.container.setStyle('border','solid 1px transparent');
      });
    }
  },
  doClick    : function() {
    var scope = this;
    scope.doMouseOut();
    if(scope.enabled && scope.handler) {
      scope.handler(scope.handlerScope);
    }
  },
  setHandler : function(handler,ascope) {
    var scope = this;
    if(scope.initDone==false) {
      return;
    }
    scope.handler = handler;  
    scope.handlerScope = ascope;  
    YUI().use('node','node-base',function(Y) {
       var tempElem2 = document.getElementById(scope.cfg.btnName+'_main');//***SD
       var tempElem = Y.Node.get(tempElem2);
       Y.on('click',  scope.doClick, tempElem2, scope);
       //Y.on('click',  scope.doClick,  '#'+scope.cfg.btnName+'_main', scope);//this code wont work with safari 3.2.1, it was replaced by the 2 preceeding lines
    });
  },
  init : function(cfg) {
    var scope = this;
    scope.cfg = cfg;
    YUI().use('node','node-base',function(Y) {
      var container2 = document.getElementById(scope.cfg.btnName+'_container');//***SD
      var container = Y.Node.get(container2);
      //var container = Y.Node.get('#' + scope.cfg.btnName+'_container');//this code wont work with safari 3.2.1, it was replaced by the 2 preceeding lines
      if(container) {
        container.setStyles({border:'solid 1px transparent',width:''+scope.cfg.w+'px'});
        scope.container=container;
        var main2 = document.getElementById(scope.cfg.btnName+'_main');//***SD
        var main = Y.Node.get(main2);
        //var main = Y.Node.get('#' + scope.cfg.btnName+'_main');//this code wont work with safari 3.2.1, it was replaced by the 2 preceeding lines
        main.setStyles({overflow:'hidden',width:''+scope.cfg.w+'px',height:''+scope.cfg.h+'px'});
        Y.on('mouseover', scope.doMouseOver, main2, scope);
        Y.on('mouseout',  scope.doMouseOut,  main2, scope);
        scope.disable();
        scope.initDone=true;
      }
    });
    return this;
  }
};

