function setupchange()
{
	$("#profile a.edit").show();
	$("#profile 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").live('click',function(sc){
	 sc.preventDefault();
	 var espan=$(this).siblings('span');
	 var asave=$(this);
	 $.post('/chsave.php',{w:asave.closest(".change").attr('id'),ck:loggedck,txt:espan.find('input').val(),chid:$("#profile .pch").attr('id')},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');
	});
}

function clearchange()
{
 $("#profile .change").die('mouseover');
 $("#profile .change").die('mouseout');
 $("#profile a.save,#profile a.edit").die('click');
 $("#profile a.edit").hide();
}

function dopageloggedin()
{
 if ($("#right #chreps a#r-"+username.toLowerCase()).length>0) {
  setupchange();
	showchange(1);
	$("#profile #aupload").show();
	var thischid=$("#profile .pch").attr('id');
	$.ajax({type:"GET",url:"http://bibliotarian.org/ajaxupload.js",dataType:"script",cache:true,success:function()
	 {
	  new AjaxUpload('aupload', {action: '/chupload.php',data:{chid:thischid,ck:loggedck},
		 onSubmit:function(file , ext){
                if (! (ext && /^(jpg|png|jpeg|gif)$/.test(ext))){
                        alert('Error: Invalid file type.  Only .jpg, .jpeg, .png, .gif are allowed.');
                        return false;
												}
               },
		 name:'logo-'+thischid,
		 onComplete:function(file,response){$("#profile #chlogo").html('').html('<img src="http://bibliotarian.org/uploads/'+response+'"/>');}		
		});
	 }
	});
 }
}

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);
});
