var duration = 300;

var default_name_val = 'First and Last Name';
var default_phone_val = '(xxx) xxx - xxxx';
var default_email_val = 'email@email.com';
var default_comments_val = 'Enter your comments here...';

$(document).ready(function() {
  //observe navigation items hover
  $('#navigation .list_item').hover(function() {
    $(this).addClass('hover');
    $('.submenu', this).fadeIn(350);
  }, function() {
    $(this).removeClass('hover');
    $('.submenu', this).fadeOut(100);
  });
  
  //create homepage top slider
  if($('#homepage_slideshow').get(0)) {
    $('#homepage_slideshow').cycle({
      fx: 'fade',
      duration: 1000,
      timeout: 0,
      pager: '#pager'
    });
    
    //observe homepage top slider buttons click
    $('#slideshow_controller a').click(function() {
      $('#slideshow_controller a').removeClass('active');
      $(this).addClass('active');
      
      var idx = $('#slideshow_controller a').index(this);
      $('#homepage_slideshow').cycle(idx);
      
      return false;
    });
  }
  
  //industries slideshow
  if($('#industry_slideshow ul').get(0)) {
    $('#industry_slideshow ul').cycle({
      fx: 'fade',
      duration: 1000,
      timeout: 0,
      next: '#industry_slideshow .arrow_right',
      prev: '#industry_slideshow .arrow_left'
    });
  }
  
  //create homepage bottom images carousel
  if($('#homepage_clients #carousel').get(0)) {
    $("#homepage_clients #carousel").jCarouselLite({
      btnNext: "#homepage_clients .forward",
      btnPrev: "#homepage_clients .back",
      visible: 7,
      speed: 2000,
      auto: 4000
    });
  }
  
  $('#contact_input_name').val(default_name_val);
  $('#contact_input_phone').val(default_phone_val);
  $('#contact_input_email').val(default_email_val);
  $('#contact_input_comments').val(default_comments_val);
  
  //observe side navigation arrow click
  $('.side_nav .submenu_arrow').click(function() {
    var p = $(this).closest('li');
    if($(p).hasClass('active')) {
      $('ul', $(p)).slideUp('fast', function() {
        $(p).removeClass('active')
      });
    } else {
      $('ul', $(p)).slideDown('fast', function() {
        $(p).addClass('active')
      });
    }
    
    return false;
  });

});

function clearInput(input, val) {
  if(input.value == val) {
    input.value = "";
  }
}

function restoreInput(input, val) {
  if(input.value == "") {
    input.value = val;
  }
}

function isEmail(x){
    var RegExp = /^[^@]+@[^@]+.[a-z]{2,}$/ig;
    return x.match(RegExp);
}

function validateContactForm() {
  var err = 0;
  
  if($('#contact_input_name').val() == "" || $('#contact_input_name').val() == default_name_val) {
    $('#error_input_name').fadeIn('fast');
    err++;
  } else {
    $('#error_input_name').fadeOut('fast');
  }
  
  if($('#contact_input_phone').val() == "" || $('#contact_input_phone').val() == default_phone_val) {
    $('#error_input_phone').fadeIn('fast');
    err++;
  } else {
    $('#error_input_phone').fadeOut('fast');
  }
  
  if($('#contact_input_email').val() == "" || $('#contact_input_email').val() == default_email_val || !isEmail($('#contact_input_email').val())) {
    $('#error_input_email').fadeIn('fast');
    err++;
  } else {
    $('#error_input_email').fadeOut('fast');
  }
  
  if($('#contact_input_comments').val() == "" || $('#contact_input_comments').val() == default_comments_val) {
    $('#error_input_comments').fadeIn('fast');
    err++;
  } else {
    $('#error_input_comments').fadeOut('fast');
  }
  
  if(err > 0) {
    return false;
  } else {
    $('#contact_form').submit();
    return true;
  }
}

