$(document).ready(function() {
	
	// Initialize related events popup on AJF homepage
	$('.upcomingEventText .more a').each(function(i) {
		if ($(this).attr('rel') == "flyout") {
			var options = { box: $('.virtualFlyOut:eq(' + i + ')'),
							close: $('.virtualFlyOut:eq(' + i + ') .virtualFlyOutClose')};
			$(this).flyOut(options); 
		}
	});
	
	if ($('.additionalEvents a').attr('rel') == 'flyout') {
		var options = { box: $('.virtualFlyOut'),
						close: $('.virtualFlyOut .virtualFlyOutClose')};
		$('.additionalEvents a').flyOut(options);
	}
	
});


/*
* Plugin Name: 		Flyout Box
* Maintainer(s):	Bryan Mills (bmills@merion.com, x1427)
* Usage: $(element).flyOut({box: $(element), close: $(element) })
*				 the options hash requires two arguments:
*				 box: the element that will be toggled
*				 close: the element that will close the box 
*/

(function($) {

	// argument hash with default values.
	$.flyOut = {
		defaults: {
			box: null, 												// the box that shows/hides. no default, must be specified
			close: null,											// element that closes the flyout box on click. no default, must be specified
			activeLinkClass: "flyOutActive" 	// optional name of the class to assign to the flyout trigger when active
		}
	};
	
	// public methods - called via the jQuery object ($.flyOut)
	$.fn.extend({
		
		flyOut: function(options) {
			
			// Merge our defaults hash with the one passed in by the user. The user's options will take precedence.
			var options = $.extend({}, $.flyOut.defaults, options);
			
			// wrap functionality in 'return this.each()' so that the behavior can be applied to a jQuery collection 
		  // e.g., $('a').flyOut(options) will apply the behavior to all <a> elements in the page.
			return $(this).each(function() {
				
				// check for presence of flyout box. jQuery objects are a collection, so we have evaluate their presence based on length
				
				if (options.box && options.box.length > 0) {
					
					// append the box to the bottom of the page to avoid the IE z-indexing bug with positioned elements
					options.box.remove().appendTo('body');
					
					$(this).click(function(e){
						$(this).toggleClass(options.activeLinkClass);
						options.box.toggle();
						options.box.css("top", e.pageY);
						options.box.css("left", e.pageX);
						return false; // disable the default behavior of the link element
					});
					
					if (options.close && options.close.length > 0) {
						options.close.click(function(e) {
								$(this).toggleClass(options.activeLinkClass);
								options.box.toggle();
								return false;
						});
					}
				}
			});
		}
	});
	
	// If we had private methods, they'd go here.

})(jQuery);