/* generates a registration form */
var OverlaidConnection =
{
	archetype:	null,
	button_style:'overlaid-connection',
	id: 'div_overlaid_connection'	//to avoid conflicts
};
OverlaidConnection.show=function()
{
	// positioning
	this.position();
	
	// cancel buttons
	this.add_cancels();
	
	var submit=$$('#'+$(this.id).identify()+' input[type="submit"]')[0];
	
	//sync data over
	$$('#'+$(this.id).identify()+' #display_name')[0].update(this.archetype.elements['display_name'].value);
	if ($$('#'+$(this.id).identify()+' #display_name_unprotected')[0]) {
		$$('#'+$(this.id).identify()+' #display_name_unprotected')[0].update(this.archetype.elements['display_name'].value);
	}
	if (this.archetype.elements['pronoun']) {
		$$('#'+$(this.id).identify()+' #pronoun')[0].update(this.archetype.elements['pronoun'].value);
	}
	if (this.archetype.elements['privacy_status']) {
		if (this.archetype.elements['privacy_status'].value == '0') { 
			if ($$('#'+$(this.id).identify()+' #protected')[0]) { $$('#'+$(this.id).identify()+' #protected')[0].hide(); }
		} else {
			if ($$('#'+$(this.id).identify()+' #unprotected')[0]) { $$('#'+$(this.id).identify()+' #unprotected')[0].hide(); }
		}
	}
	if (this.archetype.elements['profile_image']) {
		$$('#'+$(this.id).identify()+' #connection_profile')[0].src = this.archetype.elements['profile_image'].value;
	}
	$$('#'+$(this.id).identify()+' form')[0].action += this.archetype.elements['unique_display_name'].value;
	
	// add the submit handler
	Event.observe(submit,"click",function(evt){
		
		var e=Event.element(evt);
		var form=e.form;
		
		new Ajax.Request(form.action,{
			method:form.action,
			parameters:form.serialize()
		});
	});
};
OverlaidConnection.process=function(item)
{
	Event.observe(item,"click",function(){
		
		Overlay.show(this);
		this.archetype=$(item.readAttribute('rel'));

		new Ajax.Request(this.archetype.action+"/"+this.id,{
			method:this.archetype.method,
			parameters:this.archetype.serialize(),
			onComplete:this.show.bind(this)
		});
	}.bind(this));
};
/* extend */
Massify.extend(OverlaidConnection,Overlaid);
/*
*/
Event.observe(window,"load",function()
{
	$$('.'+OverlaidConnection.button_style).each(OverlaidConnection.process.bind(OverlaidConnection));	
});