

(function () {
	var resetInput = function () {
		var element = (typeof $(this).hasClassName === 'function') ? $(this) : $(arguments[0]);
		if (!element.hasClassName('prefilled') && element.getValue() === element.defaultValue) {
			element.setValue('').addClassName('prefilled');
		}
		return element;
	};
	var clearForms = function () {
		var form_elements = $$('input[type=text]', 'textarea');
		form_elements.invoke('observe', 'focus', resetInput);
		form_elements.invoke('observe', 'blur', function () {
			if ($F(this).blank()) {
				this.setValue(this.defaultValue).removeClassName('prefilled');
			}
		});
		$$('form').invoke('observe', 'submit', function () {
			this.select('input[type=text]', 'textarea').map(resetInput);
		});
	};

	//document.observe('dom:loaded', clearForms);
})();

document.observe('dom:loaded', function () {
	$$('#breadcrumb ul').each(function (breadcrumb) {
		// inserts the document title as the last element in the breadcrumb trail
		var final_item = new Element('li', { className: 'last' }),
			title = document.title.split(' | ').slice(1).join(' | ') || document.title;
		
		breadcrumb.insert(final_item.update(title));
	});
	
	var location_sub = /([^\/]+?)\/([^\/]+?)\.aspx/;
	var address = location_sub.exec(window.location.href);
	
	try {
		$$('#navigation a', 'div.submenu a').each(function (el) {
			var _address = location_sub.exec(el.readAttribute('href'));
			if (_address && _address[0] && (_address[0] == address[0])) {
				var active_elements = el.ancestors().select(function (el) {
					return (el.tagName.toUpperCase() === 'LI');
				}).invoke('addClassName', 'selected');
			}
		});
	} catch (address_error) {}

	
	// default reflection options for the mirrored images
	var reflection_options = {
		height: 0.4,
		opacity: 0.3
	};
	
	// change the reflection on taller images (0.18 is equivalent)
	if (document.body.id === 'home') { reflection_options.height = 0.18; }
	
	// add the reflection to all images in #masthead
	$$('#masthead .banner img').each(function (image) {
		Reflection.add(image, reflection_options);
		image.observe('load', function () {
			Reflection.add(image, reflection_options);
		});
	});
	
	
	// replace all banner titles with the flash replacement
/*	if (typeof Vx === 'undefined' && swfobject.getFlashPlayerVersion().major > 8) {
		$$('#masthead .banner .text h1').each(function (title) {
			// read the text and replace ligatures (like “fi” in “OFFICE”)
			var text = title.innerHTML.stripTags().replace(/fl/g, 'ﬂ').replace(/fi/g, 'ﬁ'),
				height = title.getHeight(),
				id = new Element('div'),
				variables = { titleText: text.strip() },
				parameters = { wmode: 'transparent', scale: 'noborder' };
			
			title.setStyle({ height: height + 'px' });
			title.update(id);

			swfobject.embedSWF('../_flash/banner_title_large.swf', id.identify(), 375, 200, '9.0.0', null, variables, parameters);
		});
	}*/
	
	 //setup the homepage scrolling slideshow
	$$('#home #content .slideshow').each(function (slideshow) {
		var frames = slideshow.select('.banner'),
			menu_items = $$('#navigation > ul > li'),
			animating = false,
			width = frames[0].getWidth(),
			icons = $A(),
			current_frame = frames[0],
			icon_hold = new Element('div');
		
		// create a holder for the page icons
		$('latest_news').down('.container').insert({ top: icon_hold });
		
		slideshow.setStyle({ width: (frames.length * (width + 75)) + 'px'});
		
		var slide_to = function (i) {
			if (animating || i >= frames.length) return;
			
			var offset = 420 + (i * (width + 75));
			var style = {
				marginLeft: (offset * -1) + 'px'
			};
			
			animating = true;
			current_frame = frames[i];
			
			menu_items.invoke('removeClassName', 'selected')[i].addClassName('selected');
			icons.invoke('removeClassName', 'selected')[i].addClassName('selected');
			
			slideshow.morph(style, {
				transition: Effect.Transitions.sinoidal,
				afterFinish: function () {
					animating = false;
				}
			});
		};
		
		frames.each(function (frame, i) {
			var icon = new Element('a', { href: '#frame_' + i, className: 'frame_icon' }).update(' ');
			
			icon_hold.insert(icon);
			icons.push(icon);

			if (i === 0) icon.addClassName('selected');			
			icon.observe('click', function (e) { e.stop(); slide_to(i); });
			frame.observe('click', function (e) {
				if (animating) { e.stop(); return; }
				if (current_frame === frames[i]) { return; }

				e.stop();
				slide_to(i);
			});
		});
		
		menu_items.each(function (link, i) {
			if (i === 0) link.addClassName('selected');
						
			link.down('a').observe('click', function (e) { e.stop(); slide_to(i); });
		});
	});
	
	 //fetch all of the latest news items and loop through them
	var latest_news = $$('#latest_news .vx_text > ul > li').invoke('hide'),
		current_latest_news = 0,
		max_latest_news = latest_news.length - 1,
		showing = false;
	
	// make sure we have latest news on this page
	if (latest_news[current_latest_news]) {
		latest_news[current_latest_news].show();
	
		latest_news.invoke('observe', 'mouseover', function () { showing = true; });
		latest_news.invoke('observe', 'mouseout', function () { showing = false; });
	
		new PeriodicalExecuter(function (pe) {
			if (showing) return;
		
			new Effect.Fade(latest_news[current_latest_news], { duration: 0.3 }); // fade current
			current_latest_news = (current_latest_news === max_latest_news) ? 0 : current_latest_news + 1; // find new current
			new Effect.Appear(latest_news[current_latest_news], { delay: 0.2, duration: 0.5 }); // fade new current
		}, 6);
	}
	
	if (document.getElementById('member_tools') && !(/MSIE 6/i).test(navigator.userAgent)) {
		var tools = $('member_tools');
		tools.setStyle('width:0;right:-25px').morph('width:102px;right:-1px', { delay: 0.5 });
	}
	
	$$('body.seminars #content .content table td:nth-child(4n)').invoke('addClassName', 'pdf');
	$$('body.seminars #content .content table th:nth-child(4n)').invoke('addClassName', 'pdf');
});

	


