/* Author: 

*/



function formShade(el,notify) {
	$('div.alert',el).remove();
	$(el).fadeOut(400, function() {
		if (notify !== undefined && notify !== '') {
			$(el).before('<div class="notify">'+notify+'</div>');
		}
	});
}
function formReveal(el,str,kf) {
	$('div.notify').remove();
	$(el)
		.fadeOut(200,function() {
			if (kf===true) {
				$(this).prepend(str);
			} else {	
				$(this).html(str);
			}
		})
		.fadeIn(400);
}

$.expr[':'].external = function(o) {
	return !o.href.match(/^mailto\:/) && (o.hostname != location.hostname);
};



$(function() {

	// External Links in New Tabs / Windows
	$('a:external:not(#book-online)').attr('target','_blank');
	
	// Date Selector for FP
	if ($('#fp-res-form').length) {
		$('#res-cal').datepicker({
			altField:"#fp-res-ci-field",
			altFormat:"yy-mm-dd",
			changeMonth:true,
			changeYear:true
		});
		$('#fp-res-form button').click(function() {
			/*
			var sd = new Date($('#res-cal').val());
			var ed = new Date(sd.getTime() + (1000 * 60 * 60 * 24 * $('#res-nights').val()));
			$('#fp-res-co-field').val(ed.getFullYear()+'-'+(ed.getMonth()+1)+'-'+ed.getDate());
			//_gaq.push(['_linkByPost', this]);
			$('#fp-res-form').submit();
			*/
			submitResForm();
			return false;
		});
		/*
		if ($.browser.msie && ($.browser.version == '6.0' || $.browser.version == '7.0')) {
			$('#fp-res-form button').click(function() {
				$('#fp-res-form').submit()
			});
		}
		*/
		
	}
	
	if ($('nav.breadcrumb').length) $('nav.breadcrumb ul li:last').addClass('last');
	
	if ($('ul.gallery').length) $('ul.gallery li a').fancybox({overlayOpacity:'0.4'});
	
	if ($('ul.feature li').length) {
		if ($('ul.feature li').length > 1) {
			$('ul.feature').cycle({
				timeout:6000,
				next:'#feature-right',
				prev:'#feature-left',
				pager:'#feature-ctrl',
				pagerAnchorBuilder:function(i,s) {
					return '<li><a href="#"></a></li>';
				}
			});
		} else {
			$('ul.feature.li').show();
		}
	}
	
	
	
	/* FORM: GIVEAWAY */
	if ($('form#giveaway').length) {
		$('form#giveaway').validate({
			debug:true,
			rules: {
				name:'required',
				email: {
					required:true,
					email:true
				}
			},
			errorClass:'invalid',
			highlight:function(e,ec) {
				$(e).addClass(ec);
			},
			unhighlight:function(e,ec) {
				$(e).removeClass(ec);
				$(e).siblings('span.error').remove();
			},
			submitHandler:function(f) {
				$(f).before('<div class="alert"></div>');
				var fid = $(f).attr('id');
				formShade(f);
				$.ajax({
					type:'POST',
					url:$(f).attr('action'),
					dataType:'json',
					data:{
						jssub:'1',
						name:$('input#'+fid+'-name',f).val(),
						address:$('input#'+fid+'-address',f).val(),
						city:$('input#'+fid+'-city',f).val(),
						state:$('select#'+fid+'-state',f).val(),
						zip:$('input#'+fid+'-zip',f).val(),
						phone:$('input#'+fid+'-phone',f).val(),
						email:$('input#'+fid+'-email',f).val(),
						emsub:($('input#'+fid+'-em-sub:checked').length)?'y':'n'
					},
					success:function(json) {
						var m = '';
						if (json.success) {
							m = '<h1>Thanks for registering.</h1><p>'+json.message+'</p>';
						} else {
							m = '<h1>There was a problem registering.</h1><p>'+json.message+'</p>';
						}
						$('div.alert').html(m).show(200);
					},
					error:function(x,t,e) {
						$('div.alert').html('<h1>There was a problem submitting the form.</h1>').show(200);
					}
				});
			},
			invalidHandler: function(f,v) {
			},
			errorPlacement: function(err,el) {
				var fn = $(el).siblings('span.title').text();
				$(el).siblings('span.error').remove();
				if(!$(el).siblings('span.error').length) {
					$(el).after('<span class="error">Please provide your <strong>'+fn+'</strong>.</span>');
				}
			}
		});
	}
	
	
	
	/* FORM: RFP */
	if ($('form#rfp').length) {
		$('form#rfp').validate({
			debug:true,
			rules: {
				name:'required',
				email: {
					required:true,
					email:true
				},
				phone:'required'
			},
			errorClass:'invalid',
			highlight:function(e,ec) {
				$(e).addClass(ec);
			},
			unhighlight:function(e,ec) {
				$(e).removeClass(ec);
				$(e).siblings('span.error').remove();
			},
			submitHandler:function(f) {
				$(f).before('<div class="alert"></div>');
				var fid = $(f).attr('id');
				formShade(f);
				$.ajax({
					type:'POST',
					url:$(f).attr('action'),
					dataType:'json',
					data:{
						jssub:'1',
						name:$('input#'+fid+'-name',f).val(),
						cname:$('input#'+fid+'-cname',f).val(),
						email:$('input#'+fid+'-email',f).val(),
						phone:$('input#'+fid+'-phone',f).val(),
						address:$('input#'+fid+'-address',f).val(),
						city:$('input#'+fid+'-city',f).val(),
						state:$('input#'+fid+'-state',f).val(),
						zip:$('input#'+fid+'-zip',f).val(),
						country:$('input#'+fid+'-country',f).val(),
						event:$('input#'+fid+'-event',f).val(),
						attendees:$('input#'+fid+'-attendees',f).val(),
						arrival:$('input#'+fid+'-arrival',f).val(),
						departure:$('input#'+fid+'-departure',f).val(),
						alternate:$('input#'+fid+'-alternate',f).val(),
						eventdesc:$('textarea#'+fid+'-eventdesc',f).val()
					},
					success:function(json) {
						var m = '';
						if (json.success) {
							m = '<h1>Thanks for your comments.</h1><p>'+json.message+'</p>';
						} else {
							m = '<h1>There was a problem sending your message.</h1><p>'+json.message+'</p>';
						}
						$('div.alert').html(m).show(200);
					},
					error:function(x,t,e) {
						$('div.alert').html('<h1>There was a problem submitting the form.</h1>').show(200);
					}
				});
			},
			invalidHandler: function(f,v) {
			},
			errorPlacement: function(err,el) {
				var fn = $(el).siblings('span.title').text();
				$(el).siblings('span.error').remove();
				if(!$(el).siblings('span.error').length) {
					$(el).after('<span class="error">Please provide your <strong>'+fn+'</strong>.</span>');
				}
			}
		});
	}
	
	
	
	/* FORM: CONTACT */
	if ($('form#contact').length) {
		$('form#contact').validate({
			debug:true,
			rules: {
				name:'required',
				email: {
					required:true,
					email:true
				},
				comments:'required'
			},
			errorClass:'invalid',
			highlight:function(e,ec) {
				$(e).addClass(ec);
			},
			unhighlight:function(e,ec) {
				$(e).removeClass(ec);
				$(e).siblings('span.error').remove();
			},
			submitHandler:function(f) {
				$(f).before('<div class="alert"></div>');
				var fid = $(f).attr('id');
				formShade(f);
				$.ajax({
					type:'POST',
					url:$(f).attr('action'),
					dataType:'json',
					data:{
						jssub:'1',
						name:$('input#'+fid+'-name',f).val(),
						address:$('input#'+fid+'-address',f).val(),
						city:$('input#'+fid+'-city',f).val(),
						state:$('select#'+fid+'-state',f).val(),
						zip:$('input#'+fid+'-zip',f).val(),
						phone:$('input#'+fid+'-phone',f).val(),
						email:$('input#'+fid+'-email',f).val(),
						emsub:($('input#'+fid+'-em-sub:checked').length)?'y':'n',
						comments:$('textarea#'+fid+'-comments',f).val()
					},
					success:function(json) {
						var m = '';
						if (json.success) {
							m = '<h1>Thanks for your comments.</h1><p>'+json.message+'</p>';
						} else {
							m = '<h1>There was a problem sending your message.</h1><p>'+json.message+'</p>';
						}
						$('div.alert').html(m).show(200);
					},
					error:function(x,t,e) {
						$('div.alert').html('<h1>There was a problem submitting the form.</h1>').show(200);
					}
				});
			},
			invalidHandler: function(f,v) {
			},
			errorPlacement: function(err,el) {
				var fn = $(el).siblings('span.title').text();
				$(el).siblings('span.error').remove();
				if(!$(el).siblings('span.error').length) {
					$(el).after('<span class="error">Please provide your <strong>'+fn+'</strong>.</span>');
				}
			}
		});
	}
	
	
	
	
	
	function submitResForm() {

		var checkInDate = new Date($('#res-cal').val());
		var checkOutDate = new Date($('#res-cal').val());
		var NumDays = parseInt($('#res-nights').val());
		checkOutDate.setDate(checkOutDate.getDate() + NumDays);
		var numAdults =  parseInt($('#res-adults').val());
		var numChildren =  parseInt($('#res-children').val());
			
		var bookURL = "https://be.innpoints.com/index.php?hotelId=16253" + 
			"&checkInDate=" + (checkInDate.getMonth()+1) + "/" + checkInDate.getDate() + "/" + checkInDate.getFullYear() +
			"&checkOutDate=" + (checkOutDate.getMonth()+1) + "/" + checkOutDate.getDate() + "/" + checkOutDate.getFullYear() +
			"&numAdults=" + numAdults +
			"&numChildren=" + numChildren;
		//window.open(bookURL);
		_gaq.push(['_link',bookURL]);
		//window.location.href=bookURL;
	}
	
});
