$(document).ready(function(){
	/*
	//hide content until image is loaded, then shows it.
	if ($(".imposed").length) {
		$(".floattext").hide();
		$("<img src='/images/ajax-loader.gif' alt='Activity Monitor' class='activity_monitor' />").appendTo(".imposed");
		var presrc = $(".imposed").css("background-image");
		presrc = presrc.replace('url("','');
		presrc = presrc.replace('")','');
		$("<img src='" + presrc + "' />").load(function(){
			$(".activity_monitor").remove();
			$(".floattext").show();
		});
	}
	*/
	
	//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");
	$("#inquiry").submit(function(e){
		e.preventDefault();
		var form = $(this);
		var btn = $(this).find("input[type=submit]");
		btn.attr("disabled","disabled");
		$.post("includes/mail2_plus.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.replaceWith("<p>Your message has been sent.</p>");
				}
			}
		});
	});
	
	$("#blog_posts .floattext, #all_testimonials .floattext").jScrollPane({showArrows:true,autoReinitialise:true});
	
	//clear out all the a's in the slideshow list and make it into an array.
	if ($("#slideshow").length) {
		$("<img src='/images/ajax-loader.gif' alt='Activity Monitor' class='activity_monitor' />").appendTo("#slide_wrap");
		images = new Array();
		curimg = 0;
		$("#slideshow a").each(function(i){
			images[i] = $(this).attr("href");
			$(this).parents("li").remove();
		});
		$("<img src='" + images[curimg] + "' />").load(function(){
			$(".activity_monitor").remove();
			nextImg();
		});
	}
	
	//#############ADMIN SCRIPTS################
	$("#show_new_testimonial").click(function(e){
		e.preventDefault();
		$("#new_testimonial").slideToggle();
	});
	
	$("#new_testimonial").submit(function(e){
		e.preventDefault();
		var form = $(this);
		$.post("ajax/add-testimonial.php",form.serialize(),function(data){
			var err = data.split("|");
			if (err[0] == "error") {
				alert(err[1]);
			} else {
				$(".admin_table tr:first-child").after(data);
				$("#new_testimonial textarea").val("");
				$("#new_testimonial").slideUp();
			}
		});
	});
	
	$("#edit_testimonial").submit(function(e){
		e.preventDefault();
		var form = $(this);
		$.post("ajax/edit-testimonial.php",form.serialize(),function(data){
			var err = data.split("|");
			if (err[0] == "error") {
				alert(err[1]);
			} else {
				alert("Upate Successful!");
			}
		});
	});
	
	$("#upload_photo input[type=file]").change(function(){
		$(this).parents("form").submit();
		$("#upload_status").html("...processing image.");
		$(this).val("");
	});
	
	$(".del_photo").live("click",function(e){
		e.preventDefault();
		var container = $("#testimonial_photo");
		var id = extractId($(this).attr("id"));
		var doit = confirm("Are you sure you want to delete this photo?");
		if (doit == true) {
			$.post("ajax/delete-photo.php",{"id":id},function(data){
				var err = data.split("|");
				if (err[0] == "error") {
					alert(err[1]);
				} else {
					container.html("<p>No Photo</p>");
				}
			});
		}
	});
	
	$(".del_testimonial").live("click",function(e){
		e.preventDefault();
		var container = $(this).parents("tr");
		var id = extractId($(this).attr("id"));
		var doit = confirm("Are you sure you want to delete this testimonial?");
		if (doit == true) {
			$.post("ajax/delete-testimonial.php",{"id":id},function(data){
				var err = data.split("|");
				if (err[0] == "error") {
					alert(err[1]);
				} else {
					container.detach();
				}
			});
		}
	});
	//###########END ADMIN SCRIPTS#############
});

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

function extractId(id){
	newid = id.split("_");
	return newid[1];
}

function photoUploadError(error){
	alert(error);
	$("#upload_status").html("An error occured.");
}

function photoUploadSuccess(newimg){
	$("#testimonial_photo").html(newimg);
	$("#upload_status").html("");
}
