var Site = {
	
	recommendSlider: null,
	pageScroller: null,
	
	init: function() {
		Site.setupAboutScroller();
		Site.setupNavigation();
		Site.addRolloverEffects();
		Site.setupSearchBox();
		Site.addAccordionEffect();
		Site.addRecommendSlider();
		Site.setupNewsletterFormValidation();
		Site.setupRecommendFormValidation();
	},
	
	setupAboutScroller: function() {
		var aboutContainer = $('mainAbout');
		
		if (aboutContainer) {
			var scroller = new ContentScroller({slideDuration: 5000, fadeDuration: 1000 });
			scroller.addSlides(aboutContainer.getElements('img').setStyle('z-index', 1));
			scroller.start();
		}
	},
	
	setupNavigation: function() {
		// Code to hide menus
		
		// add in selector images
		new Element('img', {
			'id': 'type',
			'alt': 'Type.',
			'src': '/images/layout/type.gif',
			'class': 'navImage'
			}).inject($('leftNav'), 'top');
		
		new Element('img', {
			'alt': ' ',
			'src': '/images/layout/divider.gif'
			}).inject($('leftNav'), 'top');

		new Element('img', {
			'id': 'brand',
			'alt': 'Brand.',
			'src': '/images/layout/brand.gif',
			'class': 'navImage'
			}).inject($('leftNav'), 'top');
		
		
		new Element('img', {
			'id': 'pleaseChoose',
			'alt': 'Please choose.',
			'src': '/images/layout/pleaseChoose.gif'
			}).inject($('leftNav'), 'top');
		
		// get the previously selected menu from the cookie, and if not exists
		// then default to Type menu
		var selectedMenu = Cookie.read('menu');
		if (!selectedMenu) {
			selectedMenu = "brand";
		}

		// now make all menus hidden before classing the selected menu
		$$('.menuCategory').each(function(el) {
			el.addClass('hidden');
		});
		$('category' + selectedMenu).removeClass('hidden').addClass('selected');
		
		// update the Brand or Type image to match which is the selected menu
		Site.navImagesSwap(selectedMenu);

		// add event handling to the two images
		$$('.navImage').addEvent('click', function(e) {
			e.stop();
			var id = this.get('id');
			var category = "category" + id;
			Site.navImagesSwap(id);
			
			$$('li.menuCategory').each(function(el){
				el.removeClass('selected');
				el.addClass('hidden');
			});
			$(category).removeClass('hidden');
			$(category).addClass('selected');
			Cookie.write('menu', id, {duration:0.1});
		});
		
		$$('.menuCategory').each(function(li){
			li.getElement('span').setStyle('display', 'none');
		});
		
		// now show the menu
		//$('leftNav').set('style', 'visibility:visible;');
	},
	
	navImagesSwap: function(id) {
		switch (id) {
			case ("type") : {
				$('type').set("src", "/images/layout/type.gif")
				$('brand').set("src", "/images/layout/brand-no.gif");
				break;
			}
			case ("brand") : {
				$('brand').set("src", "/images/layout/brand.gif");
				$('type').set("src", "/images/layout/type-no.gif");
			}
		}
	},
	
	addRolloverEffects: function() {
		$$('.rollover').each(function(el) {
			if (el.src.test(/\.(gif|jpg|jpeg|png)$/i)) {
				// preload image
				var preload = new Image();
				preload.src = el.src.replace(/\.(gif|jpg|jpeg|png)/, '-over.$1');
				
				el.addEvents({
					mouseover: function() {
						this.src = this.src.replace(/\.(gif|jpg|jpeg|png)/, '-over.$1');
					},
					
					mouseout: function() {
						this.src = this.src.replace(/-over\.(gif|jpg|jpeg|png)/, '.$1');
					}
				});
			}
		});
	},
	
	setupSearchBox: function() {
		
		$('searchText').addEvent('focus', function() {
			if (this.get('value') == "Search Room 14") {
				this.set('value','');
			}
			this.addClass('inputSelected');
		});
		$('searchText').addEvent('blur', function() {
			if (this.get('value') == "") {
				this.set('value', "Search Room 14");
				this.removeClass('inputSelected');
			}
		});
	},
	
	addAccordionEffect : function() {
		// hide all sections on opening
		var acc = new Accordion($$('.toggler'), $$('.toggledElement'), {
			show: -1,
			alwaysHide: true
		});
	},
	
	addRecommendSlider : function() {
		if (!Browser.Engine.trident4 && $('recommendForm')) {
			Site.recommendSlider = new Fx.Slide($('recommendForm'), { duration: 200, link: 'cancel' }).hide();
			if ($$('div#leftNav div#recommendForm .error').length > 0) Site.recommendSlider.slideIn();
			
			Site.pageScroller = new Fx.Scroll($(document.window), {});
			
			$('recommendus').addEvent('click', function() {
				if (!Site.recommendSlider.open) Site.pageScroller.toElement($('recommendForm'));
				Site.recommendSlider.toggle();
			});
		}
	},
	
	setupNewsletterFormValidation : function() {
		
		var signupName 			= $('signupName');
		var signupEmailAddress 	= $('signupEmailAddress');

		if (signupName && signupEmailAddress) {

			signupName.store('validator', new RequiredFieldValidator(signupName, 'Your Name', {
				'errorOnEmpty': false,
				'showTextInBox': true,
				'errorMessage': 'Please enter your name'
			}));
			
			signupEmailAddress.store('validator', new RequiredFieldEmailSubscribedValidator(signupEmailAddress, 'Your Email Address', {
				'errorOnEmpty': false,
				'showTextInBox': true
			}));
		}
	},
	
	setupRecommendFormValidation : function() {
		if($('recommendFromName')) {
			$('recommendFromName').store('validator', new RequiredFieldValidator($('recommendFromName'), "Your Name", {
				'errorOnEmpty': false,
				'showTextInBox': true,
				'errorMessage': 'Please enter your name'
			}));
	
			$('recommendToName').store('validator', new RequiredFieldValidator($('recommendToName'), "Your Friend's First Name", {
				'errorOnEmpty': false,
				'showTextInBox': true,
				'errorMessage': "Please enter your friend's name"
			}));
			
			$('recommendFromEmail').store('validator', new RequiredFieldEmailValidator($('recommendFromEmail'), "Your Email Address", {
				'errorOnEmpty': false,
				'showTextInBox': true
			}).addEvent('onComplete', function() { if (Site.recommendSlider) Site.recommendSlider.slideIn(); }));
			
			
			$('recommendToEmail').store('validator', new RequiredFieldEmailValidator($('recommendToEmail'), "Friend's Email Address", {
				'errorOnEmpty': false,
				'showTextInBox': true
			}).addEvent('onComplete', function() { if (Site.recommendSlider) Site.recommendSlider.slideIn(); }));
		}
		
	}
};

window.addEvent('domready', Site.init);