PriceForm = Class.create();

PriceForm.prototype = {
	version: '0.1.0',

	reportError: function(request) {
		alert('Došlo k neočekávané chybě, zkuste znovu nahrát stránku.');
	},

	checkSubmit: function(event) {
		if (this.loading) {
			alert('Vyčkejte prosím na spočítání ceny a po té klikňete na tlačítko koupit.')
			Event.stop(event);
			return false;
		}
	},

	removeOptions: function(select) {
		while (select.hasChildNodes()) select.removeChild(select.firstChild);
	},

	createOptions: function(select, response) {
		eval("var arr = [" + response + "];");
		for (var i = 0; i < arr.length; i++) {
			var opt   = document.createElement('OPTION');
			opt.value = i;
			var str   = arr[i].replace('&sup2;', '\u00b2');
			var text  = document.createTextNode(str);
			opt.appendChild(text);
			select.appendChild(opt);
		}
	},

	updatePriceCompleted: function(request) {
		var prices   = eval("(" + request.responseText + ")");
		this.loading = 0;
		Element.removeClassName(this.price_field.parentNode, 'loading');
		Element.removeClassName(this.price_vat_field.parentNode, 'loading');
		this.price_field.innerHTML     = prices.price;
		this.price_vat_field.innerHTML = prices.price_vat;
		new Effect.Highlight(this.price_field.parentNode, {startcolor:'#ffffcc', endcolor:'#ffffff', restorecolor:'#ffffff'});
		new Effect.Highlight(this.price_vat_field.parentNode, {startcolor:'#ffffcc', endcolor:'#ffffff', restorecolor:'#ffffff'});
	},

	updatePrice: function() {
		this.loading = 1;
		Element.addClassName(this.price_field.parentNode, 'loading');
		Element.addClassName(this.price_vat_field.parentNode, 'loading');
		var url  = '/price';
		var pars = 'render=price_all&usage=' + this.select_usage.value + '&dimension=' + this.select_dimensions.value + '&edition=' + this.select_edition.value;
		var ajax = new Ajax.Request(
			url,
			{ method: 'get', parameters: pars, asynchronous: 'false', onComplete: this.updatePriceCompleted.bindAsEventListener(this), onFailure: this.reportError }); 
	},

	updateEditionCompleted: function(ajax) {
		this.createOptions(this.select_edition, ajax.responseText);
		Element.removeClassName(this.select_edition.parentNode, 'loading');
		new Effect.Highlight(this.select_edition.parentNode, {startcolor:'#ffffcc', endcolor:'#ffffff', restorecolor:'#ffffff'});
		this.select_edition.disabled = 0;
		this.updatePrice();
	},

	updateEdition: function() {
		this.loading = 1;
		this.removeOptions(this.select_edition);
		Element.addClassName(this.price_field.parentNode, 'loading');
		Element.addClassName(this.select_edition.parentNode, 'loading');
		this.select_edition.disabled = 1;
		var url  = '/price?rand=' + Math.round((Math.random()*9)+1);
		var pars = 'render=editions-array&usage=' + this.select_usage.value + '&dimension=' + this.select_dimensions.value + '&edition=' + this.select_edition.value;
		var ajax = new Ajax.Request(
			url,
			{ method: 'get', parameters: pars, asynchronous: 'false', onComplete: this.updateEditionCompleted.bindAsEventListener(this), onFailure: this.reportError }); 
	},

	updateDimensionsCompleted: function(ajax) {
		this.createOptions(this.select_dimensions, ajax.responseText);
		Element.removeClassName(this.select_dimensions.parentNode, 'loading');
		new Effect.Highlight(this.select_dimensions.parentNode, {startcolor:'#ffffcc', endcolor:'#ffffff', restorecolor:'#ffffff'});
		this.select_dimensions.disabled = 0;
		this.updateEdition();
	},

	updateDimensions: function() {
		this.loading = 1;
		this.removeOptions(this.select_dimensions);
		Element.addClassName(this.price_vat_field.parentNode, 'loading');
		Element.addClassName(this.price_field.parentNode, 'loading');
		Element.addClassName(this.select_edition.parentNode, 'loading');
		Element.addClassName(this.select_dimensions.parentNode, 'loading');
		this.select_dimensions.disabled = 1;
		var url  = '/price';
		var pars = 'render=dimensions-array&usage=' + this.select_usage.value + '&dimension=' + this.select_dimensions.value + '&edition=' + this.select_edition.value;
		var ajax = new Ajax.Request(
			url,
			{ method: 'get', parameters: pars, asynchronous: 'false', onComplete: this.updateDimensionsCompleted.bindAsEventListener(this), onFailure: this.reportError }); 
	},

	initialize: function(options) {
		this.select_usage = $('select-usage');

		this.select_dimensions           = document.createElement('SELECT');
		this.select_dimensions.id        = 'select-dimensions';
		this.select_dimensions.className = 'text w100';
		this.select_dimensions.name      = 'dimension';
		var td                           = $('td-dimensions');
		while (td.hasChildNodes()) td.removeChild(td.firstChild);
		td.appendChild(this.select_dimensions);

		this.select_edition           = document.createElement('SELECT');
		this.select_edition.id        = 'select-edition';
		this.select_edition.className = 'text w100';
		this.select_edition.name      = 'edition';
		var td                        = $('td-edition');
		while (td.hasChildNodes()) td.removeChild(td.firstChild);
		$('td-edition').appendChild(this.select_edition);

		this.price_field = $('price-field');
		this.price_vat_field = $('price-vat-field');
		Event.observe(this.select_usage,      'change', this.updateDimensions.bindAsEventListener(this));
		Event.observe(this.select_dimensions, 'change', this.updatePrice.bindAsEventListener(this));
		Event.observe(this.select_edition,    'change', this.updatePrice.bindAsEventListener(this));

		var price_form    = $('price-form');
		price_form.action = '/objednavka/koupit';
		Event.observe(price_form, 'submit', this.checkSubmit.bindAsEventListener(this));

		this.updateDimensions();
	}
}

