// get everything after dash in string
function getSecondPart(str) {
    return str.split('-')[1];
}

function jFadeNext() {
  // get ID of button
  var jFadeButtonId = $(".jFadeButtonShown").attr('id');
  jFadeButtonID = getSecondPart(jFadeButtonId);
  // increment (or back to 1 if last image)
  var totalNumOfImages = $(".jFade").length;
  if(jFadeButtonID == totalNumOfImages) { jFadeButtonID = 1; }
  else { jFadeButtonID++; }
  
  // change button background
  $(".jFadeButtonShown").removeClass('jFadeButtonShown');
  $('#jFadeButton-'+jFadeButtonID).addClass('jFadeButtonShown');
  // fade out current image
  $(".jFadeShown").fadeOut('400', function(){
    $(".jFadeShown").removeClass('jFadeShown');
    // fade in new image
    $('#jFade-'+jFadeButtonID).fadeIn();
    $('#jFade-'+jFadeButtonID).addClass('jFadeShown');
  });
  // fade out current title
  $(".jFadeTitleShown").fadeOut('400', function(){
    $(".jFadeTitleShown").removeClass('jFadeTitleShown');
    //fade in new title
    $('#jFadeTitle-'+jFadeButtonID).fadeIn();
    $('#jFadeTitle-'+jFadeButtonID).addClass('jFadeTitleShown');
  });
}

$(document).ready(function(){
  // only run this if the jFade element exist
  if($('.image_frame_jfade').length) {
    // print marquee elements and number the images
    $count = 0;
    $('.jFade').each(function(){
      $(this).attr('id', 'jFade-'+ ++$count);
      $("#marquee").append("<li class=\"icons circle\"><a id=\"jFadeButton-"+ $count +"\" class='jFadeButton' href=\"#\">"+ $count +"</a></li>")
    });
    $num_of_images = $count;
    $count = 0;
    $('.jFadeTitle').each(function(){
      $(this).attr('id', 'jFadeTitle-'+ ++$count);
    });

    // show a random image
    var firstImageToShow = Math.floor(Math.random()*($count))+1;
    $('#jFade-'+firstImageToShow).toggle();
    $('#jFade-'+firstImageToShow).addClass('jFadeShown');
    $('#jFadeButton-'+firstImageToShow).addClass('jFadeButtonShown');
    $('#jFadeTitle-'+firstImageToShow).toggle();
    $('#jFadeTitle-'+firstImageToShow).addClass('jFadeTitleShown');

    // begin slideshow
    var timer = setInterval( jFadeNext, 5000);

    // button got clicked
    $('.jFadeButton').click(function(e){
      e.preventDefault();
      // get ID of button
      var jFadeButtonId = $(this).attr('id');
      jFadeButtonID = getSecondPart(jFadeButtonId);
      // change button background
      $(".jFadeButtonShown").removeClass('jFadeButtonShown');
      $('#jFadeButton-'+jFadeButtonID).addClass('jFadeButtonShown');
      // fade out current image
      $(".jFadeShown").fadeOut('400', function(){
        $(".jFadeShown").removeClass('jFadeShown');
        //fade in new image
        $('#jFade-'+jFadeButtonID).fadeIn();
        $('#jFade-'+jFadeButtonID).addClass('jFadeShown');
      });
      // fade out current title
      $(".jFadeTitleShown").fadeOut('400', function(){
        $(".jFadeTitleShown").removeClass('jFadeTitleShown');
        //fade in new title
        $('#jFadeTitle-'+jFadeButtonID).fadeIn();
        $('#jFadeTitle-'+jFadeButtonID).addClass('jFadeTitleShown');
      });
      // reset timer
      clearInterval(timer);
      timer = setInterval( jFadeNext, 5000);
    });
  }
  
  
  
  // only run this if the payment form exists
  if($('input[name=CB]').length) {
  
    // =======================================
    // == Terms and conditions validator
    $(".atos form").submit(function(e) {
                if(!$('#termsAndConditions').attr('checked')){
                  //Prevent the submit event and remain on the screen
                  e.preventDefault();
                  alert("You have to agree to the terms and conditions before you can proceed.");
                  return false;
                }
                else {
                    return true;
                }

        });
        
    $(".payment_module a").click(function(e){
      if(!$('#termsAndConditions').attr('checked')){
        //Prevent the submit event and remain on the screen
        e.preventDefault();
        alert("You have to agree to the terms and conditions before you can proceed.");
        return false;
      }
      else {
          return true;
      }  
    });
  
  
  }
  
  
});

