$(document).ready(function() {

  $(".clearField").clearField();

  // top nav

  $('.down-list').width($('.dropdown-menu').width()-2);
  $('.down-list').css("width","200px");
  $('.down-list').css("display","none");
  $('.menu-first').hover(
    function () {
      $('.menu-first', this).addClass('slide-down');
      $('.down-list', this).slideDown(100);
    },
    function () {
      obj = this;
      $('.down-list', this).slideUp(100, function(){ $('.menu-first', obj).removeClass('slide-down'); });
    });


  // bottom MFC slider 
  $('#containerMFCVids .arrowRight').click(function(){ 
    slideIt('#containerMFCVids', 'next');
  });
  $('#containerMFCVids .arrowLeft').click(function(){ 
    slideIt('#containerMFCVids', 'prev');
  });


  // related videos slider
  $('#containerRelated .arrowRight').click(function(){ 
    slideIt('#containerRelated', 'next');
  });
  $('#containerRelated .arrowLeft').click(function(){ 
    slideIt('#containerRelated', 'prev');
  });
  // don't show next button on related vids if there's <= 4
  if ((($('#containerRelatedVids .slider .relatedVids').size() > 1) && ($('#containerRelatedVids .slider .relatedVids').size() <= 4)) || ($('#containerRelatedVids .slider .relatedVids').size() == 0)) { 
    $('#containerRelated .next').hide();
  }
  

  // clip rating rollover stuff
  $('#iconsRate a').hover( 
    function(){ 
      // rollover
      $(this).addClass('hilite');
      $(this).prevAll().addClass('hilite');
      //console.log($(this).prevAll());

    },
    function(){
      // rollout 
      $('#iconsRate a').removeClass('hilite');
    }
  );



});







/* ------------------------------------------------------- */
function loadClips(section,tag,start, extra) { 
  var url = "get-clips.php?tag[]=" + tag + "&section=" + section + "&from=" + start + "&extra=" + extra;
  var newStuff = '';

  $.ajax({
    url: url,
    cache: false,
    async: false,
    success: function(html){
      newStuff = html;
    }
  });
  return newStuff;
}






/* ------------------------------------------------------- */
function numClips(tag) { 
  var url = "get-clips-new.php?tag=" + tag + "&count=1";
  var newStuff = '';

  $.ajax({
    url: url,
    cache: false,
    async: false,
    success: function(html){
      newStuff = html;
    }
  });
  return newStuff;

}



/* ------------------------------------------------------- */
function slideIt (item, dir) { 
  var spd = 400;
  var movingObject = $(item).find('.slider');
  var totalItems = $(movingObject).find('.group').size();
  var visibleItems = 1;

  var step = ($(item).find('.contain').css('width').replace(/px/g, '') * 1);

  // indiv. slider settings
  if (item == '#containerMFCVids') { totalItems = 24; visibleItems = 6; }
  else if (item == '#containerRelated') { totalItems = 12; visibleItems = 4; }

  var x = 0;
  if (dir == 'next') { 
    x = ($(movingObject).css('marginLeft').replace(/px/g, '') * 1) - step;
    if (item =='#latest-discussion') { 
      x = ($(movingObject).css('marginTop').replace(/px/g, '') * 1) - step;
    }
    $(item).find('.prev').show();
  }
  else { 
    x = ($(movingObject).css('marginLeft').replace(/px/g, '') * 1) + step;
    if (item =='#latest-discussion') { 
      x = ($(movingObject).css('marginTop').replace(/px/g, '') * 1) + step;
    }
    $(item).find('.next').show();
  }
  

  if (item != '#latest-discussion') { 
    $(movingObject).animate({marginLeft: x}, spd);
  } else { 
    $(movingObject).animate({marginTop: x}, spd);
  }

  if (x == 0) {
    $(item).find('.prev').hide();
  }
  else {
    $(item).find('.prev').show();
  }

  console.log(x * visibleItems + ' = ' + ((totalItems-visibleItems) * step * -1));

  if (x * visibleItems == ((totalItems-visibleItems) * step * -1)) {
    if ((item != '#containerCatVids') || ($('#numPages').val() < 5)) { 
      $(item).find('.next').hide();
    }
  }

  if (item == '#containerRelated') { 
    
  }

}



