var Site = {

	styleSheets: Array("text-decrease","text-standard","text-increase"),
	styleSheetIndex: 1,
	styleSheetCookie: null,
	cookieDomain: '.ausenco.com',

	start: function(){

		MooTools.lang.setLanguage("en-US");

		Site.styleSheetCookie = Cookie.read("styleSheet");
		Site.styleSheetIndex = Site.styleSheetCookie ? Site.styleSheetCookie : Site.getPreferredStyleSheet();

		Site.setActiveStyleSheet(0);

		// 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', '');
			});
		}

		// IE6 Suckerfish fix
		if ( navigator.appVersion.toLowerCase().indexOf('msie 6') != -1 ) {
			var sfElems = $$('#navigation li.suckerfish_level1');

			// Fix the top level elements first
			sfElems.each(function(elem, idx) {
				elem.addEvent('mouseover', function() {
					this.addClass('sfhover');
				})
				elem.addEvent('mouseout', function() {
					this.removeClass('sfhover');
				})
			});

			var sfLists = $$('#navigation ul.suckerfish_level1');

			// Now fix top elements staying active when in a child list
			sfLists.each(function(elem, idx) {
				elem.addEvent('mouseenter', function() {
					this.getParent().addClass('childhover');
				})
				elem.addEvent('mouseleave', function() {
					this.getParent().removeClass('childhover');
				})
			});
		}

		// 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
				});
			});
		}

		if($('accordion-multi')) Site.setupMultipleOpenAccordion();

		if ($('font-increase') && $('font-decrease')) Site.addEventListeners();

		if ($('slideshow')) {
			$('slideshow').fader({
				fadeWaitTime: 4500,
				duration: 500
			});
		}

		if ($('home') && $('feature')) {
			$('feature').fader({
				fadeWaitTime: 4500,
				duration: 500
			});
		}
	},

	addEventListeners : function() {

		$('font-increase').addEvent('click', function(e) {
			e.stop();
			Site.setActiveStyleSheet(1);
		});
		$('font-decrease').addEvent('click', function(e) {
			e.stop();
			Site.setActiveStyleSheet(-1);
		});

	},

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

	setupMultipleOpenAccordion: function() {
		new MultipleOpenAccordion($('accordion-multi'), {
			togglers: $$('#accordion-multi h3.toggler'),
			elements: $$('#accordion-multi div.stretcher'),
			openAll: false,
			opacity: false,
			onActive: function(togglers,elements) {
				togglers.addClass('active');
			},
			onBackground: function(togglers,elements) {
				togglers.removeClass('active');
			}
		});
	},

	getActiveStyleSheet: function() { return Site.styleSheetIndex; },

	getPreferredStyleSheet: function() { return 1; },

	getCookie: function(name) { return Cookie.read(name); },

	setActiveStyleSheet: function(index) {

		Site.styleSheetIndex = parseInt(Site.styleSheetIndex) + index;
		if(Site.styleSheetIndex < 0){
			Site.styleSheetIndex = 0;
		} else if(Site.styleSheetIndex >= Site.styleSheets.length) {
			Site.styleSheetIndex = Site.styleSheets.length - 1;
		}

		Site.setCookie("styleSheet", Site.styleSheetIndex, 1);

		var title = Site.styleSheets[Site.styleSheetIndex];

		var i, a, main;
		for(i=0; (a = document.getElementsByTagName("link")[i]); i++) {
			if(a.getAttribute("rel").indexOf("style") != -1 && a.getAttribute("title") && a.getAttribute("title").indexOf("text") > -1) {
				a.disabled = true;
				if(a.getAttribute("title") == title) a.disabled = false;
			}
		}

		return null;

	},

	setCookie: function(name,value,days) {
		Cookie.write(name, value, {domain : Site.cookieDomain, duration: days ? days : 0});
	}

};

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