// JavaScript Document
var IE6fixes = {
	
	  init: function(){
			
			// menu hover 
			$$('#menu > li').each(function(element){
				element.addEvents({
				  'mouseenter': function(){
						this.addClass('hover');
					},
					'mouseleave': function(){
						this.removeClass('hover');
					}
				});
			});			
		}
		
};
// Basket popup
BasketPopup = new Class({
	
	Implements: [Options],

  options: {
		basketUrl: '/kosik/',
		updateUrl: '/kosik/upravit',
		addUrl: '/kosik/pridat'
	},

  initialize: function(options){
		
		this.setOptions(options);
		
		// build popup
		this.container = new Element('div', {
			'id' : 'giant-basket-popup',
			'html' : '<h3>Přidáno do košíku</h3><p>V košíku nyní máte <span class="basket-popup-count">1</span> ks artiklu <strong class="basket-popup-product"></strong></p><p>Zde můžete upravit jeho množství: <input type="text" name="count" class="input" value="1" /> ks</p><a href="#" class="button continue">Pokračovat v nákupu</a><a href="' + this.options.basketUrl + '" class="button basket">Nákupní košík</a><a href="#" id="giant-basket-popup-close"></a>'
		});
		this.container.inject('page-wrapper');

		this.itemID = 0;
		
		this.title = this.container.getElement('strong.basket-popup-product');
		this.count = this.container.getElement('span.basket-popup-count');
		this.input = this.container.getElement('input[name=count]');
		this.position = { left: 0, top: 0 }
		this.text = '';
		
		// ajax request
		this.requestUpdate = new Request.JSON({
		  url: this.options.updateUrl,
			onComplete: function(jsonObj){
				if (jsonObj && $chk(jsonObj.count)){
					this.count.set('text', jsonObj.count);
					this.input.set('value', jsonObj.count);
					this.title.set('text', this.text);
					this.container.setStyles(this.position);
					this.showFx.start(1);
					if (jsonObj.all) Basket.update(jsonObj.all);
				}
			}.bind(this),
			link: 'cancel'
		});
		
		// ajax request
		this.requestAdd = new Request.JSON({
		  url: this.options.addUrl,
			onComplete: function(jsonObj){
				if (jsonObj && $chk(jsonObj.count)){
					this.count.set('text', jsonObj.count);
					this.input.set('value', jsonObj.count);
					this.title.set('text', this.text);
					this.container.setStyles(this.position);
					this.showFx.start(1);
					if (jsonObj.all) Basket.update(jsonObj.all);
				}
			}.bind(this),
			link: 'cancel'
		});
		
		// open/close fx
		this.showFx = new Fx.Tween(this.container, { property: 'opacity', duration: 200, link: 'cancel' });
		this.showFx.set(0);
		
		// close event
		this.buttonContinue = this.container.getElement('a.continue');
		this.buttonContinue.addEvent('click', function(event){
		  event.stop();
			this.close();
		}.bind(this));
		
		// button close
		$('giant-basket-popup-close').addEvent('click', function(event){
		  event.stop();
			this.close();
		}.bind(this));
		
		// input event
		this.input.addEvent('keyup', function(event){
			var value = this.input.get('value');
			value = value.replace(/[^0-9]/g, '');
			value = value.replace(/(^0)(.+)/,'$2');
			this.input.set('value', value);
			if (this.input.get('value')){
				this.requestUpdate.get({
					'id' : this.itemID,
					'count' : this.input.get('value')
				});
			}
		}.bind(this));
		
		
  },
	
	// set text in popup
	setText: function(text){
		this.text = text;
	},
	
	// set id for count update
	setID: function(id){
		this.itemID = id;
	},
	
	// set position of popup
	setPosition: function(pos){
		pos.x = (pos.x || 0);
		pos.y = (pos.y || 0);
		var dim = this.container.getSize();
		this.position.top = pos.y-dim.y+30;
		this.position.left = ((pos.x - (dim.x/3).toInt()) > 10 ? (pos.x - (dim.x/3).toInt()) : 10);
	},
	
	// updates number of products in basket
	update: function(id, count){
		this.requestUpdate.get({
			'id' : id,
			'count' : count
		});
	},
	
	close: function(){
		this.showFx.start(0);
	},
	
	open: function(){
		this.close();
		this.requestAdd.get({
			'id' : this.itemID,
			'count' : 1
		});
	}

});

