// JavaScript Document
// validation add phone method
$.validator.addMethod("phone", function(str) {
	if ((str.length==0) || (str==null) || (str=='')){
		return true;
	}
	var stripped = str.replace(/[\(\)\.\-\ ]/g, '');
	stripped = stripped.replace(/[\+]/g, '00');
	if (!/^-?\d+$/.test(stripped)) {
		return false;
	}
	if (isNaN(parseInt(stripped))) {
		return false;
	}else{
		return true;
	}
}, "Enter a valid phone No.");

$.metadata.setType("attr", "validate");

var step
var rName;
var rEmail;
var rPhone;
var rGuests;
var rBookingDate;
var rBookingHour;
var rBookingMinute;
var rComments;

function getReservation(url, step) {
	
	$("#reservation").animate( { marginTop: "0" }, { queue:false, duration:500 } )
					.animate( { height: "136px" }, 500)
					.animate( { width: "973px" },500);
					
	$('a#reservation-btn').unbind('click');
	$('a#reservation-btn').click(function() {
		closeReservation();
		return false;
	})
	
	//set placement of error labels with workaround for calendar and double select on time
	$.validator.setDefaults({
		//debug: true,
		groups: { 
			booking_time: "booking_hour booking_minute"
		},
		errorPlacement: function(label, element) {
			if (element.parent().is("span")) {
				label.insertBefore( element.parent("span") );
			} else {
				label.insertBefore( element );
			}
		},
		submitHandler: function() {
			$("#reservation-request").load(url + ' #reservation_form_result', { name: rName, email: rEmail, phone: rPhone, guests: $('#guests').val(), booking_date: $('#booking_date').val(), booking_hour: $('#booking_hour').val(), booking_minute: $('#booking_minute').val(), comments: $('#comments').val() },  function() { 																																																																												   			$("#reservation-request").append('<div class="pad"><a href="#" onclick="closeReservation(); return false;">close</a></div>'); 
			_gaq.push(['_trackPageview', 'reservationAjaxSuccess.html']);																																																																																																																																																																																	 			});
		}
	//end val defaults			
	});
	
	if (step == 2) {
		
		$("#reservation-request").load(url + ' #booking_info', '' ,function() {
		   $("#booking_info legend").html('Step 2 of 2 (Required fields <span title="required" class="important">*</span>)');
		   $("#reservation-request").append('<div id="reservation_btns"><button type="button" id="back" name="back" class="btn2ndary">back</button>&nbsp;<button type="submit" name="submit" id="submit">send</button></div>');
		   $("#comments").attr('rows','1');
		   
		    //preset values if user has used 'back'
			$('#guests').val(rGuests);
			$('#booking_date').val(rBookingDate);
		    $('#booking_hour').val(rBookingHour);
		    $('#booking_minute').val(rBookingMinute);
			$('#comments').val(rComments);
		   
		    
			//set values... using 'back'
			$('#back').bind('click', function() {
				rGuests = $('#guests').val();
				rBookingDate = $('#booking_date').val();
				rBookingHour = $('#booking_hour').val();
				rBookingMinute = $('#booking_minute').val();
				rComments = $('#comments').val();
				getReservation(url,1);
			})
		   
		   $("#reservation-request").validate();
		   
		   //set calendar
		   $("#booking_date").datepicker({
				minDate: 0,
				maxDate: '+6M',
				dateFormat: 'dd-mm-yy',
				showButtonPanel: true,
				showOn: 'both',
				buttonImage: 'images/icoCalendar.png',
				buttonImageOnly: true,
				buttonText: 'click to view calendar',
				onSelect: function(dateText, inst) { 
					$("#reservation-request").validate().element(this);
				}
			});
		   
		   _gaq.push(['_trackPageview', 'reservationAjaxStep2.html']);
		   
		 //end ajax load
		});
		
	} else {
		
		$("#reservation-request").load(url + ' #contact_info', '',function() {
		   $("#contact_info legend").html('Step 1 of 2 (Required fields <span title="required" class="important">*</span>)');
		   $("#reservation-request").append('<div id="reservation_btns"><button type="button" name="nextStep" id="nextStep">next</button></div>');
		   
		   //set previous values if user has used 'back'
		   $('#name').val(rName);
		   $('#email').val(rEmail);
		   $('#phone').val(rPhone);
		   
		   $(this).show();
		   
		   if (!$("a").is(".x")) {																 
		   		$("#reservation").prepend('<a class="x" href="#" onclick="closeReservation(); return false;">x</a>');
		   } else {
			   $("#reservation a.x").show();
		   }
		   
		   	$("#reservation-request").validate();
			$("#nextStep").click(function() {
				if ( $("#reservation-request").valid() ) {
					rName = $('#name').val();
					rEmail = $('#email').val();
					rPhone = $('#phone').val();
					getReservation(url,2);
				}
				return false;
			});
			
			 _gaq.push(['_trackPageview', 'reservationAjaxStep1.html']);
		   
		//end ajax load
		});
		
	}
	
}

function closeReservation() {
	$("#reservation").css({ width: "", height: "", marginTop: ""});
	$("#reservation-request").hide();
	$("#reservation a.x").hide();
	$('a#reservation-btn').click( function() {
			getReservation($(this).attr('href'));
			return false;
	})
}

$(document).ready(function(){			
		$('a#reservation-btn').bind('click', function() {
			getReservation($(this).attr('href'),1);
			return false;
		})
		
		$('ul#phLg').innerfade({
			speed: 1500,
			timeout: 5000,
			type: 'sequence',
			containerheight: '375px'
		});
		$.sifr({
			path: '/swf/',
			save: true
		});
		$('.ff-prom').sifr({ font: 'sIFR-promenade' });
		
		$('a[rel*=external]').click(function(){
		   window.open(this.href);
		   return false;
		});
		$('a[rel*=top]').click(function(){
		   top.location=this.href;
		   return false;
		});
});

