var Site = {

	styleSheets: Array("text-decrease","text-standard","text-increase"),
	styleSheetIndex: 1,
	styleSheetCookie: null,
	cookieDomain: 'mvm-rail.localhost', // TODO: CHANGE ON GOLIVE

	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', '');
			});
		}
		
		// 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 ($$('#primary table.pretty-table')[0]) {
			var prettyTables = $$('#primary table.pretty-table')
			$each(prettyTables, function(table, index) {
				var trs = table.getElements('tr:nth-child(2n+1)');
				$each(trs, function(tr, index) {
					tr.addClass('alt');
				});
			});
		}
		
		if ($('search')) Site.addSearchEventListeners();
		if ($('text-increase') && $('text-decrease')) Site.addToolsEventListeners();
		if ($('print')) $('print').getElement('a').addEvent('click',function(e) { e.stop(); window.print(); });
		
		if ($('suckerfish2_menu_6')) {
			var defaultBackgroundPosition = $('hero-overlay').getStyle('background-position');
			$('suckerfish2_menu_6').addEvents({
				'focus' : function() {
					$('hero-overlay').setStyle('background-position', '0 -193px');
				},
				'mouseover' : function() {
					$('hero-overlay').setStyle('background-position', '0 -193px');	
				},
				'mouseout' : function() {
					$('hero-overlay').setStyle('background-position', defaultBackgroundPosition);	
				},
				'click' : function() {
					$('hero-overlay').setStyle('background-position', '0 -193px');	
				}
			});
		};
		
	},

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

	addSearchEventListeners: function () {
		var label = $('search').getElement('label');
		var query = $('search').getElement('#query');
		query.addEvents({
			'focus' : function () {
				label.setStyle('left', '-9999px');
			},
			'blur' : function () {
				if (this.value == '')
					label.setStyle('left', '0');
			}
		});
	},
	
	addToolsEventListeners : function() {
		
		$('text-increase').addEvent('click', function(e) {
			e.stop();
			Site.setActiveStyleSheet(1);											  
		});
		$('text-decrease').addEvent('click', function(e) {
			e.stop();
			Site.setActiveStyleSheet(-1);											  
		});
		
	},
	
	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;
			}
		}
		
		if (Cufon)
			Cufon.refresh();
		
		return null;
		
	},
	
	setCookie: function(name,value,days) {
		Cookie.write(name, value, {domain : Site.cookieDomain, duration: days ? days : 0});
	}
	
};

window.addEvent('domready', Site.start);