// Basket
Basket = {
		
	options: {
	  buttons: '.book-list .buttons .add, .button-add-to-basket'
	},
	
	init: function(){
		
		this.buttons = $$(this.options.buttons);
		
		// prepare popup
		this.popup = new BasketPopup();
	
		// buttons action
		this.buttons.each(function(button){
		  button.addEvent('click', function(event){
			  event.stop();
				if ($('detail')){
					this.popup.setText(button.getParent('div#detail').getElement('h1').get('text'));
					this.popup.setID(button.get('id').replace('item-',''));
				} else {
					this.popup.setText(button.getParent('div.item').getElement('h3').get('text'));
					this.popup.setID(button.get('id').replace('item-',''));
				}
				var pos = button.getPosition('page-wrapper');
				this.popup.setPosition(pos);
				this.popup.open();
			}.bind(this));
		}, this);
		
	},

	
	update: function(count){
		var text = ' kusů';
		if (count == 1) { text = ' kus'; }
		if ((count > 1) && (count < 5)) { text = ' kusy'; }
		$('basket').getElement('a').set('text', count + text);
	}

};

var FormBasketCount = new Class({
  
	Implements: [Options],
	
	options: {
		updateUrl: '/kosik/upravit'
	},

  initialize: function(element){
		
		this.element = element;
		this.input = this.element.getElement('input');
		this.del = this.element.getElement('a.btn-del');
		this.price = this.element.getElement('td.price').get('text').toInt();
		
		this.del.addEvent('click', function(event){
      if (!confirm('Opravdu chcete odebrat produkt z košíku?')) {
				event.stop();
			}
		});
		
		// update request
		this.reqUpdate = new Request.JSON({
		  url: this.options.updateUrl,
			link: 'cancel',
			onComplete: function(jsonObj){
				if (jsonObj) this.element.highlight('#EEFFEA');
			}.bind(this)
		});
		
		// input behavior
		this.input.addEvent('keyup', function(event){
		  this.input.set('value', this.getCount() ? this.getCount() : '');
			if (this.input.value){
				this.update();
			  this.reqUpdate.get({'id' : this.getID(), 'count' : this.getCount()});
			}
		}.bind(this));
		this.input.addEvent('blur', function(event){
		  if (this.input.get('value') == ''){
				this.input.set('value', 1);
				this.update();
			  this.reqUpdate.get({'id' : this.getID(), 'count' : this.getCount()});
			}
		}.bind(this));
		
		
	},
	
	getCount: function(){
		return this.input.get('value').toInt();
	},
	
	getID: function(){
		var id = this.input.get('name').replace(/[^\[]+\[([^\]]+)\]/, '$1');
		return id;
	},
	
	update: function(){
    var priceVat = this.element.getLast('td.price');
		priceVat.set('text', this.getCount()*this.price + ',- Kč');
		FormBasket.calc();
	}
	
});

var FormBasket = {
	
	 init: function(){
		 this.form = $('form-basket');
		 this.elements = $$('#form-basket td.count');
		 this.list = new Array();
		 this.elements.each(function(element, index){
		   this.list[index] = new FormBasketCount(element.getParent('tr'));
		 }.bind(this));
		 
		 this.form.addEvent('submit', function(event){
			 event.stop();
			 if ($('form-basket-agreement').checked){
				 this.submit();
			 } else {
				 alert('Musíte souhlasit s obchodními podmínkami.');
			 }
		 });
	 },
	 
	 calc: function(){
		 var sum = 0;
		 this.list.each(function(item){
			 sum += (item.price * item.getCount());
		 });
		 $('form-basket').getElement('.product-list .last .price').set('text', sum + ',- Kč');
	 }
	 
};

