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 startUpdatingChatter()
{
	// Update Chatter after 1 minute
	setTimeout('updateChatter()', 60000);
}

function updateChatter()
{
	new Ajax.PeriodicalUpdater('chatterbox', '/littlegrey/chatter/display_chatter_box.php', { method: 'get', frequency: 60 });
}

function editVideoTitle(video_id)
{
	var htmlEscapedTitle = $('video_title_text').innerHTML.escapeHTML().gsub("'", '&#039;').gsub('"', '&quot;');
	// This one also escapes quotes so they will not mess up the Javascript function arguments
	var htmlJSEscapedTitle = $('video_title_text').innerHTML.escapeHTML().gsub("'", '\\&#039;').gsub('"', '\\&quot;');

	// Change the title to an editable text box
	$('video_title_text').update("<input id='video_title_edit_text' type='text' size='80' value='" + htmlEscapedTitle + "'>");

	// Add Save/Cancel buttons	
	$('video_title_edit_buttons').update('<a href="javascript:void(0)" onclick="saveVideoTitle(' + video_id + ', &quot;' + htmlJSEscapedTitle + '&quot;)">Save</a> | <a href="javascript:void(0)" onclick="cancelEditVideoTitle(' + video_id + ', &quot;' + htmlJSEscapedTitle + '&quot;)">Cancel</a>');
}

function saveVideoTitle(video_id, oldTitle)
{
	if ($('video_title_edit_text'))
	{	
		var newTitle = $('video_title_edit_text').value.strip();
		
		if (newTitle != '')
		{
			// Make the text field read only
			$('video_title_edit_text').disabled = true;
			
			// Change the Save/Cancel buttons to 'Saving...'
			$('video_title_edit_buttons').update('Saving...');
		
			// Update the title in the database
			var parameters = "video_id=" + video_id + "&title=" + encodeURIComponent(newTitle);
			
			new Ajax.Request('/edit_video_title.php', { method: 'post', parameters: parameters, onSuccess: function() { saveVideoTitleSuccessCallback(video_id, newTitle); }, onFailure: function() { saveVideoTitleFailureCallback(video_id, oldTitle); } });
		}
		else
		{
			alert('You must enter a title!');
		}
	}
}

function saveVideoTitleSuccessCallback(video_id, newTitle)
{
	// Remove the text box
	$('video_title_text').update(newTitle);

	// Put the Edit button back
	$('video_title_edit_buttons').update('<a href="javascript:void(0)" onclick="editVideoTitle(' + video_id + ')">Edit</a>');
}

function saveVideoTitleFailureCallback(video_id, oldTitle)
{
	alert('Failed to save title! Please try again.');
	
	var htmlJSEscapedOldTitle = oldTitle.escapeHTML().gsub("'", '\&#039;').gsub('"', '\&quot;');

	$('video_title_edit_text').disabled = false;
	
	// Add Save/Cancel buttons	
	$('video_title_edit_buttons').update('<a href="javascript:void(0)" onclick="saveVideoTitle(' + video_id + ', &quot;' + htmlJSEscapedOldTitle + '&quot;)">Save</a> | <a href="javascript:void(0)" onclick="cancelEditVideoTitle(' + video_id + ', &quot;' + htmlJSEscapedOldTitle + '&quot;)">Cancel</a>');
}

function cancelEditVideoTitle(video_id, oldTitle)
{
	// Remove the text box
	$('video_title_text').update(oldTitle);

	// Remove Save/Cancel buttons and add Edit button	
	$('video_title_edit_buttons').update('<a href="javascript:void(0)" onclick="editVideoTitle(' + video_id + ')">Edit</a>');
}

function censorVideo(video_id)
{
	var parameters = "type=1&video_id=" + video_id;
	new Ajax.Updater('censor_button_container', '/censor_video.php', { method: 'post', parameters: parameters, onFailure: function(response) { alert('Failed to censor video! Refresh and try again.'); } });
}

function unCensorVideo(video_id)
{
	var parameters = "type=0&video_id=" + video_id;
	new Ajax.Updater('censor_button_container', '/censor_video.php', { method: 'post', parameters: parameters, onFailure: function(response) { alert('Failed to uncensor video! Refresh and try again.'); } });
}

function vote(video_id, is_facebook_callback)
{
	var parameters = "video_id=" + video_id;
	
	// Check if they have already voted
	if (!is_facebook_callback)
	{
		new Ajax.Request('/vote_for_video.php', { method: 'post', parameters: parameters, onFailure: function(transport) { alert('Vote not registered. Please refresh and try again.'); } });
	}
	else
	{
		// Same ajax request as above, except does not give an error on failure because that will confuse people who have clicked the Facebook Like button
		new Ajax.Request('/vote_for_video.php', { method: 'post', parameters: parameters });
	}
	
	// If an element with the id voted_for exists, then they have already voted for this video,
	// so do not show the button fade effect
	if (!$('voted_for'))
	{
		Effect.Fade('vote_button', { duration: 0.3, from: 1.0, to: 0.1, queue: {position:'end', scope: 'vote'}, afterFinish: function() { voteButtonCallback() } });
	}
}

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_container', '/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_container', '/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_container', '/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_button').style.visibility = 'hidden';
	$('vote_button').update('<img id="voted_for" src="/images/like_button_disabled.gif" width="94" height="61" alt="You Like This">');
	$('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() { voteCallToActionCallback(); } } );
}

function voteCallToActionCallback()
{
	$('vote_count').update("You like this video");
}

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} } );
}
