jQuery(function($) {

	$('.gallery_unstyled').addClass('gallery'); // adds new class name to maintain degradability
	
	$('ul.gallery').galleria({
		history   : false, // activates the history object for bookmarking, back-button etc.
		clickNext : false, // helper for making the image clickable
		insert    : '#main_image', // the containing selector for our main image
		onImage   : function(image,caption,thumb) { // let's add some image effects for demonstration purposes
	
			// fade in the image & caption
			//if(! ($.browser.mozilla && navigator.appVersion.indexOf("Win")!=-1) ) { // FF/Win fades large images terribly slow
				image.css('display','none').fadeIn(1000);
			//}
			caption.css('display','none').fadeIn(1000);
	
			// fetch the thumbnail container
			var _li = thumb.parents('li');
	
			// fade out inactive thumbnail
			_li.siblings().children('img.selected').fadeTo(500,0.3);
	
			// fade in active thumbnail
			thumb.fadeTo('fast',1).addClass('selected');
	
			// add a title for the clickable image
			image.attr('title',thumb.title);
		
		},
		onThumb : function(thumb) { // thumbnail effects goes here
	
			// fetch the thumbnail container
			var _li = thumb.parents('li');
	
			// if thumbnail is active, fade all the way.
			var _fadeTo = _li.is('.active') ? '1' : '0.3';
	
			// fade in the thumbnail when finnished loading
			thumb.css({display:'none',opacity:_fadeTo}).fadeIn(1500);
	
			// hover effects
			thumb.hover(
				function() { thumb.fadeTo('fast',1); },
				function() { _li.not('.active').children('img').fadeTo('fast',0.3); } // don't fade out if the parent is active
			)
		}
	});
	
	var name = $("#name"),
	senderEmail = $("#sender"),
	subject = $("#subject"),
	messagebody = $("#body"),
	requestTour = $("#tour"),
	allFields = $([]).add(name).add(senderEmail).add(subject).add(messagebody).add(requestTour),
	tips = $("#validateTips");


	function updateTips(t) {
		tips.text(t).effect("highlight",{},1500);
	}

	function checkLength(o,n,min,max) {
		return checkMinLength(o,n,min,max) && checkMaxLength(o,n,min,max);
	}

	function checkMinLength(o,n,min,max) {

				if ( o.val().length < min ) {
					o.addClass('ui-state-error');
					updateTips("Please enter a longer " + n + ".");
					return false;
				} else {
					return true;
				}

	}

	function checkMaxLength(o,n,min,max) {

						if ( o.val().length > max ) {
							o.addClass('ui-state-error');
							updateTips(n + " should be shorter than "+max+" characters");
							return false;
						} else {
							return true;
						}

	}

	function checkRegexp(o,regexp,n) {

		if ( !( regexp.test( o.val() ) ) ) {
			o.addClass('ui-state-error');
			updateTips(n);
			return false;
		} else {
			return true;
		}

	}

	function sendContactForm(dataString){


	   $.ajax({
		  type: "GET",
		  url: "http://redlandsapp.appspot.com/sendmail",
		  data: dataString,
		  dataType: "jsonp",
		  success: function(data) {
			$("#messageDialog").dialog('open');
			$("#dialog").dialog('close');
		  }
		});

	}



	// Dialog
	$("#dialog").dialog({
		bgiframe: true,
		autoOpen: false,
		height: 435,
		modal: true,
		buttons: {
			'Send': function() {
				var bValid = true;
				allFields.removeClass('ui-state-error');

				bValid = bValid && checkLength(name,"Name",2,30);
				bValid = bValid && checkLength(senderEmail,"Email",6,50);
				bValid = bValid && checkLength(subject,"Subject",2,50);

				bValid = bValid && checkRegexp(name,/^(([a-z\xC0-\xFF])+(\s)?)+$/i,"Name may consist of letters and spaces.");
				// From jquery.validate.js (by joern), contributed by Scott Gonzalez: http://projects.scottsplayground.com/email_address_validation/
				bValid = bValid && checkRegexp(senderEmail,/^((([a-z]|\d|[!#\$%&'\*\+\-\/=\?\^_`{\|}~]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])+(\.([a-z]|\d|[!#\$%&'\*\+\-\/=\?\^_`{\|}~]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])+)*)|((\x22)((((\x20|\x09)*(\x0d\x0a))?(\x20|\x09)+)?(([\x01-\x08\x0b\x0c\x0e-\x1f\x7f]|\x21|[\x23-\x5b]|[\x5d-\x7e]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])|(\\([\x01-\x09\x0b\x0c\x0d-\x7f]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF]))))*(((\x20|\x09)*(\x0d\x0a))?(\x20|\x09)+)?(\x22)))@((([a-z]|\d|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])|(([a-z]|\d|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])([a-z]|\d|-|\.|_|~|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])*([a-z]|\d|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])))\.)+(([a-z]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])|(([a-z]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])([a-z]|\d|-|\.|_|~|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])*([a-z]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])))\.?$/i,"Enter a valid email address.");
				bValid = bValid && checkRegexp(subject,/^(([a-z])+(\s)?)+$/i,"Subject may consist of letters and spaces");

				if (bValid) {
					sendContactForm({
										name: name.val(),
										sender: senderEmail.val(),
										subject: subject.val(),
										tour: requestTour[0].checked,
										body: messagebody.val()
									});

					//$(this).dialog('close');
				}
			},
			Cancel: function() {
				$(this).dialog('close');
			}
			},
			close: function() {
				allFields.val('').removeClass('ui-state-error');
			}
	});

	// messageDialog
	$("#messageDialog").dialog({
		bgiframe: true,
		autoOpen: false,
		height: 235,
		modal: true,
		buttons: {
			Close: function() {
				$(this).dialog('close');
			}
		}

	});

	// Dialog Link
	$('#dialog_link').click(function(){
		$('#dialog').dialog('open');
		return false;
	});



});

