(function($) {

	window.app = {
	
		init: function() {
			this.is_ipad = navigator.userAgent.indexOf('iPad') > -1;
			return true;
		},
		
		spritely: {	
			init: function() {
				// spritely methods...

				$('#clouds').pan({fps: 80, speed: 0.9, dir: 'left', depth: 60});
				$('#hill2').pan({fps: 80, speed: 2.2, dir: 'left', depth: 80});
				$('#hill1').pan({fps: 80, speed: 3.2, dir: 'left', depth: 100});
                $('#distant').pan({fps: 80, speed: 1.2, dir: 'left', depth: 40});
				$('#distant, #hill1, #hill2, #clouds').spRelSpeed(1);
                $('#container, .stage').css({'min-width': '900px'});
                $('#slider').show()
					.slider({
						value: -5,
						min: -60,
						max: 60,
						slide: function() {window.app.spritely.sliderChange($(this).slider('value'));},
						change: function() {window.app.spritely.sliderStop($(this).slider('value'));}
					});
                            
	        },
	        
			sliderChange: function(val) {
				if($("#clouds").spGet('_stopped')){ $('#distant, #hill1, #hill2, #clouds').spStart(); }
	      		if ($('#dragMe').css('display') == 'block') {
	      			if (!$.browser.msie) {$('#dragMe').fadeOut('slow');} else {$('#dragMe').hide();}
	      		}	
	      		var sliderSpeed = val;
	      		// see if the slider position is within a narrow range at the middle (uses absolute value bec of negative values)
				//if(Math.abs(sliderSpeed) < 5){
				if(1==0){
					// it is in the narrow range of the middle so lets stop all animations
					$('#distant, #hill1, #hill2, #clouds').spStop();
				}
				else{
		      		//$('#dev').append('sliderSpeed before: '+sliderSpeed+'<br />');
	                if (sliderSpeed < 0) {
	                    var sliderSpeed = String(sliderSpeed).split('-')[1];
	                    $('#distant, #hill1, #hill2, #clouds').spChangeDir('right');
	                } else {
	                    $('#distant, #hill1, #hill2, #clouds').spChangeDir('left');
	                }
	                $('#distant, #hill1, #hill2, #clouds').spRelSpeed(sliderSpeed);
	                //$('#dev').append('sliderSpeed after: '+sliderSpeed+'<br /><br />');  
				}    		
	        },

	        sliderStop: function(val) {
				var sliderSpeed = val;
				// see if the slider position is within a narrow range at the middle (uses absolute value bec of negative values)
				if(Math.abs(sliderSpeed) < 3){
					// it is in the narrow range of the middle so lets stop all animations
					$('#distant, #hill1, #hill2, #clouds').spStop();
					$("#dev").append('<p>when stopped, fps is: '+$("#clouds").spGet('fps')+'</p><br />');
					$("#dev").append('<p>when stopped, speed is: '+$("#clouds").spGet('speed')+'</p><br /><br />');
				}
				else{
					$("#dev").append('<p>when going, fps is: '+$("#clouds").spGet('fps')+'</p><br />');
					$("#dev").append('<p>when going, speed is: '+$("#clouds").spGet('speed')+'</p><br /><br />');
					window.app.spritely.sliderChange($("#slider").slider("value"));	
				}
	        }	
		},
		
		contactForm: {
			
			init: function() {
				
				if ($('#contactForm').size() != 0) {
					$('#contactFormContainer').click(function () { 
						return false; 
					});
					$('#errorContainer').hide();
					$('.jsSubmit').show();
					$('.nojsSubmit').hide();
					
					$('#contactFormSubmit a').click(function () {
						if (window.app.contactForm.checkForm()) {$('#contactForm').submit();}
						return false;
					});
					
					if ($('#contactForm.errorOccurred').size() != 0) {
						window.app.contactForm.error('Please fill in the entire form.');
					} else if ($('#contactForm.captchaError').size() != 0) {
						window.app.contactForm.error('The verification text did not match the image.');
					}
				}
	       		
	        },
	        
			checkForm: function() {
	      		
	      		if ($('#f_name').val()=='') {
					window.app.contactForm.error('Please enter your name.');
					return false
				} else if ($('#f_email').val()=='') {
					window.app.contactForm.error('Please enter your email address.');
					return false
				} else if ($('#f_message').val()=='') {
					window.app.contactForm.error('Please enter a message.');
					return false
				} 
				
				return true;
	      		
	        },
	        
			error: function(which) {
	      		
	      		var el = $("#errorContainer");
	      		
	      		if (el.css('display') == 'block') {
	      			el.fadeTo('fast', 0.1);
	      			el.fadeTo('fast', 1);
	      			el.fadeTo('fast', 0.1);
	      			el.fadeTo('fast', 1);
	      			el.fadeTo('fast', 0.1);
	      			el.fadeTo('fast', 1);
	      		} else {$("#errorContainer").html(which).slideDown('slow');}
				
	        }
			
		}
	
	};
	
	
	$(document).ready(function() {

		window.app.init();
		window.app.spritely.init();
		window.app.contactForm.init();
	});
	

})(jQuery);

$(document).ready(function() {
	var regexp = /left: ([0-9]*\.?[0-9]*)%/i;
	var setleft = "left: 97.5%";
	
$("#slider a").mousedown(function() {
	$(document).mouseup(function() {
		var full_style = $("#slider a").attr("style");
		var left = regexp.exec(full_style)[1];
		if(left*1 > 97.5 || left*1 == 100) {
			$("#slider a").attr("style",setleft);
		}
		$(document).unbind('mouseup');
	});
});

});
