var showingprofile = 0;
var currentlyshowingcommentform = 0;
var currentlyshowingtopform = 1;

function showcommentform(id){
	$(".commentform").hide();
	if(currentlyshowingcommentform == id){
		$("#commentform"+id).hide();
		currentlyshowingcommentform = 0;
	}
	else{
		$("#commentform"+id).show();
		currentlyshowingcommentform = id;
	}
	
	return false;
}

function comment(id){
	var commenttext = $("#commenttext"+id).val();
	
	if(commenttext.length > 65000){
		showmessage('Your comment cannot be longer than 400 characters.');
		return false;
	}
	$("#commentform"+id+" input").attr("disabled","true");
	$.post("ajax_comment.php",
			{'comment': commenttext, 'commentid' : id},
			function(data){
				if(data.success == 1){
					$("#commentform"+data.commentid).hide("fast");
					$("#comments"+data.commentid).show();
					$('<li style="display:none"><span>'+stripslashes(data.commentuser)+'</span> - '+stripslashes(data.commentcontent)+'</li>').appendTo("#comments"+data.commentid).slideDown("slow");
					$("#commenttext"+data.commentid).val('Enter comment...');
					
				}
				else{
					showmessage(data.errormessage);
				}
				currentlyshowingcommentform = 0;
			},"json"
	);
}

function voteup(id){
	$.post("ajax_vote.php",
			{'id' : id},
			function(data){
				if(data.success == 1){
					$("#vote"+id).html('<span>Voted!</span>');
				}
				else{
					showmessage(data.errormessage);
				}
			},"json"
	
	);
	return false;
}

function stripslashes( str ) {
    return (str+'').replace(/\0/g, '0').replace(/\\([\\'"])/g, '$1');
}


function showmessage(message){
	if($("#message").length == 0){
		$("div.topboxcontent").prepend("<p class=\"yellow\" id=\"message\"></p>");
	}
	$("#message").html(message);
	$("#message").animate({marginLeft: '3px'},100).animate({marginLeft: '0'},100).animate({marginLeft: '3px'},100).animate({marginLeft: '0'},100).animate({marginLeft: '3px'},100).animate({marginLeft: '0'},100);
}

/*

var windowWidth = document.documentElement.clientWidth;
	var windowHeight = document.documentElement.clientHeight;
	var popupHeight = $("#message").height();
	var popupWidth = $("#message").width();
	//centering
	$("#message").css({
		"position": "absolute",
		"top": windowHeight/2-popupHeight/2,
		"left": windowWidth/2-popupWidth/2
	});
	//only need force for IE6
	
	$("#backgroundPopup").css({
		"height": windowHeight,
		"opacity": "0.7"
	});
	
	$("#message h2").text(title);
	$("#message p").text(message);

	$("#backgroundPopup").fadeIn("def");	
	$("#message").fadeIn("def");
	showingmessage = 1;
	
	*/
	
//client side form validation functions	

function checkRegisterForm(){
	
	var user = $("#user").val();
	var email = $("#email").val();
	var pass = $("#pass").val();
	var pass2 = $("#pass2").val();
	
	if(user == ""||email == ""||pass == ""||pass2 == ""){
		showmessage('Oops. Please fill out all registration fields and try again');
		return false;
	}
	
	if(pass != pass2){
		showmessage('Oops. The passwords you entered didn\'t match, please try again');
		return false;
	}
	
	//check email

	var reg = /^([A-Za-z0-9_\-\.])+\@([A-Za-z0-9_\-\.])+\.([A-Za-z]{2,4})$/;
	
	if(reg.test(email) == false) {
		showmessage('Oops. That email address doesn\'t appear to be valid, please check it and try again');
		return false;
	}
	
	
	return true;
}

function checkProfileForm(){
	var email = $("#email").val();
	var pass = $("#pass").val();
	var pass2 = $("#pass2").val();
	
	if(email == ""){
		showmessage('Oops. Please fill out all registration fields and try again');
		return false;
	}
	
	if(pass != pass2){
		showmessage('Oops. The passwords you entered didn\'t match, please try again');
		return false;
	}
	
	//check email

	var reg = /^([A-Za-z0-9_\-\.])+\@([A-Za-z0-9_\-\.])+\.([A-Za-z]{2,4})$/;
	
	if(reg.test(email) == false) {
		showmessage('Oops. That email address doesn\'t appear to be valid, please check it and try again');
		return false;
	}
	
	
	return true;
}

function checkpostform(){
	var twatter = $("#twatter").val();
	if(twatter == ""){
		showmessage('You cannot post an empty twatter.');
		return false;
	}
	
	if(twatter.length > 200){
		showmessage('Your twatter can only be a maximum of 200 characters');
		return false;
	}
	
	return true;
}

function showuploader(){
	$("#imageuploadercontainer").show();
	$("#uploadimage").attr("disabled","true").attr("value","Uploading, please wait...").width(180);;
}

//thumbnail selection code
function preview(img, selection)
{

	var scaleX = 32 / selection.width;
	var scaleY = 32 / selection.height;
	
	var imageh = $("#currentimagebig").height();
	var imagew = $("#currentimagebig").width();
	
	$('#currentimage').css({
		width: Math.round(scaleX * imagew) + 'px',
		height: Math.round(scaleY * imageh) + 'px',
		marginLeft: '-' + Math.round(scaleX * selection.x1) + 'px',
		marginTop: '-' + Math.round(scaleY * selection.y1) + 'px'
	});
	
	$('#x1').val(selection.x1);  
    $('#y1').val(selection.y1);  
    $('#x2').val(selection.x2);  
    $('#y2').val(selection.y2);  
    $('#w').val(selection.width);  
    $('#h').val(selection.height); 
}

function showthumbnailselect() {
	var bigsrc = $("#currentimagebig").attr("src");
	$("#currentimage").attr("src",bigsrc);
	var imageh = $("#currentimagebig").height();
	var imagew = $("#currentimagebig").width();
	var selsize = 32;
	if(imageh < imagew){
		selsize = imageh;
	}
	else{
		selsize = imagew;
	}
	$('#currentimagebig').imgAreaSelect({ aspectRatio: '1:1', onSelectChange: preview, minHeight: 32, minWidth : 32 , x1 : 0, y1 : 0, x2 : selsize, y2 : selsize});
	$("#thumbcontrols > a").slideUp("fast",function(){$("#thumbcontrols > div").slideDown("fast");});
	
	return false;
}

function cancelthumbnailselect(){
	$('#currentimagebig').imgAreaSelect({hide: true});
	$("#thumbcontrols > div").slideUp("fast",function(){$("#thumbcontrols > a").slideDown("fast");});	
	return false;
}



$(document).ready(function(){
	
	$(".commentform textarea").focus(function(){
		if(this.value == "Enter comment..."){
			this.value = "";
		}
	});
	
	var bigimagesrc = $("#currentimagebig").attr("src");
	$('#thumbcontainer')
	.css({
		position: 'relative',
		overflow: 'hidden',
		width: '32px',
		height: '32px'
	});
	
});
