


function rateSketchcast(id, rating)
{
	new Ajax.Request("/sketchcasts.ajax.php", {

		onSuccess : function(resp) {
			var data = resp.responseText.evalJSON();

			if(data.result == 'success'){
				var yourRating = parseInt(data.output);
				//$('current-rating').style.display = 'none';
				$('your-rating').style.width = (yourRating * 25)+'px';
				$('your-rating').style.display = 'block';
				
			}
		},

		onFailure : function(resp) {

		},

		method:'post',
		requestHeaders: {Accept: 'application/json'},
		parameters : 'action=rate&id=' + id + '&rating=' + rating

	});
}


function fullSizeRecorder(full)
{
	if( confirm("Toggle Full Screen") )
	{
		var div = $('creation');
		var flash = $('flashRecorder');
		
		if(div.style.position == 'absolute'){
		
			div.style.position = 'relative';
			div.style.width = 'auto';
			div.style.height = 'auto';
			div.style.margin = '20px 0 0 0';
			div.style.padding = '0';
			
			flash.style.width = '758px';
			flash.style.height = '500px';
			
		}else{
			
			div.style.position = 'absolute';
			div.style.width = '98%';
			div.style.height = '98%';
			div.style.margin = 'auto';
			div.style.padding = '10px 0 0 10px';
			
			flash.style.width = '100%';
			flash.style.height = '100%';
			
		}
	}
}


function pageProfileSketchcasts(page)
{
	new Ajax.Request("/sketchcasts.ajax.php", {

		onSuccess : function(resp) {
			var data = resp.responseText.evalJSON();

			if(data.result == 'success'){
				$('profileSketchcasts').innerHTML = data.output;
			}
		},

		onFailure : function(resp) {

		},

		method:'post',
		requestHeaders: {Accept: 'application/json'},
		parameters : 'action=profile_listing&page=' + page

	});
}


function pageProfileComments(page)
{
	new Ajax.Request("/comments.ajax.php", {

		onSuccess : function(resp) {
			var data = resp.responseText.evalJSON();

			if(data.result == 'success'){
				$('profileComments').innerHTML = data.output;
			}
		},

		onFailure : function(resp) {

		},

		method:'post',
		requestHeaders: {Accept: 'application/json'},
		parameters : 'action=profile_listing&page=' + page

	});
}


function submitComment()
{

	$('commentErrors').style.display = 'none';

	new Ajax.Request("/comments.ajax.php", {

		onSuccess : function(resp) {
			var data = resp.responseText.evalJSON();

			if(data.result == 'success'){
				$('commentResults').innerHTML += data.output;
				$('commentResults').style.display = 'block';
				//$('submitComment').style.display = 'none';
				$('commentFormInput').value = '';
				var numLabels = document.getElementsByClassName('numComments');
				for(var i = 0; i < numLabels.length; i++){
					numLabels[i].innerHTML = parseInt(numLabels[i].innerHTML) + 1;
				}
			}else{
				$('commentErrors').innerHTML = data.output;
				$('commentErrors').style.display = 'block';
			}


		},

		onFailure : function(resp) {
			$('commentErrors').innerHTML = "<p>An error occured during processing.</p>";
			$('commentErrors').style.display = 'block';
		},

		method:'post',
		requestHeaders: {Accept: 'application/json'},
		parameters : Form.serialize($("commentForm"))

	});

}


function deleteComment(id)
{
	if( confirm("Are you sure you want to delete this comment?") )
	{

		new Ajax.Request("/comments.ajax.php", {

			onSuccess : function(resp) {
				var data = resp.responseText.evalJSON();

				if(data.result == 'success'){
					$('commentBlock'+id).style.display = 'none';
					var numLabels = document.getElementsByClassName('numComments');
					for(var i = 0; i < numLabels.length; i++){
						numLabels[i].innerHTML = parseInt(numLabels[i].innerHTML) - 1;
					}
				}
			},

			onFailure : function(resp) {
			},

			method:'post',
		requestHeaders: {Accept: 'application/json'},
		parameters : 'action=delete&id='+id

		});

	}
}


