$(document).ready(function(){		
	
	//apply form validation
	if (document.getElementById("general-enquiries")){
		$("#general-enquiries").validate({
			rules: {
				txt_name: "required",
				txt_nature_of_enquiry: "required",
				txt_email: {
					required: true,
					email: true
				},
				txt_telephone_number: "required"
			},
			messages: {
				txt_name: "Please enter your name.",
				txt_nature_of_enquiry: "Please enter the nature of your enquiry.",
				txt_email: "Please enter your email address.",
				txt_telephone_number: "Please enter your telephone number."
			}
		});
	}
	
	//apply form validation
	if (document.getElementById("cafe-owners")){
		$("#cafe-owners").validate({
			rules: {
				txt_owner_name: "required",
				txt_company: "required",
				txt_nature_of_business: "required",
				txt_cafe_nature_of_enquiry: "required",
				txt_cafe_email: {
					required: true,
					email: true
				},
				txt_cafe_telephone_number: "required"
			},
			messages: {
				txt_owner_name: "Please enter your name.",
				txt_company: "Please enter your company name.",
				txt_nature_of_business: "Please enter the nature of your business.",
				txt_cafe_nature_of_enquiry: "Please enter the nature of your enquiry.",
				txt_cafe_email: "Please enter your email address.",
				txt_cafe_telephone_number: "Please enter your telephone number."
			}
		});
	}
	
	//apply overlay animations
	if (document.getElementById("category-list")){
		$('.overlay').each(function(){
	
			var hideDelay = 100;
			var hideDelayTimer = null;
		
			// tracker
   			var beingShown = false;
   			var shown = false;
		
			var current_bg_img = null;
			var current_overlay = $(this);
			var current_parent = $(this).parent('li');
			
			//use javascript to hide the overlay div
			current_overlay.css('display', 'none');
			
			if(current_parent.hasClass('nofade') == false){
				
				//get a reference to the current parent background image
				current_bg_img = current_parent.css('background-image');
				
				
				//handle parent / child events
				current_parent.mouseover(function(){
					//remove the parent brackground image
					$(this).css('background-image', 'none');
					
					// reset the timer if we get fired again - avoids double animations
					if (hideDelayTimer) clearTimeout(hideDelayTimer);
				  
					// store the timer so that it can be cleared in the mouseover if required
					hideDelayTimer = setTimeout(function () {
						hideDelayTimer = null;
						//show the overlay div
						current_overlay.fadeIn('fast', function () {
							shown = false;
						});
					}, hideDelay)
					
				});
				current_parent.mouseout(function(){					
					
					// reset the timer if we get fired again - avoids double animations
					if (hideDelayTimer) clearTimeout(hideDelayTimer);
				  
					// store the timer so that it can be cleared in the mouseover if required
					hideDelayTimer = setTimeout(function () {
						hideDelayTimer = null;
						//hide the overlay div
						current_overlay.fadeOut('fast', function () {
							shown = false;
							//restore the parent brackground image
							current_parent.css('background-image', current_bg_img);
						});
					}, hideDelay)
					
				});
				
			}
		});
	}
	
	if (document.getElementById("galleria")){
		jQuery(function($) {
			
			$('.gallery_demo_unstyled').addClass('gallery_demo'); // adds new class name to maintain degradability
			$('.nav').css('display','none'); // hides the nav initially
			
			$('#prev').click(function(){
				$.galleria.prev();
				return false;
			});
			$('#next').click(function(){
				$.galleria.next(); 
				return false;
			});
			$('ul.gallery_demo').galleria({
				history   : true, // activates the history object for bookmarking, back-button etc.
				clickNext : true, // 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','Next image >>');
					$('.nav').css('display','block'); // shows the nav when the image is showing
				},
				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
					)
				}
			});
		});
	}
	
});