$.fn.submitWithAjax = function() {
  this.submit(function() {
    $.post(this.action, $(this).serialize(), null, "script");
    return false;
  })
  return this;
};

$(function() {
  $('form.serious').submit(function() {
    $('button[type="submit"]', this).replaceWith('<div class="sending">Sending...</div>');
    //$(this).find('.fields').replace('Loading...');
  });

  $('input.with-maxlength').maxlength( {maxCharacters: 69} );
  $('textarea.with-maxlength').maxlength( {slider: true, maxCharacters: 156} );

  $('.coming-soon').click(function() {
    alert('Our Russian Business Visa Invitation service is coming soon. If you are interested to be a partner, please contact us via get@yourvisatorussia.com.');
  });

  activate_select = function(select) {
    container = select.parents('.contains-hidden');
    select.children(':selected').attr('activator') ? container.addClass('active') : container.removeClass('active');
    select.children(':selected').attr('activator1') ? container.addClass('active1') : container.removeClass('active1');
  }

  $('#order_delivery_type').change(function(){
    activate_select($(this));
    $(this).parents('.field').removeClass('cur-focus');
  });

  activate_select($('#order_delivery_type'));


  $('.activator').click(function() {
    $(this).parents('.contains-hidden:first').toggleClass('active');
    this.blur();
    return false;
  });


  $('.focus-first-element').ready(function() {
    $(this).find('.text:first').focus().
      blur().
      focus();
  });

  $('.remote-submit-to-target').click(function() {
    var form = $(this).parents('form');
    var form_target = "#" + form.find('.target-for-form').val();
    form.ajaxSubmit({target: form_target});
    return false;
  });

  $('.remote-submit').livequery('click', function() {
    var form = $(this).parents('form');
    form.submitWithAjax()
      .addClass('submitting')
      .submit();
    return false;
  });

  $('.visa-type-choose li a').click(function() {
    $('table.pricing td.price').removeClass('current');
    $('.visa-type-choose li a').removeClass('current');
    $(this).addClass('current').blur();
    $('#order_visa_type').val(this.rel);
    $('#new_order_form').removeClass().addClass(this.rel);
    return false;
  });

  $('.link-to-add').click(function() {
    listl = $(this).parents('.container-to-add').find('.list-to-add');
    iteml = listl.find('.item-to-add');
    if (iteml.siblings().size() < 4) {
      listl.append("<li>" + iteml.html() + "</li>");
    } else {
      $(this).hide();
    }
    return false;
  });

  $('.set-price').click(function() {
    $('table.pricing td.price').removeClass('current');
    $(this).parents('td.price').addClass('current');
    $(this).blur();

    var values = this.rel.split("-")
    $('#order_processing_time').val(values[1]);
    $('#order_pricing_id').val(values[0]);

    if ($(this).attr('double'))
      $(this).parents('form').find("#double_entry_dates").addClass('active');
    else
      $(this).parents('form').find("#double_entry_dates").removeClass('active');


    return false;  
  });
  //alert($('.current .set-price[double]').attr('class'));

  $('#new_order_form').ready(function() {
    if ($('#order_processing_time').val() && $('#order_pricing_id').val()) {
      var td = $('table.pricing td.price a[rel="' + $('#order_pricing_id').val() + '-' + $('#order_processing_time').val() + '"]').parents('td.price');
      td.addClass('current');
      td.find('.set-price[double]').parents('form').find("#double_entry_dates").addClass('active');
    }
  });

  $.ajaxSetup({
    'beforeSend': function(xhr) { xhr.setRequestHeader("Accept", "text/javascript"); }
  });

  $('#order_delivery_type').ready(function() {
    makeSublist('order_delivery_type', 'postal_address_country', true, null);
  });

  $('.link-back').click(function() {
    history.go(-1);
    return false;
  });
});

function makeSublist(parent, child, isSubselectOptional, childVal) {

  $("body").append("<select style='display:none' id='"+parent+child+"'></select>");
  $('#'+parent+child).html($("#"+child+" option"));

  var parentValue = $('#'+parent).attr('value');
  $('#'+child).html($("#"+parent+child+" .sub_"+parentValue).clone());

  childVal = (typeof childVal == "undefined")? "" : childVal ;
  $("#"+child+' option[@value="'+ childVal +'"]').attr('selected','selected');
 
  $('#' + parent).change(function() {

    var parentValue = $('#'+parent).attr('value');

    $('#'+child).html($("#"+parent+child+" .sub_"+parentValue).clone());

    if (isSubselectOptional) 
      $('#'+child).prepend("<option value='none'> -- Select -- </option>");
  
    $('#'+child).trigger("change"); 
    $('#'+child).focus();
  });

}


$(function() {
  $(":input").livequery('focus', function() {
    $(this)
      .parents('.field')
      .addClass("cur-focus");
  });

  $(":input").livequery('blur', function() {
    $(this)
      .parents('.field')
      .removeClass("cur-focus");
  });

  $('label.required').each(function() {
    mark = '<span class="required-mark">&nbsp;*</span>'
    $(this).append(mark);  
  });

  $('a[rel*=external]').attr({'target':'_blank'});

  $('.actions a.positive').livequery(function() {
    $(this).prepend('<img src="/images/accept.png" alt="">');
  });
  $('.actions a.cancel').livequery(function() {
    $(this).prepend('<img src="/images/cancel.png" alt="">');
  });
  $('.actions button.positive').livequery(function() {
    $(this).prepend('<img src="/images/accept.png" alt="">');
  });
  $('.actions a.create').livequery(function() {
    $(this).prepend('<img src="/images/add.png" alt="">');
  });
  $('.actions button.find').livequery(function() {
    $(this).prepend('<img src="/images/find.png" alt="">');
  });
  $('.actions a.pdf').livequery(function() {
    $(this).prepend('<img src="/images/pdf-icon.png" alt="">');
  });
  $('.actions a.view').livequery(function() {
    $(this).prepend('<img src="/images/view.png" alt="">');
  });
  $('.actions a.edit').livequery(function() {
    $(this).prepend('<img src="/images/pencil.png" alt="">');
  });
  $('.actions a.delete').livequery(function() {
    $(this).prepend('<img src="/images/delete.png" alt="">');
  });
  $('.actions a.list').livequery(function() {
    $(this).prepend('<img src="/images/list.png" alt="">');
  });
  $('.actions a.back').livequery(function() {
    $(this).prepend('<img src="/images/back.png" alt="">');
  });
});
