$(function(){

$('.questions-more').click( function(){
  var limit = 25,
      tokens = this.rel.split('/'),
      target = '#questions-' + tokens[0];

  this.rel = tokens[0] + '/' + (parseInt(tokens[1]) + limit);  
  
  $.get('http://'+location.host+'/wp-content/plugins/questions/ajax.php?method=more&type=' + tokens[0] + '&index=' + parseInt(tokens[1]),
    function(data) {
//      console.log(data);
      $(target).append(data);
  });  
  
  return false;
});

$('#show_question_form, #close_question_form').click( function(){
  $('#question_form').toggle();
  
  if($('#question_form').is(':visible')) {
    $('#question_title').focus();
  }
  return false;
});

$('#submit_question').click(function() {
  if($('#question_title' ).val().length == 0) {
    alert('Bitte stellen Sie eine Frage!');
    $('#question_title').focus();
    return false;
  }
  $.getJSON('http://'+location.host+'/wp-content/plugins/questions/ajax.php?method=question_add&title=' + $('#question_title').val()+'&question=' + $('#question_data').val(),
    function(data) { 
      if(parseInt(data.error) == 1) {
        alert(data.message);
      }
      else {
        $('#question_data,#question_title').val('');
        alert('Danke f\xfcr deine Frage!');
      }
    }
  );
  return false;
});

});

