function changeCommentOrder()
{
	//Determine the order selected
	select_box = $('comment_ordering').sorttype;
	comment_order = select_box.options[select_box.selectedIndex].value;

	//Get the date one year in the future
	var today = new Date();
	today.setDate(today.getDate() + 365);

	//Create the cookie, and set it to expire one year from now
	document.cookie = 'comment_order=' + comment_order + '; expires=' + today.toUTCString() + '; path=/'; // set domain parameter later on! (take out for local host testing)

	//Reload the page
	location.reload(true);
}

function deleteRecentComment(comment_id)
{
	if (confirm("Are you sure you want to delete this comment?\n 'Cancel' to stop, 'OK' to delete."))
	{
		var parameters = "id=" + comment_id + "&type=ajax";
		new Ajax.Request('/administration/comments/delete.php', { method: 'get', parameters: parameters, onLoading: function() { Effect.Fade('comment_' + comment_id, { duration: 0.5, from: 1.0, to: 0.5, queue: {position:'end', scope: 'deletecomment'} }); }, onComplete: function() { Effect.BlindUp('comment_' + comment_id, { queue: {position:'end', scope: 'deletecomment'} }); } });
	}
	
	return
}

function deleteVideo(title)
{
	if (confirm("You are about to delete the video '" + title + "'. \n 'Cancel' to stop, 'OK' to delete."))
	{
		document.delete_video_form.submit();
	}
	
	return false;
}

function vote(video_id, vote_count)
{
	var parameters = "video_id=" + video_id;
	new Ajax.Request('/vote_for_video.php', { method: 'post', parameters: parameters, onFailure: function(transport) { alert('Vote not registered. Error Code 300.'); } });
	Effect.Fade('vote_button', { duration: 0.3, from: 1.0, to: 0.1, queue: {position:'end', scope: 'vote'}, afterFinish: function() { voteButtonCallback(vote_count) } });
	
}

function comment_on_video()
{
	if (!Field.present('comment'))
	{
		alert('You can not submit an empty comment.');
		return false;
	}

	var parameters = Form.serialize('comment_form');
	
	$('submitting_comment').show();
	Form.disable('comment_form');
	
	new Ajax.Updater({success: 'comments'}, '/comment.php', { method: 'post', parameters: parameters, evalScripts: true, onSuccess: function() { commentSuccessCallback(); }, onFailure: function() { commentFailureCallback(); } });
	
	return false;
}

function flag(video_id)
{
	var parameters = "video_id=" + video_id;
	new Ajax.Updater('flag_button', '/flag_video.php', {method: 'post', parameters: parameters, onLoading: function() { fadeCallback('flag_button', 'flag'); }, onComplete: function() { appearCallback('flag_button', 'flag'); } });
}

function add_to_favorites(video_id)
{
	var parameters = "video_id=" + video_id;
	new Ajax.Updater('favorites_button', '/add_video_to_favorites.php', {method: 'post', parameters: parameters, evalScripts: true, onLoading: function() { fadeCallback('favorites_button', 'addtofavs'); }, onComplete: function() { appearCallback('favorites_button', 'addtofavs'); } });
}

function remove_from_favorites(video_id)
{
	var parameters = "id=" + video_id;
	new Ajax.Updater('favorites_button', '/remove_video_from_favorites.php', { method: 'post', parameters: parameters, evalScripts: true, onLoading: function() { fadeCallback('favorites_button', 'addtofavs'); }, onComplete: function() { appearCallback('favorites_button', 'addtofavs'); } });
}

function remove_from_favorites_page(favorite_id, user_id)
{
	var parameters = "id=" + favorite_id + '&user_id=' + user_id + '&favs_page=1';
	new Ajax.Updater('ajax_temp', '/remove_video_from_favorites.php', { method: 'post', parameters: parameters, evalScripts: true, onLoading: function() { Effect.Fade('favorite_' + favorite_id, { duration: 0.5, from: 1.0, to: 0.1, queue: {position:'end', scope: 'removefromfavspage'} }); }, onComplete: function() { Effect.BlindUp('favorite_' + favorite_id, { queue: {position:'end', scope: 'removefromfavspage'} }); } });
}

function commentSuccessCallback()
{
	Form.reset('comment_form');
	Form.enable('comment_form');
	$('submitting_comment').hide();
}

function commentFailureCallback()
{
	alert('Comment not registered. Error Code 300.');	
	Form.enable('comment_form');
	$('submitting_comment').hide();
}

function voteButtonCallback(vote_count)
{
	$('vote_button').style.visibility = 'hidden';
	$('vote_button').update('<img src="/images/button_thumbsup_disabled.gif" border="0">');
	$('vote_button').style.visibility = 'visible';
	Effect.Appear('vote_button', { duration: 0.4, from: 0.1, to: 1.0, queue: {position:'end', scope: 'vote'}, afterFinish: function() { voteCountCallback(vote_count); } } );
}

function voteCountCallback(vote_count)
{
	vote_count += 1;

	if (vote_count > 1)
		plural = 's';
	else
		plural = '';	
	
	$('vote_count').update(vote_count + ' vote' + plural + ' so far');
}

function fadeCallback(element_id, scope)
{
	Effect.Fade(element_id, { duration: 0.1, from: 1.0, to: 0.1, queue: {position:'end', scope: scope} });
}

function appearCallback(element_id, scope)
{
	Effect.Appear(element_id, { duration: 1.5, from: 0.1, to: 1.0, queue: {position:'end', scope: scope} } );
}