// IE/win background image flicker fix
// http://evil.che.lu/2006/9/25/no-more-ie6-background-flicker
try { document.execCommand('BackgroundImageCache', false, true); } catch(e) {}

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

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


HFRP.clearForm = function(form_id) {
	var form = $(form_id);
	var inputs = form.getInputs();
	form.reset();
	inputs.each(function(el) {el.checked = false;});
};

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

var DHTMLMenu = Class.create();

// subclass the PageWidget
Object.extend(DHTMLMenu.prototype, PageWidget.prototype);

// class var to contain our settings
DHTMLMenu.CONFIG = {
	submenu_class : "SubMenu", // the DOM ID of the menu container
	hover_class : "Hover", // the class to give the top-level LI to "activate" the menu
	active_class : "Active",
	menu_time : 500 // time to keep the menus on after mouseout; in ms
};

// 
Object.extend(DHTMLMenu.prototype, {
	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);

		// iterate over all the top-level LIs in the menu and add event handlers
		this.node.immediateDescendants().each(function(item){
			item.observe("mouseover", this.handleMouseover.bindAsEventListener(this, item));
			item.observe("mouseout", this.handleMouseout.bindAsEventListener(this, item));

			// only get the submenus two levels deep and attach the event handlers to the LI that wraps the submenu
			$$S(item, "." + this.CONFIG['submenu_class'] + " ." + this.CONFIG['submenu_class']).each(function(submenu) {
				var li = submenu.up();
				li.observe('mouseover', this.handleSubmenuMouseover.bindAsEventListener(this, li));
				li.observe('mouseout', this.handleSubmenuMouseout.bindAsEventListener(this, li));
			}.bind(this));

		}.bind(this));

	},

	node : null, // holds the DOM node of the menu
	menu_timeout : null, // the JS timeout ID for hiding the menu
	last_menu_on : null, // the DOM object of the waiting to close

	// 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']);

		if (!Prototype.Browser.IE6) return;

		// get at the submenu for dimension info
		var submenu = item.down().next();

		// insert the IFRAME for IE
		this.createIframe(item, {
			'left' : "0",
			'top' : (submenu.offsetTop + "px"),
			'height' : (submenu.offsetHeight + "px"),
			'width' : (submenu.offsetWidth + "px")
		});

	}, // END: showMenu()

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

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

		if (!Prototype.Browser.IE6) return;
		
		// remove the IFRAME
		item.removeChild(item.lastChild);
	}, // END: hideMenu()

	createIframe : function(node, style){
		new Insertion.Bottom(node, (new Template('<iframe src="javascript:void(0);" class="TempIframe" frameborder="0" scrolling="no" style="width: #{width}; height: #{height}; left: #{left}; top: #{top};"><\/iframe>')).evaluate(style));
	},


	handleMouseover : function(e, item) {
		// stop the menu from closing (this gets called a lot)
		clearTimeout(this.menu_timeout);

		// show the menu
		this.showMenu(item);
	},

	handleMouseout : function(e, item) {
		// store the last item on
		this.last_menu_on = item;

		// since this gets called as a setTimeout it needs to be in the scope of our object
		var turnOff = function() {
			this.hideMenu(item);
			this.last_menu_on = null;
		}.bind(this);

		// only "close" the menu in a little bit if we're over a menu with submenus, otherwise, close it right now
		if (item.immediateDescendants().length > 1) {
			this.menu_timeout = setTimeout(turnOff, this.CONFIG["menu_time"]);

		} else {
			turnOff();
		}
	},

	handleSubmenuMouseover : function(e, item) {
		item.addClassName(this.CONFIG['active_class'] + " " + this.CONFIG['hover_class']);

		if (!Prototype.Browser.IE6) return;

		// get at the submenu for dimension info
		var submenu = item.down().next();

		// insert the IFRAME for IE
		this.createIframe(item, {
			'left' : "0",
			'height' : (submenu.offsetHeight + "px"),
			'width' : (submenu.offsetWidth + "px")
		});
	},

	handleSubmenuMouseout : function(e, item) {
		item.removeClassName(this.CONFIG['active_class']);
		item.removeClassName(this.CONFIG['hover_class']);

		if (!Prototype.Browser.IE6) return;

		// remove the IFRAME
		item.removeChild(item.lastChild);
	}

});

