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

	start: function(){
		MooTools.lang.setLanguage("en-US");

		// Launch-in-new-window links automagically created
		Site.attachExternalLinks();


		// Safari Suckerfish 'fix'
		if ( navigator.appVersion.toLowerCase().indexOf('safari') != -1 ) {
			Site.applySafariFix();
		}


		// Form validation automagic
		Site.attachFormValidators();


		// Form overtext magic
		Site.attachOverTexts();


		// Submission link automagic
		Site.attachSubmitLinks();


		Site.enlargeCookie = new Hash.Cookie('enlargeCookie', {	'duration': 	60,
																'domain': 		Site.cookieDomain,
																'path': 		'/'
															});
		//Hide hidden fields from form-builder
		$$('#captureform2 input[type=hidden]').each(function(hiddenField){
			if (hiddenField.getParent('.field_contain')) {
				hiddenField.getParent('.field_contain').hide();
			}
		});

		//Fill out hidden fields in event register form.
		$$('input[fieldname=Event_Name]').set('value',$$('h1.headline').get('text'));
		$$('input[fieldname=Event_URL]').set('value',window.location);


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

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

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

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

		if ($('mbox')) {
			Site.setupNewsScroller();
		}
	},


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


	expandItem: function(elem) {
		var eTarget = $(elem.getProperty('expandTarget'));

		elem.fx.cancel();
		elem.fx.start(		{											'opacity':	[1,0]	});
		eTarget.fx.cancel();
		eTarget.fx.start(	{	'top': -200,		'height': 300, 		'opacity': [0,1]	});
	},


	contractItem: function(elem) {
		var eTarget = $(elem.getProperty('expandTarget'));

		elem.fx.cancel();
		elem.fx.start(		{											'opacity':	[0,1]	});
		eTarget.fx.cancel();
		eTarget.fx.start(	{	'top': 0,			'height': 100, 		'opacity': [1,0]	});
	},

	// Notice: Oliver
	/**
	 * I changed that code, it was not what i needed...
	 */
	modifyText: function(modifier) {
		var cur_size = Site.fontSize;

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

		if(cur_size <= 9){
			cur_size = 9;
		}
		if(cur_size > 12){
			cur_size = 12;
		}

		$('contentwrapper').style.fontSize = cur_size + "px";

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



	/**
	 *	Safari hover-tooltip-fix for suckerfish menus
	 *
	 */
	applySafariFix: function() {
		var navElems = $$('#navigation li a');
		navElems.each(function(elem, idx) {
			elem.set('title', '');
		});
	},


	/**
	 *	Pre-emptive text for input fields
	 *
	 */
	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');
					}
				}
			});
		}
	},


	/**
	 *	Submit forms from links functionality
	 *
	 */
	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();
						}
					});

					// Inject a dummy submit button for form functionality to be maintained
					// I like hitting enter to submit
					elem.getParent().adopt(new Element('input', {	'type': 'submit',			'name': 'dummy-submit',
																	'class': 'dummy-submit'
																}));
				}
			});
		}
	},


	/**
	 *	Form validation automagic
	 *
	 */
	attachFormValidators: function() {
		var valForms = $$('form.validate-form');
		if ( valForms.length ) {
			valForms.each(function(elem, idx) {
				new FormValidator.Inline(elem, {
					'onFormValidate': Site.formHandler,
					'errorPrefix': '',
					'useTitles': true,
					'scrollToErrorsOnSubmit': false
				});
			});
		}
	},


	/**
	 *	External links in new window functionality
	 *
	 */
	attachExternalLinks: function() {
		var extLinks = $$('a.external');
		if ( extLinks.length ) {
			extLinks.each(function(elem, idx) {
				elem.setProperty('target', '_blank');
			});
		}
	},


	setupNewsScroller: function() {
		var parent = $('mbox');

		// Create a second ul so we can show two at once
		children = parent.getChildren();
		if (navigator.appVersion.indexOf('MSIE 6') > 0 ) {
			ul1 = children[2]; // because of png fix, it has more children
		} else {
			ul1 = children[0];
		}

		ul1.setStyle('position', 'relative');
		ul2 = ul1.clone();

		parent.adopt(ul2);

		parent.addEvent('mouseover', function() {
			clearTimeout(timer);
		});
		parent.addEvent('mouseout', function() {
			timer = setTimeout('Site.scroll()', 45);
		});

		timer = setTimeout('Site.scroll()', 2000);
	},


	scroll: function() {

		var top = parseInt(ul1.getStyle('top'));

		if (!top) top = 0;

		if (navigator.appVersion.indexOf('MSIE 6') > 0 ) {
			var height = parseInt(ul1.offsetHeight);
		} else {
			var height = parseInt(ul1.getStyle('height'));
		}


		top--;
		ul1.setStyle('top', top+'px');

		if (top < height*-1) {
			// First ul has scrolled out of sight
			top = 0;
			ul1.setStyle('top', '0px');
		}


		ul2.setStyle('top', top+'px');

		timer = setTimeout('Site.scroll()', 45);
	},


	stopScrolling: function() {
		scroll = false;
	},


	attachProjectFunctionality: function() {

		// Homepage expandy areas
		var expanders = $$('#homepage .expander');
		if ( expanders.length ) {
			expanders.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);
						}
					});
				}

				// Ensure we have an expandTarget and it's in the DOM
				if ( elem.getProperty('expandTarget') && $(elem.getProperty('expandTarget')) ) {
					var eTarget = $(elem.getProperty('expandTarget'));

					eTarget.fx = new Fx.Morph(eTarget, {	'duration': 250,
															'transition': Fx.Transitions.Quad.easeOut
															});
					elem.fx = new Fx.Morph(elem, {			'duration': 250,
															'transition': Fx.Transitions.Quad.easeOut
															});

					//
					elem.addEvent('mouseenter', function() {
						Site.expandItem(elem);
					});
					$(elem.getProperty('expandTarget')).addEvent('mouseleave', function() {
						Site.contractItem(elem);
					});
				}
			});
		}

	}

};


function emptyfield (field) {
	document.getElementById(field).value='';
}
// Do stuff on load
window.addEvent('load', Site.start);

