var SETT = {
  
  init: function() {
    SETT.init_tables();
    SETT.init_print();
  },
  
  init_print: function() {
    $('#footer').prepend('<div class="print-icon"><a class="hyperlink">Seite drucken</a></div>');
    $('.print-icon a').click( function() {
      window.print();
    });
  },
  
  init_tables: function() {
    $('.tabelle').addClass('hide');
    $('.tab-toggle').addClass('hyperlink');
    $('.tab-toggle').text('hier');
    $('.tab-toggle').toggle(
      function(){
        $('.tabelle').removeClass('hide');
      },
      function(){
        $('.tabelle').addClass('hide');
      }
    )
  },
  
  init_form: function() {
    $('#birthdate').blur( this.guardian );
    $("input[name='teamsport']").change( this.init_teamsport );
    $("input[name='racketsport']").change( this.init_racketsport );
    $("input[name='course']").change( this.init_date );
    $("input[name='stay']").change( this.init_stay );
  },
  
  init_guardian: function() {
    now = new Date();
    bDate = $('#birthdate').val();
    if ( bDate.match(/[0-9]{2}\.[0-9]{2}\.[0-9]{4}/) ) {
      bd = bDate.split('.');
      birthdate = new Date( bd[2], bd[1] - 1, bd[0], 0, 0, 0, 0 );
      
      if ( now.getTime() - birthdate.getTime() < ( 18 * 365 * 24 * 60 * 60 * 1000 ) ) {
        $('#guardian').css('display', 'block');
      } else {
        $('#guardian').css('display', 'none');
      }
      
    }
  },
  
  guardian: function() {
    now = new Date();
    bDate = $('#birthdate').val();
    if ( bDate.match(/[0-9]{2}\.[0-9]{2}\.[0-9]{4}/) ) {
      bd = bDate.split('.');
      birthdate = new Date( bd[2], bd[1] - 1, bd[0], 0, 0, 0, 0 );
      
      if ( now.getTime() - birthdate.getTime() < ( 18 * 365 * 24 * 60 * 60 * 1000 ) ) {
        $('#guardian').css('display', 'block');
      } else {
        $('#guardian').css('display', 'none');
      }
      
    } else {
      alert('Bitte geben Sie das Geburtsdatum im Format \'tt.mm.jjjj\' an');
    }
  },
  
  init_teamsport: function() {
    newval = $("input[name='teamsport']:checked").val();
    $('#strong_teamsport').text( newval );
    $('#weak_teamsport').text( newval );
  },
  
  init_racketsport: function() {
    newval = $("input[name='racketsport']:checked").val();
    $('#strong_racketsport').text( newval );
    $('#weak_racketsport').text( newval );
  },
  
  init_gewinnspiel: function() {
    if ( $('#bestanden').is(':checked') ) {
      $('#gewinnspiel .weiter').show();
      $('#gewinnspiel .bestanden').show();
      $('#gewinnspiel .nichtbestanden').hide();
    } else if ( $('#nicht-bestanden').is(':checked') ) {
      $('#gewinnspiel .weiter').show();
      $('#gewinnspiel .bestanden').hide();
      $('#gewinnspiel .nichtbestanden').show();
    } else {
      $('#gewinnspiel .weiter').hide();
    }

  },
  
  init_date: function() {
    n = $("input[name='course']:checked").val().split('-');
    startdate = new Date( n[0], n[1], n[2] );
    enddate = new Date( startdate.getTime() + ( 4 * 24 * 60 * 60 * 1000  ) );
    str = 'vom ' + ( startdate.getDate() < 10 ? 0 : '' ) + startdate.getDate() + '.'  + ( startdate.getMonth() < 10 ? 0 : '' ) + startdate.getMonth() + '.';
    str += ' - ' + ( enddate.getDate() < 10 ? 0 : '' ) + enddate.getDate() + '.' + ( enddate.getMonth() < 10 ? 0 : '' ) + enddate.getMonth() + '. ' + enddate.getFullYear();
    
    //newdate = 'vom ' + n[2] + '.' + n[1] + '. - ' + ( parseInt( n[2] ) + 4) +  '.' + n[1] + '. ' + n[0]; 
    $('#accept-date').text( str );
  },
  
  init_stay: function() {
    n = $("input[name='stay']:checked").val();
    price = parseInt( $("input[name='price']").val() );
    stay_price = parseInt( $("input[name='stay_price']").val() );
    if ( n != 0 && n != undefined ) {
      newprice = price + ( n * stay_price );
    } else {
      newprice = price;
    }
    
    $('#accept-full-price').text( newprice + ',- Euro' );
  }
  
}

$(document).ready(function(){
	SETT.init();
});