$(window).load(function() {

	function initializeMap(){
		$('#searchlocations').hide();
		$('#map').fadeOut(500, function(){
			$('#map').empty().css({
				width: '434px',
				height: '567px',
				backgroundImage: 'url(distributors-no-flash/images/map/hpc-distributor-map.jpg)',
				position: 'relative'
			});
			$('#map').fadeIn();
			loadBullets('coast', false);
		});
	}

	function addZoomable(id, width, height, top, left){
		$('<img class="zoomable" src="distributors-no-flash/images/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', 'distributors-no-flash/images/map/' + id + '_base.jpg')
				   .fadeIn('slow')
				   .animate({
						width: '416px',
						height: '567px',
						top: '0px',
						left: '0px'
					}, 1000, '', function(){
						$('#map').css({backgroundImage: 'url(distributors-no-flash/images/map/' + id + '_base.jpg)'}).empty();
						loadBullets(id, true);
					});
		});
	}
	
	function loadBullets(id, back){
		//alert("loading bullets");
		$('#map').load('dist-list-non-flash.asp', {}, function(){
			//place bullets
		//alert("placing bullets");
			$(this).children('a.bullet').each(function(){
				var coords = $(this).attr('rel').split('-');
				//line below has factor to reflect 'squished' flash movie
				$(this).css({left: ((coords[0]*(416/490))-(-6)) + 'px', top: ((coords[1]*(567/640))-6) + 'px'})
					   .hide()
					   .fadeIn()
					   .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();
		});
	}

	
	//initialize map
	initializeMap();
	

});
