/*
   Behaviour v1.1 by Ben Nolan, June 2005. Based largely on the work
   of Simon Willison (see comments by Simon below).

   Description:
    
     Uses css selectors to apply javascript behaviours to enable
     unobtrusive javascript in html documents.
    
   Usage:   
   
     var myRules = {
       'div' : function(element){
         Behaviour.addEventObserver(element, 'click', function(){alert(this.innerHTML);}, false);
       },

       '#somediv':  function(element){
         Behaviour.addEventObserver(element, 'mouseover', function(){this.innerHTML = "BLAH!";}, false);
       }
     };

     Behaviour.register(myRules);
  
  // Call Behaviour.apply() to re-apply the rules (if you
  // update the dom, etc).

   License:
   
    This file is entirely BSD licensed.
    
   More information:
    
    http://ripcord.co.nz/behaviour/
   
*/

// Modified by Jakob Kruse <kruse@kruse-net.dk>
//
// p1:
// * Changed to use event observers instead of replacing events (from Sergio Pereira)
// * Bug fixed in checkFunction (from Ian Sollars)
// * Bug fixed with dash in css class (from james.estes)
// * Support for grouped selectors added (from Ian Sollars)
// * Method applySheet added (from Jakob Kruse)
//
// p2:
// * clearedElements checking moved to addEventObserver method to support adding
//   observers to elements other than those hit by the selector (from Jakob Kruse)
//
// p3:
// * Changed to use Prototype 1.5.1 functionality. Replaces code by Simon Willison.

if (typeof Prototype == undefined || Prototype.Version < '1.5.1') throw "Behaviour needs Prototype 1.5.1";

var Behaviour = {
  list : new Array,
  
  eventObservers : new Array,
  
  clearedElements : [],

  addEventObserver : function(element, name, observer, useCapture){
    if (!this.clearedElements.include(element)) { // should do a hashed lookup here
      this.clearedElements.push(element);
      Behaviour.removeAllObservers(element);
    }
    
    var element = $(element);
    useCapture = useCapture || false;
    Behaviour.eventObservers.push([element, name, observer, useCapture]);
    Event.observe(element, name, observer, useCapture);
  },

  removeEventObserver : function(element, name, observer, useCapture){
    var element = $(element);
    useCapture = useCapture || false;
    Behaviour.eventObservers = Behaviour.eventObservers.without([element, name, observer, useCapture]);
    Event.stopObserving(element, name, observer, useCapture);
  },

  removeAllObservers : function(element){
    var el = $(element);
    var observersForElement = Behaviour.eventObservers.findAll(
                               function(observerInfo){
                                 return (observerInfo[0]==el);
                               });

    observersForElement.each(function(observerInfo){
      Behaviour.removeEventObserver(observerInfo[0], observerInfo[1], observerInfo[2], observerInfo[3]);
    });
  },

  register : function(sheet){
    Behaviour.list.push(sheet);
  },
  
  start : function(){
    Behaviour.addLoadEvent(function(){
      Behaviour.apply();
    });
  },
  
  addLoadEvent : function(func){
    var oldonload = window.onload;
    
    if (typeof window.onload != 'function') {
      window.onload = func;
    } else {
      window.onload = function() {
        oldonload();
        func();
      }
    }
  },

  apply : function(){
    this.clearedElements = [];

    Behaviour.list.each(function(sheet) {
      for (selector in sheet) {
        list = $$(selector);

        if (!list){
          continue;
        }

        list.each(function(element) {
          sheet[selector](element);
        });
      }
    });
  },

  // applySheet can be used to (re)apply a single sheet.
  // Note that any element touched by the sheet will lose any
  // event observers added by other sheets!
  applySheet : function(sheet){
    this.clearedElements = [];

    for (selector in sheet){
      list = $$(selector);

      if (!list){
        continue;
      }

      list.each(function(element) {
        sheet[selector](element);
      });
    }
  }
}

Behaviour.start();

