var caCurrentNum = 1;
var caCanClick = true;

jQuery(document).ready(function(){


	/* Set Up Height of the assignment list. This is the height of 9 of the lis. Also set the list to absolute position (this must be done after the heights are set) */
	var oH = 0;
	$('#CA_List li').each(
		function(){
			var th = $(this).outerHeight(true);
			if(th > oH){oH = th;}
		}
	);
	
	
	$('#CA_List').css('position','absolute');

	// Display scroll buttons
	$('.ScrollUp, .ScrollDown').show().attr('href','javascript:void(0);');
	$('.ScrollDown').css('top', $('#CA_ListHolder').height() - $('.ScrollDown').height() - 15);
	
	ca_checkButtons();

	/* Set Up Print Buttons */
	$('.PrintDetails').click(function(){window.print();});

	/* Set Up Scrolling Buttons */
	$('.ScrollDown').click(
		function(){
			if(caCanClick&&!$(this).hasClass('Disabled')){
				caCurrentNum++;
				caCanClick = false;
					
				// Work out position of next visible item
				var liList = $('#CA_List li:visible');
				var newTop = $(liList[caCurrentNum]).position().top;
				newTop *= -1;
				$('#CA_List').animate({top: newTop},750,'swing',
					function(){
						ca_checkButtons();
						caCanClick = true;
					}
				);
			}
		}
	);
	$('.ScrollUp').click(
		function(){
			if(caCanClick&&!$(this).hasClass('Disabled')){
				caCurrentNum--;
				caCanClick = false;
					
				// Work out position of next visible item
				var liList = $('#CA_List li:visible');
				var newTop = $(liList[caCurrentNum]).position().top;
				newTop *= -1;
				$('#CA_List').animate({top: newTop},750,'swing',
					function(){
						ca_checkButtons();
						caCanClick = true;
					}
				);
			}
		}
	);

	/* Set Up Main Menu to trigger js instead of new page */
	$('.Nav_Secondary a').attr('href','javascript:void(0);');
	$('.Nav_Secondary a').click(
		function(){
			$('.Nav_Secondary a').removeClass('Active');
			$(this).addClass('Active');
			
			/* Which sector is this? */
			var sector = $(this).attr('id').split("_")[1];
			
			if(sector == "All"){
				$('#CA_List li').slideDown(400, function(){ca_checkButtons();});
			}else{
				$('#CA_List li').not('.CA_LI_' + sector).slideUp(400, function(){ca_checkButtons();});
				$('.CA_LI_' + sector).slideDown(400, function(){ca_checkButtons();});
			}
			
			$('#CA_List').css('top',0);
			caCurrentNum = 1;
			//ca_checkButtons();
		}
	);

	/* Click on the Assignment Link */
	$('#CA_List a').attr('href','javascript:void(0);').click(
		function(){
			// Set the link highlight
			$('#CA_List a').not($(this)).removeClass('Active');
			$(this).addClass('Active');
			
			/* Display the assignment */
			// Which one to display?
			var myId = $(this).attr('id').split("_")[1];
			$('.CAContent').not('#CAContent_' + myId).slideUp(400, function(){});
			$('#CAContent_' + myId).slideDown(400, function(){ca_updateScrollbar();});
		}
	);

	colHeights();
	
	$('#CA_ContentHolder').jScrollPane({maintainPosition: false, dragMaxHeight: 50});
//$('#CA_ContentHolder').jScrollPane();

	/* Create Scrollbar */
	/*
	$('#CA_ContentHolder').css('overflow','hidden');
	var scrollbar = $('#Scrollbar').slider({
		orientation : 'vertical'
	});
	$('#Scrollbar').bind('slide',
		function(e,ui){
		
			var diff = $('#CA_ContentInner').height() -  $('#CA_ContentHolder').height();
			if(diff > 0){
				var newTop = diff * ((ui.value - 100) / 100);
				$('#CA_ContentInner').css('top',newTop);
			}
		}
	);
	ca_updateScrollbar();
	*/
//	$('#Scrollbar').bind('slidestop', function(e,ui){alert(ui.value);});

});

function ca_checkButtons(){
	var numEntries = $('#CA_List li:visible').length;
//	alert(numEntries);
	if(numEntries <= 9){
		$('.ScrollUp, .ScrollDown').addClass('Disabled');
		return;
	}
	
	if(caCurrentNum == 1){
		$('.ScrollUp').addClass('Disabled');
	}else{
		$('.ScrollUp').removeClass('Disabled');
	}
	if(caCurrentNum > numEntries - 9){
		$('.ScrollDown').addClass('Disabled');
	}else{
		$('.ScrollDown').removeClass('Disabled');
	}
}

function ca_updateScrollbar(){
	$('#CA_ContentHolder').jScrollPane({maintainPosition: false, dragMaxHeight: 50});
//	$('#CA_ContentHolder').jScrollPane();
//	 $('#CA_ContentHolder')[0].scrollTo(0);
}