var FormAddress = {
	
	init: function(){
		
		this.form = $('form-address');
		
		// slide event
		this.toggler = $('delivery-address-toggler');
		this.slide = $('delivery-address-wrapper');
		this.slideFx = new Fx.Slide(this.slide, { duration: 500, link: 'cancel', transition: Fx.Transitions.Quad.easeOut });
		this.slideFx.hide();
		this.checkbox = this.toggler.getElement('input');
		
		this.checkbox.checked = true;
		
		this.toggler.addEvent('click', function(event){
			this.toggle();
		}.bind(this));
		
		this.checkbox.addEvent('click', function(event){
		  this.toggle();
		}.bind(this));
		
		// submit event
		this.form.addEvent('submit', function(event){
      event.stop();
			this.validate();
		}.bind(this));
		
		
	},
	
	validate: function(){
		
		var fields = ['first-name', 'last-name', 'street', 'city', 'zip', 'email'];
		var prefix = 'form-address-inv-';
		var err = new Array();
		
		var fieldNames = new Array();
		
		// get names
		fields.each(function(field, index){
		  fieldNames[index] = this.form.getElement('label[for=' + prefix + field + ']').get('text').replace(':','');
		}, this);
		
		// search for empty fields
		var emptyFields = new Array();
		fields.each(function(field, index){
		  if ($(prefix + field).get('value') == ''){
				emptyFields.include(fieldNames[index]);
			}
		});

    if (emptyFields.length){
			err.include('Je nutné vyplnit i následující položky: ' + emptyFields.join(', '));
		}
		
    if (err.length){
			alert(err.join('\n'));
		} else {
			this.form.submit();
		}
	},
	
	toggle: function(){
		this.checkbox.checked ? this.slideFx.slideOut() : this.slideFx.slideIn();
	}
	
};

var FormDelivery = {
	
	init: function(){
		this.form = $('form-delivery');
		
		this.form.addEvent('submit', function(event){
		  event.stop();
			this.validate();
		}.bind(this));
		
		this.delivery = this.form.getElements('input[name=delivery]');
		this.payment = this.form.getElements('input[name=payment]');
		this.block = {
			delivery: this.delivery.getParent('fieldset'),
			payment: this.payment.getParent('fieldset')
		};
		
    this.block.payment.setStyle('display', 'none');
		
		// bind radio options
		this.bindMatrix = new Array();
		this.bindMatrix[0] = [1, 2, 3, 4, 5];
		this.bindMatrix[1] = [0, 2, 3, 4, 5];
		this.bindMatrix[2] = [0, 3, 4, 5];
		this.bindMatrix[3] = [0, 1, 2, 3, 5];
		this.bindMatrix[4] = [0, 1, 2, 3];
		this.bindMatrixDefault = [0, 1, 1, 4, 4];
		
		this.delivery.each(function(input){
		  // label event
			input.addEvent('change', function(event){
			  this.refresh();
			}.bind(this));
			// ie fix
			if (Browser.Engine.trident){
				input.addEvent('click', function(event){
					this.blur();
				});
			}
		}, this);
		
	},
	
	refresh: function(){
		this.block.payment.setStyle('display', 'block');
		this.payment.each(function(input){
		  input.disabled = false;
			input.getParent('label').removeClass('hide');
		});
		this.delivery.each(function(input, index){
		  if (input.checked){
				this.bindMatrix[index].each(function(id){
				  this.payment[id].disabled = true;
					this.payment[id].checked = false;
					this.payment[id].getParent('label').addClass('hide');
				}, this);
				this.payment[this.bindMatrixDefault[index]].checked = true;
			}
		}, this);
	},
	
	validate: function(){
		var err = new Array();
		var errQ = true;
		this.delivery.each(function(radio){
		  if (radio.checked) errQ = false;
		});
		if (errQ){
			err.include('Musíte zvolit způsob dopravy.');
		}
		
		errQ = true;
		this.payment.each(function(radio){
		  if (radio.checked) errQ = false;
		});
		if (errQ){
			err.include('Musíte zvolit způsob platby.');
		}
		
		if (err.length){
			alert(err.join('\n\n'));
		} else {
			this.form.submit();
		}
	}
	
};

