jQuery(function() {
  var $=jQuery;
  var currentText;

  var getLanguage = function(){
    return $('#qtranslate-2-chooser li.active a').attr('hreflang');
  }
  // generate a list of tabulators according to the number of divs in step 2
  var makeTabs = function(div_id){
    var id = div_id.substring(1);
    var ul_id = div_id + ' ul';
    $(ul_id).remove();
    $(div_id).children().hide();
    $(div_id).children().first().show();
    var approachString = getLanguage() == 'de' ? 'Ansatz' : 'Approach';
    $(div_id).prepend('<ul><li><a href="#' + id + '-1" class="active">1. ' + approachString + '</a></li></ul>');
    var max = $(div_id).children().length;
    for(var i=2; i<max; i++){
      $(ul_id).append('<li><a href="#' + id + '-' + i + '">' + i + '. ' + approachString + '</a></li>');
    }
    $(ul_id).children('li').children('a').click(function(a,b){
      $(this).parent('li').parent('ul').children('li').children('a').removeClass('active');
      $(this).addClass('active');
      var tab = $(this).attr("href");
      tab ='#' +  tab.replace(/^.*#/g, '');
      $(this).parent('li').parent('ul').siblings('div').hide();
      $(tab).show();
      makeStep3(tab);
      return false;
    });
    // TODO append button for step three
    makeStep3(div_id + "-1");
  };
  var makeStep3 = function(solution){
    var questionText1 = $('#tab-' + $(solution).attr('id').substr(0,6)).parent().prev().text().replace(/^\s*/g, '').replace(/\s*$/g, '');
    var questionText2 = $('#tab-' + $(solution).attr('id').substr(0,6)).text().replace(/^\s*/g, '').replace(/\s*$/g, '');
    var solutionText = $(solution).text().replace(/^\s*/g, '').replace(/\s*$/g, '');
    var contactText = getLanguage() == 'de' ? 
      "Ich interessiere mich für folgende Frage:\n" +
      "\n" +
      questionText1 + questionText2 + "\n" +
      "\n" +
      "Lösungsansatz:\n" +
      solutionText + "\n\n" +
      "Bitte setzen Sie sich mit mir in Kontakt.\n" : 
      
      "Please send me information on:\n"+
      "\n" + 
      questionText1 + questionText2 + "\n" +
      "\n" +
      "Solution:\n" +
      solutionText + "\n\n" +
      "Please contact me.";


      currentText = contactText;
      $('#step3button').click(function(){
        $('#request').val(contactText);
        return false;
      });
   }
  // build an accordion
  $('.accordion-head').parent().click(function() {
    /* hide open sections */
    $('.accordion-head').parent().removeClass('on');
    $('.accordion-body').slideUp('normal');
    /* display opened section */
    $(this).addClass('on');
    $(this).children('.accordion-body').slideDown('normal');
  });

  //click on a link inside the accordion to display solution tabs and images
  $('.accordion-body a').click(function() {
    // mark the current element as clicked
    $('.accordion-body a').removeClass('clicked');
    $(this).addClass('clicked');
    // open the selected solution
    $('.second-step .box .step-content>div').hide();
    var tab = $(this).attr("href");
    $(tab).show();
    makeTabs(tab);
    $('.second-step .box .continuebutton').show();
    // no accordion animations and no page reload
    $('.accordion-body').stop();
    return false;
   });
});