fixWebKitInheritanceBug(DHTMLMenu);

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

// add the feature to the site
HFRP.addFeature('menus', {
	setupElements : function(root_node) {
		$$S(root_node, "#MainNav").each(function(item) {
			this.storeWidgetInstance(item.id, new DHTMLMenu(item.id));
		}.bind(this));
	}
});

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

/* BEGIN: StylesheetSwitcher ------------------------------------------- */
Object.extend(HFRP.Features, {
	StylesheetSwitcher : function() {
		var CONFIG = {};

		return {
		// find all the stylesheets in the current doc
			available_stylesheets : function() {
				var styles = [];
				// get all the links and iterate over them
				$A(document.getElementsByTagName("link")).each(function(link) {
					// test for the "rel" attr containing the right stuff
					if ( (link.rel.toLowerCase().indexOf("stylesheet") != -1) && (link.rel.toLowerCase().indexOf("alternate") != -1) ) {
						// add it to our list
						styles.push(link);
					}
				});
				return styles;

			}, // END: initialize()

			// activates the correct stylesheet based on the title
			setStylesheet : function(ss_title) {

				// bail out if the params are bad
				if (typeof ss_title != 'string') { return; }

				// loop through the alt css files and activate the correct one
				this.available_stylesheets().each(function(ss){
					// first disable it
					ss.disabled = true;
					// then enable it if matches the passed title
					if (ss.title == ss_title) { ss.disabled = false; }
				});
			}
		}; // END: return
}()
});
/* --------------------------------------------- END: StylesheetSwitcher -- */


/* -- BEGIN: TextSizer ---------------------------------------------------- */

// subclass the PageWidget
var TextSizer = Class.create(PageWidget, {
	initialize : function(text_sizer_id) {
		// bail out if the text sizer is not around
		this.node = $(text_sizer_id);
		if (!this.node) { return; }

		this.setOptions();

		// get all the anchors in the text sizing widget and iterate over them
		this.node.select("." + this.CONFIG['text_sizer_class']).each(function(anchor){

			// attach the onclick handler
			anchor.observe("click", this.handleTextSizeClick.bindAsEventListener(this));
			// anchor.onclick = Prototype.False; // fix for dumb older versions of safari
		}, this);

	}, // END: initialize()

	handleTextSizeClick : function(e) {
		// stop the event from propagating
		e.stop();

		// set the style based on the title element
		HFRP.Features.StylesheetSwitcher.setStylesheet(e.element().title);
	}
});

// class var to contain our settings
TextSizer.CONFIG = {
	text_sizer_class: "text_size_controll"
};


fixWebKitInheritanceBug(TextSizer);


/* ------------------------------------------------------ END: TextSizer -- */

// add the feature to the site
HFRP.addFeature('text-sizer', {
	setupElements : function(root_node) {
		new TextSizer('PageOptions');
	}
});


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


// Open all external links in new windows
HFRP.addFeature('ExternalLinkHandler', {

	setupElements : function() {
		var links = $A(document.getElementsByTagName('a'));
		var hostName = document.location.hostname;
		var matchStr = "http://"+hostName;
		
		links.each(function(el) {

			if (el.href && !el.href.match(matchStr))
				el.target = "_blank";
			if (el.href && el.href.match(/^javascript:/))
				el.target = "_self";
		});
	}
});

/* ------------------------------------------ END: External Link Handler -- */

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


