/**
 * $Header: /discyphor.com/js/main.js 2     11/12/08 9:26a Robinp2 $
 *
 * @name	main.js
 * @package	Discyphor.com
 * @author	Phil Robinson <phil.robinson@medtronic.com>
 * @version	$Revision: 2 $
 * @updated	$Modtime: 11/12/08 9:25a $
 */

// creates application namespace 
YAHOO.namespace('app');
YAHOO.app = function() {
	/**
	 * private properties & methods
	 */

	// YUI niceties & shortcuts
	var Dom = YAHOO.util.Dom;
	var Event = YAHOO.util.Event;
	var lang = YAHOO.lang;
	var Selector = (YAHOO.util.Selector != undefined) ?  YAHOO.util.Selector : null;
	var $ = Dom.get;

	/**
	 * convenience/utility variables and methods
	 */

	// transform Firebug console methods to alert if Firebug not available
	if (!window.console || !console.firebug) {
		var names = ['log', 'debug', 'info', 'warn', 'error', 'assert', 'dir', 'dirxml'];
		window.console = {};
		for (var i = 0; i < names.length; ++i)
			window.console[names[i]] = function(o) { alert(o)};
	}


	/**
	 * Uses SWFObject to add the Flash Discyphor Diagnosis in Motion banner and
	 * Discyphor pronunciation guide.
	 * 
	 * Requires /shared/javascript/swfobject.js.
	 */
	var loadFlash = function() {
		// banner
		var so = new SWFObject(
			'/flash/banner.swf',
			'banner',
			'780',
			'170',
			'8',
			'#9ab787'
		);
		so.write('flash-banner');

		// pronunciation
		so.attributes.swf	= '/flash/discyphor.swf';
		so.attributes.id	= 'discyphor';
		so.attributes.width	= '250';
		so.attributes.height= '27';
		so.params.bgcolor	= '#dde9d5';
		so.write('flash-discyphor');
	};


	/**
	 * "Toggles" visibility of menu items. 
	 */
	var toggleMenu = function() {
		var el = Dom.getAncestorByTagName(this, 'li');
		if (Dom.hasClass(el, 'active') == false) {
			Dom.addClass(el, 'active');
		}
		else {
			Dom.removeClass(el, 'active');
		}
	}


	/**
	 * public properties, methods
	 */
	return {
		init: function() {

			// load Flash banner, pronunciation guide on index page
			Event.onAvailable(['flash-banner', 'flash-discyphor'], loadFlash);

			// attach onclick event to print icon link  
			Event.on('link-print', 'click', function(e, el) {
				Event.stopEvent(e);
				window.print();
			});

			// attach hover, click events to main navigation sections
			Event.onAvailable('nav-main', function(){
				var els = Selector.query('#nav-main li li h4');
				Event.on(els, 'mouseover', function() {
					Dom.setStyle(this, 'cursor', 'pointer');
				});
				Event.on(els, 'mouseout', function() {
					Dom.setStyle(this, 'cursor', 'default');
				});
				Event.on(els, 'click', toggleMenu);
			});

			// initialize email form when available
			Event.onAvailable('frmEmail', this.initEmailForm);
		},

		initEmailForm: function() {
			var form = {
				formEmail:	$('frmEmail'),
				elements:	this.elements,
				isValid:	false,
				errors:		[],
				required: {
					recipientname:	'Recipient Name',
					recipientemail:	'Recipient Email',
					sendername:		'Your Name',
					senderemail:	'Your Email',
					captcha_phrase:	'Verification'

				},

				validateForm: function(e) {
					// clear out any existing errors prior to validating form
					form.errors = [];

					// check for required values
					for (el in form.required) {
						if (form.elements[el].value == '') {
							form.errors.push(form.required[el]);
						} 
					}

					// check for valid email addresses
					var patternEmail = /^\w+([\._-]?\w+)*@\w+([\._-]?\w+)*\.\w{2,6}$/; 
					var emails = ['recipientemail', 'senderemail'];
					for (i in emails) {
						value = form.elements[emails[i]].value;
						if (value != '' && !patternEmail.test(value)) {
							form.errors.push(form.required[emails[i]]);
						}
					}

					form.isValid = !!(form.errors.length == 0); // we're good as long as we don't have errors; !! converts to a boolean

					if (form.isValid) {
						$('frmEmail').submit();
					}
					else {
						// stop form submit and output error(s) 
						Event.stopEvent(e);
						form.displayErrors();
					}
				},

				displayErrors: function() {
					// create message DIV if it doesn't exist
					if ($('message') == undefined) {
						div = document.createElement('div');
						div.id = 'message';
						div.className = 'error hide';
						var message = Dom.insertAfter (div, Selector.query('#content > h2:first-child')[0]);
					}
					else {
						// clear and hide errors 
						var message = $('message');
						Dom.replaceClass(message, 'show', 'hide');
					}

					var html = '';
					for (i in form.errors) {
						html += '<li>' + form.errors[i] + '</li>\n'
					}
					message.innerHTML =	'<h3>Error</h3>' +
										'<p>There was an error with the information you provided. Please check the values for the following:</p>' +
										'<ul>' + html + '</ul>'; 

					// show and "focus" the error box
					Dom.replaceClass(message, 'hide', 'show');
					window.scrollTo(0,0);

				}
			};

			// attach validation to form submit
			Event.on('frmEmail', 'submit', form.validateForm);
		}
	}
}();


// init the app when DOM is ready
YAHOO.util.Event.onDOMReady(YAHOO.app.init, YAHOO.app, true);



