$(function() {
  /* Handle search bar */
  $("#search input.input_keyword").focus(function() { if($(this).val() == 'Search') $(this).val('');       });
  $("#search input.input_keyword").blur(function()  { if(!$(this).val().length)     $(this).val('Search'); });
  $("#search").submit(function(e) {
    var kw = $(this).find("input.input_keyword").val();
    if(kw.length && kw != 'Search') return true;
    else {
      e.preventDefault();
      var err = $(this).find('span');
      err.text('Please enter a valid keyword!').fadeIn().css('display', 'block');
      setTimeout(function() { err.fadeOut('slow'); }, 5000);
      return false;
    }
  });

  /* Subscribe email check */
  $("#subscribe input").focus(function() { if($(this).val() == 'Please enter a valid email!') $(this).val(''); });
  $("#subscribe").submit(function(e) {
    var email         = $(this).find(':input[name=email]');
    var emailAddress  = email.val();
    var atIndex       = emailAddress.indexOf('@',0);
    var periodIndex   = emailAddress.indexOf('.', atIndex);
    if(atIndex == -1 || periodIndex == -1) {
      e.preventDefault();
      email.fadeOut('fast', function() {$(this).fadeIn('fast').val('Please enter a valid email!');});
      email.blur();
      return false;
    }
    return true;
  });

  /* Call Request check */
  $("#callRequest").submit(function(e) {
    var name  = $(this).find(':input[name=name]');
    var phone = $(this).find(':input[name=phone]');
    if(!name.val().length || !phone.val().length || name.val() == 'Your Name' || phone.val() == 'Your Phone Number') {
      e.preventDefault();

      function rtrim(str, charlist) {
        charlist = !charlist ? ' \\s\u00A0' : (charlist+'').replace(/([\[\]\(\)\.\?\/\*\{\}\+\$\^\:])/g, '\\$1');
        var re = new RegExp('[' + charlist + ']+$', 'g');
        return (str+'').replace(re, '');
      }

      var errors = "Error: Please enter your ";
      errors += !name.val().length  || name.val()  == 'Your Name'         ? 'name and '    : '';
      errors += !phone.val().length || phone.val() == "Your Phone Number" ? 'phone number' : '';
      $(".call_phone .row div").text(rtrim(errors, ' and '));
      return false;
    }
  });

  /* Handle external links */
  $("a[rel=external]").click(function(e) {
    e.preventDefault();
    window.open($(this).attr('href'));
    return false;
  });

  /* Handle fader/carousel */
  if($("#fader").length) {
    $("#fader").innerfade({
      animationtype: 'fade',
      speed: 1500,
      timeout: 4500,
      type: 'sequence',
      containerheight: '281px'
    });
  }

  /* Handle footer logo bar ticker */
  if($("#logos").length) {
    var orig = $("#logos").html();
    $("#logos").html('<marquee id="marquee" scrollamount="2.5">'+orig+'</marquee>');
    $("marquee").marquee();
    /*
    $("#logos").jMyCarousel({
      visible: '281px',
      start: 0,
      auto: true,
      circular: true,
      speed: 1000
    });
    */
  }
});

