var firstassist = {
	init: function() {
		firstassist.clientLogin.init();
		var iQSmoothScroll = new SmoothScroll({transition: Fx.Transitions.Cubic.easeOut});
		firstassist.accordion.init();
		firstassist.fader.init();
		firstassist.multiBox.init();
	},
	
	clientLogin: {
		loginForm: undefined,
		init: function() {
			firstassist.clientLogin.loginForm = $('clientloginform');
		
			firstassist.clientLogin.loginForm.setStyle('display', 'block');
			firstassist.clientLogin.loginForm.slide(Browser.Engine.trident ? 'out' : 'hide');
			
			// Attach overlabels for login form fields
			firstassist.clientLogin.loginForm.getElements('label').each(function(el) {
				var relatedField = $(el.getProperty('for'));
				
				el.setStyle('display', relatedField.value.length ? 'none' : 'inline');
				el.setStyle('position', 'absolute');

				el.setStyle('left', relatedField.offsetLeft + 5 + 'px');
				
				el.addEvent('click', function() {
					$(this.getProperty('for')).focus();
				});
				
				relatedField.addEvent('focus', function(el) {
					this.getParent().getElements('label[for=' + this.id + ']').setStyle('display', 'none');
				});
				
				relatedField.addEvent('blur', function(el) {
					if(!this.value.length) {
						this.getParent().getElements('label[for=' + this.id + ']').setStyle('display', 'inline');
					}
				});
			});
						
			$('clientlogintab').addEvent('click', function(e) {
				e.stop();
				firstassist.clientLogin.loginForm.slide();
			});
		}
	},
	
	fader: {
		fader: undefined,
		currentIndex: 0,
		indicator: undefined,
		interval: 5000,
		
		
		fade: function() {
			var fadeItems = $('client-fade').getElements('img');
			firstassist.fader.fadeTo(firstassist.fader.currentIndex + 1 > fadeItems.length - 1 ? 0 : firstassist.fader.currentIndex + 1);
		},
		
		hideSelected: function() {
			$('client-fade').getElements('img')[firstassist.fader.currentIndex].fade('hide');
		},
		
		fadeTo: function(index, fadeOn) {
			if($('client-fade')) {
				var fadeItems = $('client-fade').getElements('img');
				
				fadeItems[index].fade('in', { onComplete: firstassist.fader.hideSelected });
				
				// Update the fade indicator
				if(firstassist.fader.indicator) {
					var fadeIndicators = firstassist.fader.indicator.getElements('li');
					fadeIndicators[firstassist.fader.currentIndex].removeClass('on');
					fadeIndicators[index].addClass('on');
				}
				
				// Set the current index to the shown index
				firstassist.fader.currentIndex = index;
				
				// Note, if no second argument is sent, things keep cycling
				if(fadeOn === false) {
					$clear(firstassist.fader.fader);
				} else if(fadeOn === true) {
					firstassist.fader.fader = firstassist.fader.fade.periodical(firstassist.fader.interval);
				}
			}
		},
		
		init: function() {
			var container;
			if (container = $('client-fade')) {
				container.setStyle('height', '151px');
				var fadeItems = container.getElements('img');
				
				if(container.hasClass('showindicator')) {
					// Add a location indicator
					firstassist.fader.indicator = new Element('ul', {'id': 'homepage-fader'});
					firstassist.fader.indicator.inject(container);
				}
				
				for(var i = 0, j = fadeItems.length; i < j; i++) {
					if(firstassist.fader.indicator) {
						new Element('li', {
							'class': (i === 0 ? 'on' : ''),
							'events': {
								'click': new Function('firstassist.fader.fadeTo(' + i + ', false); return false;')
							}
						}).inject(firstassist.fader.indicator);
					}
					fadeItems[i].setStyle('position', 'absolute');
				
					if(i > 0) {
						fadeItems[i].fade('hide');
					}
				}
				
				// Set a cycle event
				firstassist.fader.fader = firstassist.fader.fade.periodical(firstassist.fader.interval);
			}
		}
	},
	
	accordion: {
		currentAccordion: 0,
		startingIndex: -1,
		
		init: function() {
			$$('.accordion').each(function(el, index) {
				firstassist.accordion.currentAccordion = index;

				el.getElements('.accordionheader').each(function(header, headerIndex) {
					// Separate each accordion on each page
					header.addClass('iQ-accordionheader-' + firstassist.accordion.currentAccordion);
					header.getNext().addClass('iQ-accordiontext-' + firstassist.accordion.currentAccordion);
					if(header.hasClass('open')) {
						firstassist.accordion.startingIndex = headerIndex;
					}
				});
			
				var accordion = new Accordion($$('.iQ-accordionheader-' + index), $$('.iQ-accordiontext-' + index), {
					alwaysHide: true,
					show: firstassist.accordion.startingIndex,
					onActive: function(toggler, element) {
						toggler.getChildren(0).addClass('opennode');
					},
					onBackground: function(toggler, element) {
						toggler.getChildren(0).removeClass('opennode');
					}
				});
			});
		}
	},
	
	multiBox: {
		box: {},
	
		init: function() {
			firstassist.multiBox.box = new MultiBox('mb', {useOverlay: true});
		}
	}
};

window.addEvent('domready', firstassist.init);