$(document).ready(function() {
	// -------------------------------------------------------------------------
	// simulacia li:hover pre MSIE 6
	// -------------------------------------------------------------------------
	if ($.browser.msie && $.browser.version == '6.0') {
		$('#hlavne-menu li').hover (
			function() {
				$('ul', this).css('display', 'block');
				$('ul ul', this).css('display', 'none');
			},
			function() {
				$('ul', this).css('display', 'none');
			}
		);
	}

	// -------------------------------------------------------------------------
	// Podmenu druhej úrovne v hlavnom menu - posun o jeho šírku vľavo
	// -------------------------------------------------------------------------
	$('ul#hlavne-menu li:hover > ul.submenu').hover(
		function() {
			$(this).css('display', 'block');
		},
		function() {
			$(this).css('display', 'none');
		}
	);

	/*
	$('#hlavne-menu li li').hover(
		function() {
			var width = $('ul.submenu', this).width();
			width = 0 - width;
			$('ul.submenu', this).css({
					left: width,
					top: 0
			});

			// hover pre MSIE6
			//$('ul', this).css('display', 'block');
		},
			function() {
				//$('ul', this).css('display', 'none');
			}
	);
	*/


	// -------------------------------------------------------------------------
	// Tabuľky
	// -------------------------------------------------------------------------
	var list = $('#zoznam-ajax');
	if (list.length > 0
		&& ($.browser.msie == false
		|| $.browser.version != '6.0')) {

		var module = list.attr('class');

		var headers = $('thead', list);
		var newHeaders = $('<div>').attr('id','hlavicky');

		$('th', headers).each(function(index, value) {
				newHeaders.append($('a', this));
		});

		headers.remove();

		$('div.obal-obsahu', list).before(newHeaders);

		$('th', headers).each(function(index, value) {
				var th = $('table tr:eq(0) td:eq(' + index + ')', list);
				var width = th.width();
				var cssClass = $(this).attr('class');
				$('a:eq(' + index + ')', newHeaders).width(width - 2).attr('class', cssClass);
		});

		var width = $('a:last', newHeaders).width();
		var w1 = list.width();
		var w2 = $('table', list).width();
		width += (w1 - w2);
		$('a:last', newHeaders).width(width);

		$('ul li a', list).click(function() {
				var link = $(this)
				var url = link.attr('href');
				var ajaxurl = url + '&module=gp&cmd=' + module;

				var message = $('<div>')
					.attr('id', 'sprava')
					.css({
							position: 'absolute',
							top: 0,
							left: 0,
							width: '100%'
						})
					.html('<p>Čakajte prosím</p>');

				newHeaders.prepend(message);

				$('p', message).css({
						width: '10em',
						padding: '3px',
						background: 'red',
						color: 'white',
						margin: 'auto',
						textAlign: 'center',
						fontWeight: 'bold'
				});

				$.get(ajaxurl, function(data) {
						$('table', list).html(data);

						$('ul li a', list).removeClass('aktivne');
						link.addClass('aktivne');

						$('a', newHeaders).each(function(index, value) {
								var link = $(this);
								link.removeClass('triedenie hore');
								if (index == 0) {
									link.addClass('triedenie');
								};

								var url = link.attr('href');
								url = url.replace('desc', 'asc');
						});

						$(message).remove();
				});

				return false;
		});

		$('a', newHeaders).click(function() {
				var link = $(this)

				var startchar = $('ul li a.aktivne', list).text();
				if (startchar.length > 1) {
					startchar = 'all';
				}
				startchar = startchar.toLowerCase();

				var url = link.attr('href');
				var ajaxurl = url + '&module=gp&cmd=' + module;
				ajaxurl += '&startchar=' + startchar;

				var message = $('<div>')
					.attr('id', 'sprava')
					.css({
							position: 'absolute',
							top: 0,
							left: 0,
							width: '100%'
						})
					.html('<p>Čakajte prosím</p>');

				newHeaders.prepend(message);

				$('p', message).css({
						width: '10em',
						padding: '3px',
						background: 'red',
						color: 'white',
						margin: 'auto',
						textAlign: 'center',
						fontWeight: 'bold'
				});

				$.get(ajaxurl, function(data) {
						$('table', list).html(data);

						if (link.hasClass('triedenie') == false) {
							$('a', newHeaders).removeClass('triedenie hore');
							link.addClass('triedenie');
							url = url.replace('desc', 'asc');

						} else {
							link.toggleClass('hore');
						}

						if (link.hasClass('hore') == true) {
							url = url.replace('desc', 'asc');

						} else {
							url = url.replace('asc', 'desc');
						}

						link.attr('href', url);
						$(message).remove();
				});

				return false;
		});
	}

	// -------------------------------------------------------------------------
	// Nastavenie rovnakej výšky boxov oznamov na hlavnej stránke
	// Úradné oznamy a najnovšie územné plány
	// -------------------------------------------------------------------------
	var pageId = $('body').attr('id');
	if (pageId == 'hlavna-stranka') {
		var height1 = $('div.oznam:first').height();
		var height2 = $('div.oznam:last').height();

		if (height1 > height2) {
			var d = height1 - height2;
			var h = $('div.oznam:last ul').height();
			h = h + d;
			$('div.oznam:last ul').height(h);

		} else {
			var d = height2 - height1;
			var h = $('div.oznam:first dl').height();
			h = h + d;
			$('div.oznam:first dl').height(h);
		}
	}

	// -------------------------------------------------------------------------
	// Ankety
	// -------------------------------------------------------------------------
	$('div.anketa div#anketa a').live('click', function() {
			var url = $(this).attr('href');
			url += '&ajax';

			$.get(url, function(data) {
					$('div#anketa').html(data);
			});

			return false;
	});
});