/* foxboats.js */
function boat_details(boat_id,avl_id,sof_id)
{
	document.booking_step2_form.boat_id.value = boat_id;
	document.booking_step2_form.avl_id.value = avl_id;
	document.booking_step2_form.TR_Special_Offer_Number.value = sof_id;
	document.booking_step2_form.action = "booking/sof_boat_details.php";
	document.booking_step2_form.submit();
}

function book_boat(boat_id,avl_id,price)
{
	document.booking_step2_form.boat_id.value = boat_id;
	document.booking_step2_form.avl_id.value = avl_id;
	document.booking_step2_form.sof_price.value = price;
	document.booking_step2_form.action = "booking/booking_sof_step1.php";
	document.booking_step2_form.submit();
}

function setYearsMonths(monthsFor){
	if(!document.step1Form){
		return;
	}
  var nextYearFrom = 8 // month (0-11) to start showing next year's dates; default : 8 (September)
    , bookingMonths = new Array(false,false,false,'April','May','June','July','August','September',true,true,true) // months 0-11
    , curdate = new Date()
    , month = curdate.getMonth()
    , year = curdate.getFullYear()
    , yearSelect = document.step1Form.year_selected
    , monthSelect = document.step1Form.month_selected
    , mCt = 0
    ;
  if(!monthsFor){
    monthsFor = bookingMonths[month] ? (bookingMonths[month] === true ? (year+1) : year) : year;
    var yrCt = month < nextYearFrom || monthsFor != year ? 1 : 2;
    for(var y = 0; y < yrCt; y++){
      yearSelect.options.length = y+1;
      yearSelect.options[y].value = yearSelect.options[y].text = monthsFor+y;
    }
  }
  for(var m = (monthsFor == year ? month : 0); m < bookingMonths.length; m++){
    if(bookingMonths[m] && bookingMonths[m] !== true){
      monthSelect.options.length = mCt+1;
      monthSelect.options[mCt].value = ('00'+(m+1)).slice(-2);
      monthSelect.options[mCt++].text = bookingMonths[m];
    }
  }
}
jQuery(function($){
	setYearsMonths();
	// DAYBOAT HIRE
	var dbc = $('#dayBoatCalendars')
		, dbm = dbc.prepend('<ul></ul>').find('ul')
		, now = new Date()
		, nowd1 = new Date( now.getFullYear() + '/' + ('00'+(now.getMonth()+1)).slice(-2) + '/01' )
		, mnth = []
		;
	$('table', dbc).filter(function(){
			var m = $.trim($(this).find('th').text()), d = new Date(Date.parse('1 ' + m)), r = false;
			if(d < nowd1){
				$(this).parent('div').remove();
			}else{
				mnth.push( {m:m, d1:d} );
				r = true;
			}
			return r;
		}).each(function(i){
		  var idx = i;
		  dbm.append('<li><a href="#">' + mnth[idx].m.replace(/^(\w{3}).*?(\d+)$/, '$1 <span>$2</span>') + '</a></li>')
					.find('a').click(function(){
							var d = dbc.children('div'), t = d.eq(dbm.find('a').index(this));
							if(!t.is(':visible')){
								d.filter(':visible').hide();
								t.show();
							}
							return false;
						});
//jun2010: add check on className
			$(this).find('tbody td').filter(function(){ return !$(this).attr('bgcolor') && !this.className; }).each(function(){
				  var c = $(this), dt = new Date(Date.parse(c.text()+' '+mnth[idx].m));
				  if(dt > now){
				  	var ymd = dt.getFullYear() + '/' + ('00'+(dt.getMonth()+1)).slice(-2) + '/' + ('00'+dt.getDate()).slice(-2);
			 			c.addClass('dbcSelectable')
								.prepend('<input type="radio" name="cust_dateYMD" value="'+ymd+'">')
								.hover(function(){ $(this).addClass('dbcSelectHover'); }, function(){ $(this).removeClass('dbcSelectHover'); });
				  }
				});
		}).click(function(ev){
				var t = $(ev.target).filter('.dbcSelectable').find(':radio');
				if(t.length && !t.is(':checked')){
					$('td.dbcSelected', dbc).removeClass('dbcSelected').find(':radio').each(function(){ this.checked = false; });
					t.each(function(){ this.checked = true; }).closest('td').addClass('dbcSelected');
					$('#dayBoatStartDate').val(new Date(t.val()).toLocaleString().replace(/\s+\d+:.*$/, ''));
				}
			});
	var i = mnth.length;
	while(i && mnth[--i].d1 > now){ ; }
	dbm.find('a').eq(i).trigger('click');
	if(dbc.length){
		$('#contactForm').ajaxForm(
		  { beforeSubmit:function(data, fm, opt){
		  	  $('#contactSuccess,#contactError').hide();
				  var adults = 1*$('#dbcNumAdults').val(), kids = 1*$('#dbcNumChildren').val(), msg = '';
				  fm.find('.reqd_field').each(function(){
							if(!$(this).val()){
								msg = 'Please complete all required fields';
								$(this).addClass('dbcMissingField');
							}else{
								$(this).removeClass('dbcMissingField');
							}
						});
					if(!msg){
					 	if($('#dbcIsDayHire:checked').length){
							if(adults > 6){ msg = 'The maximum number of Adults on a Day Hire boat is 6'; }
							else if(adults + kids > 8){ msg = 'The maximum number of people on a Day Hire boat is 8'; }
						}else{
							if(adults > 4){ msg = 'The maximum number of Adults on a Mini Break boat is 4'; }
							else if(adults + kids > 6){ msg = 'The maximum number of people on a Mini Break boat is 6'; }
						}
					}
					if(msg){
						window.alert(msg);
					}
					return !msg;
				}
			, success:function(resp, status){
				  if(resp){
						$('#contactError').html(resp||'Failed to send').show();
					}else{
						$('#contactForm').resetForm();
						$('td.dbcSelected', dbc).removeClass('dbcSelected').find(':radio').each(function(){ this.checked = false; });
						$('#contactSuccess').show();
					}
				}
			});
	}
});
jQuery(function($){
	//Site search...
	$('#siteSearcher_form').bind('submit', function(){
			var f = $('#searchFor');
			if(f.length){
				if(f.val()){ $(this).ajaxSubmit({url:'index.php'
						, success:function(response, status){
								if(response){
									var sr = $('#searchResults');
									if(sr.length){
										sr.replaceWith(response);
										$('#searchResults').show();
									}else{
										$('#mainPageContent').after(response).slideUp();
										$('#searchResults').slideDown();
									}
								}
							}
						}); }
				else{ f.get(0).focus(); }
			}
			return false;
		});
	$('#searchResultsClear').live('click', function(){
			$('#searchResults').slideUp(function(){ $(this).remove(); });
			$('#mainPageContent').slideDown();
			return false;
		});
	//FAQs collapser...
	if ($.trim(($('#title').text() || '')).toLowerCase() == 'faq') {
		//...we appear to be on the 'Questions answered' page so
		//...find all Ps following an H2, that have a STRONG element in them...
		$('h2').nextAll('p').filter(function(){ return !!$('strong', this).length; })
			//...for each qualifying P :
			//   - clear out non-breaking spaces and split on BR
			//   - if 2 or more array elements (from the split), get the Query from :
			//     - grab and trim the first
			//     - if that is empty, and there's still 2 or more array elements left, grab and trim the next
			//   - if we have a non-empty Query
			//     - put the Query into its own P, class and style it, and insert it in front of the original P
			//     - construct the Answer from what's left of the original P content (after extracting the Query),
			//       put that back into the original P, and class and style the original P
			.each(function(){
					var t = $(this).html().replace(/&nbsp;/, '').split(/<br[^>]*>/) || []
						, x = t.length > 1 ? $.trim(t.shift()) : false
						, y = x !== false && !x && t.length > 1 ? $.trim(t.shift()) : x;
					if (y) {
						$('<p></p>').addClass('faqsQuery').css({ marginBottom: 0, cursor: 'pointer' }).html(y).insertBefore(this);
						$(this).addClass('faqsAnswer').css({ marginTop: 0 }).html(t.join('<br />'));
					}
				});
			//...result : each qualifying P has been split into P.faqsQuery and P.faqsAnswer
		//...for each P.faqsQuery :
		//   - give it a click handler that will close/open the following P.faqsAnswer
		//   - trigger that click handler (to close the P.faqsAnswer)
		//   - give it a hover handler
		$('p.faqsQuery').click(function(){
				$(this).next('.faqsAnswer').slideToggle();
				return false;
			}).trigger('click').hover(
					function(){ 
						$(this).css({ backgroundColor: '#ccffcc' });
						return false;
					}
				, function(){
						$(this).css({ backgroundColor: '#ffffff' });
						return false;
					}
				);
	}
});

