$(document).ready(function(){
	//standard AJAX mail form submission (requires mail2_plus.php, and captcha)
	//add form name to selector for submit event, the rest will take care of itself!
	$("input[type=submit]").removeAttr("disabled");
	$("#schedulef").submit(function(e){
		e.preventDefault();
		var form = $(this);
		var btn = $(this).find("input[type=submit]");
		btn.attr("disabled","disabled");
		$.post("/ajax/simple-mail.php",form.serialize(),function(data){
			var err = data.split("|");
			if (err[0] == "error") {
				alert(err[1]);
				btn.removeAttr("disabled");
			} else {
				//if a redirect is specified, use that. Else replace the form with a message.
				if (form.find("input[name=redirect]").length && form.find("input[name=redirect]").val() != "") {
					window.location = form.find("input[name=redirect]").val();
				} else {
					form.html("<p class='sent'>Your message has been sent.</p>");
				}
			}
		});
	});
	
	$("#loginf input[type=text],#loginf input[type=password],#newsletterf input[type=text]").focus(function(){
		v = $(this).val();
		if (v == "Email Address" || v == "Username" || v == "Password") {
			$(this).val("");
		}
	});
	$("#loginf input[type=text],#loginf input[type=password],#newsletterf input[type=text]").blur(function(){
		v = $(this).val();
		n = $(this).attr("name");
		if (n == "password" && v == "") {
			$(this).val("Password");
		} else if (n == "username" && v == "") {
			$(this).val("Username");
		} else if (n == "email" && v == "") {
			$(this).val("Email Address");
		}
	});
	
	//pdf embedder
	if ($("#pdf_wrap").length) {
		var pdfsrc = $("#pdf_select").val();
		new PDFObject({"url":pdfsrc}).embed("pdf");
		$("#pdf_select").change(function(){
			pdfsrc = $("#pdf_select").val();
			new PDFObject({"url":pdfsrc}).embed("pdf");
		});
	}
	
	//hide these fields for the schedule form and show when "yes" is clicked
	$("#schedulef #reserve_fields").hide();
	$("#schedulef input[name=boat]").change(function(){
		if ($(this).val() == "yes") {
			$("#schedulef #reserve_fields").show();
		} else {
			$("#schedulef #reserve_fields").hide();
		}
	});
	
	//class dropdown list
	if ($(".classy").length) {
		//close all divs to start with, but open the first one on each list
		$(".classy h4").addClass("closed").css({"cursor":"pointer"});
		$(".classy_content").hide();
		$(".classy li:first-child h4").removeClass("closed");
		$(".classy li:first-child .classy_content").show();
		$(".classy h4").click(function(){
			if ($(this).hasClass("closed")) {
				$(this).removeClass("closed");
				$(this).parents("li").find(".classy_content").slideDown();
			} else {
				$(this).addClass("closed");
				$(this).parents("li").find(".classy_content").slideUp();
			}
		});
	}
	//clear out all the a's in the slideshow list and make it into an array.
	if ($(".slideshow").length) {
		images = new Array();
		curimg = new Array();
		$(".slideshow").each(function(){
			var showid = $(this).attr("id");
			images[showid] = new Array();
			curimg[showid] = 0;
			$(this).find("a").each(function(i){
				images[showid][i] = $(this).attr("href");
				$(this).parent("li").remove();
			});
			var newimg = "<img src='" + images[showid][curimg[showid]] + "' />";
			$(newimg).load(function(){
				$("<li>" + newimg + "</li>").appendTo("#" + showid);
				curimg[showid]++;
				
				M = 2500;
				N = 6000;
				num = Math.floor(M + (1+N-M)*Math.random());
				setTimeout("nextImg('" + showid + "')",num);
			});
		});
	}
});

//slideshow function
function nextImg(showid) {
	if (curimg[showid] >= images[showid].length) {
		curimg[showid] = 0;
	}
	$("#" + showid + " li").addClass("lastimg");
	var newimg = "<img src='" + images[showid][curimg[showid]] + "' />";
	$(newimg).load(function(){
		$("<li>" + newimg + "</li>").appendTo("#" + showid).hide().fadeIn("slow",function(){
			$("#" + showid + " .lastimg").remove();
			curimg[showid]++;
			setTimeout("nextImg('" + showid + "')",5000);
		});
	});
}
