
	// Initialise Page
	$(document).ready(function()
	{
		// Initialise Slider
		if($(".slider").length > 0)
		{
			$(".slider ul li").each(function(Integer_Index)
			{
				$(this).css('marginLeft', ($(".slider").width() * Integer_Index) + "px");
			});

			$(".slider").append('<div>' + "&nbsp;" + '</div>');

			$(".slider div").html('<h2>' + $(".slider ul li img").eq(0).attr('title') + '</h2>' + '<p>' + $(".slider ul li img").eq(0).attr('alt') + '</p>');
			$(".slider div").fadeTo(500, 0.85, function()
			{
				$(this).unbind('click');
				$(this).bind('click', function()
				{
					window.location = $(".slider ul li img").eq(0).attr('href');
				});
			});

			$(".slider .previous").fadeTo(500, 0.25);

			// Assign Handlers
			$(".slider .previous").click(function(Event)
			{
				if(Event.originalEvent)
				{
					clearInterval(Interval_Slide);
				}

				var Integer_Offset = parseInt($(".slider ul").css('marginLeft').replace("px", "")) + 592;
				var Integer_Limit = 0;
				var Integer_Index = Math.round((Integer_Offset * -1) / $(".slider").width());

				if(Integer_Offset <= Integer_Limit)
				{
					$(".slider ul").animate({ 'marginLeft': "+=" + 592 }, 1000, function() {});
					$(".slider div").fadeOut(500, function()
					{
						$(this).html('<h2>' + $(".slider ul li img").eq(Integer_Index).attr('title') + '</h2>' + '<p>' + $(".slider ul li img").eq(Integer_Index).attr('alt') + '</p>');
						$(this).fadeTo(500, 0.85, function()
						{
							$(this).unbind('click');
							$(this).bind('click', function()
							{
								window.location = $(".slider ul li img").eq(Integer_Index).attr('href');
							});
						});
					});

					$(".slider .next").fadeTo(500, 1.0);

					if(Integer_Offset + 592 > Integer_Limit)
					{
						$(".slider .previous").fadeTo(500, 0.25);
					}
					else
					{
						$(".slider .previous").fadeTo(500, 1.0);
					}
				}

				return false;
			});

			$(".slider .next").click(function(Event)
			{
				if(Event.originalEvent)
				{
					clearInterval(Interval_Slide);
				}

				var Integer_Offset = parseInt($(".slider ul").css('marginLeft').replace("px", "")) - 592;
				var Integer_Limit = parseInt($(".slider").width() * $(".slider ul li").length) * -1;
				var Integer_Index = Math.round((Integer_Offset * -1) / $(".slider").width());

				if(Integer_Offset > Integer_Limit)
				{
					$(".slider ul").animate({ 'marginLeft': "-=" + 592 }, 1000, function() {});
					$(".slider div").fadeOut(500, function()
					{
						$(this).html('<h2>' + $(".slider ul li img").eq(Integer_Index).attr('title') + '</h2>' + '<p>' + $(".slider ul li img").eq(Integer_Index).attr('alt') + '</p>');
						$(this).fadeTo(500, 0.85, function()
						{
							$(this).unbind('click');
							$(this).bind('click', function()
							{
								window.location = $(".slider ul li img").eq(Integer_Index).attr('href');
							});
						});
					});

					$(".slider .previous").fadeTo(500, 1.0);

					if(Integer_Offset - 592 <= Integer_Limit)
					{
						$(".slider .next").fadeTo(500, 0.25);
					}
					else
					{
						$(".slider .next").fadeTo(500, 1.0);
					}
				}

				return false;
			});
			
			var Interval_Slide = setInterval(function()
			{
				var Integer_Offset = parseInt($(".slider ul").css('marginLeft').replace("px", "")) - 592;
				var Integer_Index = Math.round((Integer_Offset * -1) / $(".slider").width());
				var Integer_Slides = $(".slider ul li").length;

				if(Integer_Index < Integer_Slides)
				{
					$(".slider .next").click();
				}
				else
				{
					clearInterval(Interval_Slide);
				}

			}, 10000);
		}

		// Equalise Column Heights
		var Integer_Height = Math.max($(".column .yellow").height(), $(".column .blue").height(), $(".content").height() - 180);

		if(navigator.userAgent.toLowerCase().indexOf('chrome') > -1)
		{
			// Correct Google Chrome
			Integer_Height = Integer_Height + 120;
		}

		$(".column .blue").height(Integer_Height);
		$(".column .yellow").height(Integer_Height);
		
		if($(".panel.quotation").length < 1)
		{
			$(".column .yellow").height(Integer_Height + 145 + 4);
		}

		// Define Form Behaviours
		$("form[name=comment]").submit(function(Event)
		{
			var Array_Errors = new Array();

			if($("input[name=comment_name]").val() == "")
			{
				Array_Errors.push("- You have not entered your name");
			}

			if($("textarea[name=comment_content]").val().length < 5)
			{
				Array_Errors.push("- Your comment is too short or has not been entered");
			}

			if($("textarea[name=comment_content]").val().length > 1000)
			{
				Array_Errors.push("- Your comment is too long (1000 characters maximum)");
			}

			if(Array_Errors.length > 0)
			{
				alert("Sorry, we couldn't continue because" + "\n" + Array_Errors.join("\n"));
			}
			else
			{
				$.post("comment.php", { article_id: $("input[name=article_id]").val(), comment_name: $("input[name=comment_name]").val(), comment_content: $("textarea[name=comment_content]").val() }, function(Response)
				{
					switch(Response)
					{
						case "OK":

							alert("Thank you for your comment, it has been submitted for moderation and will appear shortly");

							window.location = 'grapevine.php?article=' + $("input[name=article_id]").val();

						break;

						default:

							alert("Sorry, we couldn't continue because there was a processing error. This may be a technical fault. Please try again, or contact us if the issue persists");

						break;
					}
				});
			}

			Event.preventDefault();

			return false;
		});

		$("form[name=register]").submit(function(Event)
		{
			var Array_Errors = new Array();

			if($("input[name=register_name]").val() == "")
			{
				Array_Errors.push("- You have not entered your name");
			}

			if($("input[name=register_email]").val().length < 5 || $("input[name=register_email]").val().indexOf("@") == -1)
			{
				Array_Errors.push("- Your e-mail address is missing or incorrect");
			}

			if(Array_Errors.length > 0)
			{
				alert("Sorry, we couldn't continue because" + "\n" + Array_Errors.join("\n"));
			}
			else
			{
				$.post("register.php", { register_name: $("input[name=register_name]").val(), register_email: $("input[name=register_email]").val() }, function(Response)
				{
					switch(Response)
					{
						case "OK":

							alert("Thank you for your interest!");

							window.location = window.location.pathname.split("/").pop();

						break;

						default:

							alert("Sorry, we couldn't continue because there was a processing error. This may be a technical fault. Please try again, or contact us if the issue persists");

						break;
					}
				});
			}

			Event.preventDefault();

			return false;
		});


		// Define Handlers
		$(".panel.grapevine").click(function()
		{
			window.location = "grapevine.php";
		});
		
		$(".header img.left").click(function()
		{
			window.location = "index.php";
		});


	});
