$(document).ready(function() {

	/*
	
	This code is shared between the map on the homepage and the map in the primary
	nav.  That's why you see .pareents('.map-navigation').find(  .. everywhere.  I'm 
	making sure that the selectors don't affect both maps at once.	
	
	*/
	
	
	$('.pa-areamap-area').mouseover(function(e){
		$(this).closest('.map-navigation').find('.pa-state-overlay').show();
		$(this).closest('.map-navigation').find('.wv-state-overlay').hide();
	});
	
	$('.pa-state-overlay').mouseout(function(e){
		$(this).closest('.map-navigation').find('.pa-state-overlay').hide();		
	});	

	$('.wv-areamap-area').mouseover(function(e){
		$(this).closest('.map-navigation').find('.wv-state-overlay').show();
		$(this).closest('.map-navigation').find('.pa-state-overlay').hide();
	});
	
	$('.wv-state-overlay').mouseout(function(e){
		$(this).closest('.map-navigation').find('.wv-state-overlay').hide();
	});		

	$('.pa-state-overlay').click(function(e){
		$(this).closest('.map-navigation').find('.pa-state-overlay').hide();
		$(this).closest('.map-navigation').find('#magnifing-glass').hide();
		$(this).closest('.map-navigation').find('.pa-state-expanded').show();
		
		$(this).closest('.map-navigation').find('.pa-map-point').show();
	});
	
	$('.wv-state-overlay').click(function(e){
		$(this).closest('.map-navigation').find('.wv-state-overlay').hide();
		$(this).closest('.map-navigation').find('#magnifing-glass').hide();
		$(this).closest('.map-navigation').find('.wv-state-expanded').show();
		
		$(this).closest('.map-navigation').find('.wv-map-point').show();
	});
	
	$('.pa-state-close-button').click(function(e){
		$(this).closest('.map-navigation').find('.pa-state-expanded').hide();		
		$(this).closest('.map-navigation').find('.pa-state-overlay').hide();
		$(this).closest('.map-navigation').find('#magnifing-glass').show();		
	});
	
	$('.wv-state-close-button').click(function(e){
		$(this).closest('.map-navigation').find('.wv-state-expanded').hide();		
		$(this).closest('.map-navigation').find('.wv-state-overlay').hide();
		$(this).closest('.map-navigation').find('#magnifing-glass').show();		
	});	
	
	$('.map-point').mouseover(function(e){
		var map_tip = $(this).parent().find('.map-tip');
		var map_point = this;
		
		var map_point_position = $(map_point).position();
		
		var map_tip_left_offset = (map_tip.width() / 2) - 8;
		var map_tip_top_offset  = map_tip.height() + 3;
		
		var map_top_position_top = (map_point_position.top - map_tip_top_offset) + "px";
		var map_top_position_left = (map_point_position.left - map_tip_left_offset) + "px";
		
		$(map_tip).css('top', map_top_position_top);
		$(map_tip).css('left',map_top_position_left);
		
		$(map_tip).show();
		
	});
	
	$('.map-point').mouseout(function(e){
		var map_tip = $(this).parent().find('.map-tip');
		$(map_tip).hide();
	});
	
	$('.map-point').click(function(e){
		window.location = $(this).attr('href');
	});
	
	$('.map-view-selector-list-view').click(function(e){
		// Show list view
		$(this).parent().find('.map-view-selector-map-view').removeClass('selected');
		$(this).addClass('selected');
		
		$(this).parent().find('.map-navigation').hide();
		$(this).parent().find('#list-view').show();
		
		$(this).parent().find('.list-view-inner-box').jScrollPane({
			showArrows: true/*,
			horizontalGutter: 26,
			verticalDragMinWidth: 21,
			verticalDragMaxWidth: 21
			*/
		});
		
		var element = $(this).closest('.map-background');
		
		$(element).removeClass('map-background');
		$(element).addClass('map-background-removed');
	});
	
	$('.map-view-selector-map-view').click(function(e){
		$(this).parent().find('.map-view-selector-list-view').removeClass('selected');
		$(this).addClass('selected');
		
		$(this).parent().find('.map-navigation').show();
		$(this).parent().find('#list-view').hide();
		
		var element = $(this).closest('.map-background-removed');
		
		$(element).removeClass('map-background-removed');
		$(element).addClass('map-background');		
	});
	
});
