$().ready(function(){

	// prace s dialogem
	$('.dialog').bind('show', function() {
		$(this).trigger('resize');
		$('.dialog .msg').empty().removeClass('error');
		$(this).fadeIn();
		$('.dialog_overlay').fadeTo("normal", 0.6);
                $('.dialog input[name=kod]').val('');
	}).bind('hide', function(){
		$(this).fadeOut();
		$('.dialog_overlay').fadeOut();		
	}).bind('resize', function(){

                $('.dialog_overlay').css({
                    position: 'absolute',
                    height: $(document).height()
                });

		var width = $(this).outerWidth();
		var height = $(this).outerHeight();
		$(this).css({
			top: Math.max(0, ($(window).height()-height)/2) + $(window).scrollTop(),
			left: Math.max(0, ($(window).width()-width)/2)
		});
	});

        // pri zmene velikosti okna presuneme dialog a pripadnou hlasku na stred
	$(window).resize(function(){
		$('.dialog').trigger('resize');
                $('.error_msg').trigger('resize');
	});
	
	// otevreni dialogu po kliknuti na tlacitko "kontaktni formular"
	$('.formular_button').click(function(){
		$('.dialog').trigger('show');
	});
	
	// zavreni dialogu "krizkem"
	$('.dialog .close').click(function(){
		$('.dialog').trigger('hide');
	});

        // vypis hlasek dialogu
        $('.error_msg').bind('show', function() {
                $(this).trigger('resize');
                $('.error_overlay').fadeTo("normal", 0.6);
                $(this).fadeIn();

        }).bind('hide', function() {
            $(this).fadeOut();
            $('.error_overlay').fadeOut();

        }).bind('resize', function() {
            
            var position = $('.dialog').position();
            $(this).css({
                top: position.top + $('.dialog').height()/2 - $(this).height()/2,
                left: position.left + $('.dialog').width()/2 - $(this).width()/2
            });

            $('.error_overlay').height($('.dialog').height());
            $('.error_overlay').width($('.dialog').width());
            $('.error_overlay').css({
                top: position.top,
                left: position.left
            });
            
        });

        // zavreni hlasky tlacitkem OK
        $('.msg_button').click(function() {
            $('.error_msg').trigger('hide');
        });

        // zavreni hlasky krizkem
        $('.msg_close').click(function() {
            $('.error_msg').trigger('hide');
        });

	// odeslani formulare dialogu ajaxem
	$('.dialog form').submit(function(e){
		e.preventDefault();
		var data = ($(this).serialize());
		$.ajax({
			url: 'scripts/kontaktni_formular.php',
			data: data,
			type: 'POST',
			dataType: 'json',
			success: function(response){
                                
				if(response && response.error) {
					$('.msg_text').text(response.error).addClass('error');
				}
				else if(response && response.msg) {
					$('.msg_text').text(response.msg).removeClass('error');
				}
				else {
					$('.msg_text').empty().removeClass('error');
				}
                                $('.error_msg').trigger('show');
				$('.dialog .spam img').attr('src', 'captcha.png?' + (new Date()).getTime());
                                $('.dialog input[name=kod]').val('');
			}
		});
	});
	
	// pridani tridy .hover pri najeti u prvku, ktere mely puvodne nastavenou tridu .hover
	$('.hover').each(function(){
		$(this).removeClass('hover');
		$(this).mouseenter(function(){
			$(this).addClass('hover');
		}).mouseleave(function(){
			$(this).removeClass('hover');
		});
	});
	
	/*
	$('#adresy .adresa').bind('show', function(){
		var content = $('#adresy .adresy_content');
		var index = ($(this).index('.adresa'));
		if($(this).is('.hide')) {
			$('.adresa.show', content).stop().animate({height: 35}, 'slow', function(){
				$(this).removeClass('show').addClass('hide');
			});
			$(this).stop().animate({height: 175}, 'slow', function(){
				$(this).removeClass('hide').addClass('show');
			});
			$('#adresy .sipka').trigger('move', 35*index);
			$('#mapy .mapa').stop().animate({height: 0}, 'slow');
			$('#mapy .mapa:eq(' + index + ')').stop().animate({height: 287}, 'slow');
		}
	}).mouseenter(function() {
		clearInterval(changeAddressInterval);
		$(this).trigger('show');
	}).mouseleave(function() {
		changeAddressInterval = setInterval('changeAddress()', 5000);
	});
	$('#adresy .sipka').each(function(){
		var offset = $(this).position();
		$(this).bind('move', function(e, top){
			if(top>=0) {
				$(this).css({left: offset.left});
				$(this).stop().animate({top: offset.top + top}, 'slow');
			}
		});
	});
	
	$('#adresy .sipka').trigger('move', 0);

	var changeAddressInterval = setInterval('changeAddress()', 5000);
	*/
});

function changeAddress(){
	//$('#adresy .adresa.hide').trigger('show');
}
