var slideshow = {
  init: function(){
      $('.slideHolder').cycle({
        fx: 'scrollHorz',
        speed: 500,
        timeout: 5000,
        next: $('.slidenext'),
        prev: $('.slideprevious')
      });
  }
};

var quickscan = {
  init: function(){
    var currentPosition = 0;
    var numberOfSlides = $('.quickscan-slides .quickscan-slide').length;
    var prevButtonClass = $('.quickscan-prevbutton');
    var nextButtonClass = $('.quickscan-nextbutton');
    var nextButton = $('#quickscan-next');
    var prevButton = $('#quickscan-prev');
    var scoreButton = $('#quickscan-score');
    var totalScore = $('#quickscan-total-score');
    var infoBox = $('.quickscan-control .info');
    var score = 0;

    infoBox.html((numberOfSlides-2) + ' vragen totaal');
    nextButton.hide();
    prevButton.hide();
    scoreButton.hide();

    $('.quickscan input:radio').bind('click', function(){
      if (currentPosition != numberOfSlides-2){
        nextButton.show();
      }
      else {
        $('input:radio:checked').each(function(el){
              if(this){
                var input = this;
                score = (score + parseInt($(input).val()));
              }
            });

        $('.quickscan-outcome').each(function(el)
        {
          outcome_score = this.id.split('-');
          if (score >= parseInt(outcome_score[2]) && score <= parseInt(outcome_score[3]))
          {
            $('#quickscan-outcome-' + outcome_score[2] + '-' + outcome_score[3]).show();
          }
        });

        totalScore.html(score);
        $('#quickscan-hidden-score').val(score);
        scoreButton.show();
      }
    });

    nextButtonClass.bind('click', function(){
      currentPosition = currentPosition+1;

      infoBox.html('vraag ' + currentPosition + ' van ' + (numberOfSlides-2));
      if ($('input[name=slide_' + (currentPosition).toString() + ']:checked').length > 0){
        nextButton.show();
      }
      else {
        nextButton.hide();
      }
      if (currentPosition == numberOfSlides-1){
        infoBox.html('Uitslag');
        scoreButton.hide();
      }
      if (currentPosition > 0){
        prevButton.show();
      }
    });

    prevButtonClass.bind('click', function(){
      currentPosition = currentPosition-1;
      if (currentPosition < 1){
        prevButton.hide();
      }
      if ($('input[name=slide_' + (currentPosition).toString() + ']:checked').length > 0){
        nextButton.show();
      }
      else {
        nextButton.hide();
      }
      if (currentPosition == 0){
        infoBox.html((numberOfSlides-2) + ' vragen totaal');
        nextButton.hide();
        prevButton.hide();
        scoreButton.hide();
      }
      else {
        infoBox.html('vraag ' + currentPosition + ' van ' + (numberOfSlides-2));
      }
    });

      $('.quickscan-slides').cycle({
        fx: 'scrollHorz',
        speed: 300,
        timeout: 0,
        nowrap: numberOfSlides,
        after: 0,
        next: nextButtonClass,
        prev: prevButtonClass
      });
  }
};

var login = {
  init: function(){
    $('.login').click(function(){
      $('#loginBox').hide();
      $('#loginDropdown').show();
    });
    $('.down').click(function(){
      $('#loginDropdown').hide();
      $('#loginBox').show();
    });
  }
};

var toggler = {
  classnames: new Array('collapse', 'expand'),
  init: function(){
    $('.toggleContent').hide();
    $('.archive-selected').parent().show().prev('a').removeClass('expand').addClass('collapse');
    $('.toggleBox').each(function(){
      var toggleBox = this;
      $(this).find('a.toggle').each(function(){
        this.counter = ($(this).hasClass('collapse')?0:1);
        $(this).click(function(){
          $(this).removeClass(toggler.classnames[this.counter++ % 2]).addClass(toggler.classnames[this.counter % 2]);
          $(toggleBox).find('.toggleContent').toggle();
          return false;
        });
      });
    });
  }
};

var externalCheck = {
  init: function(){
    $('a').each(function(){
      if ($(this).attr('rel').indexOf('external') > -1){
        $(this).attr('target', '_blank');
      }
    });
  }
};

var rating = {
  init: function(){
    $(".radio_star").rating({
       callback: function(value, link){
         $.ajax({
           type: "POST",
           url: '/async/rate/' + $("input#model").val() + '/' + $("input#item_id").val() + '/' + value,
           dataType: "text"
        });
       }
     });
  }
};

var tabs = {
  init: function(){
    $('.blogtab').hide();
    $('a.newsmessages').click(function(){
      $(this).addClass('active');
      $('.blogmessages').removeClass('active');
      $('.blogtab').hide();
      $('.newstab').show();
    });
    $('a.blogmessages').click(function(){
      $(this).addClass('active');
      $('.newsmessages').removeClass('active');
      $('.newstab').hide();
      $('.blogtab').show();
    });
  }
};

var clearInput = {
  init: function(){
    $('#header_union_number, #header_password, #header_q').focus(function() {
      var inputVal = $(this).val();
      if(inputVal == 'Lidmaatschapsnummer' || inputVal == 'Zoekopdracht' || inputVal == 'Wachtwoord'){
        $(this).val('');
      }
    }).blur(function(){
        if ($(this).val() == ''){
          $(this).val($(this)[0].defaultValue);
        }
    });

    $('form').submit(function() {
      $(this).find('#header_union_number, #header_password, #header_q').each(function(){
        var inputVal = $(this).val();
        if(inputVal == 'Lidmaatschapsnummer' || inputVal == 'Zoekopdracht'|| inputVal == 'Wachtwoord'){
          $(this).val('');
        }
      });
    });
  }
};

