/* generates a registration form */
var OverlaidLogin =
{
	archetype:	null,
	button_style:'overlaid-login',
	id: 'div-overlaid-login',	//to avoid conflicts
	key_bfx: null
};
OverlaidLogin.returnkeypressed=function(){
	//simulate submit click for IE
	if (Prototype.Browser.IE) {
		$$('#'+$(this.id).identify()+' input[type="submit"]').each(function(item) {
			item.click();
		});
	}
};
OverlaidLogin.show=function(obj,useRedirectTitle)
{
	if (useRedirectTitle) {
		$$('#'+$(this.id).identify()+' #hdr_login_title')[0].update('Login to Continue');
	} else {
		$$('#'+$(this.id).identify()+' #hdr_login_title')[0].update('Login');
	}
	
	// positioning
	this.position();
	
	// cancel buttons
	this.add_cancels();
	
	//sign up link
	var signup=$$('#'+$(this.id).identify()+' .overlay-register-link')[0];
	if (signup) {
		Event.observe(signup,"click",function(){
			OverlaidRegister.autoShow();
		});
	}

	this.sync_fields(this.archetype,$(this.id).down(),'refer');
	
	$$('#'+$(this.id).identify()+' input[type="submit"]').each(function(item) {
		// add the submit handler
		Event.observe(item,"click",function(evt){
			
			var e=Event.element(evt);
			var form=e.form;
			
			new Ajax.Request(form.action,{
				method:form.method,
				parameters:form.serialize()
			});
		});
	});
	
	try { pageTracker._trackPageview('/login1'); } catch(e) { }
};
OverlaidLogin.process=function(item)
{
	this.archetype=$(item.readAttribute('rel'));	//so autoShow has it set
	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));
};
OverlaidLogin.processAll=function()
{
	$$('.'+OverlaidLogin.button_style).each(OverlaidLogin.process.bind(OverlaidLogin));
};
OverlaidLogin.autoShow=function()
{
	Overlay.show(this);
	new Ajax.Request(this.archetype.action+"/"+this.id,{
		method:this.archetype.method,
		parameters:this.archetype.serialize(),
		onComplete:this.show.bind(this, true)
	});
};
OverlaidLogin.ShowForgotPass=function()
{
	Effect.SlideUp('divLogin1', { duration: 0.4 });
	Effect.SlideDown('divLogin2', { duration: 0.4 });
	$$('#divLogin2 div').each(function(item) {	//IE7 bugfix SXSW-550
		item.setStyle({width: '100%'});
	});
	
	$$('#'+$(this.id).identify()+' input[type="text"]').each(function(item) {
		item.value = '';
	});
	$$('#'+$(this.id).identify()+' input[type="password"]').each(function(item) {
		item.value = '';
	});
	$$('#'+$(this.id).identify()+' .login_message').each(function(item) {
		item.update();
	});
	$$('#'+$(this.id).identify()+' .login_message_2').each(function(item) {
		item.update();
	});
};
OverlaidLogin.ShowLogin=function()
{
	Effect.SlideUp('divLogin2', { duration: 0.4 });
	Effect.SlideDown('divLogin1', { duration: 0.4 });
	$$('#'+$(this.id).identify()+' input[type="text"]').each(function(item) {
		item.value = '';
	});
	$$('#'+$(this.id).identify()+' input[type="password"]').each(function(item) {
		item.value = '';
	});
	$$('#'+$(this.id).identify()+' .login_message').each(function(item) {
		item.update();
	});
	$$('#'+$(this.id).identify()+' .login_message_2').each(function(item) {
		item.update();
	});
};
/* extend */
Massify.extend(OverlaidLogin,Overlaid);
/*
*/
Event.observe(window,"load",function()
{
	OverlaidLogin.processAll();
	//auto show the first one based on QS
	if (document.location.search.indexOf('?login') >-1 || document.location.search.indexOf('&login') >-1) {
		OverlaidLogin.autoShow();
	}
});