// JavaScript Document

window.addEvent('domready', function(){

  function toggleShippingAddress(){
    $('addresscheckbox').setProperty('disabled', 'disabled');
    setTimeout(function(){
      $('addresscheckbox').setProperty('disabled', '');
    }, 600);
    $$('.shipping-address-input').each(function(thetd) {
      thetd.s.toggle();
    });
    var shouldHaveTabIndex = !$('addresscheckbox').getValue();
    var i = 10;
    $$('.shipping-address-input').each(function(theInput){
      if(shouldHaveTabIndex){
        theInput.setAttribute('tabindex', i);
      } else {
        theInput.setAttribute('tabindex', '0');
      }
      i++;
    });
  }

  if($chk($('livecontent'))){
    $('livecontent').so = new Fx.Slide('livecontent', {mode: 'horizontal'});
    $('livecontent').si = new Fx.Slide('livecontent');
  }
  
  function parseForAjax(){
  

    if($('addressspan')){
      $$('.shipping-address-input').each(function(theInput){
        theInput.setAttribute('tabindex', '0');
      });
      $$('.shipping-address-input').each(function(thetd) {	
        thetd.s = new Fx.Slide(thetd, {mode: 'horizontal'});
      });
      $('addressspan').removeClass('hiddenclass');
      if($('addresscheckbox').getValue('checked')){
        toggleShippingAddress();
      }
      $('addresscheckbox').addEvent('click', toggleShippingAddress);
    }

    if($chk($('livecontent'))){
      if($chk($('livecontent').so)){
      
        $$('.ajaxLink').each(function(theLink){
          theLink.setAttribute('onClick', 'return false;');
          theLink.addEvent('click', function(){
            var h = theLink.getAttribute('href');
            (h.indexOf('?') > -1) ? h += '&ajax=true' : h += '?ajax=true'
            var r = new Ajax(h, {
              method: 'get',
              evalScripts: true,
              update: $('livecontent')
            })
            swapOutTwo(r);
            return false;
          });
        });
        
        $$('.ajaxForm').each(function(theForm){
          theForm.setAttribute('onSubmit', 'return false;');
          theForm.addEvent('submit', function(){
            // for IE
            return false;
          });
        });
        
        
        $$('.ajaxSubmit').each(function(theSubmit){
          window.submitter = theSubmit;
          theSubmit.setAttribute('onClick', 'return false;');
          theSubmit.addEvent('click', function(){
            var r = new Ajax(theSubmit.form.getAttribute('action') + '?ajax=true', {
              method: 'post',
              postBody: theSubmit.form.toQueryString(),
              update: $('livecontent')
            });
            swapOutTwo(r);
            return false;
          });
        });
        
      }
    }
    
    $$('.focus-me').each(function(theField){
      theField.addEvent('keydown', function(e){
        if(e.keyCode == 13){
          if(e.preventDefault){
            e.preventDefault();
          } else {
            e.returnValue = false;
          }
          window.submitter.fireEvent('click');
        }
      });
      setTimeout(function(){
        theField.focus();
      }, 600);
    });
    
  };
  
  function swapOutTwo(r){
  
    if($chk($('livecontent'))){
      if($chk($('livecontent').so)){
      
        $$('.focus-me').each(function(theEl){
          theEl.blur();
        });
      
        $('livecontent').so.slideOut();
        $('page').setStyle('background', 'url(\'/img/loader2.gif\') 189px 160px no-repeat');
  
        setTimeout(function(){
          $('livecontent').innerHTML = '';
          $('bread').innerHTML = '';
          $('livecontent').so.show();
          $('livecontent').si.hide();
          r.request().addEvent('onSuccess', function(){
            sIFRnow();
            parseForAjax();
            $('page').setStyle('background', 'none');
            $('livecontent').si.slideIn();
          });      
        }, 600);
    
      }
    }

  }

  parseForAjax();
  
  $$('.sales-email').each(function(theSpan){
    theSpan.innerHTML = '<a href="mailto:sales@nevada.co.uk?subject=Maha%20Enquiry">sales@nevada.co.uk</a>';
  });
  $$('.phil-email').each(function(theSpan){
    theSpan.innerHTML = '<a href="mailto:phil@nevada.co.uk?subject=Maha%20Enquiry">phil@nevada.co.uk</a>';
  });

});
