/**************************************************************
   AUTHOR:  Pat Heard (fullahead.org)
   DATE:    2008.01.30
   PURPOSE: General Javascript fucntions for the pages
 **************************************************************/


window.addEvent('domready', function(){page.init();});



/* 
 * Page object that initializes the effects 
 */
var page = { 
  
  
  /* 
   * Called on domready to initialize the page
   */
  init : function(){
      
     // Create and bind the hover events
     page.bindEvents();

     // Check for missing prev/next buttons and inject 
     page.injectButtons();
     
  },
  
  
  /*
   * Creates the opacity fading hovers
   */
  bindEvents : function(){
  
    var link;
    var linkEffect;
    
   
    // Add opacity faders
    var elems = $$('.fade', 'a');      
    for(var i = elems.length-1; i >= 0; --i){
      link = new Element(elems[i]);
      linkEffect = link.effects({duration: 400, transition: Fx.Transitions.linear});   
      
      // Form elems
      if (link.getTag() == "input" || link.getTag() == "textarea") {
       
        link.effect('opacity').set(0.5);    
        link.addEvent('focus', hover.opacity.bindAsEventListener(linkEffect, {opacity: 1}));
        link.addEvent('blur', hover.opacity.bindAsEventListener(linkEffect, {opacity: 0.5}));
        
        // Email validation
        if(link.id == "email") 
          link.addEvent('blur', validate.email.bindAsEventListener(link));


      
      // Opacity fading 
      } else if (link.className && link.className.test(/fade/i) && !link.className.test(/here/i)){       
        
        link.effect('opacity').set(0.5);    
        link.addEvent('mouseover', hover.opacity.bindAsEventListener(linkEffect, {opacity: 1}));
        link.addEvent('mouseout', hover.opacity.bindAsEventListener(linkEffect, {opacity: 0.5})); 
               
      
      // Generic text link (bind event if link only has text nodes for children)
      } else if(link.getChildren().length == 0) {

        link.effect('border-bottom-color').set('#414515');
        link.addEvent('mouseover', hover.border.bindAsEventListener(linkEffect, {borderColour: '#f1fe61'}));
        link.addEvent('mouseout', hover.border.bindAsEventListener(linkEffect, {borderColour: '#414515'}));             
      
      }
    }   
    
    
    // Add pop-outs
    var spotlight = $$('ul.spotlight a');
    for(i = spotlight.length-1; i >= 0; --i){
      link =  new Element(spotlight[i]);
      
      
      var txt = link.title;
      link.title = "";
      
      var popout = new Element('span', {'class': 'popout'}).injectInside(link);     
      new Element('span', {'class': 'name'}).setText(txt.substring(0, txt.indexOf("|"))).injectInside(popout);
      new Element('span', {'class': 'text'}).setText(txt.substring(txt.indexOf("|") + 2, txt.length)).injectInside(popout);
      

      popout.effect('opacity').set(0);
      popout.effect('left').set('-180px');
      linkEffect = popout.effects({duration: 200, transition: Fx.Transitions.linear});
      
      link.addEvent('mouseover', hover.popout.bindAsEventListener(linkEffect, {left: -237, opacity: 1}));
      link.addEvent('mouseout', hover.popout.bindAsEventListener(linkEffect, {left: -180, opacity: 0})); 
      
    }  
    
  },

  /*
   * Adds missing buttons to project pages
   */
  injectButtons : function (){
    var paging = $('paging');
    if(paging){
      var button;
      if(!$('prevButton'))
        button = new Element('img', {'src': 'http://fullahead.org/images/interface/icon/prev_unavail.gif', 'alt': 'No previous projects', 'title': 'No previous projects', 'id': 'prevButton'}).injectTop(paging);
      else if (!$('nextButton'))
        button = new Element('img', {'src': 'http://fullahead.org/images/interface/icon/next_unavail.gif', 'alt': 'No more ...', 'title': 'No more ...', 'id': 'nextButton'}).injectInside(paging);

      if(button)
        button.effect('opacity').set(0.5);
    }
  }   
  
}




/*
 * Object for mouseover/out functions
 */
var hover = {

  reset : function(event){
    event.stop();
    event.clearTimer();
  },

  opacity : function(event, args){  
    hover.reset(this);
    this.start({
      'opacity': args.opacity  
    }); 
  },
    
  border : function(event, args){    
    hover.reset(this);
    this.start({
    'border-bottom-color': args.borderColour 
    }); 
  },
  
  popout : function(event, args){     
    hover.reset(this);    
    this.start({
      'left': args.left,
      'opacity': args.opacity
    });     
  }
}



var validate = {

  email : function(event){  
       
    if(validate.isEmail(this.value)) {
      validate.popupHide('errorEmail');
      this.getParent().className = "";
    } else {
      validate.popupShow("errorEmail", this.getParent(), "Email is invalid");
      this.getParent().className = "error";
    }  
  },
    
  isEmpty : function(str){
    return str == null || str.length == 0;
  },
  
  isEmail : function(str){
    if(validate.isEmpty(str)) return true;
    
    var re = /^[^\s()<>@,;:\/]+@\w[\w\.-]+\.[a-z]{2,}$/i
    return re.test(str);
  },
  
  submit : function(){
    
    var result = true;
    
    var email = $('email');
    var message = $('message');
    
    if(!validate.isEmail(email.value)){
      validate.popupShow("errorEmail", email.getParent(), "Email is invalid");
      email.getParent().className = "error";      
      result = false;
    }
    
    if(validate.isEmpty(message.value)){
      validate.popupShow("errorMessage", message.getParent(), "Message can't be blank");
      message.getParent().className = "error";      
      result = false;
    }
    
    
    return result;
  
  },
  
  clear : function(){
    var form = $('contactForm');
    if(form){     
      form.reset();
      
      // Clear validation errors
      validate.popupHide('errorEmail');
      validate.popupHide('errorMessage');      
      var paras = $$('form p');
      if(paras)
        for(var i = 0; i < paras.length; i++)
          paras[i].className = "";
      
    }
  },
  
  popupShow : function(id, parent, text) {
    var popup = $(id);   
    if(!popup) {
      popup = new Element('span', {'class': 'popup', 'id': id}).setText(text).injectInside(parent);      
      new Element('b').injectInside(popup);    
        
      popup.effects({duration: 200, transition: Fx.Transitions.linear}).start({
        'opacity': [0,1],
        'left': [-249, -229]
      });
    }
  },
  
  popupHide : function(id){
    var popup = $(id);
    if(popup) {
      popup.effects({duration: 200, transition: Fx.Transitions.linear, onComplete: function(popup){popup.remove();}}).start({
        'opacity': [1,0],
        'left': [-229, -249]
      });      
    }
  }
  
  
}