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

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

  return {
    init : function() {

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

    //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.SEGAL.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.SEGAL.YUIClass, true);
			Event.on(document.body, "mouseout", rollCallback, YAHOO.SEGAL.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, {'shadow': false,"position":"static","hidedelay":1250,"lazyload":false} );

          oMenu.render();                       
          oMenuBar.show(); 
          
        }
      );
  
      Dom.getElementsByClassName('initmenubar', 'div', '', function(el) 
        {

          var mCfgs = {'shadow': false,"autosubmenudisplay":true,"position":"static","hidedelay":1550,"lazyload":false};
        
          // @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.SEGAL.YUIClass.menuRollOver);
            oMenuBar.subscribe('mouseout',YAHOO.SEGAL.YUIClass.menuRollOver);
          }
        
          oMenuBar.render(); 
          oMenuBar.show(); 

        	if (YAHOO.env.ua.gecko && YAHOO.env.ua.gecko < 1.9 && YAHOO.widget.Module.prototype.platform == "mac") {
  				  YAHOO.util.Dom.addClass(oMenuBar.element, "hide-submenu-shadow");
          }
                              
        }
      );        
    },
  
    //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.SEGAL.YUIClass.rollover(elTarget,e);
     }   
    },    

    labelText : function(el) {
      var label_id= el.id.concat('_label');
      var label = Dom.get(label_id);
      Dom.setStyle(label, 'display', 'none');
      return label.innerHTML;
    }

  };

}();

YAHOO.SEGAL.util.hide = function(el) {
  YAHOO.util.Dom.setStyle(el, 'visibility', 'hidden');
	YAHOO.util.Dom.setStyle(el, 'display', 'none');
};

YAHOO.SEGAL.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.SEGAL.YUIClass.init, YAHOO.SEGAL.YUIClass,true );
