$(window).load(function() {

	function initializeMap(){
		$('#searchlocations').hide();
		$('#map').fadeOut(200, function(){
			$('#map').empty().css({
				width: '680px',
				height: '620px',
				backgroundImage: 'url(/images/zoommap/bc-full.gif)',
				position: 'relative'
			});
			$('#map').fadeIn();
			loadBullets('coast', false);
		});
	}

	function addZoomable(id, width, height, top, left){
		$('<img class="zoomable" src="/images/zoommap/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/zoommap/' + id + '_base.gif')
				   .slideUp('slow')
				   .animate({
						width: '680px',
						height: '620px',
						top: '0px',
						left: '0px'
					}, 500, '', function(){
						$('#map').css({backgroundImage: 'url(/images/zoommap/' + id + '_base.gif)'}).empty();
						loadBullets(id, true);
					});
		});
	}
	
	function loadBullets(id, back){
		$('#map').load('/labour_market_statistics/popups/' + id + '.php', {}, function(){
			//add back button
			if(back){
				$('<a id="mapback" href="javascript:void(0)"><span>Back to Overview Map &gt;&gt;</span></a>')
					.appendTo(this)
					.click(function(){initializeMap()});
			}
			else{
				addZoomable('vancouverisland', 102, 58, 486, 225);
				addZoomable('mainland', 102, 58, 473, 339);
				addZoomable('thompsonokanagan', 102, 58, 405, 452);
				addZoomable('cariboo', 102, 58, 360, 339);
				addZoomable('northeast', 102, 58, 88, 345); 
				addZoomable('northcoastnechako', 102, 58, 95, 168);
				addZoomable('kootenay', 102, 58, 476, 498);
			}
			//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();
	

});
