window.addEvent('domready', function() {
									 
	fitContent();
	
	if($('splash')) {
		var fader = new Fx.Tween($('splash'), {
			'property': 'opacity',
			'duration': 5000
		});
		$('splash').setStyles({
			display: 'block',
			visibility: 'visible'
		});
		fader.set(0);
		fader.start(1).chain(function() {
			window.location.href = '/nesterova/';  
		});
	}
									 
	window.addEvent('resize', fitContent);
									 
	if($('carousel_outer')) {
		new SlideItMoo({
			overallContainer: 'carousel_outer',
			elementScrolled: 'carousel_inner',
			thumbsContainer: 'carousel_items',		
			itemsVisible:5,
			duration:175,
			itemsSelector: '.carousel_element',
			transition: Fx.Transitions.Sine.easeIn,
			itemWidth: 174,
			showControls:1
		});
	}
	
	if($('scrollWrapper')) makeScrollbar($('scrollFrame'),$('scrollBar'),$('handle'),false,false);
		
	if($('mailer')) {
		var email = $('mailer').getElement('input');
		new inputHint(email);
	}
 
 	/* Enquiry Form */
	
	contactFormSent = false; 
	
	if($('contact')) {
		
		$('contact').addEvent('submit',function(el) {
													
			el.stop();
						
			var dataObject  = {};
			
			// Don't deliver the contact e-mails twice if the user needs to retry submitting their payment info
			if(contactFormSent) {
				return;
			}
			
			var contactForm = $('contact');
							  
			dataObject[contactForm.id] = {};
			
			var labels = contactForm.getElements('label');
			
			labels.each(function(label,i) {
								 
				var fieldLabel   = '';
				var fieldValue   = '';
				var fieldHeading = '';
				
				fieldLabel = trim(label.innerHTML);
								 
				var forAttr = label.getAttribute('for');
							
				if(forAttr) {
					// if the for attribute exists, this has a corresponding input, get the value				
					var input = contactForm.getElement('[name^=' + forAttr + ']');
					
					if(input.getProperty('type') == 'checkbox') {
						// concatenate the checkbox values
						var checkboxes = contactForm.getElements('[name^=' + forAttr + ']');
						var concatValues = new Array();
						checkboxes.each(function(checkbox) {
							if(checkbox.checked) {
								concatValues.push(checkbox.value);
							}
						});
						if(concatValues.length > 0) {
							fieldValue = concatValues.join(', ');
						}
					}
					else {
						fieldValue = input.value;
					}
				}
				else {
					forAttr      = 'heading' + i;
					fieldHeading = label.hasClass('title') ? 'title' : 'subtitle';
				}
				
				dataObject[contactForm.id][forAttr] = {
					title: fieldLabel,
					value: fieldValue,
					heading: fieldHeading
				};
			});
			
			new Request.JSON({
				url: '/mailer/send-email.php',
				data: dataObject,
				onRequest: function() {
					$('submitButton').disabled = 1;
					$('status').innerHTML = 'Sending, please wait...'
				},
				onSuccess: function(jsonObj,responseText) {
					if(!jsonObj.errors) {
						
						contactFormSent = true;
						
						$('status').innerHTML = 'Your e-mail has been sent'
					}
					else {
						$('status').innerHTML = 'Error: ';
						
						jsonObj['errors'].each(function(error) {
							$('status').appendText(error + ' ');
						});
						
						$('submitButton').disabled = 0;
					}
				}
			}).send();
		});
	}
});

function fitContent() {
	if(!$('container')) return;
	if(document.getSize().y < 670) {
		if(!$('container').hasClass('smallWindow')) {
			$('container').removeClass('regularWindow');
			$('container').addClass('smallWindow');	
		}
	}
	else if($('container').hasClass('smallWindow')) {
		$('container').removeClass('smallWindow');
		$('container').addClass('regularWindow');
	}
}

function makeScrollbar(content,scrollbar,handle,horizontal,ignoreMouse){
	
	if(content.clientHeight == content.scrollHeight) {
		/*content.setStyle('height','auto');
		content.setStyle('width','auto');*/
		if(scrollbar) scrollbar.destroy();
		return;
	}
	
	scrollbar.set('opacity',0);
	
	content.getParent().addEvents({
		'mouseenter': function() {
			scrollbar.fade('in');		
		},
		'mouseleave': function() {
			scrollbar.fade('out');	
		}
	});
	
	var steps = (horizontal?(content.getScrollSize().x - content.getSize().x):(content.getScrollSize().y - content.getSize().y))
	var slider = new Slider(scrollbar, handle, {	
		steps: steps,
		mode: (horizontal?'horizontal':'vertical'),
		onChange: function(step){
			// Scrolls the content element in x or y direction.
			var x = (horizontal?step:0);
			var y = (horizontal?0:step);
			content.scrollTo(x,y);
		}
	}).set(0);
	if( !(ignoreMouse) ){
		// Scroll the content element when the mousewheel is used within the 
		// content or the scrollbar element.
		$$(content, scrollbar).addEvent('mousewheel', function(e){	
			e = new Event(e).stop();
			var step = slider.step - e.wheel * 8;	
			slider.set(step);					
		});
	}
	// Stops the handle dragging process when the mouse leaves the document body.
	$(document.body).addEvent('mouseleave',function(){slider.drag.stop()});
}

var inputHint = new Class({
						  
	initialize: function(el) {	
		
		this.el           = el;
		this.defaultValue = el.value;
		
		this.el.setStyle('color','#666');
	
		this.el.addEvent('focus',function() {
									 
			if(this.el.value !== this.defaultValue) return;
			
			this.el.value = '';
			this.el.setStyle('color','#333');
			
		}.bind(this));
		
		this.el.addEvent('blur',function() {
									
			if(this.el.value == '') {
				
				this.el.setStyle('color','#666');
				this.el.value = this.defaultValue;	
				
			}
		}.bind(this));
	},
	setDefault: function() {
		this.el.setStyle('color','#666');
		this.el.value = this.defaultValue;
	}
});

function trim(str, chars) {
	return ltrim(rtrim(str, chars), chars);
}
 
function ltrim(str, chars) {
	chars = chars || "\\s";
	return str.replace(new RegExp("^[" + chars + "]+", "g"), "");
}
 
function rtrim(str, chars) {
	chars = chars || "\\s";
	return str.replace(new RegExp("[" + chars + "]+$", "g"), "");
}