function appear(e) {
	if (0 === e.target.className.length) {
		return;
	}
	$('#' + e.target.className).show();
}
function disappear(e) {
	if (0 === e.target.className.length) {
		return;
	}
	$('#' + e.target.className).hide();
}
$(window.document).bind('ready', function() {
	var recursion = 0;
	var tld = window.document.domain.toString();
	tld = tld.substr(tld.length - 2);
	var maxRecursion = 1;
	var ajaxLoaded = { };
	var commonClickHandler = function(id, ids, tabs, s) {
		$('#mapyObal').removeClass('slovakMap').removeClass('czechMap').removeClass('euMap').removeClass('mtMap');
		var className;
		var statValue;
		var $tabSub;
		var $tabMain;
		if (id.indexOf('Sk') !== -1) {
			className = 'slovakMap';
			statValue = 'sk';
			$tabSub = $('#skTabs [href*="#"][rel="skTabs"]').filter(':eq(0)');
			$tabMain = $('[href="#zalSk"]');
		} else if (id.indexOf('Cz') !== -1) {
			statValue = 'cz';
			className = 'czechMap';
			$tabMain = $('[href="#zalCz"]');
			$tabSub = $('#czTabs [href*="#"][rel="czTabs"]').filter(':eq(0)');
		} else if (id.indexOf('Eu') !== -1) {
			className = 'euMap';
			statValue = 'eu';
			$tabSub = $('#euTabs [href*="#"][rel="euTabs"]').filter(':eq(0)');
			$tabMain = $('[href="#zalEu"]');
		} else if (id.indexOf('Mt') !== -1) {
			className = 'mtMap';
			statValue = 'mt';
			$tabSub = $('#mtTabs [href*="#"][rel="mtTabs"]').filter(':eq(0)');
			$tabMain = $('[href="#zalMt"]');
		} else {
			return true;
		}
		$('.statSelect').val(statValue);
		$('#mapyObal').addClass(className);
		if (recursion < maxRecursion) {
			recursion++;
			$tabMain.trigger('click');
			//~ setTimeout(function() {
				//~ $tabSub.trigger('click');
			//~ }, 5);
			recursion--;
		}
		if ('zal' !== id.substr(1, 3)) {
			if (-1 !== window.document.location.search.indexOf('noAjax')) {
				return true;
			}
			var type = id.substr(1);
			if (!(type in ajaxLoaded)) {
				ajaxLoaded[type] = 'loading';
				$(id + 'Container').load('/load?doc=map&t=' + type, function(responseText, textStatus, XMLHttpRequest) {
					ajaxLoaded[type] = 'loaded';
				});
			}
		}
		return true;
	};
	var mapkaClickHandler = function(id, ids, tabs, s) {
		if ('#a' === id) {
			return true;
		}
		var type;
		switch ($('[href="' + id + '"]').attr('rel')) {
			case 'mapka_cr':
				type = 'Cz';
				break;
			case 'mapka_sk':
				type = 'Sk';
				break;
		}
		if (!(type in ajaxLoaded)) {
			if (-1 !== window.document.location.search.indexOf('noAjax')) {
				return true;
			}
			ajaxLoaded[type] = 'loading';
			$('#' + type + 'RegionsContainer').show().load('/load?doc=regions&t=' + type, function(responseText, textStatus, XMLHttpRequest) {
				ajaxLoaded[type] = 'loaded';
				$(id).show();
			});
		}
		return true;
	};
	if (window.document.location.pathname === '/') {
		$('#mapyTabs').idTabsE({ 'click': commonClickHandler });
		$('#euTabs').idTabsE({ 'click': commonClickHandler });
		$('#mtTabs').idTabsE({ 'click': commonClickHandler });
		if ('cz' === tld) {
			$('#skTabs').idTabsE({ 'click': commonClickHandler });
			$('#czTabs').idTabsE({ 'click': commonClickHandler });
		} else {
			$('#czTabs').idTabsE({ 'click': commonClickHandler });
			$('#skTabs').idTabsE({ 'click': commonClickHandler });
		}
		$('.mapka area').bind('mouseout click', disappear);
		$('.mapka area').bind('mouseover', appear);
		$('.mapka').idTabsE({ 'click': mapkaClickHandler });
		$('.closeRegion').live('click', function(e) {
			var $tabs = $('#' + e.target.className.replace(/.*tabs-(\w+).*/, '$1'));
			if ($tabs.length === 0) {
				return;
			}
			$('[href*="#"]', $tabs).filter(':eq(0)').trigger('click');
		});
		$('#mapyTabs [href*="#"][rel="mapyTabs"]').bind('click', function(e) {
			var $tabSub;
			if (e.target.href.indexOf('Sk') !== -1) {
				$tabSub = $('#skTabs [href*="#"][rel="skTabs"]').filter(':eq(0)');
			} else if (e.target.href.indexOf('Cz') !== -1) {
				$tabSub = $('#czTabs [href*="#"][rel="czTabs"]').filter(':eq(0)');
			} else if (e.target.href.indexOf('Eu') !== -1) {
				$tabSub = $('#euTabs [href*="#"][rel="euTabs"]').filter(':eq(0)');
			} else if (e.target.href.indexOf('Mt') !== -1) {
				$tabSub = $('#mtTabs [href*="#"][rel="mtTabs"]').filter(':eq(0)');
			} else {
				return;
			}
			$tabSub.trigger('click');
		});
		$('.statSelect').bind('change', function(e) {
			$('#mapyTabs .' + $(e.target).val() + ' [href*="#"][rel="mapyTabs"]').filter(':eq(0)').trigger('click');
		});
		var hash = window.document.location.hash;
		window.document.location.hash = '';
		if (hash !== '' && hash !== '#') {
			setTimeout(function() {
				$('[href="' + hash + '"]').trigger('click');
			}, 200);
		}
		$('.mapyTabs li').bind('click', function(e) {
			$(e.currentTarget).find('[href*="#"][rel="mapyTabs"]').filter(':eq(0)').trigger('click');
		});
	} else {
		$('.mapyTabs [href*="#"]').bind('click', function(e) {
			event.stopPropagation();
		});
		$('.mapyTabs li').bind('click', function(e) {
			window.document.location = $(e.currentTarget).find('[href*="#"][rel="mapyTabs"]').attr('href');
		});
	}
	
});

