/* -- BEGIN: DHTMLMenu ---------------------------------------------------- */

if (!JSGMF) { var JSGMF = new Site(); }


var DHTMLMenu = Class.create(PageWidget, {
	initialize : function(menu_id, config) {
		this.node = $(menu_id);

		// bail out if the menu doesn't exist on this page
		if (!this.node || this.initialized) return;

		this.setOptions(config);

		this.submenus = { };

		// get all the top-level LIs in the menu and iterate over them, adding event handlers
		$(menu_id).immediateDescendants().each(function(item){
			item.observe("mouseover", this.mouseoverHandler.bindAsEventListener(this, item));
			item.observe("mouseout", this.mouseoutHandler.bindAsEventListener(this, item));

			this.submenus[item.id] = item.down().next();
		}.bind(this));

	},

	initialized : false,
	node : null,              // holds the DOM node of the menu
	menu_hide_timeout : null, // the JS timeout ID for hiding the menu
	menu_show_timeout : null, // the JS timeout ID for showing the menu
	last_menu_on : null,      // the DOM object of the waiting to close

	mouseoverHandler : function(e, item) {
		// stop the menu from closing/opening (this gets called a lot)
		clearTimeout(this.menu_hide_timeout);
		clearTimeout(this.menu_show_timeout);

		// if we're mousing over the menu for the first time, set a timeout so the menu doesn't show up accidentally.
		if (this.last_menu_on == null) {
			this.menu_show_timeout = setTimeout(this.showMenu.bind(this, item),	this.CONFIG["menu_show_time"]);

		// if there's already a menu on, then we know the user is expecting to see another one, so show it immediately.
		} else if (this.last_menu_on != item) {
			this.showMenu(item);
		}
	},

	mouseoutHandler : function(e, item) {
		// clear the existing show/hide timeouts (this gets called a lot)
		clearTimeout(this.menu_hide_timeout);
		clearTimeout(this.menu_show_timeout);

		// only "close" the menu in a little bit if we're over a menu with submenus, otherwise, close it right now
		if (this.submenus[item.id]) {
			this.menu_hide_timeout = setTimeout(this.hideMenu.bind(this, item),	this.CONFIG["menu_hide_time"]);

		} else {
			this.hideMenu(item);
		}
	},

	// shows the menu
	showMenu : function(item) {
		// hide the last menu shown
		if (this.last_menu_on != null) { this.hideMenu(this.last_menu_on); }

		// adding the "hover" class turns on the menu
		item.addClassName(this.CONFIG['hover_class']);

		// insert the IFRAME for IE
		if (Prototype.Browser.IE6) {
			// get at the submenu for dimension info
			var submenu = this.submenus[item.id];
			if (submenu) {
				var iframe = submenu.next();

				if (submenu && !iframe) {
					this.createIframe(item, {
						'left'   : submenu.offsetLeft + "px",
						'height' : submenu.offsetHeight + "px",
						'width'  : submenu.offsetWidth + "px"
					});
				}
			}
		}

		// store the last item on
		this.last_menu_on = item;

	}, // END: showMenu()

	// hide the menu
	hideMenu : function(item) {
		// nothing to hide
		if (item == null) return;

		// removing the "hover" class turns off the menu
		item.removeClassName(this.CONFIG['hover_class']);

		// remove the IFRAME for IE
		if (Prototype.Browser.IE6) {
			var submenu = this.submenus[item.id];
			if (submenu) {
				var iframe = submenu.next();
				if (submenu && iframe) {
					item.removeChild(iframe);
				}
			}
		}

		this.last_menu_on = null;
	}, // END: hideMenu()

	// makes an IFRAME the exact size of the menu so elements underneath it are covered
	// (IE-only)
	createIframe : function(node, style) {
		new Insertion.Bottom(node, (new Template('<iframe class="' + this.CONFIG['iframe_class'] + '" frameborder="0" scrolling="no" style="width: #{width}; height: #{height}; left: #{left};"><\/iframe>')).evaluate(style));
	}
});

DHTMLMenu.CONFIG = {
	submenu_class : "SubMenu", // the DOM class of the menu container
	hover_class : "Hover", // the class to give the top-level LI to "activate" the menu
	menu_hide_time : 500, // time to keep the menus on after mouseout; in ms
	menu_show_time : 100, // threshold o
	iframe_class : "IframeFix"
};

fixWebKitInheritanceBug(DHTMLMenu);
		
/* ------------------------------------------------------ END: DHTMLMenu -- */

