/* Author: Brady Fopma
   Date:   06/03/2011

*/
$(document).ready(function() {
	//$('a').click(function() {
	//	alert("you clicked a link to " + $(this).attr("href"));
	//	return false;
	//});

	$('ul.sf-menu').superfish({
		autoArrows: true,
		dropShadows: false
	});

	if ($('.promotions_carousel_content').length) {
		$(".promotions_carousel_content").jCarouselLite({
			btnNext: ".next",
			btnPrev: ".prev"
		});	
	}

	//Featured Section
	if ($('ul.feature').length) {
		if ($('ul.feature li').length > 1) {
			$('ul.feature').cycle({
				containerResize: 0
			});
		} else {
			$('ul.feature li').css('display','block');
		}
	}

	//Datepicker for reservation form
	$( ".datepicker" ).datepicker({
			showOn: "button",
			buttonImage: "/cr/images/calendar_icon.png",
			buttonImageOnly: true
	});

	$(".ui-datepicker-trigger").mouseover(function() {
        	$(this).css('cursor','pointer');
	}); 

	$('#amenities_list ul').makeacolumnlists({cols:2,colWidth:0,equalHeight:'ul',startN:1});

	if ($('ul.gallery').length) {
		 $('ul.gallery li a').fancybox({overlayOpacity:'0.4'});
	}

	$("#make_a_reservation_form div.x").click(function() {
		$('#make_a_reservation_form').hide();
		$('#submit_reservation_form').hide();
	});

	$("#make_a_reservation_button h3").click(function() {
		$('#make_a_reservation_form').show();
		$('#submit_reservation_form').show();
	});

	$("#submit_reservation_form").click(function() {
		submitResForm();
	});

	/* FORM: Email Signup */
	if ($('form#email_signup').length) {
		$('form#email_signup').validate({
			debug:true,
			rules: {
				fname:'required',
				lname:'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',
						fname:$('input#'+fid+'-fname',f).val(),
						lname:$('input#'+fid+'-lname',f).val(),
						email:$('input#'+fid+'-email',f).val()
					},
					success:function(json) {
						var m = '';
						if (json.success) {
							//m = '<h2>Thanks for registering!</h2><p>'+json.message+'</p>';
							m = '<h3 class="thanks">Thanks for registering!</h3>';
						} 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: CONTACT */
	if ($('form#contact').length) {
		$('form#contact').validate({
			debug:true,
			rules: {
				fname: 'required',
				lname: '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',
						fname:$('input#'+fid+'-fname',f).val(),
						lname:$('input#'+fid+'-lname',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(),
						category:$('select#'+fid+'-category',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 = '<h2>Thanks for your comments.</h2><p>'+json.message+'</p>';
						} else {
							m = '<h2>There was a problem sending your message.</h2><p>'+json.message+'</p>';
						}
						$('div.alert').html(m).show(200);
					},
					error:function(x,t,e) {
						$('div.alert').html('<h2>There was a problem submitting the form.</h2>').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-checkInDate').val());
	var checkOutDate = new Date($('#res-checkInDate').val());
	var NumDays = parseInt($('#res-length').val());
	checkOutDate.setDate(checkOutDate.getDate() + NumDays);
	var numAdults =  parseInt($('#res-numAdults').val());
	var numChildren =  parseInt($('#res-numChildren').val());
		
	var bookURL = "https://be.innpoints.com/index.php?hotelId=HQP6X" + 
		"&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;
}

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);
}

