$.browser.msie6 = ($.browser.msie && /MSIE 6/i.test(window.navigator.userAgent));

// clanek na indexu
var indexClanek = function(element) {
	if (!$(element).hasClass('aktivni')) {
		if (!indexFirst) {
			$('#index .box1 .clanek').slideUp(1000).delay(1000);
			$('#index .box1 .cislo a').removeClass('aktivni');
		}
		indexFirst = false;

		var cislo = $(element).attr('aindex') *1;
		$('#index .box1 .clanek_' + cislo).slideDown(1000, indexBoxes);
		$(element).addClass('aktivni');
		
		if (!indexKliknuto && !indexHover) {
			if ($('#index .box1 .cislo a.cislo_' + (cislo +1)).size() == 0) {
				cislo = 0;
			}
			indexTimer = setTimeout("indexClanek($('#index .box1 .cislo a.cislo_" + (cislo +1) + "'))", 5000);
		}
	}
}

// stejna vyska index boxu
var indexBoxes = function() {
	var maxHeight = 0;
	$('#index .boxy .box .text_box').css('height', 'auto');
		
	$('#index .boxy .box .text_box').each(function() {
		if ($(this).height() > maxHeight) {
			maxHeight = $(this).height();
		}
	});

	$('#index .boxy .box .text_box').height(maxHeight);
}

var overEvent = function(element, speed) {		
	var submenu = $(element).find('ul');
	
	$(element).find('a').addClass('aktivni');
	
	if ($.browser.msie6) {
		speed = 0;
	}
	
	var left = $(element).position().left;
    
	submenu.css('left', left);
	submenu.show(speed);

	// hack pro ie6, kde se zobrazi spravne az napodruhe
	if ($.browser.msie6) {
		submenu.css('display', 'none');
		submenu.css('display', 'block');
	}
}

var outEvent = function(element, speed, current) {
    if (element == current) {
        return;
    }

	$(element).find('a').removeClass('aktivni');
	
	if ($.browser.msie) {
		speed = 0;
	}
	
	$(element).find('ul').hide(speed);	
}

var indexKliknuto = false;
var indexHover = false;
var indexTimer = null;
var indexFirst = true;

$(function() {
	// clanky na indexu
	$('#index .box1 .clanek_dalsi').hide();

	$('#index .box1 .cislo a').click(function() {
		indexKliknuto = true;
		indexClanek(this);
		return false;
	});

	$('#index .box1 .cislo a').mouseover(function() {
		if (!indexKliknuto) {
			//indexClanek(this);
		}
		return false;
	});
	
	$('#index .box1').mouseover(function() {
		indexHover = true;
		clearTimeout(indexTimer);
	});

	// spusteni stridani clanku na indexu
	if ($('#index .box1 .cislo a').size() > 0) {
		indexClanek($('#index .box1 .cislo a').first());
	}
	
    // external
    $('a.external').click(function() {
	      if (window.open(this.href)) {
	        return false;
	      }
	});

	
	// lightbox 
	$("a[rel^=lightbox]").lightBox(); 
	
	// menu
	$('#menu ul ul').hide();
	$('#menu .odkazy > ul > li').hover(
			function() {
			    var current = this;
				$('#menu .odkazy > ul > li').each(function() {
					outEvent(this, 0, current);
				});
				overEvent(this, 'fast');
			}, 
			function() {
				outEvent(this, 'fast', null);
			}
	);
		
	// aktivni polozka menu
	var aktivni_el = $('#menu .odkazy li.aktivni').first();
	if (aktivni_el.size() > 0) {
		overEvent(aktivni_el);
	}

	
	// stejna vyska index boxu
	indexBoxes();
	
	// produkty
	$('#produkty .nadrubrika .pokracovani').hide();
	$('#produkty .nadrubrika .vice').click(function(){ 
		var el = $('.pokracovani', $(this).parents('.nadrubrika'));
		
		if (el.is(':hidden')) {
			el.show('slow');	
		}
		else {
			el.hide('slow');
		}
		return false;
	});
	
	$('#produkty a.contact_form').click(function() {
		var el = $('#produkty #contact_form');
		if (el.is(':hidden')) {
			el.show('slow');
		}
		else {
			el.hide('slow');
		}
		
		return false;
	});
	
	$('#produkty #contact_form a.zavrit').click(function() {
		$('#produkty #contact_form').hide('slow');
		
		return false;
	});
});
