//<![CDATA[
$(window).load(function(){
	$("div.left, div.right").each(function(){ 
		img = $(this).find("img")
		if (img.length) {
			if ($(this).width() > img.width()+2 ) $(this).css("maxWidth",img.width()+2);	
		}
	});
});
$(function(){
	
	
	$('#c_name').blur(function(){checkField($('#c_name'),isCompleted,"Field required.");});		
	$('#c_email').blur(function(){checkField($('#c_email'),isValidEmail,"Invalid e-mail address.");});
	//$('#c_site').blur(function(){checkField($('#c_site'),isValidUrl,"Invalid site url.");});
	$('#c_comment').blur(function(){checkField($('#c_comment'),isCompleted,"Field required.");});	

	$('form#comment').submit(function(){
		var self = $(this);
		$('#error_notice').remove();
		// Check these fields
		checkField($('#c_name'),isCompleted,"Field required.");				
		checkField($('#c_email'),isValidEmail,"Invalid e-mail address.");
		checkField($('#c_comment'),isCompleted,"Field required.");				
		// If there is an error, we don't submit
		var error = $('.form_error').length ? "Please complete all fields" : "";
		if (error) {
			var error_notice = 
					'<p class="error" id="error_notice">'
					+ error	+'</p>';
			$(this).addNotice($(error_notice),5000);
			return false;
		}
		var subfail = true;
		$.ajax({
		   type: "post",
		   url: "/library/processing/process_comment.php",
		   data: "mode=add&"+self.serialize(),
		   async: false,
		   success: function(msg){
				if (msg.substring(0,4)!="ERROR") {
					$(msg).appendTo('#comments ol:first');
					$(".c_none").remove();
					self.addNotice($('<p class="notice">Your comment has been added. If it does not immediately appear, it has been submitted for moderation.</p>'),10000);
					$('#c_comment').val("");							
				} else {
					//alert("error:" + msg);
					self.addNotice($('<p class="error">An error occurred. Please try again.</p>'),10000);
				}
				subfail = false;
			},
		   error: function(obj,text,error) { 
				self.addNotice($('<p class="error">An error occurred. Please try again.</p>'),10000); }
		 });
		 return subfail;
	});	
});
/* Form Checking - checks value and applies error classes/adds error as needed
 *	@param element		form element
 *	@param check		function to check form value against
 *	@param error		error message to display
 *	@param nullpasses	boolean for allowing empty field
*/
function checkField(element,check,error,nullpasses) {
	nullpasses = nullpasses ? element.val()=="" : false;
	element.removeClass('form_error');
	if (element.parent('p,li').children("span.error").length) 
			element.parent('p,li').children("span.error").remove();			
	if (nullpasses || check(element.val())) return true; 
	element.addClass('form_error');
	$('<span class="error">'+error+'</span>').appendTo(element.parent('p,li'));
	return false;
									
}
function isValidEmail(str) {
  var format = new RegExp("^[A-Za-z0-9._%+-]+@(?:[A-Za-z0-9-]+[.])+[A-Za-z]{2,4}$"); 
  var matches = format.exec(str);
  return matches;
}
function isValidUrl(str) {
  var format = new RegExp("^(|https?:\/\/[\w\-_]+(\.[\w\-_]+)+([\w\-\.,@?^=%&:/~\+#]*[\w\-\@?^=%&/~\+#])?)$"); 
  var matches = format.exec(str);
  return matches;
}
function isCompleted(str) {
  return str != "";
}
jQuery.fn.addNotice = function(element, time) {
	jQuery(this).before(element);
	element.animate({opacity:1},time,function(){
		jQuery(this).animate( {opacity: 0.0, height:0}, 1000, function() {jQuery(this).remove();})
	});
	return this;
}


$(function() {
// insert a button for interactive demo, then apply onClick event for popup.
$("#title-children-demo-code").after($("<p class='demo'><a href='#' id='title-children-demo'>Click here to see a demonstration of the code</a></p>"));
$("#title-children-demo").click(function(){
var titles = document.getElementsByTagName("title");
var kids = titles[0].childNodes;
alert(kids.length);
return false;
});
// insert a button for interactive demo, then apply onClick event to change title.
$("#title-demo-code").after($("<p class='demo'><a href='#' id='title-demo'>Click here to see a demonstration of the code</a></p>"));
$("#title-demo").click(function(){
var foo = document.title;
alert("The title was "+foo);
document.title = "Now it's been changed.";
alert(document.title);
return false;
});
});
 $(function(){ $('p.demo a').click(function(){ _gaq.push(['_trackEvent','Demo','click',$(this).attr("id")+"/"+document.location.pathname]);}); });
//]]>
