$(function() {
	var pageContent = $("#content");
	var rightbarContent = $("#rightbarcontent");

	// resize images
	var max_width = 500; 	//Sets the max width, in pixels, for every image
												// 500 // 322
	
	$("#content img:not(.noscale)").each(function(){
		var width = $(this).width();
		var height = $(this).height();
		if (width != max_width) {
			var ratio = (height / width );
			var new_width = max_width;
			var new_height = (new_width * ratio);
			
			$(this).height(new_height).width(new_width);
		} //ends if statement
	} //ends each function
	);


//	$("a.navlink").click(
//		function(objEvent) {
//			var jLink = $(this);
//			var jUrl = "/content/"+jLink.attr("title")+".php";
//			pageContent.load(jUrl, function() {
//				$(this).hide().slideDown(1000)
//			});
//			return(false);
//		});

//	$("a.commentspopup").click(
//		function(objEvent) {
//			var jLink = $(this);
//			var jUrl = "news/news.php?fn_id="+jLink.attr("id")+"&fn_mode=comments";
//			var commentdiv = "."+jLink.attr("id");
//			var commentContent = $(commentdiv);
//			// alert(commentdiv);
//			commentContent.load(jUrl, function() {
//				$(this).hide().slideDown(1000)
//			});
//			return(false);
//		});

	$('#postcomment').ajaxForm({
		target:					'#postcomment',			// target element(s) to be updated with server response
		// beforeSubmit:		showRequest,				// pre-submit callback
		success:				formSuccess,				// post-submit callback
		url:						'ajax_post.php',		// override for form's 'action' attribute
		//	type:      type        // 'get' or 'post', override for form's 'method' attribute
		//	dataType:  null        // 'xml', 'script', or 'json' (expected server response type)
		//	clearForm: true        // clear all form fields after successful submit
		//	resetForm: true        // reset the form after successful submit
		
		//	$.ajax options can be used here too, for example:
		//	timeout:   3000
	});

	$('#commentform').ajaxForm({
		// target:					'#postcomment',			// target element(s) to be updated with server response
		// beforeSubmit:		showRequest,				// pre-submit callback
		// success:				formSuccess,				// post-submit callback
		// url:						'ajax_post.php',		// override for form's 'action' attribute
		//	type:      type        // 'get' or 'post', override for form's 'method' attribute
		//	dataType:  null        // 'xml', 'script', or 'json' (expected server response type)
		//	clearForm: true        // clear all form fields after successful submit
		//	resetForm: true        // reset the form after successful submit
		
		//	$.ajax options can be used here too, for example:
		//	timeout:   3000
	});

	$("a.rightbarnav").click(
		function(objEvent) {
			var jLink = $(this);
			var firstPost = $("#rightbarcontent p.shoutpost:first").attr("id").split("_");
			var lastPost = $("#rightbarcontent p.shoutpost:last").attr("id").split("_");
			firstPost = firstPost[1];
			lastPost = lastPost[1];
			var totalPosts = $("#rightbarcontent p.shoutpost").size();
			var jUrl = "/shoutbox_display.php?startpost=";
			if (jLink.attr("id") == "rightbarnewer")
			{
				if (firstPost == 1)
				{
					return(false)
				}
				else if (firstPost <= 10)
				{
					jUrl += 1;
				}
				else
				{
					jUrl += (firstPost-10);
				}
			}
			else if (jLink.attr("id") == "rightbarnewest")
			{
				if (firstPost == 1)
				{
					return(false);
				}
				else
				{
					jUrl += 1;
				}
			}
			else if (jLink.attr("id") == "rightbarolder") 
			{
				if (totalPosts == 11)
				{
					jUrl += lastPost;
				}
				else
				{
					return(false);
				}
			}
			else if (jLink.attr("id") == "rightbaroldest")
			{
				if (totalPosts == 11)
				{
					jUrl += "lastpage";
				}
				else
				{
					return(false);
				}
			}
			rightbarContent.load(jUrl, function () {
				$(this).hide().fadeIn(1000)
			});
			return(false);
		});

	// pre-submit callback
	function showRequest(formData, jqForm, options) {
	    // formData is an array; here we use $.param to convert it to a string to display it
	    // but the form plugin does this for you automatically when it submits the data
	    var queryString = $.param(formData);
	
	    // jqForm is a jQuery object encapsulating the form element.  To access the
	    // DOM element for the form do this:
	    // var formElement = jqForm[0];
	
	    alert('About to submit: \n\n' + queryString);
	
	    // here we could return false to prevent the form from being submitted;
	    // returning anything other than false will allow the form submit to continue
	    return true;
	}

	function formSuccess() {
		var jUrl = "/shoutbox_display.php?startpost=1";
		rightbarContent.load(jUrl, function () {
			$(this).hide().fadeIn(1000)
		});
		return(false);
		// alert('success post');
	}
});
