///////////////////////////////////////////////////////////
//////////////////// HOMEPAGE CAROUSEL ////////////////////
///////////////////////////////////////////////////////////
function mycarousel_initCallback(carousel) {
	$("#carousel-container li").show();
	jQuery('.jcarousel-control a').bind('click', function() {
		carousel.scroll(jQuery.jcarousel.intval(jQuery(this).text()));
		return false;
	});

	jQuery('.jcarousel-scroll select').bind('change', function() {
		carousel.options.scroll = jQuery.jcarousel.intval(this.options[this.selectedIndex].value);
		return false;
	});

	jQuery('#mycarousel-next').bind('click', function() {
		carousel.next();
		return false;
	});

	jQuery('#mycarousel-prev').bind('click', function() {
		carousel.prev();
		return false;
	});

}

var as_num = 2;
var t;
var auto = true;
function auto_scroll_carousel() {
	if(auto) {
		$(".jcarousel-control a").removeClass("current");
		var carousel = jQuery('#mycarousel').data('jcarousel');
		if(as_num>4) {
			as_num = 1;
		}
		$(".jcarousel-control a#car_" + as_num).toggleClass("current");
		if(as_num==1) {
			$("#banner span").html("Sailing Day <strong>Active Lives</strong>");
		} else if(as_num==2) {
			$("#banner span").html("Edna and Maureen <strong>Active Lives</strong>");
		} else if(as_num==3) {
			$("#banner span").html("Stevie <strong>Active Lives</strong>");
		} else if(as_num==4) {
			$("#banner span").html("Art Session <strong>Active Lives</strong>");
		}
		carousel.scroll(jQuery.jcarousel.intval(as_num++));
		
		t = setTimeout("auto_scroll_carousel()",3900);
	}
}
// Ride the carousel
jQuery(document).ready(function() {
	get_font_cookie();
	if($("#mycarousel").length) {
		jQuery("#mycarousel").jcarousel({
			scroll: 1,
			initCallback: mycarousel_initCallback,
			// This tells jCarousel NOT to autobuild prev/next buttons
			buttonNextHTML: null,
			buttonPrevHTML: null
		});
		setTimeout("auto_scroll_carousel()",3900);
	}
	$(".jcarousel-control a").click(function(){
		clearTimeout(t);auto = false;
		$(".jcarousel-control a").removeClass("current");
		$(this).toggleClass("current");
		if($(this).attr('id')=="car_1") {
			$("#banner span").html("Sailing Day <strong>Active Lives</strong>");
		} else if($(this).attr('id')=="car_2") {
			$("#banner span").html("Edna and Maureen <strong>Active Lives</strong>");
		} else if($(this).attr('id')=="car_3") {
			$("#banner span").html("Stevie <strong>Active Lives</strong>");
		} else if($(this).attr('id')=="car_4") {
			$("#banner span").html("Art Session <strong>Active Lives</strong>");
		}
		
	});
	
//////////////////////////////////////////////////
//////////////////// COLORBOX ////////////////////
//////////////////////////////////////////////////
$("#enquiry_form").colorbox({inline:true, href:"#popup"});
$("#shared_lives").colorbox({inline:true, href:"#popup"});
$("#choices_popup").colorbox({inline:true, href:"#popup"});
$("#activities_popup").colorbox({inline:true, href:"#popup"});
$("#housing_popup").colorbox({inline:true, href:"#popup"});
$("#shared_lives,#activities_popup,#housing_popup,#choices_popup").click(function() {
	$("#popup .form_element").show();
	$("#popup .thanks_element").hide();
});

/////////////////////////////////////////////////////
//////////////////// TEXT RESIZE ////////////////////
/////////////////////////////////////////////////////
$("#text_size .large").click(function(){
	$(this).parent().parent().parent().parent().parent().addClass("body_text_large");
	set_font_cookie("true");
});
$("#text_size .small").click(function(){
	$(this).parent().parent().parent().parent().parent().removeClass("body_text_large");
	set_font_cookie("false");
});
	
/////////////////////////////////////////////////////////////
//////////////////// WHAT WE DO SIDE NAV ////////////////////
/////////////////////////////////////////////////////////////
//$(".expand").click(function(){
	//$(".expand").children(".menu").hide();
	//$(this).children(".menu").show();
//});

//$("#side_nav .menu .active").parent().show();
	
/////////////////////////////////////////////////////
//////////////////// OTHER STUFF ////////////////////
/////////////////////////////////////////////////////
	$(".back_btn").click(function() {
		
		window.history.back();
		return false;
	});
	$(".clear_input").focus(function() {
		var v = $(this).val();
		$(this).val( this.defaultValue == v ? '' : v);
		
	});
	$('.clear_input').blur(function() {
		var v = $(this).val();
		$(this).val( v == '' ? this.defaultValue : v);
	});
	$("input[name=password]").focus(focus_password);
	$("#contact_form").submit(function() {
		$("li.error").remove();
		
		/*
		var inputs = [];
		$(this).find("select,input,textarea").each(function() {
			inputs[$(this).attr('name')] = $(this);
		});
		var error = false;
		
		if(inputs['name'].val().length<1) {
			error = true;
			$("<li class=\"error\"><span>Please enter a name.</span></li>").insertBefore($(inputs['name']).parent());
		}
		if(inputs['email'].val().split("@").length!=2) {
			error = true;
			$("<li class=\"error\"><span>Please enter a valid email address.</span></li>").insertBefore($(inputs['email']).parent());
		}
		if(inputs['message'].val().length<1) {
			error = true;
			$("<li class=\"error\"><span>Please enter a message.</span></li>").insertBefore($(inputs['message']).parent());
		}
		*/
		var data = validate_contact_forms("contact_form");
		var error = data.error;
		var inputs = data.inputs;
		
		if(false==error) {
			for(var x in inputs) {
				$(inputs[x]).attr({'disabled':'disabled'});
			}
			$.ajax({
				url: '/mail/send',
				type: 'post',
				dataType: 'json',
				data: 'department=' + inputs['department'].val() + '&name=' + inputs['name'].val() + '&email=' + inputs['email'].val() + '&message=' + inputs['message'].val() + '&mailing_list=' + (inputs['mailing_list'].attr('checked')? 'true' : 'false')  + '&send_bol=true',
				success: function(data) {
					for(var x in inputs) {
						$(inputs[x]).removeAttr('disabled');
					}
					if(data.status=="success") {
						for(var x in inputs) {
							$(inputs[x]).val("");
						}
						$("#popup").html("<h1 class=\"thank_title\">Thank You</h1><p class=\"thank_p\">Your message has been sent and we'll get back to you shortly.</p>");
						setTimeout("$.colorbox.close()",3200);
					} else {
						$("<li class=\"error\"><span>Sorry, there seems to have been an error, please try again.</span></li>").insertBefore(inputs['department'].parent());
					}
					$.colorbox.resize();
				}
			});
		} else {
			$.colorbox.resize();
		}
		
		return false;
	});
	$("#shared_lives_form").submit(function() {
		$("li.error").remove();
		var data = validate_contact_forms("shared_lives_form");
		if(data.error==false) {
			$.ajax({
				url: "/mail/shared_lives_information",
				type: "post",
				dataType: "json",
				data: "name=" + data.inputs["name"].val() + "&email=" + data.inputs["email"].val() + "&telephone=" + data.inputs["telephone"].val() + "&message=" + data.inputs["message"].val() + '&mailing_list=' + (data.inputs['mailing_list'].attr('checked')? 'true' : 'false'),
				success: function(response) {
					for(var x in data.inputs) {
						$(data.inputs[x]).removeAttr('disabled');
					}
					if(response.status=="success") {
						for(var x in data.inputs) {
							$(data.inputs[x]).val("");
						}
						//$("#popup").html("<h1 class=\"thank_title\">Thank You</h1><p class=\"thank_p\">Your message has been sent and we'll get back to you shortly.</p>");
						$("#popup .form_element").hide();
						
						data.inputs["name"].val("Your name");
						data.inputs["email"].val("Email address");
						data.inputs["telephone"].val("Telephone");
						data.inputs["message"].val("Other info");
						
						$("#popup .thanks_element").show();
						setTimeout("$.colorbox.close()",3200);
					} else {
						$("<li class=\"error\"><span>Sorry, there seems to have been an error, please try again.</span></li>").insertBefore(data.inputs['name'].parent());
					}
					$.colorbox.resize();
				}
			});
		} else {
			$.colorbox.resize();
		}
		return false;
	});
	
	$("#activities_form").submit(function() {
		
		$("li.error").remove();
		var data = validate_contact_forms("activities_form");
		
		var boxes = "";var check = 1;
		$("#activities_form").find("input[type=checkbox]:checked").each(function() {
			data.inputs[$(this).attr("name")] = $(this);
			boxes += "&check_"+ check + "=" + $(this).attr("name");
			check++;
		});
		
		if(data.error==false) {
		
			$.ajax({
				url: "/mail/active_lives_information",
				type: "post",
				dataType: "json",
				data: "name=" + data.inputs["name"].val() + "&email=" + data.inputs["email"].val() + "&telephone=" + data.inputs["telephone"].val() + "&message=" + data.inputs["message"].val() + boxes + '&mailing_list=' + (data.inputs['mailing_list'].attr('checked')? 'true' : 'false'),
				success: function(response) {
					
					for(var x in data.inputs) {
						$(data.inputs[x]).removeAttr('disabled');
					}
					if(response.status=="success") {
						for(var x in data.inputs) {
							$(data.inputs[x]).val("");
						}
						//$("#popup").html("<h1 class=\"thank_title\">Thank You</h1><p class=\"thank_p\">Your message has been sent and we'll get back to you shortly.</p>");
						$("#popup .form_element").hide();
						
						data.inputs["name"].val("Your name");
						data.inputs["email"].val("Email address");
						data.inputs["telephone"].val("Telephone");
						data.inputs["message"].val("More info e.g proposed dates, number of people");
						
						$("#activities_form").find("input[type=checkbox]:checked").each(function() {
							$(this).removeAttr("checked");
						});
						
						$("#popup .thanks_element").show();
						setTimeout("$.colorbox.close()",3200);
					} else {
						$("<li class=\"error\"><span>Sorry, there seems to have been an error, please try again.</span></li>").insertBefore(data.inputs['name'].parent());
					}
					$.colorbox.resize();
					
				}
			});
			
		} else {
			$.colorbox.resize();
		}
		
		return false;
	});
	
	$("#venues_form").submit(function() {
		
		$("li.error").remove();
		var data = validate_contact_forms("venues_form");
		

		data.inputs["venue"] = $("#venues_form").find("input[name=venue]:checked");
		
		if(data.inputs["venue"].val()==undefined) {
			data.error = true;

			$("#venues_form ul.clearfix").prepend("<li class=\"error\"><span>Please select a venue to hire.</span></li>");
		}
		
		if(data.error==false) {
			
			$.ajax({
				url: "/mail/venue_hire",
				type: "post",
				dataType: "json",
				data: "name=" + data.inputs["name"].val() + "&email=" + data.inputs["email"].val() + "&telephone=" + data.inputs["telephone"].val() + "&message=" + data.inputs["message"].val() + "&venue=" + data.inputs["venue"].val() + "&mailing_list=" + (data.inputs["mailing_list"].attr("checked")? "true" : "false"),
				success: function(response) {
					
					for(var x in data.inputs) {
						$(data.inputs[x]).removeAttr('disabled');
					}
					if(response.status=="success") {
						for(var x in data.inputs) {
							$(data.inputs[x]).val("");
						}
						//$("#popup").html("<h1 class=\"thank_title\">Thank You</h1><p class=\"thank_p\">Your message has been sent and we'll get back to you shortly.</p>");
						$("#popup .form_element").hide();
						
						data.inputs["name"].val("Your name");
						data.inputs["email"].val("Email address");
						data.inputs["telephone"].val("Telephone");
						data.inputs["message"].val("Other info");
						
						$("#venues_form").find("input[type=checkbox]:checked").each(function() {
							$(this).removeAttr("checked");
						});
						
						$("#popup .thanks_element").show();
						setTimeout("$.colorbox.close()",3200);
					} else {
						$("<li class=\"error\"><span>Sorry, there seems to have been an error, please try again.</span></li>").insertBefore(data.inputs['name'].parent());
					}
					$.colorbox.resize();
					
				}
			});
			
			
		} else {
			$.colorbox.resize();
		}
		
		return false;
	});
	
	$("#housing_form").submit(function() {
		$("li.error").remove();
		var data = validate_contact_forms("housing_form");
		if(data.error==false) {
			$.ajax({
				url: "/mail/housing_information",
				type: "post",
				dataType: "json",
				data: "name=" + data.inputs["name"].val() + "&email=" + data.inputs["email"].val() + "&telephone=" + data.inputs["telephone"].val() + "&message=" + data.inputs["message"].val() + '&mailing_list=' + (data.inputs['mailing_list'].attr('checked')? 'true' : 'false'),
				success: function(response) {
					for(var x in data.inputs) {
						$(data.inputs[x]).removeAttr('disabled');
					}
					if(response.status=="success") {
						for(var x in data.inputs) {
							$(data.inputs[x]).val("");
						}
						//$("#popup").html("<h1 class=\"thank_title\">Thank You</h1><p class=\"thank_p\">Your message has been sent and we'll get back to you shortly.</p>");
						$("#popup .form_element").hide();
						
						data.inputs["name"].val("Your name");
						data.inputs["email"].val("Email address");
						data.inputs["telephone"].val("Telephone");
						data.inputs["message"].val("Other info");
						
						$("#popup .thanks_element").show();
						
						
						setTimeout("$.colorbox.close()",3200);
					} else {
						$("<li class=\"error\"><span>Sorry, there seems to have been an error, please try again.</span></li>").insertBefore(data.inputs['name'].parent());
					}
					$.colorbox.resize();
				}
			});
		} else {
			$.colorbox.resize();
		}
		return false;
	});
	
	$("#choices_form").submit(function() {
		$("li.error").remove();
		var data = validate_contact_forms("choices_form");
		if(data.error==false) {
			$.ajax({
				url: "/mail/choices_information",
				type: "post",
				dataType: "json",
				data: "name=" + data.inputs["name"].val() + "&email=" + data.inputs["email"].val() + "&telephone=" + data.inputs["telephone"].val() + "&message=" + data.inputs["message"].val() + '&mailing_list=' + (data.inputs['mailing_list'].attr('checked')? 'true' : 'false'),
				success: function(response) {
					for(var x in data.inputs) {
						$(data.inputs[x]).removeAttr('disabled');
					}
					if(response.status=="success") {
						for(var x in data.inputs) {
							$(data.inputs[x]).val("");
						}
						//$("#popup").html("<h1 class=\"thank_title\">Thank You</h1><p class=\"thank_p\">Your message has been sent and we'll get back to you shortly.</p>");
						$("#popup .form_element").hide();
						
						data.inputs["name"].val("Your name");
						data.inputs["email"].val("Email address");
						data.inputs["telephone"].val("Telephone");
						data.inputs["message"].val("Other info");
						
						$("#popup .thanks_element").show();
						setTimeout("$.colorbox.close()",3200);
					} else {
						$("<li class=\"error\"><span>Sorry, there seems to have been an error, please try again.</span></li>").insertBefore(data.inputs['name'].parent());
					}
					$.colorbox.resize();
				}
			});
		} else {
			$.colorbox.resize();
		}
		return false;
	});
	
	if($("#main_map").length) {
		map_initialize();
	}
	$("#banner .hov").mouseenter(function() {
		var num = $(this).attr('id').substring(12);
		$("#t_name" + num).fadeIn(300);
	});
	$("#banner .hov").mouseleave(function() {
		$(".t_name").fadeOut(300);
	});
});
function validate_contact_forms(form_id) {
	var inputs = [];
	$("#" + form_id).find("select,input,textarea,checkbox").each(function() {
		inputs[$(this).attr('name')] = $(this);
	});
	var error = false;
	
	if(inputs['name'].val().length<1 || inputs['name'].val()=="Your name") {
		error = true;
		$("<li class=\"error\"><span>Please enter a name.</span></li>").insertBefore($(inputs['name']).parent());
	}
	if(form_id=="contact_form") {
		if(inputs['email'].val().split("@").length!=2) {
			error = true;
			$("<li class=\"error\"><span>Please enter a valid email address.</span></li>").insertBefore($(inputs['email']).parent());
		}
	} else {
	
		if((inputs['telephone'].val().length<1 || inputs['telephone'].val()=="Telephone") || inputs['mailing_list'].attr('checked')==true) {
			if(inputs['email'].val().split("@").length!=2) {
				error = true;
				if(inputs['mailing_list'].attr('checked')==true) {
					$("<li class=\"error\"><span>Please enter a valid email address.</span></li>").insertBefore($(inputs['email']).parent());
				} else {
					$("<li class=\"error\"><span>Please enter an email address or telephone number.</span></li>").insertBefore($(inputs['email']).parent());
				}
				
			}
		}
	}
	if(inputs['message'].val().length<1 || inputs['message'].val()=="Message" || inputs['message'].val()=="Other info") {
		error = true;
		$("<li class=\"error\"><span>Please enter a message.</span></li>").insertBefore($(inputs['message']).parent());
	}
	var data = {
	inputs:inputs,
	error:error
	}
	return data;
}
function set_font_cookie(bol) {
	var exdate=new Date();
	var exdays = 10;
	exdate.setDate(exdate.getDate() + exdays);
	var c_value=escape(bol) + ((exdays==null) ? "" : "; expires="+exdate.toUTCString());
	document.cookie="font_size=" + c_value +";path=/";
}
function get_font_cookie() {
	var i,x,y,ARRcookies=document.cookie.split(";");
	for(i=0;i<ARRcookies.length;i++) {
		x=ARRcookies[i].substr(0,ARRcookies[i].indexOf("="));
		y=ARRcookies[i].substr(ARRcookies[i].indexOf("=")+1);
		x=x.replace(/^\s+|\s+$/g,"");
		if (x=="font_size") {
			if(unescape(y)=="true") {
				$("html").addClass("body_text_large");
			} else {
				$("html").removeClass("body_text_large");
			}
		}
	}
}
function blur_password() {
	if($(this).val().length==0) {
		$(this).parent().html("<input type=\"text\" name=\"password\" value=\"Enter password\" />");
		$("input[name=password]").focus(focus_password);
	}
}
function focus_password() {
	if($(this).attr('type')=="text") {
		$(this).parent().html("<input type=\"password\" name=\"password\" value=\"\" />");
		$("input[name=password]").focus();
		$("input[name=password]").blur(blur_password);
	}
}




function map_initialize() {
  var myLatlng = new google.maps.LatLng(50.833863576121935,-0.1542);
  var myOptions = {
    zoom: 15,
    center: myLatlng,
    mapTypeId: google.maps.MapTypeId.ROADMAP
  }
  var map = new google.maps.Map(document.getElementById("main_map"), myOptions);

  var image = '/_images/icons/map_point.png';
  var myLatLng = new google.maps.LatLng(50.833863576121935,-0.1542);
  var beachMarker = new google.maps.Marker({
      position: myLatLng,
      map: map,
      icon: image
  });
}
