$(window).load(function() {

	function initializeMap(){
		$('#searchlocations').hide();
		$('#map').fadeOut(500, function(){
			$('#map').empty().css({
				width: '700px',
				height: '495px',
				backgroundImage: 'url(images/Bethlehem.jpg)',
				position: 'relative'
			});
			$('#map').fadeIn();
			loadBullets('hebron-all-small', false);
		});
	}

	function addZoomable(id, width, height, top, left){
		$('<img class="zoomable" src="images/blank.gif" id="' + id + '" />').css({
			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/' + id + '.jpg')
				   .fadeIn('slow')
				   .animate({
						width: '695px',
						height: '490px',
						top: '0px',
						left: '0px'
					}, 1000, '', function(){
						$('#map').css({backgroundImage: 'url(images/' + id + '.jpg)'}).empty();
						loadBullets(id, true);
					});
		});
	}
	
	function loadBullets(id, back){
		$('#map').load('popups/'+ id +'.php', {}, function(){
			//add back button
			if(back){
				$('<a id="mapback" href="javascript:void(0)"><span>return to Bethlehem Map </span></a>')
					.appendTo(this)
					.click(function(){initializeMap()});
			}
			else{
				addZoomable('bethlehem_small1', 349, 247, 0, 0);
				addZoomable('bethlehem_small2', 349, 247, 0, 349);
				addZoomable('bethlehem_small3', 349, 247, 247, 0);
				addZoomable('bethlehem_small4', 349, 247, 247, 349);
			}
			//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'));});
			});
		});
	}
	
	function showPopup(id){
		$('#map div.popup').fadeOut(); 
		var boxid = '#' + id + '-box';
		$(boxid).fadeIn();
		$('a.close').click(function(){
			$(this).parent().fadeOut();
		});
	}

	
	//initialize map
	initializeMap();
	

});
