jQuery(function($){
  $("input.answer_input_text").live('keyup', function(){
    $('#question_answer').val(this.id+this.value);
  });

  $("input.answer_input_text").live('change', function(){
    $('#question_answer').val(this.id+this.value);
  });
  
  $('ul.radio').live('click', function(e){
    $('#'+this.id+' li p, #'+this.id+' li p img').removeClass('selected');
    $(e.target).addClass('selected');
    
    $('#question_answer').val(e.target.id);
  });

  $('ul.check_box').live('click', function(e){
    $(e.target).toggleClass('selected');
    
    $('#question_answer').val(e.target.id);
  });

  
  $("p.select_tag_option").live('click', function(){
    $('#question_answer').val($(this).attr('id'));
  });
  
  $('a#next_question').live('click', function(){
    var aselected = $('#question_answer').val()!='';

    if (!aselected)
    {
      $('#error_message').fadeIn('slow');
      return false;
    }
    $('#loading_question').show();
    
    if (qIndex < Q.length-1)
    {
      $('#test_comments').html('');
      tAnswers += escape($('#question_answer').val())+'|';
      qIndex++;
      setTimeout(function(){ $('#loading_question').hide(); showQuestion();}, 500);
    }
    else
    {
      $('#test_comments').load('/test_comments/t'+tID+'.html');
      var href = this.href;
      $.ajax({
        "url":  href,
        "type": 'POST',
        "data": 't='+tID+'&a='+tAnswers,
        "success": function(msg) {
          $('#next_question').hide();
          $('#loading_question').hide();
          $('#questions_counter').hide();
          $('#question_caption').hide();
          $('#question_choices').html(msg);
        }
      });
    }
    return false;
  });
});

