$(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');
			}
		);
	}

	// -------------------------------------------------------------------------
	// Otváranie externých odkazov v novom okne
	// -------------------------------------------------------------------------
	var host = window.location.protocol + '//';
	host += window.location.host;
	$('a').each(function() {
			var url = $(this).attr('href');

			if (url.substr(0, 4) == 'http'
				&& url.substr(0, host.length) != host) {

				$(this).click(function(event) {
						event.preventDefault();
						event.stopPropagation();
						window.open(this.href, '_blank');
				});
			}
	});

	$('a.nove-okno').each(function() {
			$(this).click(function(event) {
					event.preventDefault();
					event.stopPropagation();
					window.open(this.href, '_blank');
			});
	});

	var settings = {
		zIndex: 9000,
		width: 800,
		height: 500,
		modal: true
	}

	$('a.dialog').click(function() {
			var url = $(this).attr('href');
			$('<div id="o-ui-dialog"></div>').dialog(settings);
			$('div#o-ui-dialog').load(url);
			return false;
	});

	$('a.banner').each(function() {
			$(this).click(function(event) {
					event.preventDefault();
					event.stopPropagation();
					window.open(this.href, '_blank');
			});
	});

	/*
	$('li.forum a').each(function() {
			$(this).click(function(event) {
					event.preventDefault();
					event.stopPropagation();
					window.open(this.href, '_blank');
			});
	});
	*/

	$('#forum-info a').each(function() {
			$(this).click(function(event) {
					event.preventDefault();
					event.stopPropagation();
					window.open(this.href, '_blank');
			});
	});


	// -------------------------------------------------------------------------
	// previesť multiselect na skupinu checkboxov
	// -------------------------------------------------------------------------
	var multiSelects = $('select[multiple="multiple"]');
	if (multiSelects.length > 0) {
		multiSelects.toChecklist();
		$('p.zameranie-specializacia em').remove();
	}


	// -------------------------------------------------------------------------
	// Podmenu druhej úrovne v hlavnom menu - posun o jeho šírku vľavo
	// -------------------------------------------------------------------------
	$('#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.panel div#anketa a').live('click', function() {
			var url = $(this).attr('href');
			url += '&ajax';

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

			return false;
	});
	*/


	// -------------------------------------------------------------------------
	// Klikacie nadpisy v Aktualitách
	// -------------------------------------------------------------------------
	$('div.aktualita').each(function(index, value) {
			var news = $(value);
			var url = $('p.viac a', news).attr('href');
			if (url != undefined) {
				var text = $('h3', news).text();
				$('h3', news).replaceWith('<h3 class="article-title"><a href="' + url + '">' + text + '</a></h3>');
			}
	});




	// -------------------------------------------------------------------------
	// Užívateľská fotogaléria
	// -------------------------------------------------------------------------
	var uploadifyForm = $('input#files-select');

	if (uploadifyForm.length > 0) {
		uploadifyForm.uploadify({
			uploader: systemPath + 'swf/uploadify.swf',
			script: systemPath,
			scriptData: {module:'gp', cmd:'upload', type:'usergallery', sid: sid},
			method: 'GET',
			cancelImg: systemPath + 'grafika/cancel.png',
			auto: false,
			buttonText: 'Vyberte obrázky',
			fileExt: '*.jpg;*.jpeg;*.JPG;*.JPEG',
			fileDesc: 'Obrázky (.JPG, .JPEG)',
			multi: true,
			onInit: function() {uploadifyInit()},
			onSelect: function() {$('p.sprava').remove()}
			//onAllComplete: function() {window.location.reload()}
		  });

		$('a.zmazat').click(function() {
				var answer = confirm('Naozaj si prajete zmazať túto položku?');
				return answer;
		});
	}

	// -------------------------------------------------------------------------
	// Záložky
	// -------------------------------------------------------------------------
	$('#tabs').tabs({
			create: function() {
				tabsScroller($(this));
			}

	});


	// -------------------------------------------------------------------------
	// Komentáre - prílohy
	// -------------------------------------------------------------------------
	/*
	var uploadifyForm = $('form#komentar p.prilohy input[type=file]');

	if (uploadifyForm.length > 0) {
		uploadifyForm.each(function(i, el) {
				if (i > 0) {
					$(el).remove();
				}

		});

		$('form#komentar p.prilohy br').remove();

		uploadifyForm.eq(0).uploadify({
			uploader: systemPath + 'swf/uploadify.swf',
			script: systemPath,
			scriptData: {module:'discussions', cmd:'upload', sid:sid},
			method: 'GET',
			cancelImg: systemPath + 'grafika/cancel.png',
			auto: true,
			buttonText: 'Vyberte prílohy',
			fileExt: '*.jpg;*.jpeg;*.JPG;*.JPEG;*.pdf;*.PDF',
			fileDesc: 'Súbory (.JPG, .JPEG, .PDF)',
			multi: true,
			queueSizeLimit: 3,
			onInit: function() {uploadifyInit()},
			onSelect: function() {$('p.sprava').remove()},
			onAllComplete: function(event,data) {
				document.getElementByID('komentar').submit();
			}
		  });
	}
	*/

	// -------------------------------------------------------------------------
	// ---
	// -------------------------------------------------------------------------
	$('p#uprava-konta a').button({
			icons: {
				primary: 'ui-icon-pencil'
			}
	});

	$('input[type=submit]').button();
	$('input[type=reset]').button();
	$('p.strankovanie a').button();
	$('p.paginator a').button();
	$('ul.paginator a').button();

	// -------------------------------------------------------------------------
	// Vyhľadávanie
	// -------------------------------------------------------------------------
	$('div#horna-lista input[name=search]').focus(function() {
			var term = $(this).val();

			if (term == 'Vyhľadávanie') {
				$(this).val('');
			}
	});

	$('div#horna-lista input[name=search]').autocomplete({
			source: 'http://www.uzemneplany.sk/?module=gp&cmd=search',
			minLength: 2,
			select: function(event, ui) {
				if (ui.item.type == 'more') {
					term = $(this).val();
					ui.item.url += term + '*';
				}

				window.location = ui.item.url;
				return false;
			},
			search: function(event, ui) {
				$('div#horna-lista form').append('<p id="loading">Vyhľadávam ...</p>');
				//console.log('start');
			}
	}).data('autocomplete')._renderItem = function(ul, item) {
			$('div#horna-lista form #loading').remove();

			ul.addClass('vyhladavanie');

			if (item.type == 'more') {
				var term = this.term;
				return $('<li class="viac"></li>')
					.data('item.autocomplete', item)
					.append('<a href="' + item.url + term + '">' + item.label + '</a>')
					.appendTo(ul);
			}

			return $('<li></li>')
				.data('item.autocomplete', item)
				.append('<a href="' + item.url +  '"><img src="' + item.image + '" alt="' + item.label + '" /><strong>' + item.label + '</strong><em>' + item.type + '</em><span class="cistic"></span></a>')
				.appendTo(ul);
	};
});

