
var addthis_config =
{
   ui_click: true,
   ui_offset_left: -240,
   ui_offset_top: 116,
   ui_header_background: "#E9F4D3",
   services_compact: "twitter, facebook, google_plusone_badge, digg, stumbleupon, reddit, print, email",
   ui_hover_direction: 1,
   data_track_clickback: true
}



var article =
{
	init: function()
	{
		articleOptions.init();
		comments.init();
	}
};



var articleOptions = 
{
	init: function()
	{
		$("span.option a").click( function(e)
		{
			e.preventDefault();
			if(articleOptions.open) articleOptions.close();
			if($(this).hasClass("print"))
			{
				window.open($(this).attr("href"), "printfriendly", "menubar=1,resizable=1,location=1,status=1,scrollbars=1,width=500,height=600", '');
			}
			else
			{
				if($(this).hasClass("addthis_button") === false)
				{
					var where = $(this).attr("href");
					var show = $(this).parent().parent().next();
					$.ajax(
					{
						url: where,
						success: function(data)
						{
							show.html(data);
							show.animate({height:'show'}, prefs.transitionSpeed/2);
							articleOptions.open = true;
							articleOptions.current = show;
						},
						error: function(xhr, textStatus, errorThrown)
						{
							error.warn("Error: " + page.httpStatus(xhr.status));
						}
					});
				}
			}
		});
	},
	close: function()
	{
		this.current.animate({height:'hide'}, prefs.transitionSpeed/2);
		this.current = false;
		this.open = false;
	},
	current: false,
	open: false,
	show: function()
	{
		
	}
};




var comments =
{
	init: function()
	{
		$(".comment-reply-link").click( function(e)
		{
			if($("#cancel").length > 0) { comments.removeReply(); }
			var url = $(this);
			var title = $(this).parent().parent().children().html();
			$(".button").val("Reply To '" + title + "'");
			$("#insert-fields").html('<input type="hidden" name="parent" value="' + url.attr("rel") + '" />');
			$("#comment-form").append('<input type="button" class="button" name="cancel" value="cancel" id="cancel" />');
			$("#cancel").click( function(e) { comments.removeReply(); });
		});
		$(".login-elsewhere").click( function(e)
		{	
			e.preventDefault();
			$("html, body").animate({scrollTop:0}, 500);
		});
		$(".login-elsewhere").parent().next().children().click( function(e) { e.preventDefault(); $(".login-elsewhere").click(); });
	},
	removeReply: function()
	{
		$("#insert-fields").html("");
		$("#cancel").remove();
		$(".button").val("Post Comment");
	}
	
};



var addthisbutton = 
{
	init: function()
	{
		$(".addthis_button_expanded").click( function(e)
		{
			e.preventDefault();
			window.open($(this).attr("href"), "addthiswindow", "menubar=1,resizable=1,location=1,status=1,scrollbars=1,width=500,height=600", '');			
		});
	}
};


var recipeCategories =
{
	init: function()
	{
		this.setupList("div.recipe-categories ul li a");
	},
	setupList: function(listname)
	{
		$(listname).click( function(e)
		{
			e.preventDefault();
			recipeCategories.currentID = 0;
			$("div.recipe-categories").css("width", "800px");
			var u = prefs.basename + prefs.paths.resourceDirectory + prefs.paths.php + "recipes_listbycategory.php?c=" + escape($(this).html());
			$("#article").html("Loading...");
			$.ajax(
			{
				url: u,
				success: function(data)
				{
					$("#article").html(data);
					recipeCategories.setupNav();
					recipeCategories.setupList(".recipe-categories2 ul li a");
				},
				error: function(xhr)
				{
					error.warn(page.httpStatus(xhr.status) + "<br />" + u);
				}
			});
		});
	},
	setupNav: function()
	{
		if($("#recipes_list_by_category").height() < $(".recipes-inner").height())
		{
			$("div.next").html('<a href="#">next</a>');
			$("div.prev").html('<a href="#">previous</a>');
			$("div.next a").click( function(e)
			{
				e.preventDefault();
				if($("#row" + (recipeCategories.currentID+recipeCategories.increment)).length > 0)
				{
					var newMargin = parseInt($("#row" + recipeCategories.currentID).offset().top) - parseInt($("#row" + (recipeCategories.currentID+recipeCategories.increment)).offset().top);
					var oldMargin = $(".recipes-inner").css("margin-top") == 'auto' ? 0 : parseInt($(".recipes-inner").css("margin-top"));
					newMargin += oldMargin;
					recipeCategories.currentID += recipeCategories.increment;
				}
				else
				{
					newMargin = parseInt($(".recipes-inner").css("margin-top"));
				}
//				window.alert(newMargin);
				newMargin += "px";
				$(".recipes-inner").animate({marginTop:newMargin}, 500);
			});
			$("div.prev a").click( function(e)
			{
				e.preventDefault();
				if($("#row" + (recipeCategories.currentID-recipeCategories.increment)).length > 0)
				{
					var newMargin = parseInt($("#row" + recipeCategories.currentID).offset().top) - parseInt($("#row" + (recipeCategories.currentID-recipeCategories.increment)).offset().top);
					var oldMargin = $(".recipes-inner").css("margin-top") == 'auto' || $(".recipes-inner").css("margin-top") == 0 ? 0 : parseInt($(".recipes-inner").css("margin-top"));
					newMargin += oldMargin;
					recipeCategories.currentID -= recipeCategories.increment;
				}
				else
				{
					newMargin = 0;
				}
//				window.alert(newMargin);
				newMargin += "px";
				$(".recipes-inner").animate({marginTop:newMargin}, 500);
			});
		}
	},
	increment: 2,
	currentID: 0
};



