// JavaScript Document


var oldIE = (Browser.Engine.trident && Browser.Engine.version < 5);

var PNGFix = new Class({

  initialize: function(img){
		this.img = $(img);
    this.src = this.img.get('src');
		this.img.set('src', '/img/blank.gif');
		this.img.style.filter = 'progid:DXImageTransform.Microsoft.AlphaImageLoader(src="'+this.src+'", sizingMethod="image")';
	}
	
});

var Labmark =  {
	
	init: function(){
		
		// png fix
		if (oldIE){
			$$('img[src$=png]').each(function(img){
				new PNGFix(img);
			});
		}
		
		// target=_blank replacement
		$$('._blank, div.additional-links li[class!=catalog] a').each(function(item){
			if(item.hasClass('clickable')) {
				var url = item.getElement('a').get('href');
				item.setStyle('cursor', 'pointer');
				item.getElements('a').set('href', '#');
				item.addEvent('click', function(){
						window.open(url, "_blank");
				});
			} else {
			  item.set('target', '_blank');
			}
		});
		
		// clickable blocks
		$$('.clickable').each(function(item){
			if(!item.hasClass('_blank')) {
				item.setStyle('cursor', 'pointer');
				var anchor = item.getElement('a');
				if (anchor){
					item.addEvent('click', function(){
						window.location = anchor.get('href');
					});
					anchor.addEvent('click', function(event){
						event.stopPropagation();
					});
				}
			}
		});
		
		this.anchors = $$('.anchors');
		if (this.anchors.length > 0){
			new Fx.SmoothScroll({
				links: '.anchors a',
				transition: Fx.Transitions.Quint.easeInOut,
				duration: 1000
			});
		}

		// remooz
		ReMooz.assign('.remooz, div.gallery a', {
			  'origin': 'img',
				'shadow': 'onOpen',
				'resizeFactor': 0.9,
				'cutOut': false,
				'opacityResize': 0,
				'dragging': true,
				'centered': true
		});

		/* Flash logo */
		if ($('logo')){
			new Swiff('/flash/labmark-logo-cz.swf', {
				id: 'flash-logo-labmark',
				container: 'logo',
				width: 203,
				height: 37,
				params: {
						wmode: 'transparent'
				}
			});
		}
	
	}

};

var InputValidator = new Class({

  Implements: [Options, Events],
	
  options: {
		required: false,
		tests: []
	},
	
  initialize: function(input, options){
		this.setOptions(options);
		this.input = $(input);
		this.tests = new Array(this.options.tests).flatten();
		this.errors = [];
		this.input.addEvent('blur', function(){
		  this.validate();
		}.bind(this));
		
		this.errorMark = null;
		
		// events
		this.addEvent('error', function(event){
		  this.placeErrors();
		});
		this.addEvent('success', function(){
		  this.removeErrors();
		});
		
		return this;
	},
	
	addTest: function(test, message){
		this.tests.include(test, message);
	},
	
	validate: function(){
		this.errors.empty();
		var value = this.input.value.trim();
		if (value == ''){
			if (this.options.required) this.errors.include('Toto pole je povinné.');
		} else {
			this.tests.each(function(test){
				value.test(test.regexp) ? this.errors.erase(test.message) : this.errors.include(test.message);
			}, this);
		}
		this.errors.length ? this.fireEvent('error') : this.fireEvent('success');
	},
	
	getError: function(){
		return this.errors;
	},
	
	placeErrors: function(){
	  var parent = this.input.getParent('td');
		var message = this.errors.join('<br />');
		if (this.errorMark) this.errorMark.destroy();
		this.errorMark = new Element('span',{
		  'class': 'error-mark',
			'text': message
		}).inject(parent);
		parent.addClass('error');
	},
	
	removeErrors: function(){
	  var parent = this.input.getParent('td');
		parent.removeClass('error');
		if (this.errorMark) this.errorMark.destroy();
	}
	
});

var FormOrder = {
	
	init: function(){

		this.form = $('form-order');
		
		if ($$('.form-inquiry')) {
			this.validatedItems = [
				new InputValidator('form-order-name',    { required: true }),
				new InputValidator('form-order-email',   { required: true, tests: { regexp: /[a-zA-Z0-9._-]+@[a-zA-Z0-9.-]+\.[a-zA-Z]{2,4}/, message: 'Zadaná emailová adresa je neplatná.' } }),
				new InputValidator('form-order-phone',   { required: true, tests: { regexp: /^[0-9 ]{9,12}$|^[0-9\+ ]{13,16}$/, message: 'Neplatný formát telefonního čísla.' } })
			];
		} else {
			this.validatedItems = [
				new InputValidator('form-order-name',    { required: true }),
				new InputValidator('form-order-surname',    { required: true }),
				new InputValidator('form-order-email',   { required: true, tests: { regexp: /[a-zA-Z0-9._-]+@[a-zA-Z0-9.-]+\.[a-zA-Z]{2,4}/, message: 'Zadaná emailová adresa je neplatná.' } }),
				new InputValidator('form-order-phone',   { required: true, tests: { regexp: /^[0-9 ]{9,12}$|^[0-9\+ ]{13,16}$/, message: 'Neplatný formát telefonního čísla.' } }),
				new InputValidator('form-order-street',    { required: true }),
				new InputValidator('form-order-city',    { required: true }),
				new InputValidator('form-order-psc',    { required: true })
			];
		}
	
		this.form.addEvent('submit', function(event){
		  event.stop();
		  if (this.isValid()){
				this.form.submit();
			} else {
				alert('Prosím, vyplňte správně všechna požadovaná pole.');
			}
		}.bind(this));
		
		// anti
		this.form.getElement('noscript').destroy();
		new Element('input', {
		  'type': 'hidden',
			'name': 'anti',
			'value': 'labmark'
		}).inject(this.form);

	},
	
	isValid: function(){
		var errors = 0;
		this.validatedItems.each(function(item){
			item.validate();
			errors += item.getError().length;
		});
		return !errors;
	}
	
};

window.addEvent('domready', function(){

  Labmark.init();
	
	if ($('form-order')) FormOrder.init();
	
  // aktuality	
	if ($('news-overview')) {
	
		var adresa = new URI([window.location]);
		var itemId = adresa.get('fragment');
	
		if(itemId != '') {
			$$('li.news-item').each(function(element, index){
				element.removeClass('highlight');
				if(element.id == itemId) {
					element.addClass('highlight');
				}
			});
		}	
		
		mySmoothScroll.addEvent('scrolledTo', function(){
				var adresa = new URI([window.location]);
				var itemId = adresa.get('fragment');
				
				$$('li.news-item').each(function(element, index){
					element.removeClass('highlight');
					if(element.id == itemId) {
						element.addClass('highlight');
					}
				});
		});
		
	}

});
