var menuDuration = 200;

window.addEvent('domready', function() {
	SqueezeBox.initialize();
	
	if ($('turne-list')) {
		var today = $('turne-list').getFirst('.today');
		if (today)
			$('turne-list').scrollTo(0, (today.getPosition($('turne-list')).y));
	}
		
	
	if ($('blogy') && $('media')) {
		$$('#blogy, #media').addEvent('click', function (e) {
			e.stop();
			var request = new Request.JSON({
				'url' : '?json=m_media&showAll=1&identifier='+this.get('title'),
				'onComplete' : function(jsonObj) {
					var content = new Element('div');
					jsonObj.each(function(val) {
						var block = new Element('div', {'class' : 'over-block'});
						var link = new Element('a', {
							'class' : 'over-link',
							'href' : val.Link,
							'text' : val.Title,
							'target' : '_blank'
						});
						var split = val.Date.split("-");
						var info = new Element('div', {'class' : 'over-info'});
						var date = new Element('span', {
							'text' : split[2]+'.'+split[1]+'.'+split[0]
						})
						info.adopt(date);
						info.appendText(' | ');
						info.adopt(new Element('span', {'text' : val.Author}));
						
						var desc = new Element('div', {
							'class' : 'over-description',
							'text' : val.Description
						})
						
						content.adopt(block.adopt(link, info, desc));
					});
					var height = ($(document.window).getSize().y - 200);
					SqueezeBox.open(content, {
						handler: 'adopt',
						size: {x: 400, y: height}
					});
				}
			}).post({});
		});
	}
	
	$$('.yotube-over').each(function(el) {
		el.addEvent('click', function(e) {
			e.stop();
			var code = this.get('ref');
			var width = 590;
			var height = 360;
			var youtube = new Element('div');
			var html = '<object width="'+width+'" height="'+height+'">';
			html += '<param name="movie" value="http://www.youtube.com/v/'+code+'&autoplay=1"></param>'
			html += '<param name="allowFullScreen" value="true"></param><param name="allowscriptaccess" value="always"></param>';
			html += '<embed src="http://www.youtube.com/v/'+code+'&autoplay=1" type="application/x-shockwave-flash"';
			html += ' allowscriptaccess="always" allowfullscreen="true" width="'+width+'" height="'+height+'"></embed></object>';			
			
			
			youtube.set('html', html);
			
			
			SqueezeBox.open(youtube, {
				handler: 'adopt',
				size: {x: (width + 4), y: (height + 5)}
			});
		});
	});
	
	$$('.kontakt_squeeze').each(function(el) {
		el.addEvent('click', function(e) {
			e.stop();
			
			var width = 640;
			var height = 480;
			var prodejna = new Element('div');
			var html = '<iframe src="/pub/local/tour/prodejna.html" style="width:640px;height:480px;"></iframe>';
			
			prodejna.set('html', html);
			
			
			SqueezeBox.open(prodejna, {
				handler: 'adopt',
				size: {x: (width + 10), y: (height + 10)}
			});
		});
	});

	
	
	$('googleSearch').addEvent('focus', function() {
		if (this.hasClass('suggestion')) {
			this.removeClass('suggestion');
			this.set('value', '');
		}
	});
	$('googleSearch').addEvent('blur', function() {
		if (!this.hasClass('suggestion') && this.get('value').trim() == '') {
			this.addClass('suggestion');
			this.set('value', 'Hledej...');
		}
	});
	$('googleSearch').addEvent('keydown', function(e) {
		if (e.key == 'enter')
			$('googleSearchConfirm').fireEvent('click');
	})
	
	$('googleSearchConfirm').addEvent('click', function() {
		var searchValue = $('googleSearch').get('value').trim();
		if (!$('googleSearch').hasClass('suggestion') && searchValue != '') {
			this.addClass('loading');
			window.location = "/vyhledavani/181?query="+encodeURIComponent(searchValue);
		}
	});
	
	new Element('img', {'src' : '/pub/local/img/ajax-loader.gif'});
	new Element('img', {'src' : '/pub/local/img/topmenu_bg.gif'});
	
	$$('.top-menu .menu-item').each(function(el) {
		el.addEvent('click', function(e) {
			var topMenu = this.getParent().getParent();
			topMenu.removeEvents();
		});
	});
	
	$$('.top-menu').each(function(el) {
		if (el.getElement('a.selected')) {
			var ul = el.getElement('ul');
			var desc = el.getElement('div.top-menu-desc');
			
			ul.setStyles({
				'opacity' : 1,
				'display' : 'block'
			});
			desc.setStyles({
				'opacity' : 0,
				'display' : 'none'
			});
			
			return;
		}
		el.addEvent('mouseenter', function() {
			var ul = this.getElement('ul');
			if (!ul)
				return;
			
			ul.setStyles({
				'opacity' : 0,
				'display' : 'block'
			});
			
			if (this.fx1 && this.fx2) {
				this.fx1.cancel();
				this.fx2.cancel();
			}
			
			this.fx1 = new Fx.Morph(ul, {'duration' : menuDuration});
			this.fx2 = new Fx.Morph(this.getElement('div.top-menu-desc'), {'duration' : menuDuration});
			
			this.fx1.start({
				'opacity' : 1
			});
			this.fx2.start({
				'opacity' : 0
			});
		});
		el.addEvent('mouseleave', function() {
			var ul = this.getElement('ul');
			if (!ul)
				return;
			
			if (this.fx1 && this.fx2) {
				this.fx1.cancel();
				this.fx2.cancel();
			}
			
			this.fx1 = new Fx.Morph(ul, {'duration' : menuDuration });
			this.fx2 = new Fx.Morph(this.getElement('div.top-menu-desc'), {'duration' : menuDuration});
			
			this.fx1.start({
				'opacity' : 0
			});
			this.fx2.start({
				'opacity' : 1
			});
		});
		
		
	});
	
	
	//eshop produkt
	
	$$('.bottom_button').each(function(el) {
		el.addEvent('click', function() {
		
			$$('.bottom_button').removeClass('bottom_button_active');
			el.addClass('bottom_button_active');
			
			$$('.bottom_kategs').setStyle('display', 'none');
			$(this.get('rel')).setStyle('display', 'block');
		
		});
	});
	
	
	//rental
	if ($('rental_forum')) {
		$('rental_forum_submit').addEvent('click', function() {
			
			if ($('rental_forum_text').get('value') == "") {
				return;
			}
		
		
			var request = new Request.JSON({
				'url' : '/?json=m_rental&action=addComment&pid='+$('rental_forum_idproduct').get('value'),
				'onComplete' : function(jsonObj) {
					
					window.location.reload();
					
				}
			}).post({
				'text': $('rental_forum_text').get('value'),
				'rate': 1
			});
		});
		
		$$('.rental_rate').each(function(el) {
		
			el.addEvent('click', function() {
			
				var request = new Request.JSON({
					'url' : this.get('rel'),
					'onComplete' : function(jsonObj) {
						
						if (jsonObj[1] == 'alreadyRated') {
							alert('Uz ste hlasovali!');
							return;
						}
						
						window.location.reload();
						
					}
				}).post({});
			
			});
		
		});
		
	}
});
