function setupchange()
{
	$("#profile a.edit").show();
	$("#profile a.edit,#private-box a.edit").live('click',function(ec){
	 ec.preventDefault();
	 var espan=$(this).siblings('span');
	 espan.html('<input type="text" maxlength="255" value="'+espan.html()+'"/>');
	 $(this).hide();
	 espan.after('<a class="save" href="/#save">(save)</a>');
	});
	$("#profile a.save,#private-box a.save").live('click',function(sc){
	 sc.preventDefault();
	 var espan=$(this).siblings('span');
	 var asave=$(this);
	 $.post('save.php',{w:espan.attr('id'),ck:loggedck,txt:espan.find('input').val(),chid:chid},function(data){
		if (data['e']=='') {asave.siblings('.error').remove();asave.remove();espan.html(data['d']).siblings('a.edit').show();}
		else {asave.after('<div class="error">'+data['e']+'</div>');}
	 },'json');
	});
  $("#main li").live('mouseover',function(){$(this).find("a.delete").show();});
  $("#main li").live('mouseout',function(){$(this).find("a.delete").hide();});
  $("#main li a.delete").live('click',function(dc){
   dc.preventDefault();
   $.post('delete.php',{w:this.id,ck:loggedck});
	 var tli=$(this).closest('li');
	 if (tli.prev().hasClass('lreco') || tli.next().hasClass('lreco')) {tli.remove();}
	 else {tli.prev().remove('.lch');tli.remove();}
  });
}

function clearchange()
{
 $("#main li,#profile .change,#private-box .change").die('mouseover');
 $("#main li,#profile .change,#private-box .change").die('mouseout');
 $("#main li a.delete,#profile a.save,#private-box a.save,#profile a.edit,#private-box a.edit").die('click');
 $("#profile a.edit,#private-box a.edit").hide();
}

function setupprivate()
{
 $.post('private.php',{ck:loggedck},function(data){
  $("#right").prepend(data);
	$("#right #private-box .change,#right #private-box a.edit").show();
	$("#right #private-box a#changepass").click(function(cp){
	 cp.preventDefault();
	 $(this).hide();
	 $("#right #private-box #passf").show();
  });
  $("#right #private-box a#savepass").click(function(sp){
	 sp.preventDefault();
	 if ($("#right #private-box #pass1").val()!=$("#right #private-box #pass2").val()) {$(this).after('<span class="error"><br/>Error: Passwords do not match.</span>');}
	 else
	 {
	  $.post('savepass.php',{w:0,ck:loggedck},function(pdata){
		 $.ajax({type:"GET",url:"http://bibliotarian.org/sha256.js",dataType:"script",cache:true,success:function(){
		  $.post('savepass.php',{w:1,p:SHA256($("#right #private-box #pass1").val()+pdata),ck:loggedck},function(){
       $("#right #private-box #passf").hide();
			 $("#right #private-box a#changepass").show();
			 $("#right #private-box #passf .error").remove();
			 $("#right #private-box #passf input").val('');
      },'text');
		 }});		 
		},'text');
	 }
	});
 },'html');
}

function clearprivate()
{
 $("#right #private-box").remove();
}

function dopageloggedin()
{
 if (username==$("#p-user").html().toLowerCase()) {
  setupchange();
	showchange(1);
	setupprivate();
 }
}

function dopageloggedout()
{
 clearchange();
 $("#profile .change").hide();
 clearprivate();
}

function showchange(w)
{
 $("#profile .change span").each(function(i){
  if ($(this).html()!='' || 1==w) {$(this).closest('.change').show();}
 });
}

$(document).ready(function(){
 showchange(0);
});