var DetailBubble = Class.create(PageWidget, {
	initialize : function(bubble_el, config) {
		this.node = $(bubble_el);
		if (!this.node) { return; }
		this.setOptions(config);

		this.nodes = {};
		
		$$S(this.node, '.CloseLink').each(function(link) {
			link.observe('click', this.closeLinkClickHandler.bindAsEventListener(this));
		}.bind(this)); 
		
		this.nodes['helpLink'] = this.node.previous('span');
		this.nodes['helpLink'].observe('click', this.openLinkClickHandler.bindAsEventListener(this));
		
		this.nodes['helpWrapper'] = this.node.up('div.help-link');

	},
	
	node : null,              // holds the DOM node of the menu
	db_hide_timeout : null, // the JS timeout ID for hiding the menu
	db_show_timeout : null, // the JS timeout ID for showing the menu
	
	createIframe : function(node, style){
		new Insertion.Bottom(node, (new Template('<iframe src="javascript:void(0);" class="IframeFix" frameborder="0" scrolling="no" style="width: #{width}; height: #{height}; left: #{left}; top: #{top};"><\/iframe>')).evaluate(style));
	},

	openLinkClickHandler : function(e) {
		//this.node.addClassName('Shown');
		clearTimeout(this.db_hide_timeout);
		clearTimeout(this.db_show_timeout);
		//this.node.show();
		
		
		if (this.constructor.currently_open == null) {
			//this.db_show_timeout = setTimeout(this.show.bind(this), this.CONFIG["db_show_time"]);
			//this.db_show_timeout = setTimeout(this.addClassName('Shown').bind(this), this.CONFIG["db_show_time"]);
			
			this.constructor.currently_open = this; // pre-populate this var to prevent a second menu from showing up before this one is shown
			this.constructor.currently_open.node.up('div.help-link').setStyle({ position: 'relative'});
			this.node.addClassName('Shown');
			
		} else if (this.constructor.currently_open != this) { 
			this.constructor.currently_open.node.removeClassName('Shown');
			this.constructor.currently_open.node.up('div.help-link').setStyle({ position: 'static'});
			/* if (Prototype.Browser.IE6) {
				// remove the IFRAME
				item.removeChild(item.lastChild);
			} */
			
			this.node.addClassName('Shown');
			this.constructor.currently_open = this;
			this.constructor.currently_open.node.up('div.help-link').setStyle({ position: 'relative'});
		}
		
		if (!Prototype.Browser.IE6) return;

		// get at the div for dimension info
		var bubble_item = this.node;

		// insert the IFRAME for IE
		this.createIframe(bubble_item, {
			'left' : "0",
			'top' : "0",
			'height' : ((bubble_item.offsetHeight-2) + "px"),
			'width' : ((bubble_item.offsetWidth-2) + "px")
		});
		
	},
	
	mouseOutLinkHandler : function(e) {
		clearTimeout(this.db_hide_timeout);
		clearTimeout(this.db_show_timeout);

		this.db_hide_timeout = setTimeout(this.hide.bind(this), this.CONFIG["db_hide_time"]);
	},
	
	closeLinkClickHandler : function(e) {
		clearTimeout(this.db_hide_timeout);
		clearTimeout(this.db_show_timeout);
		
		this.node.removeClassName('Shown');
		this.nodes['helpWrapper'].setStyle({ position: 'static'});
		this.constructor.currently_open = null;

		e.stop();
		e.element().blur();
		
		if (!Prototype.Browser.IE6) return;

		// remove the IFRAME
		this.node.removeChild(this.node.lastChild);
	},
	
	hide : function() {
		this.node.removeClassName('Shown');
		this.constructor.currently_open = null;
	}

});

DetailBubble.currently_open = null;

DetailBubble.CONFIG = {
	db_hide_time : 500, // time to keep displayed after mouseout; in ms
	db_show_time : 50 // threshold of hover behavior; in ms
};

fixWebKitInheritanceBug(DetailBubble);


HFRP.addFeature('DetailBubble', {
	setupElements : function() {
		$$('div.help-link div.helpdetail').each(function(bubble, i) {
			bubble.id = "detail-bubble_" + (i + 1);
			this.storeWidgetInstance(bubble.id, new DetailBubble(bubble));
		}.bind(this));
	}
});


/* ------------------------------------------ END: Detail Bubble -- */

