$(window).load(function() {

	function initializeMap(){
		$('#searchlocations').hide();
		$('#map').fadeOut(500, function(){
			$('#map').empty().css({
				width: '426px',
				height: '248px',
				backgroundImage: 'url(img/mapa.gif)',
				position: 'relative'
			});
			$('#map').fadeIn();
			loadBullets('cr', false);
		});
	}

	function loadBullets(id, back){
		$('#map').load('engine-ajax.php', {action: "getbullets"}, function(){
		//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()
					   .mouseover(function(){showPopup($(this).attr('id'));})
					   .mouseout(function(){hidePopup($(this).attr('id'));});
			});
		});
	}
	
	function showPopup(id){
		//$('#map div.popup').fadeOut(); 
		
		var boxid = '#' + id + '-box';
		$(boxid).fadeIn();
		$('a.close').click(function(){
			$(this).parent().fadeOut();
		});
	}
	
	function hidePopup(id){
		var boxid = '#' + id + '-box';
		$(boxid).fadeOut();
	}	
	
	//initialize map
	initializeMap();
	

});