//Init lightbox
$(document).ready(function() {
    $('.promotions a.flier').lightBox();

//    if(Math.floor(Math.random()*2) == 0) {
//        $('.promotions a.flier.superbowl').click();
//    } else {
//       $('.promotions a.flier.lakersclippers').click();
//    }

    $('.lessonsHeader a').click( function() {
        $('.lessons').slideToggle();
    });

    //Begin the slideshow
    setInterval(function() { $('.infiniteCarousel').trigger(forwardSlideshow()) }, 5000);

    initClicks();
});

function initClicks()
{
    $('button.addToMailingList').click(addToMailingList);
    $('button.removeFromMailingList').click(removeFromMailingList);

    $('.lessons a.watch').click(function () {
        $(this).parent().next('div').slideDown();
    });
    $('.lessons a.close').click(function () {
        $(this).parent().slideUp();
    })
}


function forwardSlideshow()
{
    var $active = $('#slideshow IMG.active');

    if ( $active.length == 0 ) {
      $active = $('#slideshow IMG:last');
    }

    var $next = $active.next().length ? $active.next() : $('#slideshow IMG:first');

    $active.addClass('last-active');

    $next.css({opacity: 0.0})
    .addClass('active')
    .animate({opacity: 1.0}, 1000, function() {
        $active.removeClass('active last-active');
    });
}

function show_lessons_video(video_num, show) {
    if(show == 1) {
      $('#lessons_video_' + video_num).slideDown('slow');
    } else {
      $('#lessons_video_' + video_num).slideUp('slow');
    }
}


function show_flier(index) {
    $('.main_content').hide();
    $('#flier_' + index).fadeIn('slow');
}

function is_numeric(val)
{
  digits="0123456789";

  for(i=0; i<val.length; i++) {
    if (digits.indexOf(val.charAt(i)) < 0) {
      alert("Value must be numeric. Use digits 0-9.")
      return false;
    }
  }

  if(val == '') {
    alert("Cannot leave this field blank.")
    return false;
  }

  return true;
}


function is_blank(str) {

    str = str.replace(/^\s+|\s+$/g,"");

    if(str == '') {
        alert('Cannot leave any fields blank.');
        return true;
    }
    else {
        return false;
    }
}


function is_valid_price(str)
{
  var objRegExp = new RegExp(/^((\d*)|(\d*\.\d{2})|(\d*)|(\d*\.\d{2}))$/);

  //check if string matches pattern
  
  if(objRegExp.test(str)) {
   return true;
  }
  else {
    alert('The dollar amount is not in the correct format.');
    return false;
  }
}

function validate_email(str)
{
    var at="@"
    var dot="."
    var lat=str.indexOf(at)
    var lstr=str.length
    var ldot=str.indexOf(dot)
    if (str.indexOf(at)==-1){
       return false
    }
    else if (str.indexOf(at)==-1 || str.indexOf(at)==0 || str.indexOf(at)==lstr){
       return false
    }
    else if (str.indexOf(dot)==-1 || str.indexOf(dot)==0 || str.indexOf(dot)==lstr){
        return false
    }
    else if (str.indexOf(at,(lat+1))!=-1){
        return false
    }
    else if (str.substring(lat-1,lat)==dot || str.substring(lat+1,lat+2)==dot){
      return false
    }
    else if (str.indexOf(dot,(lat+2))==-1){
      return false
    }
    else if (str.indexOf(" ")!=-1){
      return false
    }

    return true
}


function addToMailingList()
{
    $('button.addToMailingList').attr('disabled', 'disabled');

    var email = $('#email').val();
    var firstName = $('#firstName').val();
    var lastName = $('#lastName').val();
    var address = $('#address').val();
    var city = $('#city').val();
    var state = $('#state').val();
    var zip = $('#zip').val();
    var receiveEmail = $('#receiveEmail').attr('checked');
    
    if ((firstName == null)||(firstName == '')){
        alert("First name is required.")
    }
    else if ((lastName == null)||(lastName == '')){
        alert("Last name is required.")
    }
    else if ((email == null)||(email == '')){
        alert("An email address is required.")
    }
    else if (validate_email(email)== false){
        alert("Invalid email address")
    }
    else {
        //Issue AJAX request
        var url = $('#baseUrl').val() + "/mailinglist/subscribe/format/json";
        var params = "email=" + email +
                     "&lastName=" + lastName +
                     "&firstName=" + firstName +
                     "&address=" + address +
                     "&city=" + city +
                     "&state=" + state +
                     "&zip=" + zip +
                     "&receiveEmail=" + receiveEmail;

        $.post(url, params, function(response) { 
            if(response != null && response.data.success == true) {
                //Show success confirmation message


            } else {
                //Show error message
            }
            $('button.addToMailingList').removeAttr('disabled');
        }, 'json');
  }
}

function removeFromMailingList()
{
    $('button.removeFromMailingList').attr('disabled', 'disabled');

    var email = $('.emailInput').val();

    if ((email == null)||(email == '')){
        alert("An email address is required.")
    }
    else if (validate_email(email)== false){
        alert("Invalid email address")
    } else {
        //Issue AJAX request
        var url = $('#baseUrl').val() + "/mailinglist/removefromlist/format/json";
        var params = "email=" + email;
        
        $.post(url, params, function(data) { alert(data.success);
            if(data != null && data.success == true) {
                //Show success confirmation message
                var email = $('.emailInput').val();

                $('.removeForm .email').hide();
                $('.removeForm .remove').hide();
                $('.removeForm .error').hide();

                $('.removeForm .confirm i').text(email);
                $('.removeForm .confirm').fadeIn('slow');

            } else {
                $('.removeForm .error').fadeIn('slow');
            }
            $('button.removeFromMailingList').removeAttr('disabled');
        }, 'json');
    }

}