function createUser()
{

	$('registerErrors').style.display = 'none';

	new Ajax.Request("/users.ajax.php", {

		onSuccess : function(resp) {
			var data = resp.responseText.evalJSON();

			if(data.result == 'success'){
				goTo('/profile/');
			}else{
				$('registerErrors').innerHTML = data.output;
				$('registerErrors').style.display = 'block';
			}


		},

		onFailure : function(resp) {
			$('registerErrors').innerHTML = "<p>An error occured during processing.</p>";
			$('registerErrors').style.display = 'block';
		},

		method:'post',
		requestHeaders: {Accept: 'application/json'},
		parameters : Form.serialize($("registerForm"))

	});

}


function resetPassword()
{

	$('resetErrors').style.display = 'none';

	new Ajax.Request("/users.ajax.php", {

		onSuccess : function(resp) {
			var data = resp.responseText.evalJSON();

			if(data.result == 'success'){
				$('resetResults').innerHTML = data.output;
				$('resetResults').style.display = 'block';
				$('resetForm').style.display = 'none';
			}else{
				$('resetErrors').innerHTML = data.output;
				$('resetErrors').style.display = 'block';
			}


		},

		onFailure : function(resp) {
			$('resetErrors').innerHTML = "<p>An error occured during processing.</p>";
			$('resetErrors').style.display = 'block';
		},

		method:'post',
		requestHeaders: {Accept: 'application/json'},
		parameters : Form.serialize($("resetForm"))

	});

}


function showPasswordForm()
{
	$('emailForm').style.display = 'none';
	$('passwordForm').style.display = 'block';
}


function showEmailForm()
{
	$('passwordForm').style.display = 'none';
	$('emailForm').style.display = 'block';
}


function updatePassword()
{
	$('passwordErrors').style.display = 'none';

	new Ajax.Request("/users.ajax.php", {

		onSuccess : function(resp) {
			var data = resp.responseText.evalJSON();

			if(data.result == 'success'){
				$('passwordForm').style.display = 'none';
				$('passwordDisplay').className = 'green';
			}else{
				$('passwordErrors').innerHTML = data.output;
				$('passwordErrors').style.display = 'block';
			}

		},

		onFailure : function(resp) {
			$('passwordErrors').innerHTML = "<p>An error occured during processing.</p>";
			$('passwordErrors').style.display = 'block';
		},

		method:'post',
		requestHeaders: {Accept: 'application/json'},
		parameters : 'action=update_password&'+Form.serialize($("passwordForm"))

	});

}


function updateEmailAddress()
{
	$('emailErrors').style.display = 'none';

	new Ajax.Request("/users.ajax.php", {

		onSuccess : function(resp) {
			var data = resp.responseText.evalJSON();

			if(data.result == 'success'){
				$('emailForm').style.display = 'none';
				$('emailDisplay').innerHTML = data.email;
				$('emailDisplay').className = 'green';
			}else{
				$('emailErrors').innerHTML = data.output;
				$('emailErrors').style.display = 'block';
			}

		},

		onFailure : function(resp) {
			$('emailErrors').innerHTML = "<p>An error occured during processing.</p>";
			$('emailErrors').style.display = 'block';
		},

		method:'post',
		requestHeaders: {Accept: 'application/json'},
		parameters : 'action=update_email&'+Form.serialize($("emailForm"))

	});

}