function showQuestion()
{
  $('#error_message').hide();
  $('#question_answer').val('');
  $('#question_current').html(qIndex+1);
  $('#question_caption').html(Q[qIndex].question + (Q[qIndex].image!='' ? '<br /><img src="/uploads/images/questions/'+Q[qIndex].image+'" />' : ''));
  datepicker = false;
  switch (Q[qIndex].answer_type)
  {
    case 'select':
      choices_html = ''+
      '<div class="select_tag">'+
        '<div style="width: 168px; height: 0px; overflow:hidden;" class="select_tag_options">'+
          '<div style="height: 100%" class="left_side">'+
            '<div style="height: 100%" class="right_side">'+
              '<div style="height: 100%" class="select_bottom_side">'+
                '<div style="height: 100%" class="bottom_left">'+
                  '<div style="height: 100%" class="bottom_right">'+
                    '<div style="height: 142px; background:#fff; margin-left:3px;" class="content">';
                  
                    for (var i=0; i<Q[qIndex].choices.length; i++)
                    {
                      if (Q[qIndex].choices[i].i == '')
                      {
                        choices_html += '<p class="select_tag_option" id="t'+tID+'_q'+Q[qIndex].id+'_a'+Q[qIndex].choices[i].t+'_'+Q[qIndex].choices[i].s+'">'+Q[qIndex].choices[i].t+'</p>';
                      }
                      else
                      {
                        choices_html += '<p class="image"><img id="t'+tID+'_q'+Q[qIndex].id+'_a'+Q[qIndex].choices[i].t+'_'+Q[qIndex].choices[i].s+'" src="/uploads/images/answers/'+Q[qIndex].choices[i].i+'" alt=""/></p>';
                      }
                    }
      choices_html += ''+
                    '</div>'+
                  '</div>'+
                '</div>'+
              '</div>'+
            '</div>'+
          '</div>'+
        '</div>'+
        '<div style="width: 168px;" class="center_content">'+
          '<div class="left_side">'+
            '<div class="right_side">'+
              '<p class="value">Alegeti raspunsul</p>'+
            '</div>'+
          '</div>'+
        '</div>'+
      '</div>';
      break;
    
    case 'text':
      choices_html = ''+
      '<p class="answer_text">'+
        '<div class="input_tag"><div class="left_side"><div class="right_side"><input class="answer_input_text" type="text" id="t'+tID+'_q'+Q[qIndex].id+'_text" /></div></div></div>'+
      '</p>';
      break;
      
    case 'radio':
      choices_html = ''+
      '<ul id="t'+tID+'_q'+Q[qIndex].id+'" class="radio" style="height:auto !important;">';
        for (var i=0; i<Q[qIndex].choices.length; i++)
        {
          if (Q[qIndex].choices[i].i == '')
          {
            choices_html += '<li><p id="t'+tID+'_q'+Q[qIndex].id+'_a'+Q[qIndex].choices[i].t+'_'+Q[qIndex].choices[i].s+'" style="padding-left:0px;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;'+Q[qIndex].choices[i].t+'</p></li>';
          }
          else
          {
            choices_html += '<li style="float:left;"><p class="image"><img id="t'+tID+'_q'+Q[qIndex].id+'_a'+Q[qIndex].choices[i].t+'_'+Q[qIndex].choices[i].s+'" src="/uploads/images/answers/'+Q[qIndex].choices[i].i+'" alt="" style="margin-right:20px;" /></p></li>';
          }        
        }
      choices_html += '</ul>';
      break;
      
    case 'checkbox':
      choices_html = '<ul id="t'+tID+'_q'+Q[qIndex].id+'" class="check_box" style="height:auto !important; height:1px;">';
        for (var i=0; i<Q[qIndex].choices.length; i++)
        {
          if (Q[qIndex].choices[i].i == '')
          {
            choices_html += '<li><p id="t'+tID+'_q'+Q[qIndex].id+'_a'+Q[qIndex].choices[i].t+'_'+Q[qIndex].choices[i].s+'" style="padding-left:0px;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;'+Q[qIndex].choices[i].t+'</p></li>';
          }
          else
          {
            choices_html += '<li style="float:left;"><p class="image"><img id="t'+tID+'_q'+Q[qIndex].id+'_a'+Q[qIndex].choices[i].t+'_'+Q[qIndex].choices[i].s+'" src="/uploads/images/answers/'+Q[qIndex].choices[i].i+'" alt="" style="margin-right:20px;" /></p></li>';
          }        
        }
      choices_html += '</ul>';
      break;
      
    case 'date':
      choices_html = '<p class="answer_text"><div class="input_tag"><div class="left_side"><div class="right_side"><input class="answer_input_text" type="text" id="t'+tID+'_q'+Q[qIndex].id+'_text" /></div></div></div></p>';
      datepicker = 't'+tID+'_q'+Q[qIndex].id+'_text';
      break;
  }

  $('#question_choices').html(choices_html);
  $('.select_tag .content').jScrollPane({ showArrows: true });
  if (datepicker) $('#'+datepicker).datepicker({ changeYear: true, minDate: new Date(1900, 1, 1), maxDate: new Date(), yearRange: '1900:2010' });
}

var jitems = [];
function mycarousel_itemVisibleInCallback(carousel, item, i, state, evt)
{
  var idx = carousel.index(i, jitems.length);
  carousel.add(i, jQuery(jitems[idx - 1]).html());
};

function mycarousel_itemVisibleOutCallback(carousel, item, i, state, evt)
{
  carousel.remove(i);
};


/**
 * Item html creation helper.
 */
jQuery(document).ready(function() {
  jitems = jQuery('#top_test_block').children('li');
  
  jQuery('#top_test_block').jcarousel({
    wrap:   'circular',
    scroll: 1,
    animation: 'slow',
    auto:   3,
    itemVisibleInCallback: {onBeforeAnimation: mycarousel_itemVisibleInCallback},
    itemVisibleOutCallback: {onAfterAnimation: mycarousel_itemVisibleOutCallback}
  });
});

