var Site = {
	enlargeCookie: null,
	cookieDomain: 'mcr.localhost',		// TODO: Change before golive
	fontSize: 0,
	map: null,
	
	start: function() {
//		MooTools.lang.setLanguage("en-US");
		
		$(document.body).addClass('js-enabled');

		$(document.body).addClass('javascript-enabled');

		// 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();
		
		Site.attachAccordion();
		
		
		// All other actions
		Site.attachPageActions();
		
		Shadowbox.init();
		
	},
	
	
	attachPageActions : function() {
		
		if ($('page-tools')) {
			Site.enlargeCookie = new Hash.Cookie('enlargeCookie', {	'duration': 	60,
																	'domain': 		Site.cookieDomain,
																	'path': 		'/'
																});
			
																
			// Page sizing functions
			if ( Site.enlargeCookie.get('current_size') ) {
				if (Site.enlargeCookie.get('current_size') == 1) {
					Site.toggleText();
				}
			}
			
			if ( $('btn-font') ) {
				$('btn-font').addEvent('click', function(event) {
					event.stop();
					Site.toggleText();
				});
			}
			
			// Print
			if ( $('btn-print') ) {
				$('btn-print').addEvent('click', function(event) {
					event.stop();
					window.print();
				});
			}
		}
			
		// Level 2 meet the experts
		if (typeof(InfiniSlider) == 'function' && typeof(InfiniSlider.prototype) == "object") {
			if ($('features')) {	
				new InfiniSlider({
					container: $('features'), 					
					indexItems: $$('#feature-index-inner > span'),
		
					autoSlideTime: 5000,
					duration: 800,
					
					animateOn: 'left',
					showItems: 1
				});
			}
			if ($('recent-deal-features')) {	
				new InfiniSlider({
					container: $('recent-deal-features'), 					
					indexItems: $$('#recent-deals-index > span'),
		
					autoSlideTime: 3000,
					duration: 800,
					
					animateOn: 'top',
					showItems: 1
				});
			}
			if ($('experts-feature')) {		
				new InfiniSlider({
					container: $('experts-holder'), 					
					nextButton: 'experts-control-next',
					previousButton: 'experts-control-back',

					autoSlideTime: 3000,
					duration: 600,
					
					animateOn: 'left',
					showItems: 1
				});
			}
			if ($('testimonial-banner')) {
				new InfiniSlider({
					container: $('testimonial-banner-listing'), 	
					nextButton: 'testimonial-banner-next',
					previousButton: 'testimonial-banner-previous',
					duration: 600,
					
					animateOn: 'left',
					showItems: 1
				});
			}
		}
		
		if ($('calendarInner')) {
			if (typeof(mooCalendar2) == 'function' && typeof(mooCalendar2.prototype) == "object" && 
				typeof(mooCalendar2.Event) == 'function' && typeof(mooCalendar2.Event.prototype) == "object") {
				Site.setupCalendar();
			}
		}
		
		new mooCalendar2.Input();	
	},

	toggleText: function() {
		document.body.className = document.body.className.replace(/textsize\-\d/gi, '');
		var cur_size = Site.fontSize;
		
		// Now increase or decrease size based on modifier
		if (cur_size == 0) {
			cur_size = 1;
			$('btn-font').addClass('active');
		} else {
			$('btn-font').removeClass('active');
			cur_size = 0;
		}
		
		// Boundary conditions
		if ( cur_size < 0 ) {
			cur_size = 0;
		}
		
		if ( cur_size > 1 ) {
			cur_size = 1;
		}

		// 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) {
		// Do anything necessary here
	},
	
	
	/**
	 *	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() {
		// We want to clear alt texts from values if the form is submitted
		var forms = $$('form');
		forms.each(function(elem, idx) { 
			elem.addEvent('submit', function() {
				if ( elem.getProperty('valPassed') == true ) {
					formElems = elem.getElements('input, textarea');
					
					formElems.each(function(input, idx) {
						if ( input.getProperty('alt') == input.value ) {
							input.value = '';
						}
					});
				}
			});
		});
		
		
		var overElems = $$('input.overtext, textarea.overtext');
		if ( overElems.length ) {
			overElems.each(function(elem, idx) {
				
				if ( elem.getProperty('type') ) {
					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 = '';
							}
							
							this.removeClass('overtext');
						}
					});
					
					// 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');
							}
							
							this.addClass('overtext');
						}
					});
					
					// Default state
					if ( elem.value == '') {
						if ( elem.getProperty('overType') == 'password' ) {
							elem = Site.cloneAndChangeInputType(elem, 'text');
						}
						
						elem.value = elem.getProperty('alt');
					} else {
						if ( elem.value != elem.getProperty('alt') ) {
							elem.removeClass('overtext');
						}
					}
				}
			});
		}
	},
	
	
	/**
	 *	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 Form.Validator.Inline(elem, {
					'onFormValidate': Site.formHandler,
					'errorPrefix': '',
					'useTitles': true
				});
			});
		}
		
		var valForms = $$('form.validate-form-custom');
		if ( valForms.length ) {
			valForms.each(function(elem, idx) {
				elem.setProperty('valPassed', false);
				
				new Form.Validator(elem, {
					'onFormValidate': Site.formHandler,
					'errorPrefix': '',
					'useTitles': true, 
					'onFormValidate': function(passed, form, e) {
						elem.setProperty('valPassed', passed);
					}
				});
			});
		}
		
		var cmsForms = $$('div.captureform2');
		if ( cmsForms.length ) {
			cmsForms.each(function(elem, idx) {
				var form = elem.getParent('form');
				
				// Need to move all the rel attributes to classes
				var children = form.getElements('input, select, textarea');
				children.each(function(child, cidx) {
					child.className += ' ' + child.getAttribute('rel');
				});
				
				// Remove the current submit event
				form.onsubmit = null
				
				// Add the validator
				new Form.Validator.Inline(form, {
					'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');
			});
		}
	},
	
	setupCalendar: function() {
		var calendar = new mooCalendar2.Event($('calendarInner'), 	{	
			'calType': 'calTypeInline',
			'dayLength': 'dayLengthShort',
			
			'startDate': new Date(),
			
			'viewFrame': $('calViewHolder'),
			'filterFrame': $('calFilterOptionHolder'),
			'weekFrame': $('calWeekHolder'),
			'eventFrame': $('event-holder'),
			'spinnerElement': $('calendarInner'),

			'requestUrl': '/events/?json_events=1',
			
			'viewTitle': 'Events listed by:',
			'weekTitleText': 'Week ',
			'eventLinkText': 'Register for this event',
			'onDataFailure': function(msg){ alert(msg); },
			'onRender': function(){ Cufon.refresh(); }
		});
	},
	
	attachAccordion: function() {
		var myAccordion = new Fx.Accordion('.toggler', 'div.element', {
			alwaysHide: true,
			display: -1,
			opacity: false,

			onActive: function(toggler, element){
				if ( toggler ) {
					toggler.addClass('toggler_active');
				}
			},
			onBackground: function(toggler, element){
				if ( toggler ) {
					toggler.removeClass('toggler_active');
				}
			}
		});

	}

	
};


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