var FormReg = {
	init: function(){
		
		this.form = $('form-reg');
		
		// slide event
		this.toggler = $('delivery-address-toggler');
		this.slide = $('delivery-address-wrapper');
		this.slideFx = new Fx.Slide(this.slide, { duration: 500, link: 'cancel', transition: Fx.Transitions.Quad.easeOut });
		this.slideFx.hide();
		this.checkbox = this.toggler.getElement('input');
		
		this.checkbox.checked = true;
		
		this.toggler.addEvent('click', function(event){
			this.toggle();
		}.bind(this));
		
		this.checkbox.addEvent('click', function(event){
		  this.toggle();
		}.bind(this));
		
		// submit event
		this.form.addEvent('submit', function(event){
			if (!this.form.hasClass('profile')){
				event.stop();
				this.validate();
			}
		}.bind(this));
		
		
	},
	
	validate: function(){
		
		var fields = ['username', 'email', 'password'];
		var prefix = 'form-reg-';
		var err = new Array();
		
		var fieldNames = new Array();
		
		// get names
		fields.each(function(field, index){
		  fieldNames[index] = this.form.getElement('label[for=' + prefix + field + ']').get('text').replace(/[:|*]/g,'');
		}, this);
		
		// search for empty fields
		var emptyFields = new Array();
		fields.each(function(field, index){
		  if ($(prefix + field).get('value') == ''){
				emptyFields.include(fieldNames[index]);
			}
		});

    if (emptyFields.length){
			err.include('Je nutné vyplnit i následující položky: ' + emptyFields.join(', '));
		}
		
		// kontrola emailu
		var mail = $(prefix + 'email').get('value');
		if (mail){
			mail = mail.replace(/^([a-zA-Z0-9])+([\.a-zA-Z0-9_-])*@([a-zA-Z0-9_-])+(\.[a-zA-Z0-9_-]+)+/, 'valid');
			if (mail != 'valid'){
					err.include('Zadaná emailová adresa je neplatná. Prosím, zkontrolujte její zadání.');
			}
		}
		
		// kontrola hesla
		var pass = $(prefix + 'password').get('value');
		var pass2 = $(prefix + 'password2').get('value');
		if (pass){
			if (pass != pass2){
				err.include('Zadaná hesla se neschodují.');
			}
		}
		
    if (err.length){
			alert(err.join('\n\n'));
		} else {
			this.form.submit();
		}
	},
	
	toggle: function(){
		this.checkbox.checked ? this.slideFx.slideOut() : this.slideFx.slideIn();
	}
	
};

var FormSendArticle = {
	
	init: function(){
		this.form = $('form-send-article');
		this.sendTo = this.form.getElement('input[name=sendTo]');
		this.name = this.form.getElement('input[name=name]');
		this.email = this.form.getElement('input[name=email]');
		this.message = this.form.getElement('textarea');
		this.err = new Array();
		
		this.form.addEvent('submit', function(event){
		  event.stop();
		  if (this.isValid()) {
				this.form.submit();
			} else {
				alert(this.err.join('\n'));
				this.err = new Array();
			}
		}.bind(this));
	},
	
	isValid: function(){
	
		if ((this.sendTo.value == '')
		 || (this.name.value == '')
		 || (this.email.value == '')
		 || (this.message.get('value') == '')){
			this.err.include('Musíte vyplnit všechna pole.');
		}
		
		if (this.sendTo.value != ''){
			var mail = this.sendTo.value.trim();
			mail = mail.replace(/^([a-zA-Z0-9])+([\.a-zA-Z0-9_-])*@([a-zA-Z0-9_-])+(\.[a-zA-Z0-9_-]+)+/, 'valid');
			if (mail != 'valid'){
					this.err.include('Adresa příjemce je neplatná. Prosím, zkontrolujte její zadání.');
			}
		}
		
		if (this.email.value != ''){
			var mail = this.email.value.trim();
			mail = mail.replace(/^([a-zA-Z0-9])+([\.a-zA-Z0-9_-])*@([a-zA-Z0-9_-])+(\.[a-zA-Z0-9_-]+)+/, 'valid');
			if (mail != 'valid'){
					this.err.include('Adresa odesílatele je neplatná. Prosím, zkontrolujte její zadání.');
			}
		}
		
		if (this.err.length > 0) {
			return false;
		} else {
			return true;
		}
	}
	
};

