var Site = {
	enlargeCookie: null,
	cookieDomain: 'assenttecs.com.au',		// TODO: Change before golive
	fontSize: 0,

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


		// Form overtext magic
		Site.attachOverTexts();


		// Submission link automagic
		Site.attachSubmitLinks();

		Site.attachRowRadioClick();


		Site.enlargeCookie = new Hash.Cookie('enlargeCookie', {	'duration': 	60,
																'domain': 		Site.cookieDomain,
																'path': 		'/'
															});


		// Page sizing functions
		if ( Site.enlargeCookie.get('current_size') ) {
			Site.modifyText(Site.enlargeCookie.get('current_size'));
		} else {
			Site.modifyText(0);
		}

		if ( $('font-smaller') ) {
			$('font-smaller').addEvent('click', function(event) {
				Site.modifyText(-1);
			});
		}

		if ( $('font-bigger') ) {
			$('font-bigger').addEvent('click', function(event) {
				Site.modifyText(1);
			});
		}

		if ( $('print') ) {
			$('print').addEvent('click', function(event) {
				window.print();
			});
		}

		if ($('member-number')) {
			$('member-number').addEvent('keyup', Site.updateOpdForm);
			$('organisation').addEvent('change', Site.updateOpdForm);
			Site.updateOpdForm();
		}

		if ($('gst')) {
			$('gst').addEvent('change', Site.updateSpsiForm);
			Site.updateSpsiForm();
		}
	},

	updateOpdForm: function() {
		var price = parseFloat($('fee').get('value'));
		var discount = parseFloat($('discount').get('value'));
		
		if (isNaN(price)) {
			price = 0;
		}
		
		if (isNaN(discount)) {
			discount = 0;	
		}
		
		if ($('member-number').getProperty('value').length == 0) {
			Site.setPrice(price);
			return;
		}

		if ($('organisation').getProperty('value').length == 0) {
			Site.setPrice(price);
			return;
		}

		Site.setPrice(price-discount);
	},

	updateSpsiForm: function() {
		if ($('gst').getProperty('value') == 1) {
			Site.setPrice($('base-amount').get('html').toFloat());
			return;
		}

		Site.setPrice($('base-amount').get('html').toFloat() / 11 * 10);
	},

	setPrice: function(price) {
		$$('.price').each(function(elem){
			if (elem.get('tag') == 'input') {
				elem.setProperty('value', price.toFixed(2));
			} else {
				elem.set('html', price.toFixed(2));
			}
		});
	},

	modifyText: function(modifier) {
		document.body.className = document.body.className.replace(/textsize\-\d/gi, '');
		var cur_size = Site.fontSize;

		// Now increase or decrease size based on modifier
		cur_size += modifier;

		// Boundary conditions
		if ( cur_size < 0 ) {
			cur_size = 0;
		}

		if ( cur_size > 5 ) {
			cur_size = 5;
		}

		// Add the appropriate class to the body
		document.body.className += ' textsize-' + cur_size;

		Site.enlargeCookie.set('current_size', cur_size);
		Site.fontSize = cur_size;
	},


	formHandler: function(pass, form, submitEvent) {
	},


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


	attachRowRadioClick: function() {
		$$('#course-schedule tr').addEvent('click', function(event) {
				if (event.target.type !== 'radio') {
					$(this).getElements('input[type=radio]').each(function(elem) {
						if (!elem.disabled) {
							elem.checked = true;
						}
					});
				}
			});
	}

};


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