var plantlibCategories =
{
	init: function()
	{
		this.setupList("div.plantlib_categories ul li a");
	},
	setupList: function(listname)
	{
		$(listname).click( function(e)
		{
			e.preventDefault();
			plantlibCategories.currentID = 0;
			$("div.plantlib_categories").css("width", "800px");
			var u = prefs.basename + prefs.paths.resourceDirectory + prefs.paths.php + "plantlib_listbycategory.php?c=" + escape($(this).html());
			$("#article").html("Loading...");
			$.ajax(
			{
				url: u,
				success: function(data)
				{
					$("#article").html(data);
					plantlibCategories.setupNav();
					plantlibCategories.setupList(".plantlib-categories2 ul li a");
				},
				error: function(xhr)
				{
					error.warn(page.httpStatus(xhr.status) + "<br />" + u);
				}
			});
		});
	},
	setupNav: function()
	{
		if($(".plantlib-container").height() < $(".plantlib-inner").height())
		{
			$("div.next").html('<a href="#">next</a>');
			$("div.prev").html('<a href="#">previous</a>');
			$("div.next a").click( function(e)
			{
				e.preventDefault();
				if($("#row" + (plantlibCategories.currentID+plantlibCategories.increment)).length > 0)
				{
					var newMargin = parseInt($("#row" + plantlibCategories.currentID).offset().top) - parseInt($("#row" + (plantlibCategories.currentID+plantlibCategories.increment)).offset().top);
					var oldMargin = $(".plantlib-inner").css("margin-top") == 'auto' || $(".plantlib-inner.margin")==0 ? 0 : parseInt($(".plantlib-inner").css("margin-top"));
					newMargin += oldMargin;
					plantlibCategories.currentID += plantlibCategories.increment;
				}
				else
				{
					newMargin = parseInt($(".plantlib-inner").css("margin-top"));
				}
				newMargin += "px";
				$(".plantlib-inner").animate({marginTop:newMargin}, 500);
			});
			$("div.prev a").click( function(e)
			{
				e.preventDefault();
				if($("#row" + (plantlibCategories.currentID-plantlibCategories.increment)).length > 0)
				{
					var newMargin = parseInt($("#row" + plantlibCategories.currentID).offset().top) - parseInt($("#row" + (plantlibCategories.currentID-plantlibCategories.increment)).offset().top);
					var oldMargin = $(".plant-inner").css("margin-top") == 'auto' || $(".plantlib-inner").css("margin-top") == 0 ? 0 : parseInt($(".plantlib-inner").css("margin-top"));
					newMargin += oldMargin;
					plantlibCategories.currentID -= plantlibCategories.increment;
				}
				else
				{
					newMargin = 0;
				}
				newMargin += "px";
				
				$(".plantlib-inner").animate({marginTop:newMargin}, 500);
			});
		}
	},
	increment: 2,
	currentID: 0
};




var qandaCategories =
{
	init: function()
	{
		this.setupList("div#qanda-categories ul li a");
	},
	setupList: function(listname)
	{
		$(listname).click( function(e)
		{
			e.preventDefault();
			qandaCategories.currentID = 0;
			$("div#qanda-categories").css("width", "800px");
			var u = prefs.basename + prefs.paths.resourceDirectory + prefs.paths.php + "qanda_listbycategory.php?c=" + escape($(this).html());
			$("#article").html("Loading...");
			$.ajax(
			{
				url: u,
				success: function(data)
				{
					$("#article").html(data);
					qandaCategories.setupNav();
					qandaCategories.setupList("div#qanda-categories2 ul li a");
				},
				error: function(xhr)
				{
					error.warn(page.httpStatus(xhr.status) + "<br />" + u);
				}
			});
		});
	},
	setupNav: function()
	{
		if($(".qanda-container").height() < $(".qanda-inner").height())
		{
			$("div.next").html('<a href="#">next</a>');
			$("div.prev").html('<a href="#">previous</a>');
			$("div.next a").click( function(e)
			{
				e.preventDefault();
				if($("#row" + (qandaCategories.currentID+qandaCategories.increment)).length > 0)
				{
					var newMargin = parseInt($("#row" + qandaCategories.currentID).offset().top) - parseInt($("#row" + (qandaCategories.currentID+qandaCategories.increment)).offset().top);
					var oldMargin = $(".qanda-inner").css("margin-top")=='auto' || $(".qanda-inner").css("margin-top")==0 ? 0 : parseInt($(".qanda-inner").css("margin-top"));
					newMargin += oldMargin;
					qandaCategories.currentID += qandaCategories.increment;
				}
				else
				{
					newMargin = $(".qanda-inner").css("margin-top");
				}
				newMargin += "px";
				$(".qanda-inner").animate({marginTop:newMargin}, 500);
			});
			$("div.prev a").click( function(e)
			{
				e.preventDefault();
				if($("#row" + (qandaCategories.currentID-qandaCategories.increment)).length > 0)
				{
					var newMargin = parseInt($("#row" + qandaCategories.currentID).offset().top) - parseInt($("#row" + (qandaCategories.currentID-qandaCategories.increment)).offset().top);
					var oldMargin = $(".qanda-inner").css("margin-top")=='auto' || $(".qanda-inner").css("margin-top")==0 ? 0 : parseInt($(".qanda-inner").css("margin-top"));
					newMargin += oldMargin;
					qandaCategories.currentID -= qandaCategories.increment;
				}
				else
				{
					newMargin = 0;
				}
				newMargin += "px";				
				$(".qanda-inner").animate({marginTop:newMargin}, 500);
			});
		}
	},
	increment: 2,
	currentID: 0
};