function updateAbout()
{

	$('aboutUpdateButton').disabled = true;
	$('aboutErrors').style.display = 'none';
	$('aboutResults').style.display = 'none';

	new Ajax.Request("/users.ajax.php", {

		onSuccess : function(resp) {
			var data = resp.responseText.evalJSON();

			if(data.result == 'success'){
				$('aboutResults').innerHTML = data.output;
				$('aboutResults').style.display = 'block';
				setTimeout("$('aboutResults').style.display = 'none'", 3000);
			}else{
				$('aboutErrors').innerHTML = data.output;
				$('aboutErrors').style.display = 'block';
			}

			$('aboutUpdateButton').disabled = false;

		},

		onFailure : function(resp) {
			$('aboutErrors').innerHTML = "<p>An error occured during processing.</p>";
			$('aboutErrors').style.display = 'block';
			$('aboutUpdateButton').disabled = false;
		},

		method:'post',
		requestHeaders: {Accept: 'application/json'},
		parameters : 'action=update_about&'+Form.serialize($("aboutForm"))

	});

}



function updateSketchcast()
{

	$('editUpdateButton').disabled = true;
	$('editErrors').style.display = 'none';
	$('editResults').style.display = 'none';

	new Ajax.Request("/sketchcasts.ajax.php", {

		onSuccess : function(resp) {
			var data = resp.responseText.evalJSON();

			if(data.result == 'success'){
				$('editResults').innerHTML = data.output;
				$('editResults').style.display = 'block';
				setTimeout("$('editResults').style.display = 'none'", 3000);
				var labels = document.getElementsByClassName('sketchcastTitle');
				for(var i = 0; i < labels.length; i++){
					labels[i].innerHTML = data.title;
				}
			}else{
				$('editErrors').innerHTML = data.output;
				$('editErrors').style.display = 'block';
			}

			$('editUpdateButton').disabled = false;

		},

		onFailure : function(resp) {
			$('editErrors').innerHTML = "<p>An error occured during processing.</p>";
			$('editErrors').style.display = 'block';
			$('editUpdateButton').disabled = false;
		},

		method:'post',
		requestHeaders: {Accept: 'application/json'},
		parameters : Form.serialize($("editForm"))

	});

}




function checkUsername(username)
{

	if(username == ''){
		$('usernameStatus').innerHTML = 'Please enter alpha-numeric characters only';
		return;
	}

	new Ajax.Request("/users.ajax.php", {

		onSuccess : function(resp) {
			var data = resp.responseText.evalJSON();

			$('usernameStatus').innerHTML = data.output;
		},

		onFailure : function(resp) {
			$('usernameStatus').innerHTML = 'Please enter alpha-numeric characters only';
		},

		method:'post',
		requestHeaders: {Accept: 'application/json'},
		parameters : 'action=check_username&username=' + escape(username)

	});

}



function reportAbuse(id)
{
	new Ajax.Request("/sketchcasts.ajax.php", {

		onSuccess : function(resp) {
			var data = resp.responseText.evalJSON();

			if(data.result == 'success'){
				$('reportAbuse').innerHTML = data.output;
			}
		},

		onFailure : function(resp) {

		},

		method:'post',
		requestHeaders: {Accept: 'application/json'},
		parameters : 'action=abuse&id=' + id

	});
}



function undoAbuse(id)
{
	new Ajax.Request("/sketchcasts.ajax.php", {

		onSuccess : function(resp) {
			var data = resp.responseText.evalJSON();

			if(data.result == 'success'){
				$('reportAbuse').innerHTML = data.output;
			}
		},

		onFailure : function(resp) {

		},

		method:'post',
		requestHeaders: {Accept: 'application/json'},
		parameters : 'action=undo_abuse&id=' + id

	});
}



function shareSketchcast()
{
	new Ajax.Request("/sketchcasts.ajax.php", {

		onSuccess : function(resp) {
			var data = resp.responseText.evalJSON();

			if(data.result == 'success'){
				Element.remove($('lbContent'));
				$('overlay').style.display = 'none';
				$('lightbox').style.display = 'none';
			}
		},

		onFailure : function(resp) {

		},

		method:'post',
		requestHeaders: {Accept: 'application/json'},
		parameters : Form.serialize($('shareform'))

	});
}


