$(function() {

	// if (navigator.userAgent.indexOf('Mobile') != -1) {
	// 	document.location = "http://www.cocotero-restaurant.com/touch/";
	// }

	// if (screen.width <= 699) {
	// 	document.location = "http://www.cocotero-restaurant.com/touch/";
	// }

	// if ((navigator.userAgent.match(/iPhone/i)) || (navigator.userAgent.match(/iPod/i)) || (navigator.userAgent.match(/blackberry/gi)) || (navigator.userAgent.match(/android/gi))) {
	//    location.replace("http://www.cocotero-restaurant.com/touch/");
	// }

	$('#cn-scroll').jScrollPane({ reinitialiseOnImageLoad: true });
	if (!$.browser.msie) { $('#gallery-scroll').jScrollPane({ reinitialiseOnImageLoad: true }); }

	$('#lb-mailing').click( function(event) {
		event.preventDefault();
		if ($('#mailing-form').is(':hidden')) {
			$('#mailing-form').slideDown('slow');
		} else {
			$('#mailing-form').slideUp('slow');
		}
	});

	$('#m-hide').click( function(event) {
		event.preventDefault();
		$('#mailing-form').slideUp('slow');
	});

	moveMailing();
	$(window).resize(function(){moveMailing();});

	// mailing form code
	$('.mailing-form .error').hide();
	$('#mf-note').hide();

	$(".mailing-form .submitbutton").click(function() {
		// validate on clicking submit button
		// first hide error messages
		$('.mailing-form .error').hide();

		// check if the value for name is not empty or not equal to default value
		var name = $(".mailing-form input#name").val();
		if (name == ""  || name =="Name") {
			$(".mailing-form label#name_error").fadeIn('slow');
			$(".mailing-form input#name").focus();
			return false;
		}

		// check if the value for email is not empty or not equal to default value
		var email = $(".mailing-form input#email").val();
		var emailReg = /^([\w-\.]+@([\w-]+\.)+[\w-]{2,4})?$/;

		if (email == "" || email =="Email") {
			$(".mailing-form label#email_error").fadeIn('slow');
			$(".mailing-form input#email").focus();
			return false;
		}
		// test if the value for email is a valid email address
		else if(!emailReg.test(email)) {
			$(".mailing-form label#email_error2").fadeIn('slow');
			$(".mailing-form input#email").focus();
    		return false;
		}

		var str = $("form.mailing-form").serialize();

		$.ajax({
			type: "POST",
			url: "/mailinglist.php",
			data: str,

			success: function(msg){
				$("#mf-note").ajaxComplete(function(event, request, settings) {
					$("#mf-note").show();
					if(msg == 'OK'){
						result = '<span class="notification_ok">Your message has been sent.<br />We will be in touch soon.</span>';
						$("#mf-fields").hide();
						$('#mailing-form').delay(1000).slideUp('slow');
					} 
					else {
						result = '<span class="notification_error">Error submitting form.</span>';//MSG: ' + msg + '</span>';
						$("#mf-fields").hide();
						$('#mailing-form').delay(2000).slideUp('slow');
					}
					$(this).html(result);
				});
			}
		});
		return false;

	});

});

function moveMailing() {
	var x = $('#lb-mailing').offset().left;
	var y = $(window).height() - $('#logo-bar').offset().top;
	$('#mailing-form').css('left', x + 'px');	
	$('#mailing-form').css('bottom', y + 'px');	
}
