﻿var OverlayFix = new Class({

	initialize: function(el) {
		this.element = $(el);
		if (window.ie){
			this.element.addEvent('trash', this.destroy.bind(this));
			this.fix = new Element('iframe', {
				properties: {
					frameborder: '0',
					scrolling: 'no',
					src: 'javascript:false;'
				},
				styles: {
					position: 'absolute',
					border: 'none',
					display: 'none',
					filter: 'progid:DXImageTransform.Microsoft.Alpha(opacity=0)'
				}
			}).injectAfter(this.element);
		}
	},

	show: function() {
		if (this.fix) this.fix.setStyles($extend(
			this.element.getCoordinates(), {
				display: '',
				zIndex: (this.element.getStyle('zIndex') || 1) - 1
			}));
		return this;
	},

	hide: function() {
		if (this.fix) this.fix.setStyle('display', 'none');
		return this;
	},

	destroy: function() {
		this.fix.remove();
	}

});

window.addEvent('domready', function() {

	$$('.accordion').each(function(accordion) {
		var togglers = accordion.getElements('.accordion-handle');
		var wrappers = accordion.getElements('.wrapper');
		var elements = accordion.getElements('.accordion-content');
		var last        = elements.getLast();
		var lastToggler = togglers.getLast();
		var box         = accordion;
		var boxBottom   = $ES('.box-bottom, .box-bottom-title', box)[0];
		
		var height = 0;
		elements.each(function(el) {
			if (el.offsetHeight > height) {
				height = el.offsetHeight;
			}
		});
		elements.each(function(el) {
			el.setStyle('height', height);
		});
		
		var initial = 0;
		var i = 0;
		wrappers.each(function(el) {
			if (el.hasClass('active')) {
				initial = i;
			}
			
			i++;
		});
		
		var options = {
			show: initial,
			link: 'cancel',
			opacity: !window.ie6,
			fixedHeight: elements[0].offsetHeight,
			onActive: function(toggler, element) {
				if (element == last) {
					box.removeClass('title-bottom');
				}
			},
			onComplete: function() {
				if (!last.offsetHeight) {
					box.addClass('title-bottom');
				}
			}
		};
		var myAccordion = new Accordion(togglers, elements, options);
		
		var offset = boxBottom.getPosition().y - $('header').offsetHeight;
		boxBottom.setStyle('background', '#ccc url("/fileadmin/img/body.png") scroll repeat-x 0 -'+offset+'px');
		boxBottom.setStyle('bottom', 0);
		
		if (togglers.length > 1) {
			box.addClass('title-bottom');
		}
		
		box.setStyle('height', box.offsetHeight + (window.ie6 && box.offsetHeight%2 ? 1:0));
	});
	

	var enterTimer = null;
	var leaveTimer = null;
	var overlay = $('quicknav_content_overlay');
	var div = overlay.getElement('div');
	overlay.setStyle('display', 'block');
	var quicknav_l = $ES('.quicknav_l', overlay)[0];
	var quicknav_r = $ES('.quicknav_r', overlay)[0];
	
	quicknav_l.setStyle('visibility', 'hidden');
	quicknav_r.setStyle('visibility', 'hidden');
		
	var fx = new Fx.Slide(div, {onStart: function() {
		quicknav_l.setStyle('visibility', 'visible');
		quicknav_r.setStyle('visibility', 'visible');
	}, onComplete: function() {
		if (overlay.offsetHeight<20) {
			quicknav_l.setStyle('visibility', 'hidden');
			quicknav_r.setStyle('visibility', 'hidden');
		}
	}});
	fx.hide();
	$('quicknav_content').addEvent('mouseenter', function() {
		$clear(leaveTimer);
		fx.stop();
		enterTimer = (function() {
			// IE8 Fix
			fx.slideIn();
			if (div.offsetHeight < 50) {
				// IE8 Fix
				(function() {
					fx.stop();
					fx.slideIn();
				}).delay(100);
			}
		}).delay(300);
	});
	
	$('quicknav_content').addEvent('mouseleave', function() {
		$clear(enterTimer);
		leaveTimer = fx.slideOut.delay(700, fx);
	});
	
	/* Tipps für ein sicheres Passwort */
	var hintElement = $('password-hints');
	
	if (hintElement) {
		var overlayFix = new OverlayFix(hintElement);
		
		hintElement.addEvent('mouseleave', function() {
			hintElement.style.display = 'none';
			overlayFix.hide();
		});
		
		$$('.password-hints-link').each(function(hintLink) {
			hintLink.addEvent('click', function(event) {
				var event = new Event(event);
				
				if (hintElement.style.display == 'block') {
					hintElement.style.display = 'none';
					overlayFix.hide();
				} else {
					hintElement.style.left = event.page.x-20 + 'px';
					hintElement.style.top = event.page.y-20 + 'px';
					hintElement.style.display = 'block';
					overlayFix.show();
				}
			});
		});
	}
});

