function checkEmail(email)
	{	
		var pattern = /^([a-zA-Z0-9_\.\-])+\@(([a-zA-Z0-9\-])+\.)+([a-zA-Z0-9]{2,4})+$/;
		var emailVal = $("#" + email).val();
		return pattern.test(emailVal);
	}
	
$(document).ready(function(){
	$("#ebullSignup #sign-up").click(function() {	

			
			// First, disable the form from submitting
			$('#ebullSignup').submit(function() { return false; });
			
			// Grab form action
			formAction = $(this).parents('form').attr("action");
			
			// Hack together id for email field
			emailId = formAction.replace("http://ebull.dusteddesign.com/t/r/s/", "");
			emailId = emailId.replace("/", "");
			emailId = emailId + "-" + emailId;

			emailChk = true;
			
			// Validate email address with regex
			if (!checkEmail(emailId)) 
			{
				
				$("#confirmation").slideDown("slow");  // Shows "Thanks for subscribing" div
				$("#confirmation").addClass("panel");
				$("#confirmation").html("Please enter a valid email address");
				return false;
			}
			
			// Serialize form values to be submitted with POST
			var str = $(this).parents('form').serialize();
	
			// Add form action to end of serialized data
			final = str + "&action=" + formAction;
			
			// Submit the form via ajax
			$.ajax({
				url: "http://www.harrisoncatering.co.uk/proxy.php",
				type: "POST",
				data: final,
				success: function(html){
					$("#confirmation").slideDown("slow");  // Shows "Thanks for subscribing" div
					$("#confirmation").addClass("panel");
					$("#confirmation").html("Thanks for subscribing");
				}
			});
			return false;
	});
	
	$(".submit_container input:submit").each(function (i, el) {
	  $(this).hide().after( 
	    $("<a>", { 
	      href: "#", 
	      html: $(el).val(), 
	      className: "cta submit", 
	      click: function (e) {
	        e.preventDefault();
	        $(this).closest('form').submit();
	      }
	    })
	  );
	});
	
	$('#slice .cta p').wrapInner('<a href="#" class="text" />');
	$('#slice .cta p a').after('<a href="#" class="more">more</a>');
	$('#slice .sliderPanel').prepend('<a href="#" class="close">Close</a>').hide();
	$('#slice .cta p a:not(.expanded)').live('click',function(e){
		e.preventDefault();
		$('#slice .sliderPanel:not(.revealed)').addClass('revealed').slideDown();
		$('#slice .cta p a').addClass('expanded');
	});
	$('#slice .sliderPanel a.close,#slice .cta p a.expanded').live('click',function(e){
		e.preventDefault();
		$('#slice .sliderPanel.revealed').removeClass('revealed').slideUp();
		$('#slice .cta p a').removeClass('expanded');
	});
	
	$('#slice .formrow input:text, .formrow input[type=search], .formrow textarea').each(function(){
		$(this).siblings('label').find('span, sup').remove();
    	var theVal = $(this).siblings('label').html();
    	if (theVal.charAt(0) == " ") {
    		theVal = theVal.substring(1);
    	}
    	$(this)
    	.css('marginBottom','2px')
    	.siblings('label')
    	.html(theVal)
    	.hide();
	    $(this).siblings('br').remove();
    	
		if ($(this).val().length == 0){
	    	$(this)
	    	.val(function () {
		      return $(this).siblings('label').html();
		    })
		}
	    $(this)
	    .focus(function () {
	        if ($(this).val() == $(this).siblings('label').html()) {
	          $(this).val('');
	        }
	    }).blur(function () {
	        if (!$(this).val()) {
	          $(this).val($(this).siblings('label').html());
	        }
	    });
	});
	
	
	$('#slice .slideshow').append('<p id="counter"></p>');
	$('#slice .slideshow').append('<p id="playagain" class="cta"><a href="#">Play Again</a></p>');
	$('#playagain').hide();
	
	/* Amount added 10/01/2012 - Various lengths for different slideshows */
	var amount = $('#slice .slideshow ul li').length;
	
	function slideCounter(curr,next,opts) {
	    var counter = '&lt;'+(opts.currSlide +1) + '/' + opts.slideCount+'&gt;';
	    $('#counter').html(counter);
	}
	function slideEnd(opts) {
		$('#playagain').fadeIn().find('a').click(function(e){
			e.preventDefault();
			$('#slice .slideshow ul').cycle({
				autostop			: true,
				autostopCount		: amount+1,
				after				: slideCounter,
				end					: slideEnd
			});
			$(this).parent().fadeOut();
		});
	}
	
	$('#slice .slideshow ul').cycle({
		autostop			: true,
		autostopCount		: amount+1,
		after				: slideCounter,
		end					: slideEnd
	});
	
});
printLink = function() {
	if (document.getElementById("printBut")) {
		var printBut = document.getElementById("printBut");
		printBut.onclick=function() {
			if (window.print) {
				window.print();
			} else {
				alert("Unfortunately your browser doesn't support printing via Javascript.\nTo print this page please select \"File > Print...\" in your browser's menu.");
			}
			return false;
		}
	}
}
addBookmark = function() {
	if (document.getElementById("bookmarkBut")) {
		var bookBut = document.getElementById("bookmarkBut");
		bookBut.onclick=function() {
			if (document.all) {
				bookmarkurl=document.location;
				bookmarktitle="Harrison Catering";
				window.external.AddFavorite(bookmarkurl,bookmarktitle);
			} else {
				alert("Unfortunately your browser doesn't support adding bookmarks via this link.\n\nTo bookmark this page please use your browser's menu.");
			}
			return false;
		}
	}
}

window.onload = function() {
	printLink();addBookmark();
}
