$(document).ready(function() {
	$('.hotspots .areas a')
		.hover(
			function() { // Handler in
				$('.hotspots .map img').hide();

				$('.hotspots .map .' + $(this).attr('class')).show();
			},
			function() { // Handler out
				$('.hotspots .map img').hide();

				if($('.hotspots .areas .active').length > 0) {
					$('.hotspots .map .' + $('.hotspots .areas .active a').attr('class')).show();
				} else {
					$('.hotspots .map .empty').show();
				}
			}
		)
		.click(
			function() {
				// Reset the current active link
				$('.hotspots .areas li').removeClass('active');

				// Set this link as active
				$(this).parents('li').addClass('active');

				// Hide all maps
				$('.hotspots .map img').hide();

				// Show the corret map
				$('.hotspots .map .' + $(this).attr('class')).show();

				if($(this).parent().hasClass('last')) {
					// Remove the location data on show all
					$('.area', $('.hotspots')).remove();
				} else {
					// Get the location data
					jQuery.ajax({
						type: 'get',
						url: $(this).attr('href') + '?view=Ajax',
						dataType: 'html',
						success: function(data) {
							var area = $('.area', $('.hotspots'));

							if(area.length > 0) {
								area.replaceWith(data);
							} else {
								$('.hotspots').append(data);
							}
						}
					});
				}

				// Set the id as anchor
				document.location.href = document.location.href.split('#')[0] + '#' + $(this).attr('class');

				// Do not execute href
				return false;
			}
		);

	// Imagemap
	$('#imagemap area').click(function() {
		// Extract the last path of the href and use it as id
		var pathArray = $(this).attr('href').split('/');
		var id = pathArray[pathArray.length - 1];

		// Trigger the click
		$('.hotspots .areas .' + id).click();

		return false;
	});

	// Get the document location
	var docLocation = document.location.toString();

	// Check for anchor
	if (docLocation.match('#')) {
		// Get the anchor value
		var anchorName = docLocation.split('#')[1];

		// Load selected area
		$('.hotspots .areas .' + anchorName).click();
	}
});
