/**
 * Kontrola, a vlastnosti formularovych prvku
 */

Forms = new Class({
	options: {
		className: 'tips'
	},

	initialize: function(options) {
		this.toolTip = new Element('div', {
			'id': 'tip',
			'class': this.options.className + '-tip',
			'styles': {
				'position': 'absolute',
				'top': '0',
				'left': '0',
				'visibility': 'hidden'
			}
		}).inject(document.body);
		this.slide = new Fx.Slide('tip', {mode: 'horizontal'});

		this.wrapper = new Element('div').inject(this.toolTip);

		var elements = $$('input, select, textarea');
		var errorEls = $$('.error');

		elements.forEach(function(element, index){
			element.$tmp.form = this;
			if (element.getAttribute('title')) {
				element.addEvent('focus', function() {
					this.$tmp.form.displayTitle(this);
				});
			}
		}.bind(this));

		elements.forEach(function(element, index){
			if (element.getAttribute('title')) {
				element.addEvent('blur', function() {
					this.$tmp.form.hideTitle();
				});
			}
		});

		if (errorEls.length > 0) {
			errorEls[0].focus();
			window.location="#form";
		}

		var countEls = $$('table#registrace input');
		if (countEls.length > 0) {
			countEls.forEach(function(element, index) {
				element.$tmp.form = this;
				element.addEvent('click', function() {
					this.$tmp.form.count(this);
					if ($('package_1') != null) {
						$('package_1_s').checked = false;
						$('package_1').removeClass('selected');
					}
					if ($('package_2') != null) {
						$('package_2_s').checked = false;
						$('package_2').removeClass('selected');
					}
				});
			}.bind(this));
		}


		if (formSimple == null || formSimple == false) {
			if ($('tarif_pl') != null && $('tarif_pl').checked == false) {
				$('tarif_pl_1').setAttribute('disabled', 'disabled');
				$('tarif_pl_2').setAttribute('disabled', 'disabled');
				$('tarif_pl_3').setAttribute('disabled', 'disabled');
			} else if ($('tarif_pl') != null) {
				$('tarif_pl_1').removeAttribute('disabled');
				$('tarif_pl_2').removeAttribute('disabled');
				$('tarif_pl_3').removeAttribute('disabled');
			}
		}

		if ($('package_2') != null) {
			$('package_2').setStyle('cursor', 'pointer');
			$('package_2').setStyle('_cursor', 'hand');
			$('package_2').addEvent('click', function() {
				$('package_2_s').checked = true;
				$('package_2').addClass('selected');
				if ($('package_1') != null) {
					$('package_1').removeClass('selected');
				}
				/**
				 * Musi se pozaskrtat prislusna policka
				 */
				var countEls = $$('table#registrace input');
				if (countEls.length > 0) {
					countEls.forEach(function(element, index) {
						element.checked = false;
						if (element.value == 'sk' || element.value == 'cz'
							|| element.value == 'hu') {
							element.checked = true;
						}
				 	});
				}
				czUncheck();
				$('sum').innerHTML = number_format($('package_2').getAttribute('ref').toInt(), 0, ',', ' ') + ' zł';
			});
		}


	},

	displayTitle:function(element) {
		/*
		 * Je chybovy?
		 */
		var className = element.className;
		if (className == 'error') {
			this.toolTip.addClass('error');
		} else {
			this.toolTip.removeClass('error');
		}

		this.toolTip.setStyle('visibility', 'visible');
		// this.slide.slideIn();
		this.toolTip.innerHTML = element.getAttribute('title');
		var coordinates = element.getCoordinates();
		this.toolTip.setStyle('top', coordinates.top);
		this.toolTip.setStyle('left', $('aligmentForm').getCoordinates().left + 470);
	},

	hideTitle:function() {
		this.toolTip.setStyle('visibility', 'hidden');
		// this.slide.slideOut();
	},

	count: function(element) {
		if (element.id == 'tarif_pl') {
			if (element.checked) {
				$('tarif_pl_3').checked = 'true';
			}
		}
		var countEls = $$('table#registrace input');
		var price = 0;
		countEls.forEach(function(element, index) {
			if (element.id == 'tarif_pl') {
				if (!element.checked) {
					$('tarif_pl_1').setAttribute('disabled', 'disabled');
					$('tarif_pl_1').checked = false;
					$('tarif_pl_2').setAttribute('disabled', 'disabled');
					$('tarif_pl_2').checked = false;
					$('tarif_pl_3').setAttribute('disabled', 'disabled');
					$('tarif_pl_3').checked = false;
				} else {
					$('tarif_pl_1').removeAttribute('disabled');
					$('tarif_pl_2').removeAttribute('disabled');
					$('tarif_pl_3').removeAttribute('disabled');
				}
			}

			if (element.checked) {
				price = price + element.getAttribute('ref').toInt();
			}
		}.bind(this));
		if ($('sum')) {
			$('sum').innerHTML = number_format(price, 0, ',', ' ') + ' zł';
		}
	}

});

Forms.implement(new Events, new Options);
window.addEvent('domready', function() {
	var forms = new Forms();
});


/* Made by Mathias Bynens <http://mathiasbynens.be/> */
function number_format(a, b, c, d) {
 a = Math.round(a * Math.pow(10, b)) / Math.pow(10, b);
 e = a + '';
 f = e.split('.');
 if (!f[0]) {
  f[0] = '0';
 }
 if (!f[1]) {
  f[1] = '';
 }
 if (f[1].length < b) {
  g = f[1];
  for (i=f[1].length + 1; i <= b; i++) {
   g += '0';
  }
  f[1] = g;
 }
 if(d != '' && f[0].length > 3) {
  h = f[0];
  f[0] = '';
  for(j = 3; j < h.length; j+=3) {
   i = h.slice(h.length - j, h.length - j + 3);
   f[0] = d + i +  f[0] + '';
  }
  j = h.substr(0, (h.length % 3 == 0) ? 3 : (h.length % 3));
  f[0] = j + f[0];
 }
 c = (b <= 0) ? '' : c;
 return f[0] + c + f[1];
}

function czUncheck() {
	element = $('tarif_pl');
	if (element != null) {
		if (!element.checked) {
			$('tarif_pl_1').setAttribute('disabled', 'disabled');
			$('tarif_pl_1').checked = false;
			$('tarif_pl_2').setAttribute('disabled', 'disabled');
			$('tarif_pl_2').checked = false;
			$('tarif_pl_3').setAttribute('disabled', 'disabled');
			$('tarif_pl_3').checked = false;
		} else {
			$('tarif_pl_1').removeAttribute('disabled');
			$('tarif_pl_2').removeAttribute('disabled');
			$('tarif_pl_3').removeAttribute('disabled');
		}
	}
}