var FormSendTip = {
	
	init: function(){
		this.form = $('form-send-tip');
		this.name = this.form.getElement('input[name=name]');
		this.email = this.form.getElement('input[name=email]');
		this.message = this.form.getElement('textarea');
		this.err = new Array();
		
		this.form.addEvent('submit', function(event){
		  event.stop();
		  if (this.isValid()) {
				this.form.submit();
			} else {
				alert(this.err.join('\n'));
				this.err = new Array();
			}
		}.bind(this));
	},
	
	isValid: function(){
	
		if ((this.name.value == '')
		 || (this.email.value == '')
		 || (this.message.get('value') == '')){
			this.err.include('Musíte vyplnit všechna pole.');
		}
		
		if (this.email.value != ''){
			var mail = this.email.value.trim();
			mail = mail.replace(/^([a-zA-Z0-9])+([\.a-zA-Z0-9_-])*@([a-zA-Z0-9_-])+(\.[a-zA-Z0-9_-]+)+/, 'valid');
			if (mail != 'valid'){
					this.err.include('Adresa odesílatele je neplatná. Prosím, zkontrolujte její zadání.');
			}
		}
		
		if (this.err.length > 0) {
			return false;
		} else {
			return true;
		}
	}
	
};



var BookItem = new Class({

  options: {
		maxHeight: 141
	},
	
  initialize: function(item){
		
		this.item = item;

//    // truncated element
//		this.p = this.item.getElement('p.annotation');
//		this.text = this.p.get('text').split(' ');
//
//		// get available height
//		this.height = this.options.maxHeight - this.item.getElement('h3').getSize().y - this.item.getElement('p.author').getSize().y;
//    
//		while(this.p.getSize().y > this.height){
//			this.text.pop();
//			this.p.set('html', this.text.join(' ') + '&hellip;');
//		}
		
		// item events
		this.detail = this.item.getElement('div.img').getElement('a').get('href');
		this.title = this.item.getElement('h3');
		// onclick
		this.imgBox = this.item.getElement('div.img');
		this.imgBox.addEvent('click', function(event){
		  event.stop();
			window.location = this.detail;
		}.bind(this));
		
		this.imgBox.addEvent('mouseenter', function(){
			this.title.setStyle('text-decoration', 'underline');
		}.bind(this));
		this.imgBox.addEvent('mouseleave', function(){
		  this.title.setStyle('text-decoration', 'none');
		}.bind(this));
		
		this.textBox = this.item.getElement('div.text');
		this.textBox.setStyle('cursor', 'pointer');
		this.textBox.addEvent('click', function(event){
		  event.stop();
			window.location = this.detail;
		}.bind(this));
		this.textBox.addEvent('mouseenter', function(){
		  this.title.setStyle('text-decoration', 'underline');
		}.bind(this));
		this.textBox.addEvent('mouseleave', function(){
		  this.title.setStyle('text-decoration', 'none');
		}.bind(this));
		
		
		
	}
	
});

var FormSearch = {
	
	
	init: function(){
		
		this.form = $('form-search');
		this.term = $('form-search-term');
		if($('form-search-vocabulary')) {
			this.dictionary = $('form-search-vocabulary');
		}
		else {
			this.dictionary = $('form-search-vocabulary-right');
		}
		this.lexikon = $('form-search-lexikon-term');
		this.lexikonId = $('form-search-lexikon-id');
		
	
		
		// basic events
		this.form.getElements('input.input').each(function(input){
		  input.addEvent('click', function(){
			  if (input.get('value') == input.defaultValue) input.set('value', '');
			});
			input.addEvent('blur', function(){
			  if (input.get('value') == '') input.set('value', input.defaultValue);
			});
		});
		
		// submit form
		this.form.addEvent('submit', function(event){
		  event.stop();
			//console.log('test');
		  
			if (this.term){
				if (this.term.get('value') == this.term.defaultValue) this.term.set('value', '')
			}
			
			if (this.dictionary){
				if (this.dictionary.get('value') == this.dictionary.defaultValue) this.dictionary.set('value', '');
			}
			
			if (this.lexikon){
				if (this.lexikon.get('value') == this.lexikon.defaultValue) {
					this.lexikon.set('value', '');
					this.lexikonId.set('value', '');
				}
			}
			
			if (this.check()){
  				this.form.submit();
			} else {
				if(this.term) this.term.set('value', this.term.defaultValue);
				if(this.dictionary) this.dictionary.set('value', this.dictionary.defaultValue);
				if(this.lexikon) this.lexikon.set('value', this.lexikon.defaultValue);
			}
			
		}.bind(this));
		
	},
	
	check: function(){
		var status = false;
		
		if (this.term && this.dictionary) {
			if(this.term.get('value') || this.dictionary.get('value')) status = true;
		}

		if(this.dictionary && this.lexikon) {
			
			if(this.dictionary.get('value') || this.lexikon.get('value')) status = true;
				
		}
		
		if (this.term) {
			if(this.term.get('value')) status = true;
		}
		
		return status;
	}
	
};

