// JavaScript Document
var InputFieldClicker = function(inputElement, text, color)
{
	this._inputElement = inputElement;
	this._realElement = this._inputElement;
	this._text = text;
	this._color = (color!=undefined)?color:"#fec7a1";
	this._oldcolor = this._inputElement.style.color;
	this._type = this._inputElement.type;
	this.initializeElement();
	this.createEventCallbacks(this._inputElement);
}

InputFieldClicker.prototype.initializeElement = function()
{

	this._inputElement.value="";
	try
	{
		this._inputElement.type="text";	
	}
	catch(e)
	{
		this.changeTypeForBullshitBrowsersThatTryToControlAndDominatePeople("text");	
	}
	
	this._inputElement.style.color = this._color;
	this._inputElement.value=this._text;

}

InputFieldClicker.prototype.useElement = function()
{	

	if(this._inputElement.value==this._text)
		this._inputElement.value="";
	this._inputElement.style.color = this._oldcolor;
	try
	{
		this._inputElement.type=this._type;	
	}
	catch(e)
	{
		this.changeTypeForBullshitBrowsersThatTryToControlAndDominatePeople(this._type);
	}

}

InputFieldClicker.prototype.handleFocus = function()
{
	this.useElement();
}

InputFieldClicker.prototype.handleBlur = function()
{
	if(this._inputElement.value=="")
		this.initializeElement();
}

InputFieldClicker.prototype.createEventCallbacks = function(element)
{
	element.__cb = this;
	element.onfocus = function(){this.__cb.handleFocus();}
	element.onblur = function(){this.__cb.handleBlur();}
}

InputFieldClicker.prototype.removeEventCallbacks = function(element)
{
	element.__cb = this;
	element.onfocus = null;
	element.onblur = null;
}

InputFieldClicker.prototype.changeTypeForBullshitBrowsersThatTryToControlAndDominatePeople = function(type)
{
	// Remove event handling so that events dont fire during swap.
	this.removeEventCallbacks(this._inputElement);
	if(type == "text")	// Show the text field
	{
		var parentElement = this._inputElement.parentElement;
		
		if(this._fakePasswordElement == undefined)	// Create the text element
		{
			this._fakePasswordElement = document.createElement("input");
			this._fakePasswordElement.type="text";
			this._fakePasswordElement.id = this._realElement.id + "_fake";
			this._fakePasswordElement.className = this._realElement.className;
			this._fakePasswordElement_display = this._realElement.style.display;
			
			parentElement.appendChild(this._fakePasswordElement);
		}
		// copy properties and swap elements
		this._realElement.style.display="none";
		this._fakePasswordElement.text = this._realElement.text;
		this._fakePasswordElement.style.display = this._fakePasswordElement_display;
		
		// set currentElement to be the fake one
		this._inputElement = this._fakePasswordElement;
	}
	else // Show the password field
	{
		// copy properties and swap elements
		this._fakePasswordElement.style.display = "none";
		this._realElement.style.display = this._fakePasswordElement_display;
		this._realElement.focus();
		
		// set currentElement to be the real one
		this._inputElement = this._realElement;
	}
	this.createEventCallbacks(this._inputElement);
}
