var Site = {
	enlargeCookie: null,
	cookieDomain: 'www.movieconvention.com.au',
	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();
		}


		// IE6 Suckerfish fix
		if ( navigator.appVersion.toLowerCase().indexOf('msie 6') != -1 ) {
			Site.applySuckerfishFix();
		}


		// Form validation automagic
		Site.attachFormValidators();


		// Form overtext magic
		Site.attachOverTexts();


		// Submission link automagic
		Site.attachSubmitLinks();


		// Any additional project-specific functionality
		Site.attachProjectFunctionality();


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

		Site.attachAccordions();

		// 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 ($('gallery')) {
			initImageZoom();
		}
	},


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


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


	/**
	 *	Suckerfish fix for IE6
	 *
	 */
	applySuckerfishFix: function() {
		var sfElems = $$('#navigation li.suckerfish_level1');

		// Fix the top level elements first
		sfElems.each(function(elem, idx) {
			elem.addEvent('mouseover', function() {
				this.addClass('sfhover');
			})
			elem.addEvent('mouseout', function() {
				this.removeClass('sfhover');
			})
		});

		var sfLists = $$('#navigation ul.suckerfish_level1');

		// Now fix top elements staying active when in a child list
		sfLists.each(function(elem, idx) {
			elem.addEvent('mouseenter', function() {
				this.getParent().addClass('childhover');
			})
			elem.addEvent('mouseleave', function() {
				this.getParent().removeClass('childhover');
			})
		});
	},


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


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


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

	},

	attachAccordions: function() {
		// All the accordion-content divs are initially hidden using CSS (display: none).
		// They must be hidden using the slider tool (which applies it to a different element),
		// then unhidden from our CSS. If that makes sense.
		$$('.accordion-content').each(function(elem){
			new Fx.Slide(elem).hide();
			elem.show();
			// elem's parent is hidden, so elem is hidden too
		});

		// Add click handlers for titles
		$$('.accordion-toggler').each(function(elem){
			elem.addEvent('click', Site.toggleAccordionContent);
		});

		// If we've loaded the page with #item-1234 in the URL, open that accordion
		if (document.location.toString().indexOf('#') > 0) {
			var hash_value = document.location.toString().split('#')[1];
			if (hash_value.contains('item-')) {
				var node_id = hash_value.replace('item-', '');

				if ($('accordion-content-'+node_id)) {
					var slider = new Fx.Slide('accordion-content-'+node_id);
					slider.show();
					$('accordion-title-'+node_id).addClass('accordion-open');
				}
			}
		}
	},

	toggleAccordionContent: function(event) {
		// Get the node ID from the href and create the slider element
		var node_id = this.getProperty('href').replace('#item-', '');
		var slider = new Fx.Slide('accordion-content-'+node_id);

		// We can't use .open properly until after we toggle
		var anchor = this;
		slider.toggle().chain(function(){
			if(this.open) {
				anchor.removeClass('accordion-closed').addClass('accordion-open');
			} else {
				anchor.removeClass('accordion-open').addClass('accordion-closed');
			}
		});
	}
};


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