var Site = {
	currentDivision: 0,
	currentSize: 0,
	
	start: function(){
		MooTools.lang.setLanguage("en-US");
		Site.attachPageActions();

		// Launch-in-new-window links automagically created
		var extLinks = $$('a.external, a[title=Print]');
		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
				});
			});
		}
	},
	
	setFontSize: function(size) {
		if (size > 3 || size < 0) return false;
		$('content').removeClass('increase-font-size-1');
		$('content').removeClass('increase-font-size-2');
		$('content').removeClass('increase-font-size-3');
		$('content').addClass('increase-font-size-'+size);
		Cookie.write('font-size', size, {path:'/', duration:0});
		Site.currentSize = size;
	},
	
	attachPageActions: function() {
		if ( $('search_input') ) {
			$('search_input').addEvent('blur', function(){
				if(this.value=='')
					this.style.backgroundPosition='0 0';
			});
			$('search_input').addEvent('focus', function(){
				this.style.backgroundPosition='0px -22px';
			});
		}
		
		
		if ( $('flash_replacement') ) {
			var flashvars = {
				settings:'/xml/banner.xml'
			};
			var params = {	
				wmode:'transparent'
			};
			swfobject.embedSWF("/swfs/photoview.swf", "flash_replacement", "616", "415", "8.0.0", "", flashvars, params);
		}
		
		//Font Size Links 
		if ( $('increase-font-size') && $('decrease-font-size') ) {
			Site.currentSize = parseFloat(Site.currentSize);
			
			if (!isNaN(Cookie.read('font-size')) && Cookie.read('font-size') >= 0) Site.setFontSize(Cookie.read('font-size')); 
			
			if (Site.currentSize == null) Site.currentSize = 0;
			
			$('increase-font-size').addEvent('click', function(event) {
				Site.setFontSize(parseFloat(Site.currentSize) + 1);
				event.preventDefault();
			});
			$('decrease-font-size').addEvent('click', function(event) {
				Site.setFontSize(parseFloat(Site.currentSize) - 1);
				event.preventDefault();
			});
		}
		
		// accordion listing
		if ( $('accordion') ) {
			
			// Extend the Accordion class so we can open multiple elements
			var MultipleAccordion = new Class({
				Extends: Accordion,
				options: {
					allowMultipleOpen: true,
					display: -1
				},

				initialize: function(togglers,togglees,options){
					this.parent(togglers,togglees,options);
				},

				display: function(index){
					index = ($type(index) == 'element') ? this.elements.indexOf(index) : index;
					if ((this.timer && this.options.wait) || (index === this.previous && !this.options.alwaysHide)) return this;
					
					var obj = {};
					if(this.options.allowMultipleOpen){
						var el = this.elements[index];
						obj[index] = {};
						var hide = (el.offsetHeight > 0);
						this.fireEvent(hide ? 'onBackground' : 'onActive', [this.togglers[index], el]);
						for (var fx in this.effects) obj[index][fx] = hide ? 0 : el[this.effects[fx]];
					}else{
						this.previous = index;
						this.elements.each(function(el, i){
							obj[i] = {};
							var hide = (i != index) || (this.options.alwaysHide && (el.offsetHeight > 0));
							this.fireEvent(hide ? 'onBackground' : 'onActive', [this.togglers[i], el]);
							for (var fx in this.effects) obj[i][fx] = hide ? 0 : el[this.effects[fx]];
						}, this);
						
					}
					return this.start(obj);
				},

				onComplete: function() {
					/* Trick IE6 into working correctly */
					$('footer').style.bottom = 1;
					$('footer').style.bottom = 0;
				}
			});

			// Instantiate the accordion
			var myAccordion = new MultipleAccordion($('accordion'), 'h3.toggler', 'div.element', {
				display: -1,
				opacity: false,
				alwaysHide: true,
				allowMultipleOpen: true,

				onActive: function(toggler, element){
					toggler.addClass('toggler_active');
				},
				onBackground: function(toggler, element){
					toggler.removeClass('toggler_active');
				}
			});
			
			// Change the cursor to a hand-pointer
			$$('h3.toggler').each(function(elem, idx) {
				elem.addEvent('mouseenter', function() {
					this.style.cursor = 'pointer';
				});
			});
		}
		
		
		// Division/labs lookup
		if ( $('division-lookup') ) {
			var divisionCbo = $('cbo-division');

			if ( Site.currentDivision ) {
				for ( x = 0; x < divisionCbo.options.length; x++ ) {
					if ( 'divisionId'+Site.currentDivision == divisionCbo.options[x].id ) {
						divisionCbo.options[x].selected = true;
						Site.ajaxGetLabs(divisionCbo.options[x].value);
					}
				}
			}
			
			$('cbo-division').addEvent('change', function() {
				if ( this.options[this.selectedIndex].value ) {
					Site.ajaxGetLabs(this.options[this.selectedIndex].value);
				}
			});
			$('cbo-lab').addEvent('change', function() {
				if ( this.options[this.selectedIndex].value ) {
					window.location = this.options[this.selectedIndex].value;
				}
			});
		}
		
	},
	
	
	ajaxGetLabs: function(pageUrl) {
		var request = new Request.JSON({	'method': 'get',
											'url': '/index.php',
											'data': {	'action': 'getPageFromName',
														'page': pageUrl,
														'rtemplate': '10'
													},
											'onRequest': function() {
												// Do something
											},
											'onComplete': function(response) {
												Site.fillLabs(response);
											}
										}).send();
	},
	
	
	fillLabs: function(labList) {
		var selectLab = $('cbo-lab');
		selectLab.empty();
		selectLab.adopt(new Element('option', {'value': '', 'text': '-- Lab --'}));
		
		labList.each(function(lab, idx) {
			if ( !lab.final ) {
				selectLab.adopt(new Element('option', {'value': lab.url, 'text': lab.title}));
			}
		});
	},
	
	
	formHandler: function(pass, form, submitEvent) {
		// Do anything necessary here
	}
	
};


// Do stuff on load
window.addEvent('load', Site.start);
