$(window).load(function() {

	function initializeMap(){
		$('#searchlocations').hide();
		$('#map').fadeOut(500, function(){
			$('#map').empty().css({
				width: '445px',
				height: '475px',
				backgroundImage: 'url(../Global_Include/MBFC_Map/images/map/map_base.jpg)',
				position: 'relative'
			});
			$('#map').fadeIn();
			loadBullets('../Global_Include/MBFC_Map/map-info/Bullets_3.asp', false);
		});
	}
	
	function loadBullets(id, back){

		
		$('#map').load('../Global_Include/MBFC_Map/map-info/Bullets_3.asp', {}, function(){
			//add back button
			if(back){
				$('<a id="mapback" href="javascript:void(0)"><span>return to full map &gt;</span></a>')
					.appendTo(this)
					.click(function(){initializeMap()});
			}
			
			//place bullets
			$(this).children('a.bullet').each(function(){
				var coords = $(this).attr('rel').split('-');
				$(this).css({left: coords[0] + 'px', top: coords[1] + 'px'})
					   .hide()
					   .fadeIn()
					   .click(function(){$('a.bullet').removeClass('active');  })
					   .click(function(){$('#static-text').fadeOut('fast');  })
					   .click(function(){$(this).addClass('active');  })
					   .click(function(){showPopup($(this).attr('id'));  });
			});
		});
	}
		
	function showPopup(id){
		$('#map div.popup').fadeOut(); 
		var boxid = '#' + id + '-box';
		$(boxid).fadeIn();
		$('a.close').click(function(){
			$(this).parent().fadeOut();
		});
		$('a.close').click(function(){$('a.bullet').removeClass('active');  })
		$('a.close').click(function(){$('#static-text').fadeIn('fast');  })
	}
		
	//show & hide pins from legend
	$("li.legend_1").click(function(){
		$("a.blue").fadeIn("normal");
		$("a.black").fadeOut("fast");
		$("a.purple").fadeOut("fast");
		$("li.legend_1").removeClass("grayedOut");
		$("li.legend_2").addClass("grayedOut");
		$("li.legend_3").addClass("grayedOut");
		$("li.legend_4").addClass("grayedOut");
		$("#static-text").fadeOut("fast");
		//fades in the specified info box when legend item is clicked
		$('#map div.popup').fadeOut(); 
		$("#map div.popup#alex-blum-box").fadeIn();
		$('a.bullet').removeClass('active');
		$("a#alex-blum").addClass('active');
		$('a.close').click(function(){
			$("#map div.popup#alex-blum-box").fadeOut();
			$("#static-text").fadeIn("fast");
		});
	});
	
	$("li.legend_2").click(function(){
		$("a.black").fadeIn("normal");
		$("a.blue").fadeOut("fast");
		$("a.purple").fadeOut("fast");
		$("li.legend_2").removeClass("grayedOut");
		$("li.legend_1").addClass("grayedOut");
		$("li.legend_3").addClass("grayedOut");
		$("li.legend_4").addClass("grayedOut");
		$("#static-text").fadeOut("fast");
		//fades in the specified info box when legend item is clicked
		$('#map div.popup').fadeOut(); 
		$("#map div.popup#carol-anderson-box").fadeIn();
		$('a.bullet').removeClass('active');
		$("a#carol-anderson").addClass('active');
		$('a.close').click(function(){
			$("#map div.popup#carol-anderson-box").fadeOut();
			$("#map div.popup a#carol-anderson").removeClass('active');
			$("#static-text").fadeIn("fast");
		});
	});
	
	$("li.legend_3").click(function(){
		$("a.purple").fadeIn("normal");
		$("a.blue").fadeOut("fast");
		$("a.black").fadeOut("fast");
		$("li.legend_3").removeClass("grayedOut");
		$("li.legend_1").addClass("grayedOut");
		$("li.legend_2").addClass("grayedOut");
		$("li.legend_4").addClass("grayedOut");
		$("#static-text").fadeOut("fast");
		//fades in the specified info box when legend item is clicked
		$('#map div.popup').fadeOut(); 
		$("#map div.popup#earl-anderson-box").fadeIn();
		$('a.bullet').removeClass('active');
		$("a#earl-anderson").addClass('active');
		$('a.close').click(function(){
			$("#map div.popup#earl-anderson-box").fadeOut();
			$("#static-text").fadeIn("fast");
		});
	});
	
	$(".showAll").click(function(){
		$("a.bullet").fadeIn("normal");
		$("li.legend_1").removeClass("grayedOut");
		$("li.legend_2").removeClass("grayedOut");
		$("li.legend_3").removeClass("grayedOut");
		$("li.legend_4").removeClass("grayedOut");
		$('a.bullet').removeClass('active');
		$('#map div.popup').fadeOut(); 
		$("#static-text").fadeIn("fast");
	});
	
	
	//initialize map
	initializeMap();
	
	

});


