var Site = {
	
	start: function(){
		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': '',
					'useTitles': true
				});
			});
		}

		// Zoom links
		Site.attachZoomLinks();

		// Print button
		Site.attachPrintLink();

		// Form overtext magic
		Site.attachOverTexts();
		
		// Submission link automagic
		Site.attachSubmitLinks();
	},

	attachZoomLinks: function() {

		var container = $$('#page-content-container');
		var size = 2;
		var classes = [ 'zoom-smallest', 'zoom-small', 'zoom-normal', 'zoom-big', 'zoom-biggest' ];

		var cookie = new Hash.Cookie('zoom', {
			duration: 60,
			path : '/',
			domain : document.location.hostname
		});

		var resize = function(newSize, save) {

			//Limits
			if (newSize < 0) {
				newSize = 0;
			} else if (newSize >= classes.length) {
				newSize = classes.length - 1;
			}

			//Apply styles
			container.removeClass(classes[size]);
			container.addClass(classes[newSize]);
			size = newSize;

			//Save to cookie
			if (save) {
				cookie.set('size', size);
				cookie.store();
			}
		}

		cookie.load();
		if (cookie.get('size') !== null) {
			resize(parseInt(cookie.get('size'), 10), false);
		}

		$$('#resize-large').addEvent('click', function() {
			resize(size + 1, true);
		});

		$$('#resize-small').addEvent('click', function() {
			resize(size - 1, true);
		});
	},


	attachPrintLink: function() {
		$$('#print-page').addEvent('click', function() {
			window.print();
		});
	},

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

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