var Site = {
	slide_link: false,

	start: function(){
		MooTools.lang.setLanguage("en-US");

		// Launch-in-new-window links automagically created
		var extLinks = $$('a.external');
		if ( extLinks.length ) {
			extLinks.each(function(elem, idx) {
				elem.setProperty('target', '_blank');
			});
		}


		// Safari Suckerfish 'fix'
		if ( navigator.appVersion.toLowerCase().indexOf('safari') != -1 ) {
			var navElems = $$('#navigation li a');
			navElems.each(function(elem, idx) {
				elem.set('title', '');
			});
		}


		// Form validation automagic
		var valForms = $$('form.validate-form');
		if ( valForms.length ) {
			valForms.each(function(elem, idx) {
				new FormValidator.Inline(elem, {
					'onFormValidate': Site.formHandler,
					'errorPrefix': '',
					'useTitles': true
				});
			});
		}

		// Homepage Feature
		if ( $('home_feature_image') ) {
			var so = new SWFObject('/swfs/feature.swf','feature-swf','326','184','9');
			so.addParam("wmode","transparent");
			so.useExpressInstall("/swfs/expressinstall.swf");
			so.write('home_feature_image');
			$('home_feature_image').style.backgroundImage = 'none';
		}


		var Tips3 = new Tips(('.rss'), { fixed: true, offsets: {x: -20, y: 30} });

		Site.attachResizeButtons();
		Site.attachPageActions();

		// google map scroller
		if ( $('map') ) {
			GEvent.addDomListener($('map'), "DOMMouseScroll", wheelevent);
			$('map').onmousewheel = wheelevent;
			loadGMap();
		}

		// Tiny calendar
		if ($('events_calendar')) {
			$$('.tc_hasevents').each(function(elem) {
				// When a date is hovered...
				elem.addEvent('mouseover', function(){
					// Hide any other popups that might be showing
					// and remove the timer if it's there
					Site.hideCalendarPopups();
					clearTimeout(Site.calendarTimer);

					// Display this popup
					elem.getChildren('ul').show();
				});

				// When not hovered, start timer to remove the box
				elem.addEvent('mouseout', function(){
					Site.calendarTimer = setTimeout(Site.hideCalendarPopups, 1000);
				});
			});

			// .tc_multi_event = popup box
			$$('.tc_multi_event').each(function(elem){
				// When we mouseover the popup box...
				elem.addEvent('mouseover', function(){
					// Remove the timer so it doesn't disappear
					clearTimeout(Site.calendarTimer);
				});
			});
		}
	},

	hideCalendarPopups: function() {
		$$('.tc_multi_event').each(function(elem){
			elem.hide();
		});
	},

	calendarTimer: null,

	attachPageActions: function() {


		if ($('query')) {
			$('query').addEvent('focus', function() {
				if (this.value == 'Search our site') {
					this.value = '';
				}
			});
			$('query').addEvent('blur', function() {
				if (this.value == '') {
					this.value = 'Search our site';
				}
			});
		}
		if ($('email')) {
			$('email').addEvent('focus', function() {
				if (this.value == 'Email') {
					this.value = '';
				}
			});
			$('email').addEvent('blur', function() {
				if (this.value == '') {
					this.value = 'Email';
				}
			});
		}
		if ($('password')) {
			$('password').addEvent('focus', function() {
				if (this.value == 'Password') {
					this.value = '';
				}
			});
			$('password').addEvent('blur', function() {
				if (this.value == '') {
					this.value = 'Password';
				}
			});
		}

		if ($('login_button')) {

			$('login_button').addEvent('mouseover', function() {
				this.src = "/images/interface/buttons/login_hover.gif"
			});

			$('login_button').addEvent('mouseout', function() {
				this.src = "/images/interface/buttons/login.gif"
			});
		}

/*		if ($('cbx_topic_gps')) {
			$('cbx_topic_gps').addEvent('change', function() {
			  location.href = this.value;
			});
		}

		if ($('cbx_topic_nurses')) {
			$('cbx_topic_nurses').addEvent('change', function() {
			  location.href = this.value;
			});
		}

		if ($('cbx_topic_staff')) {
			$('cbx_topic_staff').addEvent('change', function() {
			  location.href = this.value;
			});
		}

		if ($('cbx_topic_consumers')) {
			$('cbx_topic_consumers').addEvent('change', function() {
			  location.href = this.value;
			});
		}*/

		if ($$('.resource_content')[0]) {
			$$('.resource_content').each(function(slide_container, i){
				slide_container.slide('hide');

			});
			$$('.resource_title').each(function(slide_link, i){
				if ($$('.resource_content')[i]) {
					slide_link.href = "javascript:void(0)";
					slide_link.addEvent('click', function() {
						this.toggleClass('slide_link_active');
						$$('.resource_content')[i].slide();
						return false;
					});
				}
			});
/*			$$('.letter').each(function(alpha_link){
				alpha_link.addEvent('click', function(){
					var letter = this.toString().split('#')[1].replace('content-', '');

					var slide_links = $$('#content-'+letter+' .resource_title');

					$$('#content-'+letter+' .resource_content').each(function(element, i){
						if (element.open) {
							return;
						}
						slide_links[i].toggleClass('slide_link_active');
						element.slide();
						return false;
					});
				});
			});*/
		}


	},

  attachResizeButtons: function() {
		if ( $('text_smaller') ) {
			$('text_smaller').addEvent('click', function() {
				scaleDown();
			});
		}
		if ( $('text_bigger') ) {
			$('text_bigger').addEvent('click', function() {
				scaleUp();
			});
		}
	},

	formHandler: function(pass, form, submitEvent) {
		// Do anything necessary here
	},

	iDirectorySubmission: function() {

		$$('.group_container').each(function(group_container) {
			var i = 0;
			group_container.getElements('.group_item').each(function(group_item) {
				if (i != 0) {
					group_item.hide();
					group_item.addClass('hidden');
				}
				i++;
			});

			var groupContainerClass = group_container.get('class');
			groupContainerClass = groupContainerClass.replace('group_container ','');

			var duplicatorButton  = new Element('button', {
				'html': 'Add Another',
				'class':'duplicator_button'
			});
			duplicatorButton.inject(group_container);

			duplicatorButton.addEvent('click',function(el) {
				el.stop();

				var nextGroupItem = Site.getNextHiddenFieldset(group_container);

				if (nextGroupItem) {
					nextGroupItem.reveal();
					nextGroupItem.removeClass('hidden');
				}

				if (!Site.getNextHiddenFieldset(group_container)) duplicatorButton.hide();

				return false;
			});
		});
	}



};


// Do stuff on load
window.addEvent('load', Site.start);