SZN.PassStrength = SZN.ClassMaker.makeClass({
	NAME: "PassStrength",
	VERSION: "1.0",
	CLASS: "class"
});

SZN.PassStrength.prototype.$constructor = function(inputs, passwordInput, againPasswordInput, chkspan, samespan, minPower) {
	this.inputs = inputs;
	this.passInput = SZN.gEl(passwordInput);
	this.againPasswordInput = SZN.gEl(againPasswordInput);
	this.samespan = SZN.gEl(samespan);
	for (var k in this.inputs) {
		if (typeof this.inputs[k] == "object") {
			SZN.Events.addListener(this.inputs[k], "keyup", this, this.stPassImage);
		}
	}

	/* posluchac na kontrolu hesla */
	SZN.Events.addListener(this.againPasswordInput, "keyup", this, this._checkSame);

	this.isTimeout = null;
	this.imgWidth = 0;
	if (arguments.length == 4) {
		this.minPower = minPower;
	} else {
		this.minPower = 40;
	}

	this.str = SZN.gEl(chkspan);

	this.wpImg = new Image();
	SZN.Events.addListener(this.wpImg, 'load', this, 'measureImage');

	this.passImage = SZN.bind(this, this.passImage);
	SZN.Events.addListener(this.passInput, 'keyup', this, this.stPassImage);
};

SZN.PassStrength.prototype.stPassImage = function(e, elm) {
	this._clearErrors("errPassword");
	if (this.isTimeout != null) {
		window.clearTimeout(this.isTimeout);
	}
	this.isTimeout = window.setTimeout(this.passImage, 500);
};

SZN.PassStrength.prototype._checkSame = function(e, elm) {
	this._clearErrors("errSamePassword");
	if (this.againPasswordInput.value != this.passInput.value) {
		this.samespan.innerHTML = 'Zadaná hesla jsou rozdílná';
		SZN.Dom.addClass(this.samespan, 'worst');
		SZN.Dom.removeClass(this.samespan, 'verystrong');
	} else {
		this.samespan.innerHTML = 'Zadaná hesla jsou  stejná';
		SZN.Dom.addClass(this.samespan, 'verystrong');
		SZN.Dom.removeClass(this.samespan, 'worst');
	}
};

SZN.PassStrength.prototype._clearErrors = function(className) {
	var errElms = SZN.Dom.getElementsByClass(className, null, "div");
	for (i = 0; i < errElms.length; i++) {
		errElms[i].parentNode.removeChild(errElms[i]);
	}
};

SZN.PassStrength.prototype.passImage = function(e, elm) {
	if (this.passInput.value.length > 5 && this.passInput.value.length < 64) {
		var wpInputTxt = this.passInput.value;
		var url = '/getPasswordPower?password=' + encodeURIComponent(wpInputTxt);
		for (var j in this.inputs) {
			if (typeof this.inputs[j] != "object") {
				url += '&'+j+'=' + encodeURIComponent(this.inputs[j]);
			} else {
				url += '&'+j+'=' + encodeURIComponent(this.inputs[j].value);
			}
		}
		url += '&r='+Math.random();
		this.wpImg.src = url;
	}

	if (this.passInput.value.length < 6) {
		if (this.passInput.value.length == 0) {
			this.str.innerHTML = 'nezadané';
			this.str.className = 'worst';
		} else {
			this.str.innerHTML = 'příliš krátké';
			this.str.className = 'worst';
		}
	} else if (this.passInput.value.length > 63) {
		this.str.innerHTML = 'hustodémonsky krutopřísné';
		this.str.className = 'worst';
	}
};

SZN.PassStrength.prototype.measureImage = function(e, elm) {
	this.imgWidth = this.wpImg.width;
	if (this.imgWidth <= 50) {
		this.str.innerHTML = 'příliš slabé';
		this.str.className = 'worst';
	} else if (this.imgWidth > 50 && this.imgWidth <= 65) {
		this.str.innerHTML = 'dobré';
		this.str.className = 'good';
	} else if (this.imgWidth > 65 && this.imgWidth <= 80) {
		this.str.innerHTML = 'silné';
		this.str.className = 'strong';
	} else if (this.imgWidth > 80 && this.imgWidth <= 101) {
		this.str.innerHTML = 'velmi silné';
		this.str.className = 'verystrong';
	}
}

