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 + 4)}
			});
		});
	});
	
	
	
	
	$('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', 'Hladať...');
		}
	});
	$('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 = "/vyhladavanie/14?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
			});
		});
		
		
	});
});