/**
 * @param strPassword
 * @constructor
 */
function PasswordMask(strPassword)
{
	var _self=this;

	this._strPassword=strPassword;


	this._elSpan=document.createElement("span");
	this._elSpan.style.whiteSpace="nowrap";
	this._elSpan.classList.add("PasswordMask");

	this._elSpanPassword=document.createElement("span");
	this._elSpanPassword.classList.add("PasswordClearText");
	this._elSpanPassword.appendChild(document.createTextNode(strPassword));



	this._elAnchorCopyToClipboard=document.createElement("a");
	this._elAnchorCopyToClipboard.innerHTML=PasswordMask.Texts.copyToClipboard;
	this._elAnchorCopyToClipboard.href="javascript:void(0);";

	this._elAnchorCopyToClipboard.addEventListener(
		"click",
		function(event) {
			if(
				_self._strPassword.indexOf("Not authorized")>-1
				|| _self._strPassword.indexOf("!user_password_reveal_has")>-1
			)
			{
				_self.reveal();
				return;
			}

			if (window.clipboardData && window.clipboardData.setData)
			{
				window.clipboardData.setData("Text", _self._strPassword);
			}
			else
			{
				var elInput = document.createElement("input");
				elInput.style.position="fixed";
				elInput.style.top="0px";
				document.body.appendChild(elInput);
				elInput.value = _self._strPassword;
				elInput.focus();
				elInput.setSelectionRange(0, _self._strPassword.length);

				try
				{
					document.execCommand("copy");
				}
				catch(error)
				{
					console.log(error);
				}

				document.body.removeChild(elInput);
			}
		}
	);



	this._elAnchorClickToReveal=document.createElement("a");
	this._elAnchorClickToReveal.innerHTML=PasswordMask.Texts.clickToReveal;
	this._elAnchorClickToReveal.href="javascript:void(0);";

	this._elAnchorClickToReveal.addEventListener(
		"click",
		function(event){
			_self.reveal();
		}
	);


	this._elAnchorClickToReveal.style.display="inline";
	this._elSpanPassword.style.display="none";


	var elSpanSpacer=document.createElement("span");
	elSpanSpacer.innerHTML="&nbsp;|&nbsp;";


	this._elSpan.appendChild(this._elAnchorCopyToClipboard);
	this._elSpan.appendChild(elSpanSpacer);
	this._elSpan.appendChild(this._elAnchorClickToReveal);
	this._elSpan.appendChild(this._elSpanPassword);
}

PasswordMask.prototype={
	/**
	 * @protected
	 * @type {Element}
	 */
	_elSpan: null,


	/**
	 * @protected
	 * @type {Element}
	 */
	_elSpanPassword: null,


	/**
	 * @protected
	 * @type {Element}
	 */
	_elAnchorCopyToClipboard: null,


	/**
	 * @protected
	 * @type {Element}
	 */
	_elAnchorClickToReveal: null,


	/**
	 * @protected
	 * @type {String}
	 */
	_strPassword: null,


	/**
	 * @protected
	 * @type {Integer}
	 */
	_nSetTimeoutID: null,


	/**
	 * @return {HTMLElement}
	 */
	container: function()
	{
		return this._elSpan;
	},


	/**
	 * @return {undefined}
	 */
	reveal: function()
	{
		var self=this;

		this._elAnchorClickToReveal.style.display="none";
		this._elSpanPassword.style.display="inline";

		if(this._nSetTimeoutID)
		{
			clearTimeout(this._nSetTimeoutID);
		}

		this._nSetTimeoutID=setTimeout(
			function(){
				self._nSetTimeoutID=null;

				self._elAnchorClickToReveal.style.display="inline";
				self._elSpanPassword.style.display="none";
			},
			this._strPassword.length*2*1000
		);
	},


	setPassword: function(strPassword)
	{
		this._strPassword=strPassword;

		while (this._elSpanPassword.firstChild) {
			this._elSpanPassword.removeChild(this._elSpanPassword.firstChild);
		}

		this._elSpanPassword.appendChild(document.createTextNode(strPassword));
	}
};

/**
 * @static
 * @type {{copyToClipboard: string, clickToReveal: string}}
 */
PasswordMask.Texts={
	copyToClipboard: "Copy",
	clickToReveal: "Reveal"
};