$(function(){
	
	//Fade out and back in the home panels on hover
	$(".panel").hover(function() {
		$(".panel").stop().animate({ "opacity" : .7 }, { "duration" : "normal" });
		$(this).stop().animate({ "opacity" : 1 });
	}, function() {
		$(".panel").stop().animate({ "opacity" : 1 });
	}).click(function(){
		window.location=$(this).find("a").attr("href"); return false;
	});

	//Add cycle script to testimonials on homepage
	if ($("#homeTestimonials li").length > 1) {
		$('#homeTestimonials ul').cycle({
			fx: 'fade',
			speed: 1500,
			timeout: 10000,
			pause: 1
		});
	};

	//Add cycle script to banner images on homepage
	if ($("#homeBanner").length > 0) {
		$('#homeBanner').cycle({
			fx: 'fade',
			speed: 1000,
			timeout: 7000,
			pause: 0
		});
	};
	
	//Add scrollTo for the TOC
	if($("#content").length > 0) {
		jQuery.easing.def = "easeOutExpo";
		$('#toc').localScroll( 800, {easing:'elasout'} );
		$('#tocTeam').localScroll( 800, {easing:'elasout'} );
		$('.btt').localScroll();
	};


	//Use AJAX to submit enquiry form
	if($("#sideBar").length > 0) {
	    $('#sideBar label').removeClass("error");
	    $("#sideBar #enquirySubmit").click(function() {

	    	$('#sideBar label').removeClass("error");
			
			var errorCount = 0;
			var sName = $("input#enquiryName").val();
			if (sName == "") {
				$("label#enquiryNameLabel").addClass("error");
				if(errorCount == 0) { 
					$("input#enquiryName").focus();
				}
				errorCount ++;
			}
			
			var sEmail = $("input#enquiryEmail").val();
			if (sEmail == "") {
				$("label#enquiryEmailLabel").addClass("error");
				if(errorCount == 0) { 
					$("input#enquiryEmail").focus();
				}
				errorCount ++;
			}
			
			var sPhone = $("input#enquiryPhone").val();
			if (sPhone == "") {
				$("label#enquiryPhoneLabel").addClass("error");
				if(errorCount == 0) { 
					$("input#enquiryPhone").focus();
				}
				errorCount ++;
			}

			var sMessage = $("textarea#enquiryMessage").val();
			if (sMessage == "") {
				$("label#enquiryMessageLabel").addClass("error");
				if(errorCount == 0) { 
					$("input#enquiryMessage").focus();
				}
				errorCount ++;
			}
			
			if (errorCount > 0) {
				return false;
			}
			
			
			
			var actionURL = $("#sideBar form").attr("action")
			//alert (actionURL);return false;

			var dataString = 'name='+ sName + '&email=' + sEmail + '&phone=' + sPhone + '&message=' + sMessage;
			//alert (dataString);return false;

			$('#enquiryForm').html("<div id='enquiryLoading'>Sending Message...</div>")
				.hide()
				.fadeIn(1000);
					
			$.post(actionURL,{
				message: sMessage,
				name: sName,
				phone: sPhone,
				email: sEmail
			}, function(xml) {
				if($("status",xml).text() == "Fail") { 
					alert('Sorry, unexpected error. Please try again later.'); 
				}
				else
				{
					$('#enquiryForm').html("<div id='enquiryResponse'></div>");
					$('#enquiryResponse').html("<h4>Thank you, " + $("name",xml).text() + "!</h4>")
					.append("<p>Your enquiry has been successfully sent to us and we will be in contact with you shortly. If you would like to phone our offices in the meantime, our full contact details can be found <a href='contact'>here</a>.<br/><br/><em>McGovern Surveyors Team</em></p>")
					.hide()
					.fadeIn(1000);
					//alert("You sent name = "+$("name",xml).text());
				}
			});

			return false;

		});

  	};
 
 


});