$(function () {

	$('table#vypis tr.odd td, table#vypis tr.even td').hover(function() {
		$(this).parent().addClass('hover');
		$('table#vypis thead th').removeClass('hover');
		$('table#vypis thead th').eq($(this).parent().find('td').index(this)).addClass('hover');
	},function() {
		$(this).parent().removeClass('hover');
		$('table#vypis thead th').removeClass('hover');
	})
	$('table#vypis tr.odd, table#vypis tr.even').hover(function () {
		var c = $(this).attr('data-buldingnumber')
		cisloBudovy(c);
	}, function () {
		$('#map .mapouter').css('background-position','0px 0px');
	})
	.click(function () {
		window.location = $('a',this).attr('href');
	});


	$('#detail-list .item').hover(function () {
		$(this).addClass('hover');
	},function () {
		$(this).removeClass('hover');
	});

	$('.galerie-block .galerie .move-left').click(function () {
		var n = $('.galerie .item:visible:first',$(this).parent().parent()).prev();
		if (n.is('.item'))
		{
			$('.galerie .move-left, .galerie .move-right',$(this).parent().parent()).show();
			$('.galerie .item',$(this).parent().parent()).stop(false,true);
			$('.galerie .item:visible:last',$(this).parent().parent())
			.animate({ width: '0px'},function () { $(this).hide()});
			n
			.animate({ width: '135px'},function () { $(this).show()});
			if (!n.prev().is('.item'))
				$(this).hide();
		}
		return false;
	});
	$('.galerie-block .galerie .move-right').click(function () {
		var n = $('.galerie .item:visible:last',$(this).parent().parent()).next();
		if (n.is('.item'))
		{
			$('.galerie .move-left, .galerie .move-right',$(this).parent().parent()).show();
			$('.galerie .item',$(this).parent().parent()).stop(false,true);
			$('.galerie .item:visible:first',$(this).parent().parent())
			.animate({ width: '0px'},function () { $(this).hide()});
			n
			.animate({ width: '135px'},function () { $(this).show()});
			if (!n.next().is('.item'))
				$(this).hide();
		}
		return false;
	});
	$('.galerie-block .galerie .item').click(function () {
		var href = $(this).attr('href');
		var src = $(this).attr('data-imgsrc');
		var title = $(this).attr('title');
		var viewer = $('.viewer',$(this).parent().parent());
		var img = viewer.find('img');
		if (viewer.is(':visible') && src == img.attr('src'))
			viewer.click();
		else
		{
			img.hide();
			img.parent().find('.loader').show();
			img.attr('src',src).attr('title',title+ ' (kliknutím otevřít do nového okna)').attr('alt',title+ ' (kliknutím otevřít do nového okna)');
			viewer.find('h2 a').html(title+' <small>(otevřít v plné velikosti)</small>').attr('href',href).attr('title',title);
			viewer.show();
			viewer.animate({ height: '440px'});
		}
		return false;
	});
	$('.galerie-block .viewer').click(function () {
		$(this).animate({ height: '0px'},function (){
			$(this).hide();
			if ($(this).parent().parent().parent().is('.lightbox'))
				$(this).parent().parent().parent().find('.lightbox-cover').click();
		});
	});
	$('.galerie-block .viewer img').load(function () {
		$(this).show();
		$(this).parent().find('.loader').hide();
	}).click(function () {
		$(this).parent().find('h2 a').click();
		return false;
	});
	$('.galerie-block .viewer h2 a').click(function () {
		window.open($(this).attr('href'),$(this).attr('target'));
		return false;
	});
	$('.galerie-block .viewer a.close').click(function () {
		$(this).parent('.viewer').click();
		return false;
	});



	function lightbox(href, lightbox, after)
	{
		var a = $('.lightbox-box .galerie-block .galerie .item[href='+href+']',lightbox);
		if (a.is('a'))
		{
			$('.lightbox-cover',lightbox).css('opacity','0.9');
			if ($('.lightbox-cover',lightbox).css('position')=='absolute')
			{
				$('.lightbox-cover',lightbox)
				.css('height',$('body').height())
				.css('width',$('html').width())
				.css('left',-1*$('body').offset().left);
			}
			setTimeout(function () {
				$(lightbox).show('slow',after);
			},10);
			a.click();
			return true;
		}
		return false;
	}

	$('.lightbox .lightbox-cover').click(function () {
		$(this).hide('slow',function () {
			$(this).parent().hide();
			$(this).parent().find('> *').show();
		});
		$(this).parent().find('.lightbox-box').hide('fast');
	});

	$('#aside-galerie > a, .site-galerie > a').click(function () {
		var lb = $(this).parent().next();
		if (lb.is('.lightbox'))
		{
			if ($.browser.mozilla)
				lb.find('~ .site-galerie').css('opacity',0.1);
			return ! lightbox($(this).attr('href'), lb, function () { $('.site-galerie').css('opacity',1);});
		}
		else console.log(lightbox);
		return false;
	});

	$('img').each(function () {
		$(this).attr('galleryimg','false');
	});



});

function cufonize() {
	Cufon.replace('#header #nav menu li a',{ hover: true });
	Cufon.replace('#section #article h1, #section #article h2, #section #article h3, #section #article h4, #aside #contact h3');
	Cufon.replace('.cufon');
}

var cisloBudovyToOrder = {};
$.each(['2','3','4','5','6','7','8','21','22','23','23a','24','25','26','27','45','46','47','61','63','79','83','84','84a','85'], function (i, val) {
	cisloBudovyToOrder[val] = i+1;
});
function cisloBudovy(c)
{
	if (cisloBudovyToOrder[c])
		$('#map .mapouter').css('background-position',((cisloBudovyToOrder[c]*-250)-1)+'px 0px');
	else
		$('#map .mapouter').css('background-position','0px 0px');
}

