
/*
* Copyright (C) 2009 Joel Sutherland
* Licenced under the MIT license
*/
(function($) {
	$.fn.zoommap = function(settings) {
		settings = $.extend({
			// Width and Height of the Map Area
			width: '100px',
			height: '100px',
			
			//Misc Settings
			blankImage: './images/blank.gif',
			loadingImage: './images/loading.gif',
			fadeDuration: 500,
			zoomDuration: 1000,
			
			//ids and classes
			bulletClass: 'zoomable',
			popupSelector: 'div.popup',
			popupCloseSelector: 'a.close',
			
			//Return to Initial Region Link
			homeId: 'homelink',
			homeText: 'Return to Full Map',
			
			//Initial Region to be shown
			initialRegion: {},
				
			//Zoomable Regions
			zoomableRegions: []
		}, settings);
		
		var map = $(this);
		
		//Set up initial Map Area and the initial region that is shown
		function initializeMap(){
			map.fadeOut(settings.fadeDuration, function(){
				$(this).empty().css({
					width: settings.width,
					height: settings.height,
					backgroundImage: 'url(' + settings.initialRegion.image + ')',
					position: 'relative'
				});
				$(this).fadeIn();
				loadBullets(settings.initialRegion, false);
			});
		}
		
		//Load the Bullets 
		function loadBullets(region, showHomeLink){
			map.load(region.data, {}, function(){
				//add back button
				if(showHomeLink){
					$('<a id="' + settings.homeId + '" href="javascript:void(0)"><span>' + settings.homeText + '</span></a>')
						.appendTo(map)
						.click(function(){initializeMap()});
				}
				else{
					for(var i=0; i<settings.zoomableRegions.length; i++){
						addZoomable( settings.zoomableRegions[i] );
					}
				}
				//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 addZoomable( subregion ){
			$('<img class="' + settings.bulletClass + '" src="' + settings.blankImage + '" id="' + subregion.id + '" />').css({
				border: 'none',
				position: 'absolute',
				width: subregion.width,
				height: subregion.height,
				top: subregion.top,
				left: subregion.left,
				cursor: 'pointer'
			}).appendTo(map).click(function() {
				$(this).siblings().fadeOut();
				$(this).hide()
					   .attr('src', subregion.image)
					   .fadeIn('slow')
					   .animate({
							width: settings.width,
							height: settings.height,
							top: '0px',
							left: '0px'
						}, settings.zoomDuration, '', function(){
							map.css({backgroundImage: 'url(' + subregion.image + ')'}).empty();
							loadBullets(subregion, true);
						});
			});
		}
		
	
	
		function showPopup(id){
			map.find(settings.popupSelector).fadeOut(); 
			var boxid = '#' + id + '-box';
			$('#fancybox-overlay').fadeTo('fast', 0.4);
			$(boxid).fadeIn();
			
			$(settings.popupCloseSelector).click(function(){
				$('#fancybox-overlay').fadeOut();
				$(this).parent().parent().fadeOut();
			});
		}

	
		//initialize map
		initializeMap();
			
	}	
})(jQuery);
$(document).ready(function(){
   
    
	$('div.map').zoommap({
		width: '934px',
		height: '493px',
		initialRegion: {
				id: 'generalMap',
				data: './request/waypoint.request.php?name=generalMap',
				image: './images/tadoussac123.JPG'
				},
		zoomableRegions: [
		
					]
	});
	
	
	$("#participant_select").change(function() {
		day = $('#date_select').val();
		
		var uri_query = "?p=0&action=" + _GET['action'];
		
		if (day != null)
			uri_query += "&day=" + day;
		
		uri_query += "&pID=" + $(this).val();
		
		if (_GET['wID'] != null)
			uri_query += "&wID=" + _GET['wID'];
		
		window.location = uri_query;
	});
	
	$("#date_select").change(function() {
		participant = $('#participant_select').val();
		window.location = "?p=0&action=entries&day=" + $(this).val() + "&pID=" + participant;
	});
	
	$("#orderby_select").change(function() {
		//sortby= $('#sortby_select').val();
		window.location = "?p=0&action=participants&orderby=" + $(this).val();
	});
	
	
});
