$().ready(function(){

  load_background();
  
  if($(".full_page")) {
    resize_full_page();
  }
  
  $(".search").toggle(function(){
    $("#search").slideDown(50);
    return false;
  }, function(){
    $("#search").slideUp(50);
    return false;
  })
  
});

$(window).resize(function(){
  $("#background img").css({
    width : $(window).width()
  });
  
  resize_full_page();
  
});

function validate_form(form_id) {
  
  $(".fields_with_error").removeClass('fields_with_error');
  var errors = 0,
      error_message = "",
      form = $("#"+form_id+" .req");
      
  form.each(function(){
    if($(this).val() == "") {
      errors+=1;
      error_message += $(this).attr("e_msg")+"\n";
      $(this).addClass('fields_with_error');
    }
  });
  
  if(errors == 0) {
    return true;
  }
  alert(error_message);
  $(".fields_with_error")[0].select();
  return false;
  
}

function resize_full_page() {
  
  if($(window).width() <= 1055) {
    $(".header").addClass('expanded');
    $("#background img").show();
    load_background();
  } else {
    $(".header").removeClass('expanded');
    $("#background img").show();
    load_background();
  }
  
  //var padding = Math.round($(".full_page").css('paddingTop').replace('px', ''))+Math.round($(".full_page").css('paddingBottom').replace('px', ''));
  var padding = 20;
  $(".full_page").css({minHeight : $(window).height()-padding});
  
}

var image_in_background = 0;

function load_background() {
  if($(window).width() > 600 && image_in_background == 0) {
    image_in_background+=1;
    var img = new Image();
    img.src = background_image;
    $(img).load(function(){
      $("#background").append(img);
      $("#background img").css({
        width : $(window).width()
      }).fadeIn(500);
    });
  }
}

function filter_stores() {
  
  setTimeout(function(){
    
    var keyword = $("#filter_stores").val().toLowerCase();
    
    $('#list_store_categories li div h4').each(function(){
      if(!$(this).text().toLowerCase().match(keyword)) {
        $(this).hide();
      } else {
        $(this).show();
      }
    });
    
  }, 50);
  
}
