$(document).ready(function(){

var usernm =	$('#usernm').text();
var lim =	18;
var thispg =	$('#thispage').text();
thispg =	thispg * 1;
//must set lim same as limit in cp_friends.php

	//friend check
	$.post("modules/backend.php",{ "friend":"check", "friendtocheck": usernm },
		function(data){
			if(data == 'requested'){
				var friendstring = '<a href="controlpanel.php?mode=friends" style="font-size:7pt;">Friend requested</a>';
				$('#friendreq').html(friendstring);
			}else if(data == 'confirmed'){
				var friendstring = '<a href="controlpanel.php?mode=friends" style="font-size:7pt;">Friend confirmed</a>';
				$('#friendreq').html(friendstring);
			}
		});


	/*//friend requests
	var ts = Math.round(new Date().getTime() / 1000);
	$.get("modules/backend.php", {"friendreq": "display", "limit": "6", "page": thispg, "hash": ts } , 
	function(data){
		$('#friendrequests').html(data);
	});*/
	

	//friends pagination
	var ts = Math.round(new Date().getTime() / 1000);
	$.get("modules/backend.php", {"friends": "paginate", "limit": lim, "page": thispg, "hash": ts } , 
	function(data){
		$('#friendspaginate').html(data);
	});


	//wait window
	$('#wait').ajaxStart(function() {
	    $(this).show();
	}).ajaxComplete(function() {
	    $(this).hide();
	});

	$('#pagesubmit').submit(function(){
		$('#wait').show();
	});

	$('#addfile').live("click", function(e){
		e.stopPropagation();
		var newEntryRow = $('#fileform').clone();
		$('#filesdiv').append('<hr>');
		newEntryRow.appendTo('#filesdiv');
		return false;
	});

	$('#shoutsubmit', $('#shoutform')[0]).live("submit", function(e){
		e.stopPropagation();
		var shouttext	=	$('#shout').val();
		$.post("modules/backend.php",{ "shout":"submit", "shouttext": shouttext, "path": usernm },
		function(data){
			$('#shouts').append(data);
			$('#shouts').show();
		});
		return false;
	});
	
	$('#shoutdelete', $('#shouts')[0]).live("submit", function(e){
		e.stopPropagation();
		shoutid		=	$(this).find('#deleteid').text();
		$.post("modules/backend.php",{ "shout":"delete", "shouttodelete": shoutid },
		function(data){
			//var deletediv	=	'shout' + data;
			$(data).remove();
			var shoutcheck	=	$('#shouts').html();
			if(shoutcheck == '')	{$('#shouts').hide();}
		});
		return false;
	});
	
	$('#shoutedit', $('#shouts')[0]).live("submit", function(e){
		e.stopPropagation();
		var shoutid	=	$(this).find('#editid').text();
		var newid	=	'#shout' + shoutid;
		var theText	=	$(newid).find('#theshout').text();
		var newform	=	'<form id="submitedit" method="post" action="#" style="margin:0;padding:0;width:72%;"><textarea cols="40" rows="6" id="edittext">';
		newform		+=	theText;
		newform		+=	'</textarea><div id="editing" style="display:none;">'+shoutid+'</div><input type="submit" value="submit"></form>';
		$(newid).find('#theshout').html(newform);
		$(newid).focus();
		return false;
	});

	$('#submitedit', $('#shouts')[0]).live("submit", function(e){
		e.stopPropagation();
		var shoutid	=	$(this).find('#editing').text();
		var newid	=	'#shout' + shoutid;
		var newtext	=	$(newid).find('#edittext').text();
		$.post("modules/backend.php",{ "shout":"edit", "shouttoedit": shoutid, "newtext": newtext },
		function(data){
			$('body').append(data);
			var returndata	=	$('#returndata').text();
			var hideid	=	$('#hideid').text();
			var newid	=	'#shout'+hideid;
			$(newid).find('#submitedit').replaceWith(returndata);
			$('#returndata').remove();
			$('#hideid').remove();
		});
		return false;
	});

	$('#friendconfirm').live("submit", function(e){
		e.stopPropagation();
		var friendname	=	$(this).find('#confirmname').text();
		$.post("modules/backend.php",{ "friend":"confirm", "friendtoconfirm": friendname },
		function(data){
			var thispage =	$('#thispage').text();
			var ts = Math.round(new Date().getTime() / 1000);
			if(data != ''){
				$.get("modules/backend.php", {"friendreq": "display", "hash": ts } , 
				function(data){
					$('#friendrequests').html(data);
				});
				
				$.get("modules/backend.php", {"friends": "display", "limit": lim, "page": thispage, "hash": ts } , 
				function(data){
					$('#friends').html(data);
				});
			}
			var friendcheck	=	$('#friendrequests').text();
			if(friendcheck == '')	{
				$.get("modules/backend.php", {"friendreq": "display", "hash": ts } , 
				function(data){
					$('#friendrequests').html(data);
				});
				var friendcheck =	$('#friendrequests').text();
				if(friendcheck == '')	{$('#friendrequests').hide();}
			}//friendcheck
		});
		return false;
	});//friendconfirm

	$('#frienddelete').live("submit", function(e){
		e.stopPropagation();
		var friendid	=	$(this).find('#deleteid').text();
		$.post("modules/backend.php",{ "friend":"delete", "friendtodelete": friendid },
		function(data){
			var thispage =	$('#thispage').text();
			var ts = Math.round(new Date().getTime() / 1000);
			if(data != ''){
				$.get("modules/backend.php", {"friendreq": "display", "hash": ts } , 
				function(data){
					$('#friendrequests').html(data);
				});

				$.get("modules/backend.php", {"friends": "display", "limit": lim, "page": thispage, "hash": ts } , 
				function(data){
					$('#friends').html(data);
				});
			}
			var friendcheck	=	$('#friends').text();
			if(friendcheck == '')	{
				if((thispage-1)<1)	{thispage =	1;}
					else		{thispage =	thispage-1;}
				$.get("modules/backend.php", {"friends": "display", "limit": lim, "page": thispage, "hash": ts } , 
				function(data){
					$('#friends').html(data);
				});
				var friendcheck	=	$('#friends').text();
				if(friendcheck == '')	{$('#friends').text('No friends to display.');}
			}//friendcheck
			var friendcheck	=	$('#friendrequests').text();
			if(friendcheck == '')	{
				$.get("modules/backend.php", {"friendreq": "display", "hash": ts } , 
				function(data){
					$('#friendrequests').html(data);
				});
				var friendcheck =	$('#friendrequests').text();
				if(friendcheck == '')	{$('#friendrequests').hide();}
			}//friendcheck
		});
		return false;
	});

	$('#friendrequest').click(function(e){
		e.stopPropagation();
		$.post("modules/backend.php",{ "friend":"add", "friendtoadd": usernm },
		function(data){
			$('#friendreq').html(data);
		});
		return false;
	});

	$('#friendback').live("click", function(e){
		e.stopPropagation();
		var thispage =	$('#thispage').text();
		thispage = thispage * 1;
		var ts = Math.round(new Date().getTime() / 1000);
		if((thispage-1)<1)	{backpage =	1;}
			else		{backpage =	thispage-1;}

		$.get("modules/backend.php", {"friends": "display", "limit": lim, "page": backpage, "hash": ts } , 
		function(data){
			$('#friends').html(data);
		});

		$('#thispage').text(backpage);
		return false;
	});

	$('.friendpg').live("click", function(e){
		e.stopPropagation();
		var linkpage	=	$(this).text();
		var ts		= 	Math.round(new Date().getTime() / 1000);

		$.get("modules/backend.php", {"friends": "display", "limit": lim, "page": linkpage, "hash": ts } , 
		function(data){
			$('#friends').html(data);
		});

		$('#thispage').text(linkpage);
		//$('#thispage').show();
		return false;
	});

	$('#friendnext').live("click", function(e){
		e.stopPropagation();
		var thispage =	$('#thispage').text();
		thispage =	thispage * 1;
		var nextpage =	thispage + 1;
		var timestr = Math.round(new Date().getTime() / 1000);

		$.get("modules/backend.php", {"friends": "display", "limit": lim, "page": nextpage, "hash": timestr } , 
		function(data){
			$('#friends').html(data);
		});

		$('#thispage').text(nextpage);
		return false;
	});

});

function show_div(whichdiv){
if(document.getElementById(whichdiv).style.display == 'none'){
      document.getElementById(whichdiv).style.display = 'block';
    }else{
      document.getElementById(whichdiv).style.display = 'none';
    }
}

function clickclear(thisfield, defaulttext) {
if (thisfield.value == defaulttext) {
thisfield.value = "";
}
}

function clickrecall(thisfield, defaulttext) {
if (thisfield.value == "") {
thisfield.value = defaulttext;
}
}