var AlertWindow = {
	
	init: function(){
		this.container = $('alert');
		this.fx = new Fx.Tween(this.container, { property: 'opacity', duration: 400 });
		this.fx.set(1);
		this.container.getElement('a.btn-close').addEvent('click', function(event){
			event.stop();
			Cookie.write('isDoctor', false, { duration: 1, path:'/' });
			this.fx.start(0);

			this.nextAlert = $('opacity-overlay');
			this.nextAlert.addClass('invisible_overlay');

		}.bind(this));

		this.container.getElement('a.btn-open').addEvent('click', function(event){
			event.stop();
			this.fx.start(0);

			this.nextAlert = $('alert1');

			this.nextAlert.addClass('alert1_visible');
			this.nextAlert.removeClass('alert1_hidden');

		}.bind(this));
		
	}
};

var AlertWindow1 = {
	
	init: function(){
		this.container = $('alert1');
		this.fx = new Fx.Tween(this.container, { property: 'opacity', duration: 400 });
		this.fx.set(1);
		this.container.getElement('a.btn-close').addEvent('click', function(event){
			event.stop();
			Cookie.write('isDoctor', false, { duration: 1, path:'/' });
			this.fx.start(0);

			this.nextAlert = $('opacity-overlay');
			this.nextAlert.addClass('invisible_overlay');
			
			window.location = '/';

		}.bind(this));
		
		this.container.getElement('a.btn-open').addEvent('click', function(event){
			event.stop();
			Cookie.write('isDoctor', true, { duration: 1, path:'/' });
			this.fx.start(0);

			this.nextAlert = $('opacity-overlay');
			this.nextAlert.addClass('invisible_overlay');
			
		}.bind(this));
		
	}
};

var RelToggler = new Class({

  initialize: function(el){
		this.el = $(el);
		this.rel = this.el.get('rel');
		this.target = null;
		
		if (this.rel) this.target = $(this.rel);
		
		if (this.target){
			// target Fx
			this.targetFx = new Fx.Slide(this.target, { duration: 500, link: 'cancel', transition: Fx.Transitions.Quad.easeInOut });
			this.targetFx.hide();
			
			// close button
			this.target.getElement('.btn-close').addEvent('click', function(event){
				event.stop();
			  this.targetFx.slideOut();
			}.bind(this));
			
			// toggle button
			this.el.addEvent('click', function(event){
			  if (this.el.get('href').replace('#','') == '') event.stop();
				this.targetFx.toggle();
			}.bind(this));
		} else {
			this.el.addEvent('click', function(event){
			  event.stop();
			});
		}
	}
	
});

