var Site = {
	
	start: function(){
		
		var dontHideShowroomTooltip = false;
		
		MooTools.lang.setLanguage("en-US");

		// 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': '',
					'scrollToErrorsOnSubmit': false,
					'useTitles': true
				});
			});
		}
		
		// Form overtext magic
		Site.attachOverTexts();
		
		// Submission link automagic
		Site.attachSubmitLinks();
		
		//Attach font resize button actions
		Site.attachResizeButtons();
		
		//Attach print button action
		Site.attachPrintButton();
		
		//Showroom Nav
		// $$('#showroom-nav > li').each(function(thisone) {
		// 	childrens = thisone.getElements('.sitemap_category');
		// 	childrens.hide();
		// 	thisone.addEvent('click',function(e){
	
		// 		childrens = thisone.getElements('.sitemap_category');
		// 		childrens.show();
		// 		return false;
		// 	});
		// });
		
		// Showroom Thumbnail Effects (fades out all 
		// thumbnails except the one hovered over. 
		// Erase removes the current element for us.
		// Fades back in as well)
		$$('.showroom-item').addEvent('mouseover',function() {
			$$('.showroom-item').erase(this).fade(0.3);
		});
		
		$$('.showroom-item').addEvent('mouseout',function() {
			$$('.showroom-item').fade('in');
		});
		
		//Setup radio groups
		Site.radioSelectorGroups();
		
		//Add validation for application form.
		if ($('job-application-form')) {
			new FormValidator.Inline($('job-application-form'), {
				'useTitles': true
			});
		}
		
		//Accordion
		if ($$('.accordion')) {
			
			var pageUri = new URI(window.location);
			var anchor = pageUri.get('fragment');
			
			var networkAccordionSectionMapping = new Array(
				'Australia',
				'American_Samoa',
				'Fiji',
				'Papua_New_Guinea',
				'Samoa',
				'Solomon_Islands',
				'Tonga',
				'Vanuatu'
			);
			
			selectedItem = networkAccordionSectionMapping.indexOf(anchor);
			
			if (selectedItem < 0) selectedItem = 0;
			
			//Attach network accordion
			var networkAccordion = new Fx.Accordion(
				$$('.accordion .accordion-toggle'), 
				$$('.accordion .accordion-content'),
				{
					display: selectedItem,
					initialDisplayFx: false,
					onActive: function(toggler,element) {
						toggler.addClass('active');
					},
					onBackground: function(toggler,element) {
						toggler.removeClass('active');
					}
				}
			);
			
			var windowScroll = new Fx.Scroll(window);
			
			if ($('accordion-sub-content')) {
				$$('#accordion-sub-content a').each(function(linkElement,index) {
					linkElement.addEvent('click',function(event) {
						//plenty could go wrong here that we'd rather not know about, so we have an empty error handler
						//try {
							$$('#accordion-sub-content li').each(function(listItem) {
								listItem.removeClass('ctoc_current');
							});
							linkElement.getParent().addClass('ctoc_current ');
							var item = linkElement.getProperty('rel');
							$$('.accordion .'+item).each(function(contentItem) {
								// We use getPrevious further down to make the scroll too a bit nicer, 
								// otherwise the heading gets cut off. Unfortunately this doesnt work 
								// for the first element, as there are no previous siblings, so we've 
								// hard coded it to another element near the top of the page.
								if (index <= 1) {
									toel = $('banner');
								} else {
									toel = contentItem.getPrevious().getPrevious();
								}
								networkAccordion.display(contentItem);
								windowScroll.toElement(toel);
							});
						//} catch(e) {}
						return false;
					});
				});
			}
		}
		
		//slideitmoo
		
		if ($('SlideItMoo_outer')) {
			new SlideItMoo({
				overallContainer: 'SlideItMoo_outer',
				elementScrolled: 'SlideItMoo_inner',
				thumbsContainer: 'SlideItMoo_items',		
				itemsVisible:1,
				elemsSlide:1,
				duration:800,
				autoSlide: 2000,
				itemsSelector: '.SlideItMoo_element',
				itemWidth: 293,
				showControls:1,
				startIndex:1,
				onChange: function(index){
					
				}
			});
		}
		
		/*$$('#SlideItMoo_outer .SlideItMoo_element > a[title]').each(function(el) {
			console.log(el);
			el.addEvent('mouseover',function(e){
				
				var elFx = new Fx.Tween(el);
				elFx.start('height', 300);
			});
			el.addEvent('mouseout',function(e){
				var elFx = new Fx.Tween(el);
				elFx.start('height', '300', '260');
			});

		})
		*/
		
		//slideitmoo tooltips
		/*
		Site.displayingShowroomTooltip = false;
		Site.dontHideShowroomTooltip = false;
		
		var showroomItems = $$('#SlideItMoo_outer .SlideItMoo_element > a[title]');
		showroomItems.each(function(el) {
			
			el.store('tooltiptext',el.get('title'));
			el.set('title','');
			
			el.addEvent('mouseover',function(e) {
				if (!Site.displayingShowroomTooltip) {
					Site.displayingShowroomTooltip = true;
					var tooltip = new Element('div', {id: 'showroom-tooltip',html:el.retrieve('tooltiptext')});
					tooltip.inject(el,'before');
					
					tooltip.addEvent('mouseover',function() {
						Site.dontHideShowroomTooltip = true;
					});
					tooltip.addEvent('mouseout',function() {
						Site.dontHideShowroomTooltip = false;
					});
					
					tooltip.hide();
					tooltip.reveal();
				}
			});
		});
		
		
		
		var hideSlideshowCaption = function(){
			console.log('hhhh');
			hideSlideshowCaptionNow.delay(100);
		}

		var hideSlideshowCaptionNow = function(){
			if ($('showroom-tooltip') && !Site.dontHideShowroomTooltip) {
				$('showroom-tooltip').nix(true);
				console.log('hiding');
				Site.displayingShowroomTooltip = false;
			}
		}
		
		showroomItems.each(function(el) {
			el.addEvent('mouseout',hideSlideshowCaption);
		});
		*/
		
		
		
		
		
		
		
		if ($('country_quick_select')) {
			$('country_quick_select').addEvent('change', function(e) {
				$('directory-country').submit();
			});
		}

		if ($('directory-country-button')){
			$('directory-country-button').hide();
		}
		
		
		if ($('contact_form')){
			new FormValidator.Inline($('contact_form'));
		}
		
		window.addEvent('TBshowWindow',function(){
			if ($('contact_form')){
				new FormValidator.Inline($('contact_form'), {
					'scrollToErrorsOnSubmit': false,
					'useTitles': true
				});
			}
		});
	
	},
	
	
	formHandler: function(pass, form, submitEvent) {
		// Do anything necessary here
	},
	
	
	attachOverTexts: function() {
		var overElems = $$('input.overtext');
		if ( overElems.length ) {
			overElems.each(function(elem, idx) {
				elem.setProperty('overType', elem.getProperty('type'));
				
				if ( elem.getProperty('alt') ) {
					// Focus state
					elem.addEvent('focus', function() {
						if ( this.value == this.getProperty('alt')) {
							if ( this.getProperty('overType') == 'password' ) {
								elem = Site.cloneAndChangeInputType(elem, 'password', true);
							} else {
								this.value = '';
							}
						}
					});
					
					// Blur state
					elem.addEvent('blur', function() {
						if ( this.value == '') {
							if ( this.getProperty('overType') == 'password' ) {
								elem = Site.cloneAndChangeInputType(elem, 'text');
								elem.value = elem.getProperty('alt');
							} else {
								this.value = this.getProperty('alt');
							}
						}
					});
					
					// Default state
					if ( elem.value == '') {
						if ( elem.getProperty('overType') == 'password' ) {
							elem = Site.cloneAndChangeInputType(elem, 'text');
						}
						
						elem.value = elem.getProperty('alt');
					}
				}
			});
		}
	},
	
	
	attachSubmitLinks: function() {
		// Submit link magic
		var submitLinks = $$('.submit-link');
		if ( submitLinks.length ) {
			submitLinks.each(function(elem, idx) {
				var props = elem.getProperty('class').split(' ');
				
				if ( props.length ) {
					props.each(function(propItem, pidx) {
						if ( propItem.indexOf(':') != -1 ) {
							var parsedProps = JSON.decode('{'+propItem+'}');
							elem.setProperties(parsedProps);
						}
					});
				}
				
				if ( elem.getProperty('submitTarget') ) {
					elem.addEvent('click', function(event) {
						if ( $(this.getProperty('submitTarget')).validate() ) {
							$(this.getProperty('submitTarget')).submit();
						}
					});
				}
			});
		}
	},
	
	attachResizeButtons: function() {
		if ( $('font-size-decrease') ) {
			$('font-size-decrease').addEvent('click', function(event) {
				event.stop();
				Site.decreaseSize('main_content');
				return false;
			});
		}
		if ( $('font-size-increase') ) {
			$('font-size-increase').addEvent('click', function(event) {
				event.stop();
				Site.increaseSize('main_content');
				return false;
			});
		}
	},
	
	attachPrintButton: function() {
		if ($('print-page')) {
			$('print-page').addEvent('click', function() {
				window.print();
				return false;
			});
		}
	},
	
	/**
	 * A litte script to increase / decrease the size of the
	 * font for a particular section
	 */
	
	 increaseSize: function(elementId) {
		 
		var element = document.getElementById(elementId);
		
		if (element != null) {
			// we have an element, increase the size...
			currentSize = element.style.fontSize;
			var newSize = 130;
			if (currentSize != '') {
				newSize = parseInt(currentSize);
				newSize += 10;
			}
			
			if (newSize > 200) return false;
			element.style.fontSize = newSize + "%";
		}
	},
	
	decreaseSize: function(elementId) {
		var element = document.getElementById(elementId);
		if (element != null) {
			// we have an element, increase the size...
			currentSize = element.style.fontSize;
			var newSize = '120';
			if (currentSize != '') {
				newSize = parseInt(currentSize);
				newSize -= 10;
			}
			if (newSize < 90) return false;
			element.style.fontSize = newSize + "%";
		}
	},
	
	radioSelectorGroups: function() {
		$$('.radio-selector-group').each(function(group) {
			var fields = group.getElements('.radio-selector-field input[type=radio]');
			var items = group.getElements('.radio-selector-item');
			items.hide();
			fields.each(function(field){
				if (field.get('checked')) $$('.'+field.get('id')).show();
				if (field.get('checked')) {
					$$('.'+field.get('id')+' input').addClass('required');
					$$('.'+field.get('id')+' textarea').addClass('required');
				}
				field.addEvent('click',function(e){
					items.getElements('textarea').each(function(e) {e.removeClass('required')});
					items.getElements('input').each(function(e) {e.removeClass('required')});

					items.dissolve();
					$$('.'+field.get('id')).reveal();
					$$('.'+field.get('id')+' input').addClass('required');
					$$('.'+field.get('id')+' textarea').addClass('required');
				});
			});
		});
	}
	
};

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

function emptyfield (field) {
	document.getElementById(field).value='';
}

