$(document).ready(function() {

	if( jQuery().nivoSlider ) {
		$('#nivoslider').nivoSlider({
			effect: 'fold',
			directionNav: false
		});
	}
	
	$.ajax({
		url:	'/wp-content/themes/raftTheRogue/ajax/weather.php',
		type:	'GET',
		success:function(html) {
			//$('#weather').append(html);
			//alert(html);
			$('#weatherLoader').fadeOut('fast',function() {
				$('#weather').append(html);
				$('#weather a').click(function() {
					if( $(this).attr('rel') == 'targetblank' ) {
						window.open( $(this).attr('href') );
						return false;
					}
				});
			});
		}
	});
	
	/* contact form */
	
	/* calendar controls */
	$('#date').focusin(function() {
		$('#popupCalendarHolder').stop().fadeIn('fast');
	})
	
	function applyCalendarControls() {
		$('#prevMonth').click(function() {
			
			var val = $(this).attr('href').replace('#','');
			var valArray = val.split('/');
			var month = valArray[0];
			var year = valArray[1];
			
			//alert( 'month: '+month+' year: '+year);
			//testing
			
			$.ajax({
				url:	'/wp-content/themes/raftTheRogue/ajax/printCalendar.php',
				type:	'GET',
				data:	'month='+month+
						'&year='+year,
				success:function(html) {
					$('#calendarHolder').html(html);
					applyCalendarControls();
				}
			});
			return false;
		});
		
		$('#nextMonth').click(function() {
			
			var val = $(this).attr('href').replace('#','');
			var valArray = val.split('/');
			var month = valArray[0];
			var year = valArray[1];
			
			//alert( 'month: '+month+' year: '+year);
			//testing
			
			$.ajax({
				url:	'/wp-content/themes/raftTheRogue/ajax/printCalendar.php',
				type:	'GET',
				data:	'month='+month+
						'&year='+year,
				success:function(html) {
					$('#calendarHolder').html(html);
					applyCalendarControls();
				}
			});
			return false;
		});
		
		$('#closeCalendar').click(function() {
			$('#popupCalendarHolder').stop().fadeOut('fast');
			return false;
		});
		
		$('#calendar_days a').click(function() {
			var val = $(this).attr('href').replace('#','');
			$('#date').val(val);
			$('#closeCalendar').click();
			return false;
		});
	}
	
	//	pply them originally
	applyCalendarControls();
	
	//	choose your craft button
	var addCraftHtml = '<option value="none" selected>none</option><option value="raft - 10 6 - 2 to 4 adults">Raft 10\'6" - 2 to 4 adults</option><option value="raft - 11 6 - 2 to 6 adults">Raft 11\'6" - 2 to 6 adults</option><option value="raft - 13 - 4 to 8 adults">Raft 13\' - 4 to 8 adults</option><option value="raft - 14 - 4 to 10 adults">Raft 14\' - 4 to 10 adults</option><option value="raft - 16 - 4 to 12 adults">Raft 16\' - 4 to 12 adults</option><option value="Inflatable Kayak - 1 person">Inflatable Kayak - 1 person</option><option value="Inflatable Kayak - 2 person">Inflatable Kayak - 2 person</option>';
	var numCrafts = 0;
	$('#addACraft').click(function() {
		
		numCrafts ++;
		html = '<select id="craft'+numCrafts+'" name="craft'+numCrafts+'" class="craftSelector regularSelect">'+addCraftHtml+'</select>';
		html += '<a href="#craft'+numCrafts+'" class="removeThisCraft floatLeft">Remove</a>';
		$('#craftHolder').append(html);
		
		$('.removeThisCraft').click(function() {
			var target = $(this).attr('href');
			$(target).fadeOut('fast',function() {
				$(target).remove();
			});
			$(this).remove();
			return false;
		});
		
		return false;
	});
	
	//	if we're viewing the make reservations page
	//	click the addacraft button
	if( $('#addACraft').length > 0 ) {
		$('#addACraft').click();
	}
	
	$('#contactForm').submit(function() {
		$('.ajaxLoader').fadeIn('fast',function() {
			$.ajax({
				url: 	'/wp-content/themes/raftTheRogue/ajax/contactFormProcess.php',
				type: 	'POST',
				data:	'contactName='+escape($('#contactName').val())+
						'&contactEmail='+escape($('#contactEmail').val())+
						'&contactPhone='+escape($('#contactPhone').val())+
						'&contactInquiry='+escape($('#contactInquiry').val()),
				success:function(html) {
					//$('.formResponse').empty().append(html);
					interveneAlert("<div style=\"width: 450px; height: 50px;\"><h2>"+html+"</h2></div>");
					$('.ajaxLoader').fadeOut('fast');
					$('#contactName').val('');
					$('#contactEmail').val('');
					$('#contactPhone').val('');
					$('#contactInquiry').val('');
				}
			});
		});		
		return false;
	});
	
	$('#makeReservationsForm').submit(function() {
	
		if( $('#reservationName').val() =='' ) {
			interveneAlert( "You must enter your name!" );
			return false;
		}
		
		if( $('#email').val() == '' ) {
			interveneAlert( "You must enter an email address!" );
			return false;
		}
		
		if( $('#phoneArea').val() == '' || $('#phoneOne').val() == '' || $('#phoneTwo').val() == '' ) {
			interveneAlert( "You must enter a phone number!" );
			return false;
		}
		
		if( $('#date').val() == '' ) {
			interveneAlert( "You must enter the date of your reservation!" );
			return false;
		}
		
		if( $('#preferredTime').val() == '' ) {
			interveneAlert( "You must enter a preferred time!" );
			return false;
		}
	
		$('.ajaxLoader').fadeIn('fast',function() {
			
			var crafts = '';
			$('.craftSelector').each(function() {
				crafts += $(this).val() + ', ' + "\r\n";
			});
			
			$.ajax({
				url:	'/wp-content/themes/raftTheRogue/ajax/reservationFormProcess.php',
				type:	'POST',
				data:	'reservationName='+escape($('#reservationName').val())+
						'&email='+escape($('#email').val())+
						'&phone='+escape($('#phoneArea').val() + '-' + $('#phoneOne').val() + '-' + $('#phoneTwo').val() )+
						'&date='+escape($('#date').val())+
						'&bestTime='+escape($('#bestTime').val())+
						'&preferredTime='+escape($('#preferredTime').val() + $('#ampm').val() )+
						'&additionalQuestions='+escape($('#additionalQuestions').val())+
						'&numberOfAdults='+escape($('#numberOfAdults').val())+
						'&numberOfChildren='+escape($('#numberOfChildren').val())+
						'&crafts='+crafts,
				success:function(html) {
					//$('.formResponse').empty().append(html);
					interveneAlert("<div style=\"width: 450px; height: 50px;\"><h2>"+html+"</h2></div>");
					$('.ajaxLoader').fadeOut('fast');
					$('#reservationName').val('');
					$('#email').val('');
					$('#phoneArea').val('');
					$('#phoneOne').val('');
					$('#phoneTwo').val('');
					$('#date').val('');
					$('#preferredTime').val('');
					$('#numberOfAdults').val('0');
					$('#numberOfChildren').val('0');
					$('.removeThisCraft').click();
					$('#addACraft').click();
					$('#bestTime').val('Anytime');
					$('#additionalQuestions').val('');
				}
			});
		});
		return false;
	});
	
	//	new window links
	$('a').click(function() {
		if( $(this).attr('rel') == 'targetblank' ) {
			window.open( $(this).attr('href') );
			return false;
		}
		if( $(this).attr('rel') == 'scrollto' ) {
			var scrollTo = $(this).attr('href');
			scrollTo = $(scrollTo);
			var anchorOffset = scrollTo.offset();
				//	scroll to the anchor
				$('html, body').stop().animate({
					scrollTop: anchorOffset.top
				}, 1000);
			return false;
		}
	});
});
