(function($){
  var autowill;
  $(function(){
    // step 2
    $(':radio[name="funeral_instructions"]').live('click',function(){
        var val = $(':radio[name="funeral_instructions"]:checked').val();
        if (val=='Other') {
            $('#other_funeral_instructions').attr('disabled',false).val('I express the wish that...');
        } else {
            $('#other_funeral_instructions').attr('disabled',true).val('');
        }
    });
    
    // step 3
    $('[name^=circumstances]').live('click',function(){
      var $t,checked,val;
      $t = $(this);
      checked = $t.attr('checked');
      val = $t.val();
      if (checked && val == 0) {
        $('[name^=circumstances]').not($t).attr('checked',false);
      } else if (checked && val != 0) {
        $('[name^=circumstances][value=0]').attr('checked',false);
      }
    });
    $('input.loadoption').live('click',function(){
      var $t,option;
      $t = $(this);
      option = $t.attr('data-option');
      autowill.loadOption(option)
    });
    
    // step 6
    $('input[name=will_for_spouse]').live('click',function(){
      var $t,checked,val;
      $t = $(this);
      checked = $t.attr('checked');
      val = $t.val();
      if ( checked && val == 1 ) {
        $('#will_for_spouse_yes').slideDown();
      } else {
        $('#will_for_spouse_yes').slideUp();
      }
    });
    
    $('#sendwilltoemail').live('click',function(){
        var $t;
        $t = $(this);
        $loader = $('<span class="loader">Please wait while sending will to your email...</span>');
        $t.replaceWith($loader);
        $.get(AJAXURL,{
            action: 'autowill_sendtoemail'
        },function(rs){
            alert(rs.message);
            if (rs.success) {
                $loader.replaceWith($t);
            } else {
                autowill.loadStep(1);
            }
        },'json');
    });
    
    $('.autowillstepform').live('submit',function(){
      var $t;
      $t = $(this);
      var step = $(':input[name="step"]', $t).val();
      if (step==5 && $(':input[name="confirm"]:checked').length == 0) {
          alert("Sorry, we cannot process if you haven't read and didn't agree with the terms above.");
          return false;
      }
      $t.ajaxSubmit({
        dataType: 'json',
        beforeSubmit: function(){
            $('.error',$t).removeClass('error');
          $('.msg',$t).remove();
        },
        success: function(rs){
          if ( rs.success ) {
            window.location.href="#autowill";
            $('#autowill_content').html(rs.data.html);
            index = rs.data.next_step - 1;
            $('#autowill_steps li').removeClass('cur');
            $('#autowill_steps li:eq('+index+')').addClass('cur');
          } else {
            $msgbox = $('.msg',$t);
            if ( $msgbox.length == 0 )
            {
              message = '';
              for (var i in rs.data)
              {
                  $(':input[name="'+i+'"]', $t).addClass('error');
                message += '<li>'+rs.data[i]+'</li>';
              }
              $msgbox = $('<div class="msg msgerror"><p>'+rs.message+'</p><ul>'+message+'</ul></div>');
              
              $t.prepend($msgbox);
            }
          }
        }
      });
      return false;
    });

    $('#autowill_steps a').live('click',function(){
      var $t, step;
      $t = $(this);
      step = $t.attr('rel');
      autowill.loadStep(step);
    });
    
    $('#autowill .gotostep').live('click',function(){
      var $t;
      $t = $(this);
      autowill.gotoStep($t.attr('data-step'));
    });
  });

  autowill = {
    setCookie:function(name,value,days){if(days){var date=new Date();date.setTime(date.getTime()+(days*24*60*60*1000));var expires="; expires="+date.toGMTString()}else var expires="";document.cookie=name+"="+value+expires+"; path=/"}
    ,readCookie:function readCookie(name){var nameEQ=name+"=";var ca=document.cookie.split(';');for(var i=0;i<ca.length;i++){var c=ca[i];while(c.charAt(0)==' ')c=c.substring(1,c.length);if(c.indexOf(nameEQ)==0)return c.substring(nameEQ.length,c.length)}return null}
    ,eraseCookie:function(a){autowill.createCookie(a,"",-1)}
    ,gotoStep:function(i){var $steps;$steps=$('#autowill_steps');$('li',$steps).removeClass('cur');$a=$('a[rel='+i+']',$steps);$a.parent().addClass('cur');window.location.href='#autowill';autowill.setCookie('autowill_current_step',i);$('#autowill .autowill_ct').hide();$('#autowill_step'+i).show()}
    ,loadStep:function(i){var $step,index;index=i-1;$step=$('#autowill_steps a:eq('+index+')');$step.addClass('loader');$.get(AJAXURL,{action:'autowill_load_form',step:i},function(rs){var $steps;if(rs.success){$steps=$('#autowill_steps');$('li',$steps).removeClass('cur');$step.removeClass('loader').parent().addClass('cur');$('#autowill_content').html(rs.data.html)}else{alert(rs.message);$step.removeClass('loader')}},'json')}
    ,loadOption:function(i) {
      var $options;
      $options = $('#step3options');
      $options.html('Loading option '+i+'...');
      $.get(AJAXURL,{
        action: 'autowill_load_form',
        step: 3,
        option: i
      },function(rs){
        var $steps;
        if (rs.success) {
          $options.html(rs.data.html);
        } else {
          alert(rs.message);
          autowill.loadStep(3);
        }
      },'json');
    }
  };
})(jQuery);
