Fx.Morph = Fx.Styles.extend({
  start: function(className){
    var to = {};
    $each(document.styleSheets, function(style){
      var rules = style.rules || style.cssRules;
      $each(rules, function(rule){
        if (!rule.selectorText.test('\.' + className + '$')) return;
        Fx.CSS.Styles.each(function(style){
          if (!rule.style || !rule.style[style]) return;
          var ruleStyle = rule.style[style];
          to[style] = (style.test(/color/i) && ruleStyle.test(/^rgb/)) ? ruleStyle.rgbToHex() : ruleStyle;
        });
      });
    });
    return this.parent(to);
  }
});
Fx.CSS.Styles = ["backgroundColor", "backgroundPosition", "color", "width", "height", "left", "top", "bottom", "right", "fontSize", "letterSpacing", "lineHeight", "textIndent", "opacity"];
Fx.CSS.Styles.extend(Element.Styles.padding);
Fx.CSS.Styles.extend(Element.Styles.margin);
Element.Styles.border.each(function(border){
  ['Width', 'Color'].each(function(property){
    Fx.CSS.Styles.push(border + property);
  });
});


var Site = {
 start: function() {
   Site.effects();
 },
 effects: function() {
   var togglers = $$('.toggler');
   var togglersContents = $$('.togglerContents');
   var myMorph = [];
   var scroll  = new Fx.Scroll(window, {duration: 500, transition: Fx.Transitions.backOut, fps: 60});
   var scroll2 = new Fx.Scroll(window, {duration: 250, transition: Fx.Transitions.backOut, fps: 30});
   togglers.each(function(el, i){
     el.origStyles = el.getStyles('background-color','border','margin','color');
     myMorph[i] = new Fx.Morph(el, {duration: 500,transition: Fx.Transitions.cubicOut,fps: 30,wait: false});
     el.addEvent('mouseover', function(e){
        new Event(e).stop();
        if (el.hasClass('selected') ) {el.setStyles("");}
        else {myMorph[i].start('mouse-over');}
     });
     el.addEvent('mouseout', function(e){
        new Event(e).stop();
        if (el.hasClass('selected') ) {el.setStyles("");}
        else {myMorph[i].start('mouse-out');}
     });
   });
   var accordion = new Accordion(togglers, togglersContents, {
                    show: -1,
                    display: -1,
                    opacity: true,
                    alwaysHide: true, 
                    duration: 1000,
                    fps: 30,
                    onActive: function(toggler, element){
                      toggler.addClass('selected');
                      toggler.setStyles("");
                      (function() { 
                                    scroll.scrollTo(0,toggler.getTop()-20);
                                  }).bind(toggler).delay(1000);
                    },
                    onBackground: function(toggler, element){
                      toggler.removeClass('selected');
                      toggler.setStyles("");
                      scroll2.toTop();
                   }
                  }, $('accordion'));

//  var MyTips = new Tips($$('.tips'), {
//    initialize:function(){
//      this.fx = new Fx.Style(this.toolTip, 'opacity', {duration: 500, wait: false}).set(0);
//    },
//    onShow: function(toolTip) {
//      this.fx.start(1);
//    },
//    onHide: function(toolTip) {
//      this.fx.start(0);
//    }
//  });

 }
}
window.addEvent('domready', Site.start);

//------------------------------------------------------------------------------
function init() {
  externalLinks();
}

window.onload = init;

function externalLinks() {
 if (!document.getElementsByTagName) return;
 var anchors = document.getElementsByTagName("a");
 for (var i=0; i<anchors.length; i++) {
   var anchor = anchors[i];
   if (anchor.getAttribute("href") &&
       anchor.getAttribute("rel") == "external")
     anchor.target = "_blank";
 }
}
