var tweetChangeSpeed = 7000;
var tweetFadeSpeed = 'slow';
function tweetRoll() {
  var lastEl = null;
  $('.tweet').each(function(i, el) {
    if ( lastEl != null ) {
      $(lastEl).fadeOut(tweetFadeSpeed, function() {
        $(lastEl).removeClass('show');
        $(el).fadeIn(tweetFadeSpeed, function() {
          $(el).addClass('show');
        });
      });
      return false;
    }
    if ( $(el).hasClass('show') ) {
      lastEl = el;

      /* if last go to first */
      if ( (i+1) == $('.tweet').length ) {
        $(el).fadeOut(tweetFadeSpeed, function() {
          $(el).removeClass('show');
          $('.tweet:first').fadeIn(tweetFadeSpeed, function() {
            $('.tweet:first').addClass('show');
          });
        });
        return false;
      }

    }
  });
}



$(document).ready(function(){

  function IsValidEmail(email){
    var filter = /^([\w-\.]+)@((\[[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.)|(([\w-]+\.)+))([a-zA-Z]{2,4}|[0-9]{1,3})(\]?)$/;
    return filter.test(email);
  }
  
  function formChange() { 
    if ($('.formType').val() == 0) {
      $('.notGeneral').hide();
      $('#li-contactform-4').hide();
      $('#li-contactform-5').hide();
      $('#li-contactform-6').hide();
      $('#li-contactform-7').hide();
      $('#li-contactform-9').hide();
      $('#contactForm_enquiry_lab').text('Your enquiry:');
    } else {
      $('.notGeneral').show();
      $('#li-contactform-4').show();
      $('#li-contactform-5').show();
      $('#li-contactform-6').show();
      $('#li-contactform-7').show();
      $('#li-contactform-9').show();
      $('#contactForm_enquiry_lab').text('Any other details:');
    }
  }


  if ($('#contactForm').length) {
    $('.formType').change(function() {
      formChange();
    });
    
    $('#contactForm_service_0, #contactForm_service_1, #contactForm_service_2, #contactForm_service_3').live('click', function(){
        $('#contactForm_budget').val(3);
    });
    
    formChange();
    
    $('#contactForm').submit(function(e) {
      var err = false;
      var message = '<h5>Enquiry Form:</h5><ul>';
      
      if ( $('#contactForm_name').val() == '' ) {
        e.preventDefault();
        message += '<li>Please enter your name</li>';
        $('#contactForm_name').css('outline', '#f00 1px solid');
        $('#contactForm_name').blur(function() { $(this).css('outline', 'none'); });
        
        err = true;
        $('#contactForm_name').focus();
      }

      if ( $('#contactForm_email').val() == '' || !IsValidEmail($('#contactForm_email').val()) ) {
        e.preventDefault();
        message += '<li>Please enter a valid email</li>';
        $('#contactForm_email').css('outline', '#f00 1px solid');
        $('#contactForm_email').blur(function() { $(this).css('outline', 'none'); });
        if ( !err ) {
          $('#contactForm_email').focus();
        }
        err = true;
      }

      if ( $('#contactForm_enquiry').val() == '' ) {
        e.preventDefault();
        message += '<li>Please fill out the enquiry box</li>';
        $('#contactForm_enquiry').css('outline', '#f00 1px solid');
        $('#contactForm_enquiry').blur(function() { $(this).css('outline', 'none'); });
        if ( !err ) {
          $('#contactForm_enquiry').focus();
        }
        err = true
      }

      if ( $('#contactForm_budget').val() == '' ||  $('#contactForm_budget').val() == 0 ) {
        e.preventDefault();
        message += '<li>Please select your budget range</li>';
        $('#contactForm_budget').css('outline', '#f00 1px solid');
        $('#contactForm_budget').blur(function() { $(this).css('outline', 'none'); });
        if ( !err ) {
          $('#contactForm_budget').focus();
        }
        err = true
      }
      
      if ( err ) {
        $('html, body').animate({
          scrollTop: 0 
        },500);
        alert(message+'</ul>');
      }
    })
  }

  if ($('#signupForm').length) {
    $('#signupForm').submit(function(e) {
      e.preventDefault();
      var err = false;
      var message = '<h5>Email Updates:</h5><ul>';
      
      if ( $('#signup_email').val() == '' || !IsValidEmail($('#signup_email').val()) ) {
        $('#signup_email').css('outline', '#f00 1px solid');
        $('#signup_email').focus();
        $('#signup_email').blur(function() { $(this).css('outline', 'none'); });
        err = true;
        message += "<li>Please enter a valid email address</li>";
      }
      
      if ( $('#signup_name').val() == '' ) {
        $('#signup_name').css('outline', '#f00 1px solid');
        if ( !err ) { 
          $('#signup_name').focus();
        }
        
        $('#signup_name').blur(function() { $(this).css('outline', 'none'); });
        err = true;
        message += "<li>Please enter your name</li>";
      }

      
      if ( !err ) {
        $('#signupForm').fadeOut('fast',function() { $('#ajaxTicker').fadeIn();  });

        $.post('/news/signup', {
          'email':$('#signup_email').val(),
          'name':$('#signup_name').val()
        }, function(data, status) {
          $('#ajaxTicker').hide();
          if (status == 'success') {
            if (data.signedUp) {
              $('#signup_email').val('');
              $('#signup_name').val('')
              $('#signupForm').fadeIn();
              alert('Thank you for signing up');
            } else {
              alert('Unfortunately there seems to have been a problem.');
              $('#signupForm #submit').fadeIn();
            }
          } else {
            $('#signupForm #submit').fadeIn();
            alert('Unfortunately there seems to have been a problem.');
          }
        } , 'json')
      } else {
        alert(message + "</li>");
        return false;
      }
    })
  }


  
  $("#lavaMenu").lavaLamp({
    fx: "backout",
    speed: 700,
    click: function(event, menuItem) {
      return true;
    }
  });

  if ( $('#slider-code').length ) {
    $('#slider-code .pager').hide();
    setTimeout("$('#slider-code').tinycarousel({ pager: true }); $('#slider-code .overview li').css({'display':'block'}); $('#slider-code .pager').fadeIn();",3000);
  }

  setInterval('tweetRoll()',tweetChangeSpeed);
  
  window.alert = function(message) { jQuery('#messageBox').html(message).fadeIn().delay(3000).fadeOut('slow'); }  
  
});