ToggleElement = Class.create();

ToggleElement.prototype = {
	version: '0.0.1',

	toggle: function(event) {
		switch (this.launcher.tagName) {
			case 'A':
				if (Element.hasClassName(this.toggle_element, this.toggle_element_class)) {
					Element.removeClassName(this.toggle_element, this.toggle_element_class);
				} else {
					Element.addClassName(this.toggle_element, this.toggle_element_class);
				}
				break;
			case 'INPUT':
				if (this.launcher.checked) {
					this.toggle_element.id = this.toggle_element_class;
				}
				break;
		}
	},

	initialize: function(element) {
		this.launcher             = element;
		this.toggle_element       = $(this.launcher.className.match(/toggle-element-parent\-([a-zA-z0-9\-]+)/)[1]);
		this.toggle_element_class = this.launcher.className.match(/toggle-element-id\-([a-zA-z0-9\-]+)/)[1];
		if (this.toggle_element) {
			Event.observe(this.launcher, 'click', this.toggle.bindAsEventListener(this));
		}
	}
}

function initToggleElements() {
	var elements = document.getElementsByClassName('toggle-element');
	var myArray  = new Array();
	elements.each(function(elm, index) {
		myArray[index] = new ToggleElement(elm);
	});
	myArray.each(function(item, index) {
		item.toggle();
	});
}

function initSimplePopUps() {
	document.getElementsByClassName('simple-pop-up').each(function(anchor) {
		if (anchor.tagName == 'A') {
			anchor.target = '_blank';
		}
	});
}

function checkPrototypeSupport() {
	if ($$) {
		Element.addClassName(document.body, 'prototype');
	}
}

Event.onDOMReady(function() {
	if ($('price-form')) {
		var myPriceForm = new PriceForm();
	}
	initToggleElements();
	initSimplePopUps();
});

if (parent.frames.length > 0) {
        top.location.replace(document.location); // prevent from iframing
}