$(document).ready(function(){
	
// 1.0 Make the class titles slowly fade in on page load	
	$('.titles').hide().fadeIn(2000);



// 2.0 Toggle show/hide copyright info
$('.aboutcopy').hide();
 $('<p><input type="button" value="Show Copyright Info" id="toggleButton"></p>').insertBefore('.aboutcopy');
 $('#toggleButton').click(function(){
    $('.aboutcopy').toggle(); 
    if($('.aboutcopy').is(':visible')) {
      $(this).val('Hide Copyright Info');
    } else {
      $(this).val('Show Copyright Info');
    }
  });


		
// 3.0 warning message to those who don't have javascript enabled	
$('#no-script').remove();



// 4.0 leftsidebar and rightsidebar links move over 10px when hover over them
$('#leftsidebar a, #rightsidebar a').hover(function(){
    $(this).animate({paddingLeft: '+=10px'}, 200);
  }, function(){
    $(this).animate({paddingLeft: '-=10px'}, 200);
  });
  
// accordian menu on links page---------------------------------------------
/* For more information see textbook Jquery Novice to Ninja pg 149 */
$('#linksweb ul > li ul')
    .click(function(e){
      e.stopPropagation();
    })
    .filter(':not(:first)')
    .hide();
    
  $('#linksweb ul > li').click(function(){
    var selfClick = $(this).find('ul:first').is(':visible');
    if(!selfClick) {
      $(this)
        .parent()
        .find('> li ul:visible')
        .slideToggle();
    }
    
    $(this)
      .find('ul:first')
      .stop(true, true)
      .slideToggle();
  });
  

// lightbox located on about page---------------------------------------------------------
$('a.lightbox').click(function(e) {
    $('body').css('overflow-y', 'hidden'); // hide scrollbars!
    
    $('<div id="overlay"></div>')
      .css('top', $(document).scrollTop())
      .css('opacity', '0')
      .animate({'opacity': '0.5'}, 'slow')
      .appendTo('body');
      
    $('<div id="lightbox"></div>')
      .hide()
      .appendTo('body');
      
    $('<img />')
      .attr('src', $(this).attr('href'))
      .load(function() {
        positionLightboxImage();
      })
      .click(function() {
        removeLightbox();
      })
      .appendTo('#lightbox');
    
    return false;;
  });




  // slideshowtwo defined using jquery.cycle on homepage
$('#slideshowtwo').cycle();


// Form Validation with validate.min plugin. Pg 236 of textbook JQuery: Novice to Ninja
$('#contact form').validate({
    rules: {
      firstname: { //firstname is required
        required: true,
      },
	  lastname: { //lastname is required
		  required: true,
	  },
      email: {  //email is also required
        required: true,
        email: true
      },
       
    },
    success: function(label) {
      label.text('OK!').addClass('valid');  //upon submit checks to see if form is validated
    }
  });

// end Form Validation

// Maximum length Indicator taken from textbook JQuery: Novice to Ninja, pg 239
 $('.textboxmax')
    .after("<span></span>") //add span after textbox to display number of characters
    .next()
    .hide()
    .end()
    .keypress(function(e) {
      var current = $(this).val().length;
      if(current >= 400) {  // set to 400 characters
        if(e.which != 0 && e.which != 8) { // allow backspace and delete key even if reach max
          e.preventDefault(); //prevent keypress from registering if max reached
        }
      }
      $(this).next().show().text(400 - current);
    });
// End Maximum lenght Indicator


// Check All Checkboxes taken from textbook JQuery: Novice to Ninja pg 242
// Sets all related checkboxes to have same value as the master checkbox if checked
$('.check-all:checkbox').change(function() {
    var group = ':checkbox[name=' + $(this).attr('name') + ']';
    $(group).attr('checked', $(this).attr('checked'));
  });
// End Check All Checkboxes


  // End. close document ready at top of file
});




// ---------------------------------------------------- define functions for lightbox located on about page-----------------
/* Got this script from the Jquery Novice to ninja pg. 94 */
// loads image and displays in center of page
function positionLightboxImage() {
  var top = ($(window).height() - $('#lightbox').height()) / 2;
  var left = ($(window).width() - $('#lightbox').width()) / 2;
  $('#lightbox')
    .css({
      'top': top + $(document).scrollTop(),
      'left': left
    })
    .fadeIn();
}
// removes lightbox when user clicks on image
function removeLightbox() {
  $('#overlay, #lightbox')
    .fadeOut('slow', function() {
      $(this).remove();
      $('body').css('overflow-y', 'auto'); // show scrollbars!
    });
}



