/*
	Functions relating to guestbook
*/

SCRIPT_URL = "../../cgi-bin/passthru_to_act_on_sagan.cgi";
//--------------------------------------------------------

currpage = 0;
whichPage = 0;
numpages = 0;
debug = false;

function gb_init() {
	gb_requestPage(0);

	$('#gb_newEntry_link').click(function() {
			$('#gb_form_container').slideDown();
			$('#gb_newEntry_link').slideUp();
			$('#gb_message').fadeOut();
		}
	);

	$('#gb_cancel_link').click(function() {
			$('#gb_form_container').slideUp();
			$('#gb_newEntry_link').slideDown();		
		}
	);

	$('#gb_next_page_link').click(function() { gb_changePage('next'); } );
	$('#gb_prev_page_link').click(function() { gb_changePage('prev'); } );
	$('#gb_first_page_link').click(function() { gb_changePage('first'); } );
	$('#gb_last_page_link').click(function() { gb_changePage('last'); } );

	$('#gb_form').validate();
	$('#gb_form').ajaxForm({
//		url: 		SCRIPT_URL,
		type: 		'post',
		dataType: 	'json',
		success: 	gb_processSubmit
	});
}

function gb_processSubmit(json_response) {
	$('#gb_message').html(json_response.message);
	$('#gb_message').fadeIn();
	if (json_response.state == 'success') {
		$('#gb_form').clearForm();
		gb_requestLast();
		$('#gb_newEntry_link').slideDown();
		$('#gb_form_container').slideUp();
	}
}

function gb_requestPage(reqd_page) {
	if (debug) { alert('gb_requestPage: '+reqd_page); }
	$.getJSON(
		SCRIPT_URL,
		{ request:'json', page:reqd_page },
		function(pagedata) {
			gb_update(pagedata);
		}
	);				
}

function gb_requestLast() {
	$.getJSON(
		SCRIPT_URL,
		{ 	request:'json',
			purpose:'rnpdata',
			stupidie:new Date().getTime()
			// If 'stupidie' isn't sent, then ie uses a cached response
			// instead of sending a request like it should. Stupid IE.
		},
		function(json) {
			gb_requestPage(json.numpages-1);
		}
	)
}

function gb_update(posts) {
	if (debug) { alert('gb_update'); }

	$('#gb_entries').slideUp('slow', function() {
		content = '';

		for (i=0; i<posts.numentries; i++) {
			//G'mornin!</div></div>
			content += 	'<div class="entry">'+
						'<div class="name">';
			if (posts.entries[i].pub_email == 1) {
				content += 	'<a href="mailto:'+posts.entries[i].email+'">'+
							posts.entries[i].name+'</a>';
			} else {
				content += posts.entries[i].name;
			}

			content += '</div><div class="message">'+
						posts.entries[i].message+'</div></div>';

						// attn'l closing divs for <name> and <entry>
		}

		$('#gb_entries').html(content);	// load in new content
		$('#gb_entries').slideDown();
		$('#gb_entries').show();

		currpage = Number(posts.page_requested);
		numpages = Number(posts.numpages-1);

		gb_updatePageNav();
	});
}

function gb_changePage(which) {
	if (debug) { alert('gb_changePage: '+which); }
	changePage = false;

	if (which == 'last' && currpage != numpages) {
		gb_requestLast();
	}
	else {
		if (which == 'next') {
			if (currpage < numpages) {
				whichPage = currpage+1;
				changePage = true;
			}
		}
		else if (which == 'prev') {
			if (currpage > 0) {
				whichPage = currpage-1;
				changePage = true;
			}
		}
		else if (which == 'first') 	{
			whichPage = 0;
			if (currpage != 0) { changePage = true;	}
		}

		if (changePage) {
			$.getJSON(
				SCRIPT_URL,
				{ request:'json', page:whichPage },
				function(pagedata) {
					gb_update(pagedata);
				}
			);
		}
	}
}

function gb_updatePageNav() {
	if (currpage != 0) {
		$('#gb_first_page_link').slideDown();
		$('#gb_prev_page_link').slideDown();
	}
	else {
		$('#gb_first_page_link').slideUp();
		$('#gb_prev_page_link').slideUp();		
	}

	if (currpage != numpages) {
		$('#gb_next_page_link').slideDown();
		$('#gb_last_page_link').slideDown();
	}
	else {
		$('#gb_next_page_link').slideUp();
		$('#gb_last_page_link').slideUp();
	}
}