/*-----------------------------------------------------
  
  SOSADY.JS - Events and triggers for http://sosady.com
  Requires Mootools v. 1.11
  Written by Dan Drinkard, http://displayawesome.com
  
-----------------------------------------------------*/

window.addEvent('domready', function(){
  //one initial:
  if(window.ie6){ var expWidth=630; }else{ var expWidth=610; }
  
  //now go nuts
  el = $$('.drawer');
  el.each(function(el){
    //initialize slider for this element
    var mySlider = new Fx.Styles(el, {
      mode: 'horizontal',
      duration: 400,
      transition: Fx.Transitions.Quad.easeIn,
      wait:false,
      oncomplete:function(){ el.style.opacity = 1; } //hack to initiate screen redrawing
    });
    //set initial state to closed
    el.expanded = false;
    //add toggle event to process opening and closing
    el.addEvents({
      'toggle': function(){ 
        if(! el.expanded){
          el.addClass('active');
          el.expanded = true;
          mySlider.start({
            'width': expWidth,
            'opacity': 0.99 //hack to initiate screen redrawing
          })
        }else{
          el.removeClass('active');
          el.expanded = false;
          mySlider.start({
            'width': 35,
            'opacity': 0.98 //hack to initiate screen redrawing
          });
        }
      }
    });
    $ES('h2', el)[0].addEvents({
      'click':function(){ el.fireEvent('toggle'); el.removeClass('over'); },
      'mouseenter':function(){ el.addClass('over'); },
      'mouseleave':function(){ el.removeClass('over'); }
    });
  });
  // 'hit me up' link
  $$('.hitme')[0].addEvent('click', function(){$('contact').fireEvent('toggle')});
  // add accesskeys for safari and mozilla
  if(window.gecko || window.webkit){
    $$('#work h2')[0].title += " // ctrl+w to expand";
    $$('#notables h2')[0].title += " // ctrl+n to expand";
    $$('#contact h2')[0].title += " // ctrl+c to expand";
    window.addEvents({
      'keydown':function(evt){
        var evt = new Event(evt);
        if(evt.key == 'w' && evt.control) $('work').fireEvent('toggle');
        if(evt.key == 'n' && evt.control) $('notables').fireEvent('toggle');
        if(evt.key == 'c' && evt.control) $('contact').fireEvent('toggle');
      }
    });
  }
  // open a drawer if we're linking to it
  qs = window.location.href;
  if(qs.contains('#')){
    var anc = qs.substr(qs.indexOf('#')+1);
    el = $(anc);
    if(el.hasClass('drawer')){
      el.fireEvent('toggle');
    }
  }
  
  //finally, init slimbox
  // Lightbox.init.bind(Lightbox)
  var myform = $('contactform');
   myform.addEvent('submit', function(e){
     new Event(e).stop();
     submitContact();
   });
  
});
//do a little wufoo magic
function submitContact(){
 el = $('sendme');
 el.addClass('sending');
 el.disabled = 'disabled';
 var postForm = new Ajax('php/contact-submit.php', {method: 'post', data: $('contactform'), onComplete: function(t){ alertResponse(t);} }).request();
}

function alertResponse(r){
 
 if($defined($('notify')))
 {
   $('notify').remove();
 }
 
 myP = new Element('p', {
   'events': {
     destroy: function(){
       var kill = new Fx.Styles($('notify'), {duration:1500, wait:false, onComplete:function(){ $('notify').remove(); } });
       kill.start({ 'opacity':0 });
     },
     reveal: function(){
       var show = new Fx.Styles($('notify'), {duration:450, wait:false, onComplete:function(){ removed = removeNotify.delay(3000);} });
       show.start({ 'opacity':1 });
     }
   },
   'id': 'notify',
   'class': 'notify'
 });
 myP.setHTML(r);
 el = $('contactform');
 myP.injectBefore(el);
 myP.fireEvent('reveal');
 el = $('sendme');
 el.removeClass('sending');
 el.disabled = '';
}
function removeNotify(){
 $('notify').fireEvent('destroy');
}