function deleteShared(id)
{
	new Ajax.Request("/sketchcasts.ajax.php", {

			onSuccess : function(resp) {
				var data = resp.responseText.evalJSON();

				if(data.result == 'success'){
					var block = $('sketchcastBlock'+id);
					if(block){
						block.style.display = 'none';
					}
				}
			},

			onFailure : function(resp) {
			},

			method:'post',
			requestHeaders: {Accept: 'application/json'},
			parameters : 'action=delete_shared&id='+id

	});
}


function deleteSketchcast(id, redirect)
{
	if( confirm("Are you sure you want to delete this sketchcast?") )
	{
		new Ajax.Request("/sketchcasts.ajax.php", {

			onSuccess : function(resp) {
				var data = resp.responseText.evalJSON();

				if(data.result == 'success'){
					var block = $('sketchcastBlock'+id);
					if(block){
						block.style.display = 'none';
					}
					if(redirect != ''){
						goTo(redirect);
					}
				}
			},

			onFailure : function(resp) {
			},

			method:'post',
			requestHeaders: {Accept: 'application/json'},
			parameters : 'action=delete&id='+id

		});
	}
}


function searchSketchcasts()
{
	var query = $("exploreSearchBox").value;

	$('content').innerHTML = "<h3>Searching...</h3>";

	new Ajax.Request("/search.ajax.php", {

		onSuccess : function(resp) {
			var data = resp.responseText.evalJSON();

			$('searchHeader').innerHTML = 'Search Results for &quot;'+query+'&quot;';
			$('content').innerHTML = data.output;
		},

		onFailure : function(resp) {
			$('content').innerHTML = "<p>An error occured during processing.</p>";
		},

		method:'post',
		requestHeaders: {Accept: 'application/json'},
		parameters : 'query='+query

	});

}



function submitContact()
{

	$('contactErrors').style.display = 'none';

	new Ajax.Request("/contact.ajax.php", {

		onSuccess : function(resp) {
			var data = resp.responseText.evalJSON();

			if(data.result == 'success'){
				$('contactForm').style.display = 'none';
				$('contactResults').innerHTML = data.output;
				$('contactResults').style.display = 'block';
			}else{
				$('contactErrors').innerHTML = data.output;
				$('contactErrors').style.display = 'block';
			}


		},

		onFailure : function(resp) {
			$('contactErrors').innerHTML = "<p>An error occured during processing.</p>";
			$('contactErrors').style.display = 'block';
		},

		method:'post',
		requestHeaders: {Accept: 'application/json'},
		parameters : Form.serialize($('contactForm'))

	});

}


function showComment(id, link)
{
	$('comment' + id).style.display = 'inline';
	$('showComment' + id).style.display = 'none';
}


function changeLanguage(lang)
{
	url = window.location;

	if(false){
		goTo(url+'&language='+lang);
	}else{
		goTo(url+'?language='+lang);
	}
}


function goTo(url)
{
	window.location.href = url;
}

function confirmGoTo(url)
{
	if( confirm("Are you sure you want to do this?") ){
		goTo(url);
	}
}


function popupWindow(URL, width, height)
{
	day = new Date();
	id = day.getTime();
	var left = (screen.width-width) / 2;
	var top = (screen.height-height) / 2;
	eval("page" + id + " = window.open(URL, '" + id + "', 'toolbar=0,scrollbars=1,location=0,statusbar=0,menubar=0,resizable=yes,width=" + width + ",height=" + height + ",left=" + left + ",top=" + top + "');");
}


function popupStaticWindow(name, URL, width, height)
{
	var left = (screen.width-width) / 2;
	var top = (screen.height-height) / 2;
	eval(name + " = window.open(URL, '" + name + "', 'toolbar=0,scrollbars=1,location=0,statusbar=0,menubar=0,resizable=yes,width=" + width + ",height=" + height + ",left=" + left + ",top=" + top + "');");
	eval(name + ".opener = self;");
}