function uploadifyInit() {
	$('form#uploadify label[for=pictures]').remove();
	$('form#uploadify br').remove();
	$('form#uploadify p').css('margin', 0);

	$('form#uploadify p.buttons input[name=send]').click(function() {
			$('input#files-select').uploadifyUpload();
			return false;
	});
}

function getBaseURL() {
	var url = location.href;
	var baseURL = url.substring(0, url.indexOf('/', 14));


	if (baseURL.indexOf('http://localhost') != -1) {
		var url = location.href;
		var pathname = location.pathname;
		var index1 = url.indexOf(pathname);
		var index2 = url.indexOf('/', index1 + 1);
		var baseLocalUrl = url.substr(0, index2);

		return baseLocalUrl + '/';
	}
	else {
		return baseURL + '/';
	}
}

function getUrlHash(url) {
	var hash = url.substring(0, url.indexOf('#'));
	return hash;
}

function tabsScroller(tabs) {
	var tabsPanel = $('<div class="ui-tabs-nav-wrapper" />');
	tabsPanel.css({
			position: 'relative',
			width: '100%',
			overflow: 'hidden'
	});

	$('ul.ui-tabs-nav', tabs).wrap(tabsPanel);

	tabsPanel = $('div.ui-tabs-nav-wrapper', tabs);

	var tabsPanelWidth = 0;
	$('ul.ui-tabs-nav li', tabs).each(function(index, tab) {
			var tab = $(tab);
			tabsPanelWidth += tab.outerWidth();

			var leftMargin = tab.css('marginLeft');
			leftMargin = leftMargin.replace('px', '');
			tabsPanelWidth += parseInt(leftMargin);

			var rightMargin = tab.css('marginRight');
			rightMargin = rightMargin.replace('px', '');
			tabsPanelWidth += parseInt(rightMargin);
	});

	if (tabsPanel.width() < tabsPanelWidth) {
		$('ul.ui-tabs-nav', tabs).width(tabsPanelWidth);

		var buttonHeight = tabsPanel.height();

		var top = tabs.css('paddingTop');
		top = top.replace('px', '');
		top = parseInt(top);

		var right = tabs.css('paddingRight');
		right = right.replace('px', '');
		right = parseInt(right);
		right -= 2;

		var rightButton = $('<div class="ui-tabs-nav-scroll-button ui-tabs-nav-scroll-button-right" />');
		rightButton.css({
				position: 'absolute',
				top: top + 'px',
				right: right + 'px',
				width: '32px',
				height: buttonHeight + 'px'
		});

		rightButton.button({
				icons: {
					primary: 'ui-icon-circle-triangle-e'
				}
		});

		tabs.append(rightButton);

		var leftButton = $('<div class="ui-tabs-nav-scroll-button ui-tabs-nav-scroll-button-left" />');
		leftButton.css({
				position: 'absolute',
				top: top + 'px',
				right: rightButton.outerWidth() + right + 'px',
				width: '32px',
				height: buttonHeight + 'px'
		});

		leftButton.button({
				icons: {
					primary: 'ui-icon-circle-triangle-w'
				}
		});

		tabs.append(leftButton);

		var buttonWidth = leftButton.width();
		var iconLeftPosition = (buttonWidth - 16) / 2;

		$('div.ui-tabs-nav-scroll-button span.ui-icon', tabs).css('left', iconLeftPosition + 'px');

		tabsPanelWidth += (buttonWidth * 2);
		$('ul.ui-tabs-nav').width(tabsPanelWidth);

		var buttonHeight = tabsPanel.height() + (buttonHeight - rightButton.outerHeight());
		$('div.ui-tabs-nav-scroll-button').height(buttonHeight);

		rightButton.click(function() {
				tabsPanel.animate({
						scrollLeft: tabsPanel.scrollLeft() + 250
				})
		});

		leftButton.click(function() {
				tabsPanel.animate({
						scrollLeft: tabsPanel.scrollLeft() - 250
				})
		});
	}
}
