/*

	RUN some javascript after all other javascript in the load flow

*/
if(jQuery){(function($){
	$(document).ready(function(){
		
		if (location.hash) {
			if ($(location.hash).is('.popup')) {
				$(location.hash).popup();
			}
		}

		$('.popupTip a.close').click(function(){
			$(this).closest('.popupTip').data('overlay').close();
			return false;
		});
	
		// Close current popover, open new one, and scroll up if necessary
		$('.popup a.hasPopup.closeThis[href]').live('click', function(){
			
			// Get the current overlay with the link to another overlay
			var $overlay = $(this).closest('.popup');
			
			// check to see if scrolling will be necessary
			// note that _scrollable() is defined in jquery.scrollTo and helps with
			// getting the main scrollable content area in a cross-browser way (more difficult than you think)
			if ($overlay.offset().top < $(window)._scrollable().scrollTop()) {
				$.scrollTo($overlay, { offset: -50, axis:'y' });
			}
			
			// Close this popup and open the other one
			$.popup.close($(this).closest('.popup'));
			
			$($(this).attr('href')).popup();
			
			return false;
		});
		
		// OVERLAYS
		$('a.hasPopup[href], a.hasPrimaryOverlay[href]').live('click', function(){
			if (isProfile = $(this).data('profile')) {
				// ignore profile links, they have their own popups
				return;
			}
			
			var contentTarget = $(this).data('target');
			var targetID = $(this).attr('href');

			if ($(targetID).length == 0 || contentTarget) {
				var targetURL = $(this).data('path');

				if (!targetURL) {
					targetURL = '/infoPopup/' + targetID.substring(1);	
				}

				$.popup.loading(true);
				
				$.ajax({
					url : targetURL,
					success : function(data) {						
						if (contentTarget) {
							$(contentTarget).replaceWith(data);
							$(contentTarget).popup();
						} else {
							$('body').append(data);
							$(targetID).popup();
						}
					},
					complete : function(data) {
						$.popup.loading(false);
					}
				});
			} else {
				$(targetID).popup();
			}
			
			
			return false;
		});
		
		$('.popup a.closeThis').live('click', function(){
			$.popup.close($(this).closest('.popup'));
		});
		
		$('a.hasPopup[href], a.hasPrimaryOverlay[href], .popup a.closeThis').live('click', function(){
			return false;
		});
		
		
		
		/*------------------ WHAT'S NEXT TIPS ------------------*/
		$('a.hasTipOverlay[href]').click(function(){
			var currentController = $('body').attr('id');
			
			//$.popup.loading(true);
			
			$.ajax({
				url : '/whatsNext/' + currentController,
				success : function(data) {					
					$('#whatsNextPopup').remove();
					
					if ($(data).data('stay')) {
						$('body').append(data);
					} else {
						$('#whatsNextPopupHolder').append(data);
					}
					
					$('#whatsNextPopup').popup();
					
					var navItem = $('#whatsNextPopup').data('highlight-nav');
					if (navItem) {
						$('#whatsNextPopup').one('load', function(){
							$('#topNav li a').removeClass('hilite');
							$('#topNav li a.' + navItem).addClass('hilite');
						}).one('close', function(){
							$('#topNav li a').removeClass('hilite');
						});
					}
					
				},
				complete : function(data) {
					//$.popup.loading(false);
				}
			});
			
			return false;
		});
		
	});
	
})(jQuery);}
