// Form handlers
// these functions remove any default text from form fields
// and disable submit buttons when a form is sent
(function () {
	var resetInput = function () {
		var element = (typeof $(this).hasClassName === 'function') ? $(this) : $(arguments[0]);
		if (element.getValue().toLowerCase() === element.defaultValue.toLowerCase()) element.setValue('');
		return element;
	};
	
	var clearForms = function () {
		var form_elements = $$('input[type=text]:not(.prefilled)', 'textarea:not(.prefilled)');
		form_elements.invoke('observe', 'focus', resetInput);
		form_elements.invoke('observe', 'blur', function () {
			if ($F(this).blank()) this.setValue(this.defaultValue);
		});
		
		$$('form').invoke('observe', 'submit', function () {
			this.select('input[type=text]:not(.prefilled)', 'textarea:not(.prefilled)').map(resetInput);
		});
	};

	document.observe('dom:loaded', clearForms);
})();

// the redheat version of sIFR
Element.addMethods({
	makeFlash: function (element) {
		if (!swfobject.hasFlashPlayerVersion('9.0.18')) { return element; }
		var text = element.innerHTML,
			id = element.update(new Element('div')).identify(),
			variables = { titleText: text.strip().toUpperCase() },
			parameters = { wmode: 'transparent' };
		
		swfobject.embedSWF('/flash/title.swf', id, 600, 62, '8.0.0', null, variables, parameters);
	}
});

var matchHeight = function (rule) {
	var elements = $$(rule);
	var height = elements.inject(0, function (acc, el) {
		var h = el.getHeight() - (parseInt(el.getStyle('marginBottom')) * 12);
		return (acc > h) ? acc : h;
	});
	elements.invoke('setStyle', { height: height + 'px' });
};

document.observe('dom:loaded', function () {
/* 	$$('#title h2').invoke('makeFlash'); */
	$$('#site_search input[type=text]').map(function (element) {
		element.setValue(element.getValue().toLowerCase());
	});
	
	// mark the current page link as 'selected'
	$$('a[href]').select(function (link) { return window.location.href.endsWith(link.readAttribute('href')); }).invoke('addClassName', 'selected');
	
	$$('#slideshow').each(function (slideshow) {
		var images = slideshow.childElements(),
			counter = 0,
			maximum = images.size()-1;
		images.invoke('hide').first().show();
		
		new PeriodicalExecuter(function (pe) {
			var next = (counter < maximum) ? counter + 1 : 0;
			Effect.Fade(images[counter]);
			Effect.Appear(images[next], { delay: 0.3 });
			
			counter = next;
		}, 8);
	});
	
	$$('#credits a[href]').invoke('writeAttribute', 'target', '_blank');
	
	if (/iPhone/.test(navigator.userAgent)) {
		$('content').down('.container').insert({ bottom: $('submenu') });
	}
	
	/*
	$$('body#practitioners .content #portraits div.row > div').each(function (practitioner) {
		practitioner.setStyle('opacity:0.618;background-color:#fff').identify();
		var queue_options = {
			duration: 0.3,
			queue: {
				position: 'end',
				scope: practitioner.id
			}
		}
		
		practitioner.observe('mouseenter', function (event) {
			this.morph('opacity:1', queue_options);
		});
		practitioner.observe('mouseleave', function (event) {
			this.morph('opacity:0.618', queue_options);
		});
	});
	*/
});
