YAHOO.namespace('BELL');
YAHOO.namespace('BELL.util'); //I just append useful funcs to this one.
YAHOO.namespace('BELL.bb'); //Blackboard to hold info needed by other parts of the app, ie: the highlite colour, which is set in the sifr_BELL.js. 
 
YAHOO.BELL.YUIClass = function () {

  var Dom   = YAHOO.util.Dom,
      Event = YAHOO.util.Event,
      Lang  = YAHOO.lang,
      U     = YAHOO.BELL.util,
      $     = YAHOO.util.Selector.query; //I know, i know...

  //Hold names and ids of various bits.
  var names = {
    "calloutsWrapper"     : "callout_container",
    'singleCalloutClass'  : 'callout'
  };

  return {
    init : function() {
      //console.log('init');
      this.menu();

      this.rolloverInit();
      this.togglerInit();  
    },

    //Empty for now...
    clickHandler : function(e) {
      var passthru = true;

      if(false == passthru) {
			  Event.stopEvent(e);		                
      }
    },

    //Utility functions! like anim wrappers.
    flashEl : function(el,attribs) {

      attribs = attribs || {};

      var attributes = { 
        backgroundColor: { 
          from : attribs.from || '#ff0',
          to: attribs.to || '#fff' 
        } 
      };

      var duration = attribs.duration || 1.0;
      var anim = new YAHOO.util.ColorAnim(el, attributes, duration, YAHOO.util.Easing.easeOutStrong); 
      anim.animate();
    },


    /**
     * Initialize toggler. 
     * @docs Expects element to have id="uid-toggler-target-action"
     * expects to find an element to show/hide with id="toggler_target"
     * possible actions: show/hide/toggle
     *
     * @return void
     **/
    togglerInit : function() {
      //Trying to put listeners in their relevant inits... optimize in production 
      Event.on( document.body, 'click', function(e) 
        {
          var passthru = true;

          //Something toggler should handle?
    			var elTarget = Event.getTarget(e);
    			var command = elTarget.id.split('-')[1];

    			if( 'toggler' !== command ) { return; }
    			//Its a link, we'll want to stop the event. IN theory, can use divs and spans, or whatever actually
          passthru = false;

    			var togConfig = { 
    			  'target' : Dom.get("toggler_"+elTarget.id.split('-')[2]),
    			  'action' : elTarget.id.split('-')[3]
  			  };

          this.toggler(togConfig);

          if(false == passthru) {
    			  Event.stopEvent(e);		                
          }
        }, 
        YAHOO.BELL.YUIClass, 
        true
      );

    },
    
    
		/*
		 * To toggle visibility of element
		 * @todo override - pass in config object to override
		 * Uses "visibility" property to decide whether to show or hide
		 */
		toggler : function (oConfig) {

      // @todo how to check if el is an html element?
      var el = oConfig.target;

      if(!el || !el.tagName ) {
        YAHOO.log('No el for toggling', 'warn');
        return;
      }

			var curVisibility = Dom.getStyle(el,'visibility'); 

      //Check for override.
      if(!Lang.isUndefined(oConfig.action)) {

        //we want to show, and it's already visible
        if('show' == oConfig.action && curVisibility == 'visible'){
          return;
        } else if('hide' == oConfig.action && curVisibility != 'visible') {
          //want to hide, already hidden...
          return;
        }
      }


			if( 'visible' == curVisibility ) {

				//store the elements current height, to restore once hidden, so its available on the next call.
				var anim = new YAHOO.util.Anim(el, {opacity: {to: 0 }, height: {to:0, unit: '%' } } , .5);

				//Once the animation is done.
				anim.onComplete.subscribe(
					function () {
						Dom.setStyle(el, 'visibility', 'hidden');
						Dom.setStyle(el, 'display', 'none');
					}
				);

			} else {
				//Set Opacity to 0, so it doesnt pop in.
				Dom.setStyle(el, 'opacity', '0');
				var anim = new YAHOO.util.Anim(el, {opacity: { to: 1 }, height:{ from: 0 , to: 100, unit: '%'} } , .5);
			}

			//Set this up first, results in smoother anims, though not on table row efects
			Dom.setStyle(el, 'visibility', 'visible');

			var displayStyle = (el.tagName.toLowerCase() == 'tr') ? 'table-row' : 'block' ;

			//Compensate for IE bug - make it block regardless of if its a tr
			if(YAHOO.env.ua.ie > 0) { displayStyle = 'block';}

			Dom.setStyle(el, 'display', displayStyle);

			anim.animate();

		},
		
		//Handle General Rollovers
		//Handle Photocredit Rollover
		rolloverInit : function() {

      var rollCallback = function(e) {

        var el = Event.getTarget(e);
        
        //Return ASAP if no rollover found        
  			if( !Dom.hasClass(el, 'rollover')  && !Dom.hasClass(el, 'rollover_credits')	) { return; }	

  			switch(true) {
  			  case(Dom.hasClass(el, 'rollover_credits')):
  			    var credit = $('img + span.credits', el.parentNode, true);

  			    if(credit && (e.type == 'mouseover')) {
              U.show(credit);
  			    } else if(credit && (e.type == 'mouseout')) {
  			      U.hide(credit);
  			    }
  			    break;
  			  default:
  			  // Default is normal rollovr behaviour
  			    this.rollover(el,e);  			  
  			    break;
  			}

  			
      };

			Event.on(document.body, "mouseover", rollCallback, YAHOO.BELL.YUIClass, true);
			Event.on(document.body, "mouseout", rollCallback, YAHOO.BELL.YUIClass, true);

		},
		
	/*
	 * Handles rollovers on images
	 * Currently targeted to element having class=rollover
	 * original_file.xyz
	 * rolloverfile == original_file_o.xyz
	 *
	 * @TODO: Q: Is it better to just attach a few listeners to the els with 
	 * rollover class as oppossed to capturing and analyzing every mouseover?
	 * @param el to rollover
	 * @param e event - specifically interested in e.type
	 */
	rollover : function(el,e) {

		var filetype = el.src.substring(el.src.lastIndexOf('.'), el.src.length);

		switch(e.type){
			case 'mouseover':
				el.src = el.src.replace(filetype, '_o'+filetype);
			break;
			case 'mouseout':
				el.src = el.src.replace("_o"+filetype, filetype);
			break;
			default:
				throw('Unexpected e.type in rollover handler');
		}
	},    

  menu : function() {
    
    YAHOO.util.Dom.getElementsByClassName('initmenu', 'div', '', function(el) {
        var oMenu = new YAHOO.widget.Menu(el, {"position":"static","hidedelay":1250,"lazyload":true} );
        oMenu.render();                       
      }
    );
  
    Dom.getElementsByClassName('initmenubar', 'div', '', function(el) 
      {
        
        var mCfgs = {"autosubmenudisplay":true,"position":"static","hidedelay":1550,"lazyload":true};
        
        // @docs submenu nav bar requires initsubmenubar
        if(Dom.hasClass(el, 'initsubmenubar') ) {
          mCfgs.position = 'dynamic';
        }
        
        var oMenuBar = new YAHOO.widget.MenuBar(el,mCfgs );

        // @doc - menubar can have rollover effect. 
        if(Dom.hasClass(el, 'rollover')){
          oMenuBar.subscribe('mouseover',YAHOO.BELL.YUIClass.menuRollOver);
          oMenuBar.subscribe('mouseout',YAHOO.BELL.YUIClass.menuRollOver);
        }
        
        oMenuBar.render(); 
                              
      }
    );        
  },
  
  //Handle roll over on menubars.
  menuRollOver : function (type, args) {
    // @docs Ok, the rule specifically is that the first child will always be the img we are interested in.
    var elTarget = args[1].element.getElementsByTagName('img')[0] || args[1].parent.parent.element.getElementsByTagName('img')[0];

    //If its already in the on state, do nothing
    if(Dom.hasClass(elTarget, 'noRoll')){
     return;
    }

   if(elTarget.tagName.toLowerCase() == 'img') {
     var e = { 'type' : type} ;
     YAHOO.BELL.YUIClass.rollover(elTarget,e);
   }   
  }

  };

}();

YAHOO.BELL.util.hide = function(el) {
  // @debug
  //return false;
  YAHOO.util.Dom.setStyle(el, 'visibility', 'hidden');
	YAHOO.util.Dom.setStyle(el, 'display', 'none');
};

YAHOO.BELL.util.show = function(el) {
	//Compensate for IE bug - make it block regardless of if its a tr
	var displayStyle = ((el.tagName.toLowerCase() == 'tr') && (YAHOO.env.ua.ie == 0)) ? 'table-row' : 'block' ;
  YAHOO.util.Dom.setStyle(el, 'visibility', 'visible');
	YAHOO.util.Dom.setStyle(el, 'display', displayStyle);
};


YAHOO.util.Event.onDOMReady(YAHOO.BELL.YUIClass.init, YAHOO.BELL.YUIClass,true );
