Event.addBehavior({
	'#mytags a.edittags,#yourtags a.edittags' : function(e)
	{
		this.observe('click',function(event){Event.stop(event);editTags(this);},false);
	}
});





function editTags(element)
{
	var tagStyle="tags";
	for(i=0;i<=element.ancestors().length;i++)
	{
		if(element.ancestors()[i].hasClassName('yourtags')){tagStyle="yourtags";break;}
	  if(element.ancestors()[i].hasClassName('mytags')){tagStyle="mytags";break;}
	}
	var tagedit = new Element('div',{className:'tags'});
	var editer = new Element('form',{action:'/action/editprofile.html',method:'post',enctype:'multipart/form-data',onsubmit:'return sendEditFormTags(this)',onreset:'refreshTags(this)'});
	if(tagStyle=='yourtags'){editer.setAttribute('id','edit-yourtags');}
	if(tagStyle=='mytags'){editer.setAttribute('id','edit-mytags');}
	var fieldset = new Element('fieldset');
	var input0 = new Element('textarea');
	if(tagStyle=='yourtags'){input0.setAttribute('name','required_tags');input0.setAttribute('id','required_tags');}
	if(tagStyle=='mytags'){input0.setAttribute('name','supplied_tags');input0.setAttribute('id','supplied_tags');}
	var tagContent = "";
	if(tagStyle=='yourtags'){$$("#yourtags li").each(function(item){tagContent+=item.firstChild.innerHTML;tagContent+=", "});}
	if(tagStyle=='mytags'){$$("#mytags li").each(function(item){tagContent+=item.firstChild.innerHTML;tagContent+=", "});}
	if(tagContent.endsWith(", ")){tagContent=tagContent.substring(0,tagContent.length-2)}
	tagContent.strip()
	input0.appendChild(document.createTextNode(tagContent));
	var fieldsetButton = new Element('fieldset',{className:'buttons'});
	var saveButton = new Element('button',{name:'save',type:'submit'});
	saveButton.appendChild(document.createTextNode("Save"));
	var cancelButton = new Element('button',{name:'cancel',type:'reset'});
	cancelButton.appendChild(document.createTextNode("Cancel"));	
	/* ASSEMBLE */
	fieldset.appendChild(input0);
	fieldsetButton.appendChild(saveButton);
	fieldsetButton.appendChild(cancelButton);
	editer.appendChild(fieldset);
	editer.appendChild(fieldsetButton);
	tagedit.appendChild(editer);
	if(tagStyle=='yourtags'){$$("#yourtags ul.tags")[0].replace(serializeNode(tagedit));}
	if(tagStyle=='mytags'){$$("#mytags ul.tags")[0].replace(serializeNode(tagedit));}
}

function sendEditFormTags(editForm)
{
	pars="";
	try{pars=editForm.serialize();}
	catch(e)
	{
		if(editForm.id=="edit-yourtags"){pars="required_tags="+$("required_tags").value+"&";}// THE $F() STUFF DIDNT WORK OUT WITH IE6
	  if(editForm.id=="edit-mytags"){pars="supplied_tags="+$("supplied_tags").value+"&";}
	}
	pars+="&action=update_profile";
	new Ajax.Request("/ajax/response_editprofile.xml",
	{
		method:'post',
		postBody:pars,
		onCreate:function(){ajaxCursorStart();},
		onComplete:function(){ajaxCursorEnd();},
		onSuccess:function(transport){refreshTags(editForm)}
	});
	return false;
}

function refreshTags(editForm)
{	
	if(editForm.id=='edit-yourtags'){var tagbox="yourtags";}
	if(editForm.id=='edit-mytags'){var tagbox="mytags";}
	new Ajax.Request("/ajax/response_tags.xml",
	{
		method:'post',
		postBody:"username="+$("loggedUser").innerHTML,
		onSuccess:function(transport)
		{
			if(tagbox=="mytags"){var newHTML = serializeNode(transport.responseXML.documentElement.firstChild);}
			else{var newHTML = serializeNode(transport.responseXML.documentElement.lastChild);}
			$(tagbox).replace(newHTML);
		}
	});
}