var Rating = {
	
	init: function(){
		
		this.btn = $('btn-rating');
		this.rating = $('rating');
		this.rated = false;
		this.pos = this.btn.getPosition('main-content');
		
		this.ratingFx = new Fx.Tween(this.rating, { property: 'width', duration: 200, transition: Fx.Transitions.Quad.easeOut });
		this.ratingOp = new Fx.Tween(this.rating, { property: 'opacity',  duration: 400 });
		this.ratingOp.set(0);
		
		if (!this.rating.getElement('ul')) this.rated = true;
		
		this.btn.addEvent('click', function(event){
			//if (this.rated){
			//  this.rating.getElement('p').set('text', 'Tento článek jste již hodnotili');
			//}
		  event.stop();
			this.ratingOp.start(1);
			this.rating.setStyles({
			  'top' : this.pos.y - 20,
				'left' : this.pos.x,
				'display' : 'block'
			});
  		if (this.rated){
				(function(){ this.ratingOp.start(0) }).delay(2000, this);
			}

		}.bind(this));
		
		this.req = new Request.JSON({
		  url: '',
			onComplete: function(){
				this.rated = true;
				this.rating.getElement('ul').destroy();
				var p = new Element('p', { 'text' : 'Děkujeme za ohodnocení tohoto článku.' });
				var pFx = new Fx.Tween(p, { property: 'opacity', duration: 300 });
				pFx.set(0);
 				p.inject(this.rating);
  		  this.ratingFx.start(260).chain(
					function(){
						pFx.start(1);
						(function(){ this.ratingOp.start(0) }).delay(3000, this);
					}.bind(this)
        );
			}.bind(this)
		});
		
		this.rating.getElements('li a').each(function(link){
		  link.addEvent('click', function(event){
			  event.stop();
				this.req.get({'rating' : link.get('text'), 'id' : link.getParent('ul').get('id').replace('item','')});
			}.bind(this));
		}, this);
	}

};

var Maxdorf = {
	
	  init: function(){
			
			if (Browser.Engine.trident4){
        IE6fixes.init();
			}
			
			// common slides
			var slides = $$('.slide');
			var togglers = $$('.toggler');
			togglers.setStyles({
			  'text-decoration': 'underline',
				'cursor': 'pointer'
			});
		  togglers.each(function(element, index){
			  var slideFx = new Fx.Slide(slides[index], { duration: 500, link: 'cancel' });
				slideFx.hide();
				element.addEvent('click', function(event){
				  event.stop();
					slideFx.toggle();
				});
			});
			
			// clickable box 
			$$('.clickable').each(function(item){
			  item.setStyle('cursor', 'pointer');
				item.addEvent('click', function(event){
				  event.stop();
					window.location = item.getElement('a').get('href');
				});
			});
			
			// target: _blank
			$$('._blank').each(function(link){
			  link.addEvent('click', function(event){
				  event.stop();
					window.open(link.get('href'));
				});
			});
			
			// backlink
			$$('.backlink').each(function(link){
			  link.addEvent('click', function(event){
				  event.stop();
					window.history.back();
				});
			});
			
			// print
			$$('.print').each(function(link){
			  link.addEvent('click', function(event){
				  event.stop();
					window.print();
				});
			});
			
			// relative togglers
			$$('.rel-toggler').each(function(toggler){
			  new RelToggler(toggler);
			});
			
			// flash insertion
			var flashBaseUrl = '/data/ad/';
			$$('.insert-flash').each(function(flash){
				var params = flash.get('id').split('|');
				var options = {
					container: flash,
					width: params[1],
					height: params[2],
					params: {
						wmode: 'window'
					}
				}
				if (flash.getParent('#main-content')) flashBaseUrl = '/data/dad/';
				new Swiff(flashBaseUrl + params[0] + '.swf', options);
			});
			
			// basket
			if ($$('.book-list') || $('detail')) Basket.init();
			
			if ($$('.article-popup')) ArticlePopup.init();
			
			// remooz for book detail
			if ($('detail')){
				$$('.book-img a').each(function(element){
					new ReMooz(element, {
						resizeOptions : {
							duration: 400,
							transition: Fx.Transitions.Quart.easeOut
						},
						opacityResize: 0,
						cutOut: false,
						centered: true
					});	
				});
			}
			
			// book items events
			$$('.book-list .item').each(function(item){
				new BookItem(item);
			});
					
			// login block
			if ($('block-login')){
				var blockLogin = $('block-login');
				var blockLoginFx = new Fx.Morph('block-login', { duration: 700, link: 'cancel', transition: Fx.Transitions.Quint.easeOut  });
				var blockLoginVisible = false;
				var blockLoginClose = false;
				blockLoginFx.set({
				  width: 0,
					opacity: 0
				});
				blockLogin.setStyles({'display' : 'block'});
				
				$('block-login').addEvent('mouseenter', function(){
				  blockLoginClose = false;
				});
				
				$('block-login').addEvent('mouseleave', function(){
				  blockLoginClose = true;
				});
				
				$('menu-login').getElement('a').addEvent('click', function(event){
				  event.stop();
					if (blockLogin.getStyle('width').toInt() > 10){
						blockLoginFx.start({
						  width: 0,
							opacity: 0
						});
						blockLoginVisible = true;
					} else {
						blockLoginFx.start({
							width: 261,
							opacity: 1
						});
						blockLoginVisible = true;
					}
				});
				
				// global close event
				document.addEvent('click', function(){
				  if (blockLoginVisible && blockLoginClose) {
						blockLoginFx.start({
						  width: 0,
							opacity: 0
						});
					}
				});
			}
			
			// rating button
			if ($('btn-rating')) Rating.init();
			
			// forms
			if ($('form-basket')) FormBasket.init();
			if ($('form-address')) FormAddress.init();
			if ($('form-delivery')) FormDelivery.init();
			if ($('form-reg')) FormReg.init();
			if ($('form-search')) FormSearch.init();
			if ($('form-send-article')) FormSendArticle.init();
			if ($('form-send-tip')) FormSendTip.init();
			if ($('alert')) AlertWindow.init();
			if ($('alert1')) AlertWindow1.init();
			
		}
		
};



