/*
	Quick Login Tool
		
	by Phill Sparks <phills@cuttlefish.com>
*/

	function quickLogin (formElement) {
		this.formElement = document.getElementById(formElement);

		this.userElement     = ccms.dom.getElementsByClassName('fm-user', 'input', this.formElement)[0];
		this.passwordElement = ccms.dom.getElementsByClassName('fm-password', 'input', this.formElement)[0];

		var self = this;
		// This gives browsers like WebKit a chance to put in their stored username / password
		window.setTimeout(function() {
			self.setup.apply(self);
		}, 1);
	}

	quickLogin.prototype.setup = function() {
		if (this.userElement.value == '' && this.passwordElement.value == '') {
			this.userElement.setAttribute('type', 'email');
			this.userElement.setAttribute('value', ccms.dom.getLabelFor(this.userElement).innerHTML);
			ccms.dom.addClass(this.userElement, 'label');
			
			// Make this feel like the password box; we need a separate box otherwise some browsers will
			// show the saved password in plain text :-/
			this.dummyElement = this.passwordElement.cloneNode(true);
			this.dummyElement.setAttribute('id', '');
			this.dummyElement.setAttribute('name', '');
			this.dummyElement.setAttribute('type', 'text');
			this.dummyElement.setAttribute('value', ccms.dom.getLabelFor(this.passwordElement).innerHTML);
			ccms.dom.addClass(this.dummyElement, 'label');
			
			this.passwordElement.parentNode.insertBefore(this.dummyElement, this.passwordElement);
			ccms.dom.hide(this.passwordElement);
		
			// Events
			ccms.event.attach(this.userElement, 'focus', this.onFocus, this, true);
			ccms.event.attach(this.userElement, 'blur', this.onBlur, this, true);
		
			ccms.event.attach(this.dummyElement, 'focus', this.onFocus, this, true);
			ccms.event.attach(this.passwordElement, 'focus', this.onFocus, this, true);
			ccms.event.attach(this.passwordElement, 'change', this.onFocus, this, true);
		}
	}

	quickLogin.prototype.onFocus = function(event) {
		var oElm = ccms.event.getTarget(event);
		if (ccms.dom.hasClass(oElm, 'fm-password')) {
			ccms.dom.hide(this.dummyElement);
			ccms.dom.show(this.passwordElement);
			
			if (event.type == 'focus')
				this.passwordElement.focus();
		}
		else {
			if (oElm.value == ccms.dom.getLabelFor(oElm).innerHTML)
				oElm.value = '';
			ccms.dom.removeClass(oElm, 'label');
		}
	}

	quickLogin.prototype.onBlur = function(event) {
		var oElm = ccms.event.getTarget(event);
		if (oElm.value == '') {
			oElm.value = ccms.dom.getLabelFor(oElm).innerHTML;
			ccms.dom.addClass(oElm, 'label');
		}
	}

