(function($) {
	$.fn.handleMaps = function() {
		return this.each(function() {

			var $this = $(this),
			title = $this.data('title'),
			posX = $this.data('posx'),
			posY = $this.data('posy'),
			$maps = $('.find-box .map'),
			$kursistMap = $("#kursist .map"),
			$kommuneMap = $("#kommune .map"),
			dot = '<div title="' + title + '" class="dot">' + $this.html() + '</div>';

			$(dot).appendTo($maps).css({ 'left': posX-8, 'top': posY-8 }).hover(function(){
				$('a', this).show();
			}, function(){
				$('a', this).hide();
			}).click(function(){
				window.location = $('a', this).attr('href');
			});
			if ($kursistMap.length) {
				var kursistRef = $(dot).find('a').attr('href') + 'kursist';
				$(dot).find('a').attr('href', kursistRef).end().appendTo($kursistMap).css({ 'left': posX-8, 'top': posY-8 }).live({ mouseenter: function(){
					$('a', this).show();
				}, mouseleave: function(){
					$('a', this).hide();
				}}).live('click', function(){
					window.location = $('a', this).attr('href');
				});
			}
			if ($kommuneMap.length) {
				var kommuneRef = $(dot).find('a').attr('href') + 'kommune';
				$(dot).find('a').attr('href', kommuneRef).end().appendTo($kommuneMap).css({ 'left': posX-8, 'top': posY-8 }).live({ mouseenter: function(){
					$('a', this).show();
				}, mouseleave: function(){
					$('a', this).hide();
				}}).live('click', function(){
					window.location = $('a', this).attr('href');
				});
			}

			$('a', $this).live({ mouseenter: function(){
				$maps.find('.dot[title=' + title + '] a').show();
				$maps.find('.specialDot[title=' + title + '] a').show();
			}, mouseleave: function(){
				$maps.find('.dot[title=' + title + '] a').hide();
				$maps.find('.specialDot[title=' + title + '] a').hide();
			}});

		});
	};
}(jQuery));
