var the_height = 120;
var shown_videos = 3;
var new_width   = 185;
var container_width = 205;
var marginLeft  = 20;
function loadVideos() {
// return false;
  $('#video_container').css('overflow','hidden');
  $('.current_videos').css('overflow','hidden').height(the_height).eq(1).hide();
  $('.current_videos, .category_title, .video_selected').width(container_width);
  //alert($('.current_videos').eq(0).height());
  $('.category_title').each( function() {
     $.data($(this).get(0), 'currentIndex', 0);
     var total = $(this).next().find('.video_selected').length;
    	if (total == 0) return false;
     $.data($(this).get(0), 'total', total);
     //$(this).parent().find('.arrow-up').hide();
     if (total > shown_videos) {
        $(this).next().find('.video_selected').css('marginLeft',marginLeft).width(new_width);
        $(this).next().find('.video-scroller, .arrow-up, .arrow-down').show();
     }
     rollover($(this).next().find('.arrow-up'));
  });
  $('.category_title').css('cursor','pointer').click( function() {
     $('.current_videos').slideUp();
     $(this).next().slideDown();
  });
  $('.arrow-up').click(function() {
     var currentIndex = $.data($(this).parent().prev().get(0), 'currentIndex');
     currentIndex--;
     $.data($(this).parent().prev().get(0), 'currentIndex', currentIndex);
     rollover($(this));
     return false;
  });
  $('.arrow-down').click(function() {
     var currentIndex = $.data($(this).parent().prev().get(0), 'currentIndex');
     currentIndex++;
     $.data($(this).parent().prev().get(0), 'currentIndex', currentIndex);
     rollover($(this));
     return false;
  });
}

function rollover(object) {
  //alert('test');
  var total = $.data($(object).parent().prev().get(0), 'total');
  //alert(total);
  //alert($.data($(object).parent().prev().get(0), 'currentIndex'));
 	$(object).parent().find('.arrow-up, .arrow-down').show();
  if ($.data($(object).parent().prev().get(0), 'currentIndex') <= 0) {
  	$(object).parent().find('.arrow-up').hide();
//  	return false;
  }
  if ($.data($(object).parent().prev().get(0), 'currentIndex') < 0) {
  	$.data($(object).parent().prev().get(0), 'currentIndex', 0);
  }
  if ($.data($(object).parent().prev().get(0), 'currentIndex') + shown_videos >= total) {
 		$(object).parent().find('.arrow-down').hide();
//  	return false;
  }
  if ($.data($(object).parent().prev().get(0), 'currentIndex') > total) {
  	$.data($(object).parent().prev().get(0), 'currentIndex', total);
  }
  $(object).parent().find('.video_selected').hide(); //fadeOut('fast');
  var current = $.data($(object).parent().prev().get(0), 'currentIndex');
  //alert(total + ' ' + current);
  for(i = current; i < total; i++) {
     $(object).parent().find('.video_selected').eq(i).fadeIn('slow');
  }
  //current = current * -50;
  //alert(current);
  //$(object).parent().css('top', current);
}

function loadScrolling() {
  $('.current_videos').width(container_width + 3);
  $('.video_selected').width(container_width -7);
}
