$(window).load(function() {

	function initializeMap(){
		$('#dealerlocations').hide();
		$('#map').fadeOut(500, function(){
			$('#map').empty().css({
				width: '924px',
				height: '492px',
				backgroundImage: 'url(/images/dealer-map/europe_base.jpg)',
				position: 'relative'
			});
			$('#map').fadeIn();
			loadBullets('europe', false);
		});
	}

	function addZoomable(id, width, height, top, left){
		$('<img class="zoomable" src="/images/dealer-map/blank.gif" id="' + id + '" />').css({
			border: 'none',
			position: 'absolute',
			width: width + 'px',
			height: height + 'px',
			top: top + 'px',
			left: left + 'px',
			cursor: 'pointer'
		}).appendTo('#map').click(function() {
			$(this).siblings().fadeOut();
			$(this).hide()
				   .attr('src', '/images/dealer-map/' + id + '_base.jpg')
				   .fadeIn('slow')
				   .animate({
						width: '924px',
						height: '492px',
						top: '0px',
						left: '0px'
					}, 1000, '', function(){
						$('#map').css({backgroundImage: 'url(/images/dealer-map/' + id + '_base.jpg)'}).empty();
						loadBullets(id, true);
					});
		});
	
	}
	
	function loadBullets(id, back){
		$('#map').load('/dealer-info/' + id + '.mapinfo', {}, function(){
			//add back button
			if(back){
				$('<a id="mapback" href="javascript:void(0)"><span>return to world map &gt;</span></a>')
					.appendTo(this)
					.click(function(){initializeMap()});
			}
			else{
				//addZoomable('europe', 256, 135, 63, 383);
			}
			//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(){
						showPopup($(this).attr('id'));
						return false;
						});
			});
		});

	}
	
	function showPopup(id){
		$('#map div.popup').fadeOut(); 
		var boxid = '#' + id + '-box';
		$(boxid).fadeIn();
		$('a.close').click(function(){
			$(this).parent().fadeOut();
			return false;
		});
  
//what to do when the 'more information' link is clicked:
	$('.more-dealer-info-link').click(function() {
	moreDealerInfo($(this).attr('href'));
	return false;
	});	
//end that

	}

	
	//initialize map
	initializeMap();
	

});