$(document).ready(function() {
  $('.required').each(function(){
    $.data($(this).get(0), 'default', $(this).val());
  })
  $('.required').focus(function(){
    if($(this).val() == $.data($(this).get(0), 'default'))
      $(this).val('')
  }).blur(function(){
    if($(this).val() == '')
      $(this).val($.data($(this).get(0), 'default'))
  })
  $('.postform').submit(function(){
    var submit = true;
    $('.error-state').remove();
    $('.required').css('backgroundColor', '#2A3746');
    $('.required').each(function(){
      if($(this).val() == '' || $(this).val() == $.data($(this).get(0), 'default'))
      {
        submit = false;
        $(this).css('backgroundColor', '#F05E5E');
      }
    })
    if(!submit)
    {
      if(window.location.href.search(/\/en\//) == -1)
        msg = 'Molimo ispunite sva polja';
      else
        msg = 'Please fill in all fields';
      $('.postform').after($('<div/>').css('backgroundColor', '#E74141')
                                      .css('color', 'white')
                                      .css('margin', '10px 0px')
                                      .css('padding', '10px')
                                      .css('text-align', 'center')
                                      .css('font-size', '11px')
                                      .css('font-family', 'Verdana, Arial, Helvetica, sans-serif')
                                      .addClass('error-state')
                                      .html(msg));
    }
    else
    {
      form = $(this)
      $.post('/mailer/mailer.php', $(this).serialize(), function(data, textStatus){
        if(window.location.href.search(/\/en\//) == -1)
          msg = 'Vaša poruka je poslana';
        else
          msg = 'Your mail was sent';
          
        form.before(  $('<div/>').css('backgroundColor', '#2A3746')
                                          .css('color', 'white')
                                          .css('clear', 'both')
                                          .css('margin', '10px 0px')
                                          .css('padding', '10px')
                                          .css('text-align', 'center')
                                          .css('font-size', '11px')
                                          .css('font-family', 'Verdana, Arial, Helvetica, sans-serif')
                                          .html(msg))
        form.remove()
        $('.submit-postform').remove();
        
      })
    }
    return false;
  })
  $('.submit-postform').click(function(){
    $('.postform').submit();
    return false;
  })
});
