$(document).ready(function() {

	// Disable caching
	$.ajaxSetup({
		cache: false
	});

	var error = "0";

	function hide_submits() {
		$('#contribute_video').slideUp();
		$('#contribute_image').slideUp();
		$('#contribute_blog').slideUp();
	}


	/*---------------------------------------------------------------------------*/
	// VOTE
	/*---------------------------------------------------------------------------*/

	// Liked it
	$('a[id=vote_media]').click(function() {
		// Get the content ID
		var media_id	= $(this).attr('name');
		
		// Process this vote
		$.ajax({
			type: 'GET',
			data: 'q=vote&media_id=' + $(this).attr('name'),
			url: '/inc/ajax_users.php',
			success: function(response) {
				if (response == 'redirect') {
					window.location = "/register.htm"
				}

				if (response == 'success') {
					var old_votes = $('div[id=media_vote_' + media_id + ']').html();
					new_votes = eval(old_votes)+1;
					$('div[id=media_vote_' + media_id + ']').text(new_votes);
				}
			}
		});
	});
	
	/*---------------------------------------------------------------------------*/
	// REPORT CONTENT
	/*---------------------------------------------------------------------------*/

	// Liked it
	$('a[id=report_media]').click(function() {
		// Get the content ID
		var media_id	= $(this).attr('name');

		// Process this vote
		$.ajax({
			type: 'GET',
			data: 'q=report_media&media_id=' + $(this).attr('name'),
			url: '/inc/ajax_users.php',
			success: function(response) {
				if (response == 'redirect') {
					window.location = "/register.htm"
				}

				if (response == 'success') {
					$('#report_response').html('Thank you for reporting this media. A moderator will check it out shortly.');
				}
				
				if (response == 'error') {
					$('#report_response').html('Someone beat you to it! This media is already reported and waiting for moderation. Thanks anyway!');
				}
			}
		});
	});
	

	$('a[id=submit_video]').click(function() {
		hide_submits();
		$('#contribute_video').slideDown();
	});

	$('a[id=submit_image]').click(function() {
		hide_submits();
		$('#contribute_image').slideDown();
	});

	$('a[id=submit_blog]').click(function() {
		hide_submits();
		$('#contribute_blog').slideDown();
	});

	// ---- POST COMMENT FORM
	var postcommentvars = {
		url:	'/postcomment.php',
		type: 'POST',
		beforeSerialize: function() { 
       CKEDITOR.instances.usercomment.updateElement();               
		},
		beforeSubmit: function() {
			$('#responsemsg').empty();
			if ($('textarea[name=usercomment]').val()=="") {
			
			//if (CKEDITOR.instances.usercomment.getData()=="") {
				$('#responsemsg').append('<div id="error">Please enter a valid comment.</div>');
				error="1";
			}
			else
				{
					//CKEDITOR.instances.usercomment.updateElement();
					error="0";
				}
											
			// display error response
			if (error=="1") {
				$('#error').fadeIn('slow');
				return false;
			} else {
				return true;
			}            	
		},
		success:	function (response) { 
			$('#responsemsg').empty();           		
			if (Left(response,8) == "success|") {
				$('#commentbox').fadeOut('slow');
				response = SubStr(response,8);
				$('#commentbox').remove();
				$('#responsemsg').empty();
				$('#responsemsg').append('<div id="success">Your comment has been posted.</div>');
				$('#success').fadeIn('slow');
				$('#newcomment').load(response);
			} else {
				response = SubStr(response,6);
				$('#responsemsg').append('<div id="error">'+response+'</div>');
				$('#error').fadeIn('slow');
			}
		}
	};	
	 // submit the form
	$('form[name=postcomment]').ajaxForm(postcommentvars);
	// ----End Edit Account Form


});

// Random functions
function Left(str, n) {
	if (n <= 0)
		return "";
	else if (n > String(str).length)
		return str;
	else
		return String(str).substring(0,n);
}

function SubStr(str, n) {
	if (n <= 0)
		return "";
	else if (n > String(str).length)
		return str;
	else
		return String(str).substring(n,String(str).length);
}

