//*****Load all functions**************************************************//
$(document).ready(function(){
  // toggle the checkbox for a lecture
  $('.lecture').click(function(){
    var col2 = $(this).parent().parent().children('td.col2');
    var courseID = $('#courseID').attr('value');
    var courseCode = $('#courseCode-hidden').attr('value');
    var lessonID = $(this).attr('id');
    
    // Add or remove the strike for the name of the course
    var isComplete;
    if($(this).attr('checked')){
      col2.html('<strike>'+col2.html()+'</strike>');
      isComplete = '1';
    }
    else{
      col2.html(col2.children('strike').html());
      isComplete = '0';
    }
    $.post('/myclassroom/togglecomplete/', { lessonID: ""+lessonID+"", courseID: ""+courseID+"", isComplete: ""+isComplete+"" });
    
    // Mark course as complete
    if(isAllChecked()){
      $.get('/myclassroom/coursecomplete/'+courseID+'/'+courseCode+'/');
      if(is_numeric(courseCode)){
        $('#courseCode').html('<em>(Completed)</em>');  
      }
      else{
        $('#courseCode').html(courseCode+' <em>(Completed)</em>');  
      }
      $('#completed').attr('value', 'true');
      $('#courseCompleteLink').hide();
    }
    
    // Mark course as incomplete
    if(isCompletedCourse() && !isAllChecked()){
      $.get('/myclassroom/courseincomplete/'+courseID+'/');
      if(is_numeric(courseCode)){
        $('#courseCode').html('');
      }
      else{
        $('#courseCode').html(courseCode);
      }
      $('#completed').attr('value', 'false');
      $('#courseCompleteLink').show();
    }
  });
  
  
  // show all lectures
  $('#view-all').click(function(e){ 
    e.preventDefault();
    showAll();
  });
  
  // show completed
  $('#view-completed').click(function(e){ 
    e.preventDefault();
    hideCompleted(); // running hideCompleted fixes a bug the background colors of each row
    showCompleted();
  });  
  
  // hide completed
  $('#view-remaining').click(function(e){ 
    e.preventDefault();  
    showCompleted(); // running showCompleted fixes a bug the background colors of each row    
    hideCompleted();
  });
  
  videoCourse();
});


// determines if every lecture has been checked
function isAllChecked(){
  var allChecked = true;  
  $('.lecture').each(function(){
    if(!$(this).attr('checked')){
      allChecked = false;
    }
  })
  return allChecked;
}


function isCompletedCourse(){
  if($('#completed').attr('value') == 'true'){
    return true;
  }
  else{
    return false;
  }
}


function showAll(){
  var count=1;
  $('.lecture').each(function(){
    $(this).parent().parent().show();
    if(count % 2 == 0){ $(this).parent().parent().attr('class', 'even'); }
    else{ $(this).parent().parent().attr('class', 'odd'); }
    count++;      
  });
}


function showCompleted(){
  var count=1;
  $('.lecture').each(function(){
    $(this).parent().parent().show();
    if(!$(this).attr('checked')){
      $(this).parent().parent().hide();
      if(count % 2 == 0){ $(this).parent().parent().attr('class', 'even'); }
      else{ $(this).parent().parent().attr('class', 'odd'); }
      count++;
    }
  });
}

function hideCompleted(){
  var count=1;
  $('.lecture').each(function(){
    $(this).parent().parent().show();      
    if($(this).attr('checked')){
      $(this).parent().parent().hide();
      if(count % 2 == 0){ $(this).parent().parent().attr('class', 'even'); }
      else{ $(this).parent().parent().attr('class', 'odd'); }
      count++;        
    }
  });
}

function is_numeric( mixed_var ) {
    if (mixed_var === '') {
        return false;
    } 
    return !isNaN(mixed_var * 1);
}
//you tube video switcher
function videoCourse() {
  if ($('#video-player').length < 1) {return false;}
  var video = $('#video-list a:first').attr('rel');
  //load the first you tube video
  var flashvars = {};
  var params = {wmode: 'transparent'};
  var attributes = {};
  swfobject.embedSWF("http://www.youtube.com/v/" + video + "?fs=1&amp;hl=en_US&amp;rel=0", "swfoFlash", "640", "385", "8.0.0", "/swf/expressInstall.swf", flashvars, params, attributes);
  //remove the href because the external link tracker is opening youtube in a new window
  $('#video-list a').each(function(i) {
    var link = $(this).attr('href')
    $(this).attr('href','').attr('youtube',link);
  });
  //change the video if a new one is clicked.
  $('#video-list a').click(function(e) {
    e.preventDefault();
    if (video == $(this).attr('rel')) {return false;} //do not switch the video if link to current video is clicked
    $('.play-video').removeClass('active');
    $(this).parents('li').find('.play-video').addClass('active');
	  swfobject.embedSWF("http://www.youtube.com/v/" + $(this).attr('rel') + "?fs=1&amp;hl=en_US&amp;rel=0&amp;autoplay=1", "swfoFlash", "640", "385", "8.0.0", "/swf/expressInstall.swf", flashvars, params, attributes);
    $('html,body').animate({ scrollTop: $('#contentContainer').offset().top - 15},'slow'); //move the window up to show the video
    video = $(this).attr('rel');
		$('.video-info h4').text($(this).closest('li').children('p').children('a').text()); //change the title of the text to match the video
		//add ga link tracking for the videos see https://github.com/pnmg/jquery-external-links-tracker/blob/master/jquery.external-links-tracker.js
		var linkLocation = $(this).attr('youtube').replace(/^(http|https):\/\//, '').replace(/www\./i, '').replace(/\./gi, '_');
    var track = '/outgoing/'+ linkLocation
    if(typeof(pageTracker) != 'undefined'){
      pageTracker._trackPageview(track);
      if(window.console){ window.console.log('Tracked external link: '+ track) };
    } else if(typeof(_gat) == 'object') {
      _gaq.push(['_trackPageview', track]);
      if(window.console){ window.console.log('Tracked external link: '+ track) };
    } else {
      if(window.console){ window.console.log('Could not track external link: '+ track) }
    }
  });
}