var MaxdorfAkceVls = {
	
	init: function(){
		this.wrapper = $('maxdorf-akce-vls');
		this.closeBtn = this.wrapper.getElement('a.close-btn');
		this.open = false;
		
		this.wrapperFx = new Fx.Tween(this.wrapper, { property: 'opacity', duration: 250, link: 'chain' });
		this.wrapper.setStyle('display', 'block');
		this.wrapperFx.set(0);

    this.wrapper.addEvent('click', function(event){
		  event.stop();
  		Cookie.write('maxdorfAkceVls', 'closed', { duration: 1, domain: document.URL.replace('http://','').replace('/','') });
			window.location = this.wrapper.getElement('a').get('href');
		}.bind(this));
		
		this.wrapper.getElement('a').addEvent(function(event){
		  event.preventDefault();
		});
		
		this.closeBtn.addEvent('click', function(event){
		  event.stop();
			this.close();
		}.bind(this));
		
		this.switcher = Cookie.read('maxdorfAkceVls');
		if (this.switcher != 'closed'){
			this.wrapperFx.set(1);
			this.open = true;
		}
	},
	
	close: function(){
		this.wrapperFx.start(0);
		Cookie.write('maxdorfAkceVls', 'closed', { duration: 1, domain: document.URL.replace('http://','').replace('/','') });
	},
	
	isOpen: function(){
		return this.open;
	}
	
};

window.addEvent('domready', function(){
	
	Maxdorf.init();
	
	Cookie.write('javaScriptOn', true, { duration: 1, path:'/' });
	
	// akce
	if ($('maxdorf-akce-vls')){
		MaxdorfAkceVls.init();
		$(document.body).addEvent('click', function(event){
		  if (MaxdorfAkceVls.isOpen()){
				MaxdorfAkceVls.close();
			}
		});
	}
	
});

//Article popup
ArticlePopup = {

	options: {
		buttons: '.article-popup'
	},
	
	init: function(){
		this.buttons = $$(this.options.buttons);

		// buttons action
		this.buttons.each(function(button){

			href = button.get('href');
			newHref = '/popup-article/' + lexikonUrl + '/' + href;
			button.set('href', newHref);
			button.set('target', '_blank'); 

		}, this);

	}

};


function closeWindow() {
	//uncomment to open a new window and close this parent window without warning
	//var newwin=window.open("popUp.htm",'popup','');
	if(navigator.appName=="Microsoft Internet Explorer") {
	this.focus();self.opener = this;self.close(); }
	else { window.open('','_parent',''); window.close(); }
}