// AJAX stuff for BTC Upstander videos

// Calls a video by node ID. nid is an integer, and l is the link 
// that's invoking this function.
function switchVideo(nid, l) {
	if(typeof nid != 'number') {
		alert('Nice try, sally.');
		return false;
	}

	$('#loading').show();

	// I used .ajax here because I can put in useful error handlers
	// which will bitch if something goes wrong, which is useful for
	// debuggin'.
	$.ajax({
		data: 'q=btcm/get/' + nid,	// URL of the AJAX loader
		dataType: 'json',	// Expected data format
		
		error: function(http, textStatus, errorThrown) {
			alert('AJAX Error: ' + errorThrown + ' (' + textStatus + ')');
		},
		
		success: function(data, textStatus) {
			// data.code will contain the full <object> tag for the video, so we can
			// drop it in wholesale.
			$('#video-player').html(data.code);
			
			// Set this video's link (referred to by the variable l) to active
			$('#videos a.title').removeClass('active');
			$(l).addClass('active');
			
			// Update the answer form. data.question will contain the question we're
			// asking for this video.
			$('#question h3').text(data.question);

      // Update the #nid and #cid fields so that we're pointing to this video node,
      // and to the existing answer (if any, it will be in data.cid).
			$('#myAnswer #nid').attr('value', nid);
			$('#myAnswer #cid').attr('value', data.cid);
			
			// data.cid will be 0 if you haven't answered the question, or higher
			// than 0 if you have.
			if(data.cid == 0) {
				$('#myAnswer #content').removeAttr('disabled').attr('value', '');
				$('#myAnswer #submit').show();
				$('#answers').hide();
			}
			else {
				// data.status will be 0 (COMMENT_PUBLISHED, as of D6) if your answer has been approved,
				// so you'll be able to see and edit it. Otherwise, it'll be 1 (COMMENT_NOT_PUBLISHED),
				// and the submit button will vanish, leaving only a stern message.
				if(data.status == 0) {
					$('#myAnswer #content').removeAttr('disabled').attr('value', data.answer);
					$('#myAnswer #submit').show();
				}
				else {
				  $('#myAnswer #content').attr('disabled', 'true').attr('value', 'Thanks! Your response has been submitted and will be added once it is approved.');
				  $('#myAnswer #submit').hide();
				}
				$('#answers').show();
			}
			
			// data.others contains other users' answers to the question. If it's populated,
			// we can rebuild the #answers area with those answers.
			$('#answers .answer').remove();
			if(data.others.length > 0) {
				for(i = 0; i < data.others.length; i++) {
					$('#answers > .inner').append('<div class="answer">' + data.others[i] + '</div>');
				}
				$('#answers .answer:first').addClass('active');
				// Determine the prev/next button states
				$('#prev').css('visibility', 'hidden');
				$('#next').css('visibility', data.others.length == 1 ? 'hidden' : 'visible');
			}
			else {
				$('#answers').hide();
			}
		}
	});
	$('#loading').hide();
	return false;	// Prevent jumping to the top of the page
}

function sendAnswer() {
	// Quick validation
	var answer = $('#myAnswer #content').attr('value');
	var nid = $('#myAnswer #nid').attr('value');
	var cid = $('#myAnswer #cid').attr('value');
	
	if(answer.length > 0 && nid > 0 && cid.match(/^[0-9]+$/)) {
		$('#loading').show();
		$.ajax({
			data: {
				answer: answer,
				nid: nid,
				cid: cid
			},
			dataType: 'text',
			type: 'POST',
			url: '/index.php?q=btcm/put',
			
			error: function(http, textStatus, errorThrown) {
				alert('AJAX Error: ' + errorThrown);
			},
			
			success: function(data, textStatus) {
				// If the input data passed validation, a numeric result is returned.
				// 0 indicates a problem in comment_save(), anything else is the ID of the
				// saved answer.
				if(data.match(/^[0-9]+$/)) {
					var result = parseInt(data);
					if(result == 0) {
						// Problem
						alert('Answer could not be saved.');
					}
					else {
						// The answer was successfully saved, so we disable the text area, hide the
						// button, leave a stern message, and show the other answers.
						$('#myAnswer #cid').attr('value', result);
						$('#myAnswer #content').attr('disabled', 'yes').attr('value', 'Thanks! Your response has been submitted and will be added once it is approved.');
						$('#myAnswer #submit').hide();
						// Only show the other answers if there are, well, other answers
						if($('#answers > .inner').children().length > 0) {
							$('#answers').show();
						}
					}
				}
				else {
					// Validation error
					alert('Error: ' + data);
				}
			}
		});
		$('#loading').hide();
	}
	else {
		alert('Invalid values entered.');
	}
	
	return false;	// Prevent jumping to the top of the page
}

function nextAnswer() {
	$('#prev').css('visibility', 'visible');
	$('#answers .active').each(function() {
		if($(this).next().length == 1) {
			// If we're here, there's a comment following this one
			$(this).removeClass('active');
			$(this).next().addClass('active');
			// Grey this out?
			if($(this).next().next().length == 0) {
				$('#next').css('visibility', 'hidden');
			}
		}
	});
	return false;
}

function prevAnswer() {
	$('#next').css('visibility', 'visible');
	$('#answers .active').each(function() {
		if($(this).prev().length == 1) {
			// If we're here, there's a comment preceding this one
			$(this).removeClass('active');
			$(this).prev().addClass('active');
			// Grey this out?
			if($(this).prev().prev().length == 0) {
				$('#prev').css('visibility', 'hidden');
			}
		}
	});
	return false;
}

// -----

// Student Spotlight on the front page

function prevSpotlight() {
	$('#spotlight .scroll').each(function() {
		var left = parseInt($(this).css('left'));
		if(left != 0) {
			$(this).animate({ left: '+=715px' }, 1000);
		}
	});
	return false;
}

function nextSpotlight() {
	$('#spotlight .scroll').each(function() {
		var left = parseInt($(this).css('left'));
		var end = (0 - ($(this).children().length - 1) * 715);
		if(left != end) {
			$(this).animate({ left: '-=715px' }, 1000);
		}
	});
	return false;
}
