MODO = {

  initPopupSwitches : function(){
    $('.popup').each(function(){
      var p = $(this);
      var openers     = ".po_"+this.id;
      var indicators  = ".pi_"+this.id;
      var rollers     = ".ro_"+this.id;
    
      p.bind("open",  function(){         
        MODO.active && $(MODO.active).trigger('close');
        MODO.active = p;
        p.show();               
        $(openers+","+indicators).each(function(){this.setHigh && this.setHigh()});
      });
      
      p.bind("close", function(){ 
        MODO.active = null;
        p.hide();
        $(openers+","+indicators).each(function(){this.setLow && this.setLow()});
      });

      $(rollers).mouseover(function(event, data){if (!data) $(rollers).trigger("mouseover", true);})
                .mouseout (function(event, data){if (!data) $(rollers).trigger("mouseout",  true);});
                
      $(openers).click(function(){ p.trigger("open");});
      p.click         (function(){ p.trigger("close");});
    });
  },

  initRolloverImages : function(){
    $('img.ro').each(function(){
      var src     = this.src;
      var roSrc   = src.replace(/\.(png|jpg|gif)/, '-a.$1');
      var loader  = new Image(); 
      loader.src  = roSrc;

      $(this).hover(
        function(){ this.src = roSrc;},
        function(){ if (!this.high) this.src = src;}
      );

      this.setHigh = function(){ 
        this.high = true;  
        $(this).trigger('mouseover', true); 
      };
      this.setLow  = function(){ this.high = false; $(this).trigger('mouseout', true);  };
    });
  }
}

$(function(){
  MODO.initRolloverImages();
  MODO.initPopupSwitches();
});