
	/* Globals
	--------------------------------------------------------*/
	return_false = function () { this.blur (); return false; };

	window.addEvent ('domready', function () {
		/* Menu
		--------------------------------------------------------*/
		// Add the right classes and events
		$$('#menu li ul').each (function (element, index) {
			element.getParent ('li').getFirst ('a')
				.addClass ('parent')
				.onclick = return_false;
		});

		// Focus lines in Firefox... Ugh.
		blur_func = function () { this.blur (); };
		$$('a').each (function (element, index) {
			element.addEvent ('focus', blur_func);
		});

		// Loop through links and find active parents (if any) to auto-expand it
		var active_parent = -1;
		var parents = -1;
		$$('#menu a').each (function (element, index) {
			var is_parent = false;
			if (element.hasClass ('parent'))
			{
				is_parent = true;
				parents++;
			}
			var window_location = window.location.toString ();
			var a_href = element.get ('href');
			if ((window_location.match (/\.com\/?$/) && a_href == '/') || (a_href != '/' && window_location.indexOf (a_href) >= 0))
			{
				if (is_parent)
				{
					active_parent = parents;
				}
				else
				{
					element.addClass ('active');
				}
			}
		});

		var menu = new Fx.Accordion ($$('#menu .parent'), $$('#menu li ul'), {
			display: active_parent,
			initialDisplayFx: false,
			alwaysHide: true,
			onActive: function (toggler, element) {
				toggler.addClass ('open');
			},
			onBackground: function (toggler, element) {
				toggler.removeClass ('open');
			}
		});

		/* Forms
		--------------------------------------------------------*/
		$$('form td').each (function (element, index) {
			element.set ('html', element.get ('html').replace ('*', '<span class="required">*</span>'));
		});
		$$('form input.button').each (function (element, index) {
			element.setStyle ('display', 'none');
			new Element ('a', {
				'class': 'button',
				'href': '#',
				'html': '<span>' + element.get ('value') + '</span>'
			})
				.inject (element.getParent ())
				.addEvent ('click', function () {
					this.getParent ('form').submit ();
				})
				.onclick = function () { return false; };
		});

		/* Prompts
		--------------------------------------------------------*/
		show_prompts = function () {
			// Get size of prompts
			var prompts_size = $('prompts').getSize ();

			// Vertically center prompts
			final_location = Math.round ((window.getHeight () / 2) - (prompts_size.y / 2));

			$('prompts').set ('tween', {
				duration: 1000,
				transition: 'elastic:out'
			});

			$('prompts').tween ('top', final_location + window.getScrollTop ().toInt ());
		}

		hide_prompts = function () {
			$('prompts').set ('tween', {
				duration: 400,
				transition: 'linear'
			});
			$('prompts').tween ('top', -400);
		}

		if ($('prompts'))
		{
			$('prompts').addEvent ('click', hide_prompts);
			$('prompt_closer').onclick = function () { return false; };
			(function () { show_prompts (); }).delay (250);
		}

		// IE can't handle white-space: nowrap in cells.
		if ($('IE'))
		{
			$(document.body).getElements ('form table th').each (function (element, index) {
				element.set ('html', '<span>' + element.get ('html') + '</span>');
			});
		}

		/* Images
		--------------------------------------------------------*/
		$$('#content img').each (function (element, index) {
			if (!element.hasClass ('ignore'))
			{
				// Outer wrapper
				new Element ('span', {
					'class': 'image_wrapper ' + element.get ('class')
				}).wraps (element);
				element.set ('class', '');

				// IE 6 needs a delay because... Well, who knows.
				(function () {
					// Inner wrapper
					new Element ('span', {
						'class': 'image_inner_wrapper'
					}).wraps (element);
				}).delay (100);
			}
		});

		/* Videos
		--------------------------------------------------------*/
		play_video = function () {
			var obj              = this;
			var bits             = obj.get ('id').match (/video__([a-z0-9]+)__([0-9]+)/);
			var video_file_name  = bits[1];
			var video_length     = bits[2];
			var video_width      = 470;
			var video_height     = 338;
			obj.set ('morph', {
				'onComplete': function () {
					obj.set ('morph', {
						'onComplete': function () {
							obj.set ('html', '<object classid="clsid:D27CDB6E-AE6D-11cf-96B8-444553540000" codebase="http://download.macromedia.com/pub/shockwave/cabs/flash/swflash.cab#version=6,0,29,0" '
				           + 'width="' + video_width + '" height="' + video_height + '"> '
				           + '<param name="movie" value="/videos/' + video_file_name + '.swf"> '
				           + '<param name="quality" value="high"> '
				           + '<param name="loop" value="false"> '
				           + '<embed src="/videos/' + video_file_name + '.swf" quality="high" '
				           + 'width="' + video_width + '" height="' + video_height + '" loop="false" '
				           + 'type="application/x-shockwave-flash" pluginspage="http://www.macromedia.com/go/getflashplayer" /> '
				           + '</object>');

							(function () { obj.fireEvent ('stop'); }).delay (video_length);
						}
					});
					obj.morph ({
						'height': video_height
					});
				}
			});
			obj.morph ({
				'width': video_width,
				'margin-left': 0
			});
		};
		stop_video = function () {
			obj = this;
			obj.set ('html', '');
			obj.set ('morph', {
				'onComplete': function () {
					obj.morph ({
						'width': 200,
						'height': 144,
						'margin-left': 30
					});
				}
			});
			obj.morph ({
				'height': 144
			});
		};
		$$('.video').each (function (element, index) {
			element.addEvents ({
				'click': play_video,
				'stop': stop_video
			});
		});

		/* Jump-To Menus
		--------------------------------------------------------*/
		if (!window.location.toString ().match (/(orthodontic_care)/))
		{
			// Count all H2's
			h2s = $$('#content h2');

			// More than one? Jump-To Menu!
			if (h2s.length > 1)
			{
				// No smooth for IE6!
				if (!$chk ($('IE6')))
				{
					var scroller = new Fx.Scroll (window, {
						'wheelStops': false
					});
					var smooth_scroll = function () {
						scroller.toElement (this.get ('href').replace ('#', ''));
					};
				}
				else
				{
					var smooth_scroll = function () {};
				}

				// <h2>Jump To:</h2>
				jump_to_title = new Element ('h2', {
					'html': 'Jump To:',
					'id': 'jump_to'
				});
				jump_to_list = new Element ('ul');

				// Back to Top link (cloned many times)
				back_to_top = new Element ('a', {
					'class': 'back_to_top',
					'html': 'Back to Top',
					'href': '#jump_to',
					'events': {
						'click': smooth_scroll
					}
				});

				h2s.each (function (element, index) {
					new Element ('li', {
						'html': '<a href="#jump_to_' + (index + 1) + '">' + element.get ('text') + '</a>'
					}).inject (jump_to_list);

					element.set ('id', 'jump_to_' + (index + 1));

					if (index > 0)
					{
						back_to_top.clone ().cloneEvents (back_to_top).inject (element, 'before');
					}
				});

				jump_to_title.inject (h2s[0], 'before');
				jump_to_list.inject (h2s[0], 'before');
				back_to_top.clone ().inject (h2s[0], 'before');

				// Enable smooth-scrolling
				jump_to_list.getElements ('a').each (function (element, index) {
					element.addEvent ('click', smooth_scroll);
				});
			}
		}
	});
