(function($){  
	$(document).ready(function() {
		// all go story1-5a then story5-1b
		var smWidth = [125,128,122,106,85,86,106,123,129,126];
		var smHeight = [55,91,113,121,124,125,121,113,91,56];

		var lgWidth = [194,200,191,166,126,131,166,196,202,195];
		var lgHeight = [87,141,176,188,182,181,190,177,145,86];
		
		var smX = [-4, 	10,	42,92,153,	297,343,378,406,424];
		var smY = [177,	116,64,27,4,	2,	24,	60,	114,176];
		
		var lgX = [25,	35,	75,	132,207,391,425,462,494,511];
		var lgY = [202,	130,67,	23,	6,	2,	19,	63,	125,206];
		
		//offset of the home navigation
		var offsetX = 93;	
		var offsetY = 57;
		
		var imgPath = "/Portals/0/images/engagement/";		//path to images
		var prevMap = "StoryNav";
		var prevStory = "";
		var navLinks = $('#jq-home a');
						
		$('#jq-engagement').css({'height': '320px'});		//sets height of engagement
		$('#jq-home').addClass('jqnav');					//positions home nav at start
		$('#jq-home p').addClass('intro');					//displays intro text at start
		
		$('#jq-home a img').each(function(index){
				imgPostion(index, this);
		});
		
		$('body').mousemove(function(e){
        		var x = e.pageX;
        		var y = e.pageY;
				
				var topLim = $('#jq-engagement').offset().top;
				var bottomLim = $('#jq-engagement').offset().top + 320;
				var leftLim = $('#jq-engagement').offset().left;
				var rightLim = $('#jq-engagement').offset().left + 728;
				
       			if( y < topLim || y > bottomLim || x < leftLim || x > rightLim) {
				 	//if not over engagement, reset to default
					$('.imgOverlay').attr({'src': imgPath + "story0.jpg", 'usemap': "#StoryNav"});
					$('#jq-home a[rel="' + prevStory + '"]').each(function(index) { 
						hoverOut( index, this )
					});
					prevStory = "";
					prevMap = "StoryNav";
				} 
   		 });
		
		$('#jq-home area').live('mouseenter mouseleave', function(event) {
				/*var story = $(event.target).attr('href');
				if(story) {
					story = story.substring(story.indexOf('#')+1, story.length);
				}*/
				var story = $(event.target).attr('alt');
				if(story) {
					story = "story" + story.substring(story.indexOf(' ')+1, story.length);
				}
				var myMap = $(this).parent('map').attr('name');
								
				if (event.type == 'mouseenter' && story) {
					//change nav image and image map on hover
					$('.imgOverlay').attr({'src': imgPath + story + ".jpg", 'usemap': "#" + story});
					
					//console.log( "Entering: "  + prevMap + "/" + myMap + " " + prevStory + "/" + story );
 				  	
					//if the map is the same and leaving a story area
					if( prevMap === myMap && prevStory !== story){
						$('#jq-home a[rel="' + prevStory + '"]').each(function(index) { 
							hoverOut( index, this );
						});
					}
					
					prevStory = story;
					prevMap = myMap;		
					
  			    } else {	
					
				}
		}).live('click', function(event) {
  		    event.preventDefault();
			navClick(event, event.currentTarget);
		});
		
		$('.imgOverlay').live('mouseenter mouseleave', function(event) {
			if (event.type == 'mouseenter') { 
				$('.imgOverlay').attr({'src': imgPath + "story0.jpg", 'usemap': "#StoryNav"});
				$('#jq-home a[rel="' + prevStory + '"]').each(function(index) { 
					hoverOut( index, this )
				});
				prevStory = "";
				prevMap = "StoryNav";
			} else {
				
			}											
		});

		$('#jq-toHome a').live('click', function(event) {
  		    event.preventDefault();
			homeClick(event);
		});
  		
				
		function imgPostion(index, object) {
			$(object).css({
				'width': lgWidth[index], 
				'height': lgHeight[index], 
				'top': lgY[index], 
				'left': lgX[index]
			});
		}
		
		function hoverOut(index, object) {
			var index = navLinks.index($(object));
			//console.log($(object).find('img'));
			$(object).removeClass('hide').find('img').clearQueue().delay(100).animate({
					'width': smWidth[index], 
					'height': smHeight[index], 
					'top': smY[index] + offsetY, 
					'left': smX[index] + offsetX
			}, 200, function() { 
				$(object).addClass('hide').find('img').css({
					'width': lgWidth[index], 
					'height': lgHeight[index], 
					'top': lgY[index], 
					'left': lgX[index]
				});; 
			});
		}
		
		function navClick(event, myLink) {
			//hide the home content
			$('#jq-home').hide();
			$('#jq-toHome').removeClass('hide');
			
			//determine which story was clicked and show its content
		  	var str = $(myLink).attr('href');
			str = str.substring(str.indexOf('#'), str.length);
			$(str).show();
		}
		
		function homeClick(event) {
			$('#jq-home').show();
			$('.jq-story').hide();
			$('#jq-toHome').addClass('hide');
		}

	});  
})(jQuery);  