JSGMF.addFeature('menus', {
	setupElements : function(root_node) {
		$$S(root_node, "#MainNav").each(function(item) {
			this.storeWidgetInstance(item.id, new DHTMLMenu(item.id));
		}.bind(this));
	}
});


// document.observe('dom:loaded', function(){
//  $$(".DHTMLMenu").each(function(menu) {
//    new DHTMLMenu(menu.id);
//  });
// });



/* ======================================================================== */


var DetailBubble = Class.create();

DetailBubble.last_shown = null;

Object.extend(Object.extend(DetailBubble.prototype, PageWidget.prototype), {
	initialize : function(item) {
		this.node = $(item);

		this.node.observe('mouseover', this.handleItemMouseover.bindAsEventListener(this));
		this.node.observe('mouseout', this.handleItemMouseout.bindAsEventListener(this));

		this.bubble_node = $$S(item, ".DetailBubble")[0];
		if (!this.bubble_node) { return; }

		this.bubble_node.observe('mouseover', this.handleDetailBubbleMouseover.bind(this));
		this.bubble_node.observe('mouseout', this.handleDetailBubbleMouseout.bind(this));

		// move the detail bubble to the page wrapper
		$('PageWrapper').appendChild(this.bubble_node);
	},

	hide_timeout : null,

	handleItemMouseover : function(e) {
		if (!this.bubble_node) { return; }

		clearTimeout(this.hide_timeout);

		if (this.constructor.last_shown && (this.constructor.last_shown != this)) {
			this.constructor.last_shown.hideDetailBubble();
		}

		this.showDetailBubble();

	},

	handleItemMouseout : function(e) {
		if (!this.bubble_node) { return; }

		this.hide_timeout = setTimeout(this.hideDetailBubble.bind(this), 500);

		this.constructor.last_shown = this;
	},

	handleDetailBubbleMouseover : function(e) {
		clearTimeout(this.hide_timeout);
	},

	handleDetailBubbleMouseout : function(e) {
		this.hide_timeout = setTimeout(this.hideDetailBubble.bind(this), 500);
		this.constructor.last_shown = this;
		
	},

	showDetailBubble : function() {
		this.node.addClassName('Active');

		Position.clone(this.node, this.bubble_node, {
			setWidth : false,
			setHeight : false,
			offsetTop : 172,
			offsetLeft : 5
		});

	},


	hideDetailBubble : function() {
		this.bubble_node.setStyle({
			top : "-9999em",
			left : "-9999em"
		});

		this.constructor.last_shown = null;
		this.node.removeClassName('Active');
	}

});

fixWebKitInheritanceBug(DetailBubble);

/* ------------------------------------------------------------------------ */


JSGMF.addFeature('carousel_detail_bubble', {
	setupElements : function(root_node) {
		$$S(root_node, ".Carousel LI").each(function(item) {
			new DetailBubble(item);
		}.bind(this));
	}
});


/* ------------------------------------------------------------------------ */
// Print page functionality

JSGMF.addFeature('PrintLink', {
	setupElements : function(source) {
		$$('#tools-print').each(function(print_link){
			print_link.observe('click', function(e) {
				Event.stop(e);
				if (window.print) { window.print(); }
			});
		});
	}
});


// ---------------------------------------------------------------------------
// Email page / Send to a friend functionality

var EmailPageLink = Class.create({
	initialize: function(el) {
		this.node = $(el);
		if (!this.node) { return; }

		this.node.observe('click', this.showWindow.bindAsEventListener(this));
	},

	window_ref : null,

	showWindow: function(e) {
		e.stop();

		var page_title = document.title.gsub(/\s+-\s+John Simon Guggenheim Memorial Foundation/,'').gsub(/\\n/, "").strip();
		var url = "http://" + window.location.host + "/send_page?url=" + encodeURIComponent(window.location.href) + "&page_name=" + encodeURIComponent(page_title);

		this.window_ref = window.open(url,'email_page_window','height=680,width=450,scrollbars=yes,location=no,menubar=no,toolbar=no,status=no,directories=no,resizable=yes');
		this.window_ref.focus();
	}
});

document.observe("dom:loaded", function(){ new EmailPageLink($('tools-email')); });


/* ------------------------------------------------------------------------ */
// MailChimp popup

function newsletter_popup(url) {
	newwindow=window.open(url,'name','height=600,width=660');
	if (window.focus) {newwindow.focus()}
	return false;
}
