window.addEvent('domready', function() {
        if ($('nojs')) $('nojs').set('display', 'none');

	// Preload images for the hover effects
	var myImages = new Asset.images(
			['http://www.nebenet.cz/images/icon-blog-active.jpg', 'http://www.nebenet.cz/images/icon-contact-active.jpg', 'http://www.nebenet.cz/images/icon-rss-active.jpg', 'http://www.nebenet.cz/images/icon-who-active.jpg', 'http://www.nebenet.cz/images/tab-grafika.jpg', 'http://www.nebenet.cz/images/tab-tisk.jpg', 'http://www.nebenet.cz/images/tab-web.jpg'], {}
	);
	
	
	// Load the CSS for Opera (just some cosmetic changes for forms)
	if (Browser.Engine.presto)
	{
		var css = new Asset.css('http://www.nebenet.cz/css/nebe-opera.css', { });
	}
	
	
	// Redirect old browser to proper place:-)
	if (Browser.Engine.trident && Browser.Engine.version < 5)
	{
		if (!$('nocheck')) window.location = "http://www.nebenet.cz/ie6/";
	}
	
	
	// Make swapable inputs
	var swap = [];
	$$('input.swap').each(function(el){
		swap[el.id] = $(el).value;
		el.addEvents({
			'focus' : function(){ this.value = "" },
			'blur' : function(){ if (this.value == ''){ this.value = swap[this.id] } }
		});
	});
	
	
	// Password swap
        if ($('client_password_over'))
        {
	$('client_password_over').addEvents({
		'focus' : function(){ this.setStyle('display', 'none'); $('client_password').setStyle('display', 'inline'); $('client_password').focus(); }
	});
	
	$('client_password').addEvents({
		'focus' : function(){ if (this.value == ''){ $('client_password_over').setStyle('display', 'none') } },
		'blur' : function(){ if (this.value == ''){ $('client_password_over').setStyle('display', 'inline'); $('client_password').setStyle('display', 'none') } }
	});
        }
	
	
	// External anchors in valid XHTML strict
	$$('a[rel=external]', 'a.external').each(function(el){
		if (el.href) el.target = "_blank";
	});
	
	
	// Menu items slidein and out
        if ($('menu'))
        {
	$each(['web','tisk','grafika'], function(d, i){
		var tab = $('tab_'+d);
		if (tab.get('class') == 'active') return;
		
		var mel = new Fx.Morph('tab_'+d, {wait: false, duration: '250', transition: Fx.Transitions.Expo.easeOut});
		tab.set('styles', {'margin-top': 230});
		$('arrow_'+d).set('styles', {'display': 'none'});
		
		$('menu_'+d).addEvents({
			'mouseenter': function() {
			  	mel.start({
					'margin-top': 130,
					'opacity': 1
				});
			},
	  		'mouseleave': function() {
				mel.start({
					'margin-top': 230,
					'opacity': 0
				});
  			}
		});
	});
        }
	
	
	// Portfolio carousel variables
        if ($('portfolio'))
        {
	var classes   = ['first', 'second', 'third', 'fourth', 'fifth'];
	var icarousel = $$('#portfolio_content li');
	var id        = 0;
	var carousel;

	
	// Previous button
	$('ap').addEvent('click', function (e) {
		e.stop();
                carousel = $clear(carousel);
		if (id - 1 > 0)
		{
			id = id - 2;
		}
		else if (id == 0)
		{
			id = icarousel.length - 2;
		}
		else
		{
			id = icarousel.length - 1;
		}
		scarousel();
	});
	
	// Next button - no need to add to id, because it's done by scarousel function
	$('an').addEvent('click', function (e) {
		e.stop();
                carousel = $clear(carousel);
		if (id >= icarousel.length)
		{
			id = 0;
		}
		scarousel();
	});
	
	// Stop automatic rotation on mouseover and restart on mouseleave
	$('portfolio').addEvents({
		'mouseenter': function() {
			carousel = $clear(carousel);
		},
		'mouseleave': function() {
                        carousel = $clear(carousel);
			carousel = scarousel.delay(5000);
  		}
	});

        $$('.web_descr, #ap, #an').addEvents({
		'mouseenter': function() {
			carousel = $clear(carousel);
		},
		'mouseleave': function() {
                        carousel = $clear(carousel);
			carousel = scarousel.delay(5000);
  		}
	});
	
	// Effect for portfolio items
	$$('#portfolio li').each(function(el){
		el.set('opacity', 0.7);
		
		el.addEvents({
			'mouseenter': function() {
                                carousel = $clear(carousel);
				$(el).fade(1);
			},
			'mouseleave': function() {
				if (!el.hasClass('third'))
				{
					$(el).fade(0.7);
				}
  			},
			// Display them immediately
			'click': function() {
                carousel = $clear(carousel);
				var num = el.getProperty('id');
				num = num.replace("portf_", "");
				id  -= 1;
				num -= 2;
				if (num == 0)
				{
					num = 0;
				}
				else if (num < 0)
				{
					num = icarousel.length + num;
				}
				id = num;
				scarousel();
			}
		});
	});
	
	
	// Heaven portfolio carousel function:-]
	var scarousel = function() {
		carousel = $clear(carousel);
		
		// Hide descriptions
		$$('.web_descr').each(function(el){
			el.addClass('off');
		});
		
		
		$$('img.clickable').each(function(el){
			el.removeEvents ('click');
		});
		

		// Get through all elements
		for (i = 0; i < icarousel.length; i++) {
			// Show the five from all
			if (i >= id && i < (id + 5))
			{
				// Little dim for unfocused images
				$(icarousel[i]).set('opacity', 0.7);
				
				
				// Return to first ones if necessary 
				if (id >= icarousel.length - 4)
				{
					$(icarousel[i]).setProperty('class', classes[(i-id)]);
					for (k = 0; k <= (id - icarousel.length + 4); k++) {
						$(icarousel[k]).setProperty('class', classes[(i-id+k+1)]);
					}
				}
				else
				{
					$(icarousel[i]).setProperty('class', classes[(i-id)]);
				}
			}
			// Other hide
			else
			{
				$(icarousel[i]).setProperty('class', "off");
			}
		}
		

		// Show descripiton in the middle
		$$('.third .web_descr').each(function(el){
			el.removeClass('off');
		});
		
		// Show descripiton in the middle
		$$('.third img.clickable').each(function(el){
			url = el.getProperty('rel');
			el.addEvent ('click', function(){
				window.open (url);
			});
		});
		
		
		// Focus in middle image
		$$('li.third').set('opacity', 1);
		
		// At the end null the counter
		if (id < (icarousel.length - 1))
		{
			id += 1;
		}
		else
		{
			id = 0;
		}
		
		
		// And start again in 5 seconds
		carousel = scarousel.delay(5000);
	};
	scarousel();
        }
});