String.prototype.reverse = function() {
	return this.split("").reverse().join("");
};

function getPasswordStrength(pwd) {
	
	var quality = 0;
	var length = pwd.length;
	var lower = pwd.toLowerCase();
	
	var lowercase = false;
	var uppercase = false;
	var numeric = false;
	var special = false;
	
	var usedChars = $A([]);
	var i = 0;
	
	for(i = 0; i < length; i++) {
		var ord = pwd.charCodeAt(i);
		
		if (ord >= 48 && ord <= 57) numeric = true;
		else if (ord >= 65 && ord <= 90) uppercase = true;
		else if (ord >= 97 && ord <= 122) lowercase = true;
		else if (ord >= 32 && ord <= 126) special = true;
		else if (ord >= 128) special = true;
		
		if (ord >= 48 && ord <= 57) quality += 8; // bonus for numeric
		else if (ord >= 65 && ord <= 90) quality += 8; // bonus for uppercase
		else if (ord >= 97 && ord <= 122) quality += 0; // nothing for lowercase
		else if (ord >= 32 && ord <= 126) quality += 15; // bonus for special
		if (!usedChars.contains(ord)) usedChars.push(ord);
	}
	quality += usedChars.length * 6;
	
	quality += length * 4;
			
	if (quality > 100) quality = 100;
	
	if (!lowercase) quality *= 0.8;
	if (!uppercase) quality *= 0.7;
	if (!numeric) quality *= 0.8;
	if (!special) quality *= 0.8;
	
	for (i = 0; i <= length - 3; i++) {
		var str = lower.substr(i, 3);
		if (str.charAt(0) == str.charAt(1) && str.charAt(1) == str.charAt(2)) {
			quality *= 0.9;
			break;
		}
	}
	
	var sequences = [
		'abcdefghijklmnopqrstuvwxyz',
		'1234567890',
		'!"ß$%&/()=?',
		'qwertzuiop¸+',
		'asdfghjklˆ‰#',
		'<yxcvbnm,.-', ';:_',
		'147', '258', '369', '741', '852', '963', '159', '357', '753', '951'
	];
	
	for (i = 0; i <= length - 3; i++) {
		var str = lower.substr(i, 3);
		for (var j = 0; j < sequences.length; j++) {
			var sequence = sequences[j];
			if (sequence.indexOf(str) != -1) {
				quality *= 0.7;
				break;
			} else if (sequence.reverse().indexOf(str) != -1) {
				quality *= 0.7;
				break;
			}
		}
	}
	
	return quality;
}


function checkPasswordStrength(sourceElement, repeatElement, submitButton) {
	sourceElement = $(sourceElement);
	repeatElement = $(repeatElement);
	submitButton = $(submitButton);
	
	var pwd = sourceElement.value;
	var pwd2 = repeatElement.value;
	
	var indicatorCount = $$('div.password-strength span.indicator').length;
	var indicateTo = Math.ceil(getPasswordStrength(pwd) / 100 * indicatorCount);
	
	if (sourceElement.old && sourceElement.old == pwd) {
		indicateTo = 0;
	}
	
	for (var i = 0; i < indicatorCount; i++) {
		var indicator = $$('#indicator-'+i);
		if (i < indicateTo) {
			indicator.addClass('active indicator-active');
		} else {
			indicator.removeClass('active indicator-active');
		}
	}
	
	var minlength = 8;
	var seqmaxlength = 3;
	var result = 'none';
	
	var lowercase = false;
	var uppercase = false;
	var numeric = false;
	var special = false;
	
	for (i = 0; i < pwd.length; i++) {
		var ord = pwd.charCodeAt(i);
		
		if (ord >= 48 && ord <= 57) numeric = true;
		else if (ord >= 65 && ord <= 90) uppercase = true;
		else if (ord >= 97 && ord <= 122) lowercase = true;
		else if ((ord >= 32 && ord <= 126) || ord >= 128) special = true;
	}
	
	if (sourceElement.old && sourceElement.old == pwd) result = 'none';
	else if (pwd.length == 0) result = 'none';
	else if (pwd.length < minlength) result = 'minlength';
	else if (!numeric) result = 'numeric';
	else if (!uppercase) result = 'uppercase';
	else if (!lowercase) result = 'lowercase';
	else if (!special) result = 'special';
	else if (pwd != pwd2) result = 'match';
	
	$$('.password-notice').each(function(e) {
		e.style.display = 'none';
	});
	$('password-quality-'+result).style.display = 'block';
	
	if (result != 'none') {
		submitButton.disabled = true;
		submitButton.addClass('disabled');
	} else {
		submitButton.disabled = false;
		submitButton.removeClass('disabled');
	}
}