var signupForm = {
  init: function()
  {
    if ($('#step_2-discount').length == 0) return;

    $('#step_2-discount').checked ? $('#discount-price').show() : $('#discount-price').hide();
    $('#step_2-discount').bind('change', function()
    {
      this.checked ? $('#discount-price').show() : $('#discount-price').hide();
    });

    $('#step_2-employment').bind('change', function()
    {
      switch (this.value)
      {
        default:
        case '0':
          $('#contribution-price').text('16,27');
          break;

        case '1':
          $('#contribution-price').text('16,27');
          break;

        case '2':
          $('#contribution-price').text('6,50');
          break;
      }
    });

    $('#step_2-occupation_select').val() == '' ? $('#occupation-label, #occupation-element').show() : $('#occupation-label, #occupation-element').hide();
    $('#step_2-occupation_select').bind('change', function()
    {
      this.value == '' ? $('#occupation-label, #occupation-element').show() : $('#occupation-label, #occupation-element').hide();
    });
  }
};

var profileForm = {
  init: function()
  {
    if ($('#employment').length == 0) return;

    profileForm.current = $('#employment').val();
    profileForm.switchFields(profileForm.current);

    $('#employment').bind('change', function()
    {
      profileForm.switchFields(this.value);

      this.value != profileForm.current && this.value == 2 ? $('#contribution-change').show() : $('#contribution-change').hide();
    });

    $('#occupation_select').val() == '' ? $('#occupation-label, #occupation-element').show() : $('#occupation-label, #occupation-element').hide();
    $('#occupation_select').bind('change', function(e)
    {
      this.value == '' ? $('#occupation-label, #occupation-element').show() : $('#occupation-label, #occupation-element').hide();
    });
  },

  switchFields: function(value)
  {
    switch (value)
    {
      default:
      case '0':
        $('#contribution-price').text('16,27');
        $('#employer_name-label').show();
        $('#employer_name-element').show();
        $('#employer_street-label').show();
        $('#employer_street-element').show();
        $('#employer_number-label').show();
        $('#employer_number-element').show();
        $('#employer_addition-label').show();
        $('#employer_addition-element').show();
        $('#employer_postal_code-label').show();
        $('#employer_postal_code-element').show();
        $('#employer_residence-label').show();
        $('#employer_residence-element').show();
        $('#unemployed-info').hide();
        break;

      case '1':
        $('#contribution-price').text('16,27');
        $('#employer_name-label').hide();
        $('#employer_name-element').hide();
        $('#employer_street-label').hide();
        $('#employer_street-element').hide();
        $('#employer_number-label').hide();
        $('#employer_number-element').hide();
        $('#employer_addition-label').hide();
        $('#employer_addition-element').hide();
        $('#employer_postal_code-label').hide();
        $('#employer_postal_code-element').hide();
        $('#employer_residence-label').hide();
        $('#employer_residence-element').hide();
        $('#unemployed-info').hide();
        break;

      case '2':
        $('#contribution-price').text('6,50');
        $('#employer_name-label').hide();
        $('#employer_name-element').hide();
        $('#employer_street-label').hide();
        $('#employer_street-element').hide();
        $('#employer_number-label').hide();
        $('#employer_number-element').hide();
        $('#employer_addition-label').hide();
        $('#employer_addition-element').hide();
        $('#employer_postal_code-label').hide();
        $('#employer_postal_code-element').hide();
        $('#employer_residence-label').hide();
        $('#employer_residence-element').hide();
        $('#unemployed-info').show();
        break;
    }
  }
};

var pdList = {
  init: function()
  {
    var trigger = $('.pd-list .sub');

    trigger.addClass('plus');
    trigger.next().hide();

    trigger.click(function(){
      $(this).toggleClass('plus');
      $(this).next().toggle();
    });
  }
};

var calculatorTabs = {
  init: function(){
    var tabs = $('#tabs a');
    var tabData = $('#tab-container > div');
    var activeTab = $('#tab1');

    tabData.hide();
    activeTab.show();
    tabs.bind('click', function(){
      tabs.removeClass('current')
      $(this).addClass('current');
      tabData.hide();
      var activeTab = $(this).attr('data-id');
      $('#' + activeTab).show();
    });
  }
}

var tooltip = {
  init: function() {
    $('.qtip').qtip({ style: { name: 'dark', tip: true } });
  }
}

var chooseDate = {
  init: function()
  {
    $('#select-date').datepicker({
      dateFormat: 'dd-mm-yy',
      regional: 'nl',
      onClose: function(dateText, inst) {
        $(this).val(dateText).parents('form:first').submit();
      }
    });
  }
}

var chooseTrainer = {
  init: function()
  {
    $('#select-trainer').change(function() {
      $(this).parents('form:first').submit();
    });
  }
}

var chooseAppointment = {
  init: function()
  {
    $('#select-appointment').change(function()
    {
       window.location.href = $(this).val();
    });
  }
}

$(document).ready(function(){
  login.init();
  chooseAppointment.init();
  chooseDate.init();
  chooseTrainer.init();
  clearInput.init();
  slideshow.init();
  quickscan.init();
  toggler.init();
  externalCheck.init();
  rating.init();
  tabs.init();
  signupForm.init();
  profileForm.init();
  pdList.init();
  calculatorTabs.init();
  tooltip.init();
});

