$(function(){
	function preload(arrayOfImages) {
    $(arrayOfImages).each(function(){
        $('<img/>')[0].src = this;
        // Alternatively you could use:
        // (new Image()).src = this;
    });
	}
	
	// Usage:
	
	preload([
		'images/bg1.jpg',
		'images/bg2.jpg',
		'images/bg3.jpg',
		'images/bg4.jpg',
		'images/bg5.jpg',
		'images/bg6.jpg',
		'images/bg7.jpg',
		'images/bg8.jpg',
		'images/bg9.jpg',
	]);
	
	$('.clickArea .slide').click(function(e){
		e.stopPropagation();
		$('.clickArea').animate({height: '215px'}, 200);
	});
	$(document).click(function(){
		$('.clickArea').animate({height: '45px'}, 500);
	});
	
	$('.clickArea').click(function(){
		$(this).animate({height: '45px'}, 500);
	});
	
	var deviceAgent = navigator.userAgent.toLowerCase();
	var agentID = deviceAgent.match(/(iphone|ipod|ipad)/);
	 
	  if (agentID) {
		  $('.section:first').show();
		  $('#intro').hide();
	   }
	
	
	$('a.fx').click(function(){
		var $bgImg = $('#backstretch img');
		var rndNum = Math.floor(Math.random() * 8 + 1);
		var target = $(this).attr('href');
		var targetClipped = (target.slice(0,6));
		var where = location.hash;
		var whereClipped = (where.slice(0,6));
		
		if ( target == '#gallery') {
			$('.section').fadeOut();
			$(target).delay(1000).fadeIn().animate({"width": "100%"}, 1500);
			$('#flash').show();
		} else {
			$('#flash').hide();
			$('.section').fadeOut();
				if (whereClipped == '#cater' || whereClipped == '#about')  {
					if (whereClipped != targetClipped) {
						//$("html").css({ backgroundImage: "url(images/backgrounds/bg" + rndNum + ".jpg)" });
						$bgImg.attr('src', 'images/backgrounds/bg' + rndNum + '.jpg');
					}
					
				}
				if (target == '#aboutNav') {
					//$("html").css({ backgroundImage: "url(images/about-bg.jpg)" });
					$bgImg.attr('src', 'images/about-bg.jpg');
				}
				if (target == '#caterNav') {
					//$("html").css({ backgroundImage: "url(images/catering-bg.jpg)" });
					$bgImg.attr('src', 'images/catering-bg.jpg');
				}
			
				
			$(target).delay(1000).fadeIn().animate({"width": "100%"}, 1500);
		}
		
	});
	
	
	//QUICK CONTACT
	$('#submit').click(function () {		
		
		//Get the data from all the fields
		var name = $('input[name=name]');
		var phone = $('input[name=phone]');
		var email = $('input[name=email]');
		var city = $('input[name=city]');
		var state = $('input[name=state]');
		var comment = $('textarea[name=comment]');
		

		//Simple validation to make sure user entered something
		//If error found, add hightlight class to the text field
		if (name.val()=='') {
			name.addClass('hightlight');
			return false;
		} else name.removeClass('hightlight');
		
		if (phone.val()=='') {
			phone.addClass('hightlight');
			return false;
		} else phone.removeClass('hightlight');
		
		if (email.val()=='') {
			email.addClass('hightlight');
			return false;
		} else email.removeClass('hightlight');
		
		if (comment.val()=='') {
			comment.addClass('hightlight');
			return false;
		} else comment.removeClass('hightlight');
		
		//organize the data properly
		var data = 'name=' + name.val() + '&phone=' + phone.val() + '&email=' + email.val() + '&city=' + city.val() + '&state=' + state.val() + '&comment=' + encodeURIComponent(comment.val());
		
		//disabled all the text fields
		$('.text').attr('disabled','true');
		
		//show the loading sign
		$('.loading').show();
		
		//start the ajax
		$.ajax({
			//this is the php file that processes the data and send mail
			url: "process.php",	
			
			//GET method is used
			type: "GET",

			//pass the data			
			data: data,		
			
			//Do not cache the page
			cache: false,
			
			//success
			success: function (html) {				
				//if process.php returned 1/true (send mail success)
				if (html==1) {					
					//hide the form
					$('.form').fadeOut('slow');					
					
					//show the success message
					$('.done').fadeIn('slow');
					
				//if process.php returned 0/false (send mail failed)
				} else alert('Sorry, unexpected error. Please try again later.');				
			}		
		});
		
		//cancel the submit button default behaviours
		return false;
	});	
	
	
	
	
});
