Event.addBehavior({
	'#greeting .title .headline span.empty' : function(e)
	{
		if(!$(this.parentNode.parentNode.parentNode.parentNode).hasClassName("mygreeting"))
		{
		    this.observe('mouseover', function(event){this.setStyle({textDecoration:'underline'});});
		    this.observe('mouseout', function(event){this.setStyle({textDecoration:'none'});});
		    this.observe('click', function(event){location.href=location.protocol+"//"+location.host+"/action/mail.html?e_action=compose&username="+$$(".profile")[0].readAttribute("id");});
		}
	},
	'#greeting .content .empty span,#greeting .content .empty p span' : function(e)
	{
		if(!$(this.parentNode.parentNode.parentNode.parentNode).hasClassName("mygreeting"))
		{
		    this.observe('mouseover', function(event){this.setStyle({textDecoration:'underline'});});
		    this.observe('mouseout', function(event){this.setStyle({textDecoration:'none'});});
		    this.observe('click', function(event){location.href=location.protocol+"//"+location.host+"/action/mail.html?e_action=compose&username="+$$(".profile")[0].readAttribute("id");});
		}
	},
    '#greeting.mygreeting .title .headline span' : function(e)
	{
		this.observe('mouseover', function(event){this.setStyle({background:"#FF9"});});
		this.observe('mouseout', function(event){this.setStyle({background:"none"});});
		this.observe('click', function(event){greetingEdit(this)});
	},
	'#greeting.mygreeting .content .description span,.mygreeting .content .description p span' : function(e)
	{
		this.observe('mouseover', function(event){this.setStyle({background:"#FF9"});});
		this.observe('mouseout', function(event){this.setStyle({background:"none"});});
		this.observe('click', function(event){greetingEdit(this)});
	},
	'#greeting.mygreeting .content .description textarea' : function(e)
	{
		if(Prototype.Browser.Gecko){this.setAttribute("oninput","countUpdate(this)")}
		else
		{
			//if(Prototype.Browser.IE){this.setAttribute("onpaste","countUpdate(this)")}
			this.observe("keyup", function(event){countUpdate(this)});
		}
	}
});

function countUpdate(textField)
{
	var newCount=2000-$F(textField).length;
	if(newCount>0){$("fld-count").value=newCount;}
	else{$("fld-count").value=0;$(textField).value=$F(textField).substring(0,1999);}
}

function greetingEdit(element)
{
	var field = $w(element.parentNode.className)[0];
	var editer = new Element('form',{action:'/action/editprofile.html',method:'post',id:field+'-edit',className:field+'-edit',onsubmit:'return sendEditFormGreeting(this)',onreset:'refreshGreeting()'});
	if(field=="headline")
	{
		var editField = new Element('input',{type:'text',id:'fld-headline',name:'headline'});
		if(!element.hasClassName('empty')){editField.setAttribute('value',element.firstChild.nodeValue);}
		editer.appendChild(editField);
	}
	if(field=="description")
	{
		var editField = new Element('textarea',{id:'fld-description',name:'description',cols:'50',rows:'100'});
		if($(element.parentNode).hasClassName('empty')){editField.appendChild(document.createTextNode(""));}
		else{editField.appendChild(document.createTextNode(element.childNodes[0].nodeValue));}
		editer.appendChild(editField);
		var editFieldCount = new Element('input',{id:'fld-count'});
		var charCount=2000;
		if(!$(element.parentNode).hasClassName('empty')){charCount=2000-element.childNodes[0].nodeValue.length;}
		editFieldCount.setAttribute("value",charCount);
		editer.appendChild(editFieldCount);
	}
	var actionField = new Element('input',{type:'hidden',name:'action',value:'update_profile'});
	var saveButton = new Element('input',{type:'submit',name:'Submit',value:'submit',className:'submit'});
	//saveButton.appendChild(document.createTextNode("Save"));
	var cancelButton = new Element('input',{type:'reset',name:'Cancel',value:'cancel',className:'cancel'});
	//cancelButton.appendChild(document.createTextNode("Cancel"));
	
	editer.appendChild(actionField);
	editer.appendChild(saveButton);
	editer.appendChild(cancelButton);
	//alert(serializeNode(editer));
	new Insertion.After(element,serializeNode(editer));//element.appendChild(editer);
	element.remove();
	//alert($F("fld-description").length)
}

function sendEditFormGreeting(editForm)
{
	url="/ajax/response_editprofile.xml";//"/ajax/response_editprofile.xml";//editForm.attributes.action.nodeValue;
	pars="";
	try{pars=editForm.serialize();}
	catch(e)
	{
		if($("fld-headline")){pars+="headline="+encodeURIComponent($F("fld-headline"))+"&";}//$("fld-headline").value
		if($("fld-description")){pars+="description="+encodeURIComponent($F("fld-description"))+"&";}//$("fld-description").value
		pars+="action=update_profile"
	}
	new Ajax.Request(url,
	{
		method:'post',
		postBody:pars,
		onCreate:function(){ajaxCursorStart();},
		onComplete:function(){ajaxCursorEnd();},
		onSuccess:function(transport){refreshGreeting();}
	});
	return false;
}

function refreshGreeting()
{
	new Ajax.Request("/ajax/response_greeting.xml",
	{
		method:'post',
		postBody:"username="+$("loggedUser").innerHTML,
		onSuccess:function(transport){$("greeting").replace(serializeNode(transport.responseXML.documentElement.firstChild));}
	});
}
