/* Load the post. */
function jsforum_loadpost(msgId, type)
{
	if (msgId == 0 && type < 2) {
		return;
	}
	type = 1 * type;

	switch (type)
	{
		case 0: // "LOAD RIGHT" 2->1, load new into 2.
			$('.next').addClass('inactive');
			$('#post1').html($('#post2').html());
			jsforum_onloadpost(1);
			$('#post2').html('<br />').addClass('loading');
			jsforum_loadpostid(msgId, 2);
			break;
		case 1: // "LOAD LEFT", load both posts.
			$('.previous').addClass('inactive');
			$('.next').addClass('inactive');
			$('#post1').html('<br />').addClass('loading');
			$('#post2').html('<br />').addClass('loading');
			jsforum_loadpostid(msgId, 1);
			$('#post2').load('/jsforum/view/post/' + msgId + '/1', {}, function(m){jsforum_onloadpost(2, m)});
			break;

		case 2: // "NEXT" 2->1, load new into 2.
			$('.next').addClass('inactive');
			$('#post1').html($('#post2').html());
			jsforum_onloadpost(1);
			$('#post2').html('<br />').addClass('loading');
			jsforum_loadpostid(parseInt($('#post1 .post-nextid').html()), 2);
			break;
		case 3: // "PREV" 1->2, load new into 1.
			$('.previous').addClass('inactive');
			$('#post2').html($('#post1').html());
			$('#post1').html('<br />').addClass('loading');
			jsforum_onloadpost(2);
			jsforum_loadpostid(parseInt($('#post2 .post-previd').html()), 1);
			break;
	}
}

function jsforum_loadpostid(msgId, tgtId)
{
	// Look for the msgId.
	if ($('#preload-message-' + msgId).length !== 0) {
		$('#post' + tgtId).addClass('load-message-' + msgId);
		if ($('#preload-message-' + msgId).html().length != 0) {
			$('#post' + tgtId).html($('#preload-message-' + msgId).html());
			jsforum_onloadpost(tgtId);
			$('#post' + tgtId).removeClass('load-message-' + msgId);
		}
		return;
	}

	$('#post' + tgtId).load('/jsforum/view/post/' + msgId, {}, function(m){jsforum_onloadpost(tgtId, m)});
}

function jsforum_loadtimeline(pd)
{
    var flashvars = {
        postData: pd
    };
    var params = {
        quality: "high",
        scale: "noscale",
        wmode: "transparent"
    };

    swfobject.embedSWF("/_ui/srawn/swf/timeline.swf", "flash_timeline", "829", "160", "8.0.23", "/_ui/srawn/swf/expressintall.swf", flashvars, params);
}

function jsforum_onpreload(msgId)
{
	$('.load-message-' + msgId).each(function(){
		var id = parseInt($(this).eq(0).attr('id').replace(/[^0-9]/g, ''));
		jsforum_loadpostid(msgId, id);
	});
}

function jsforum_onloadpost(posId, html)
{
	$('#post' + posId).removeClass('loading');

	// Cache the current message.
	var msgId = $('#post' + posId + ' .post-id').html();
	if (msgId == '') {
		$('#post').html(html);
		return jsforum_onloadpost(posId);
	}
	if ($('#preload-message-' + msgId).length == 0) {
		$(document.body).append('<div style="display:none" id="preload-message-' + msgId + '">' + $('#post' + posId).html() + '</div>');
	}

	switch (posId) {
		case 1:
			// Get the date.
			$('#postHeaderDate').html('Posts for: ' + $('#post1 .post-date').html());

			// Update prev button.
			msgId = $('#post1 .post-previd').html();
			if (msgId == '0' || isNaN(msgId) || msgId === null) {
				$('.previous').addClass('inactive');
			} else {
				$('.previous').removeClass('inactive');
				if ($('#preload-message-' + msgId).length == 0) {
					$(document.body).append('<div style="display:none" id="preload-message-' + msgId + '"></div>');
					$('#preload-message-' + msgId).load('/jsforum/view/post/' + msgId, {}, function(){jsforum_onpreload(msgId)});
				}
			}
			break;
		case 2:
			// Set the current date.
			$('#jsforumActiveDate').html($('#post2 .post-date').html());

			// Update next button.
			msgId = $('#post2 .post-nextid').html();
			if (msgId == '0' || isNaN(msgId) || msgId === null) {
				$('.next').addClass('inactive');
			} else {
				$('.next').removeClass('inactive');
				if ($('#preload-message-' + msgId).length == 0) {
					$(document.body).append('<div style="display:none" id="preload-message-' + msgId + '"></div>');
					$('#preload-message-' + msgId).load('/jsforum/view/post/' + msgId, {}, function(){jsforum_onpreload(msgId)});
				}
			}

			// Convert to the YYYYMMDD format and pass to the Flash
//			$('#jsforumTimeline').eq(0).setActiveDate($('#post2 .YYYYMMDD').html());
			break;
	}

	weatherbynation_ready();
}

function weatherbynation_newTopic_ajax(toporbot) {
	$('#newTopicButtonAjax').toggleClass('active');
	$('#newTopicList' + toporbot).toggle();
	$('#newTopicList' + toporbot + ' ul#newTopicMenu li').hover(function() {
		$(this).addClass('hover');
	},function() {
		$(this).removeClass('hover');
	});
}

function weatherbynation_show_smf_image_selector(linkId) {
 	$.getJSON('/image/ajax/own_images?small=AjaxThumb&large=preview', function(albums) {
		var images_per_row = 5;
		var link = $('#' + linkId);
		link.hide();
		var div = $('<div id="spikeCamp_ajaxImageChooser" />').insertAfter(link);
		for (var i = 0; i < albums.length; ++i) {
			var album = albums[i];
			var content = '<span class="album">Album: ' + album.title;
			if( album.privacy ) {
				content = content+' (Private)';
			}
			content = content+'</span><ul class="album">';
			for (var j = 0; j < album.images.length; ++j) {
				var prefix = '<li class="image';
				if (j % images_per_row == 0) {
					prefix = prefix+' break';
				}
				prefix = prefix+'">';
				var image = album.images[j];
				var url = "/image/tid/" + album.tid + "?startId=" + image.nid;
				var href = "javascript:surroundText('[url=" + url + "][img]" + image.largeURL + "[/img][/url]', '', document.forms.postmodify.message);";
				var markup = '<a class="image" href="'+ href +'" title="'+image.title+'">';
				markup = prefix+markup+'<img src="'+ image.smallURL +'" height="57" /></a></li>';
				content = content+markup;
			}
			content = content+'</ul>';
			$(content).appendTo(div);
		}
		if (div.children().length == 0) {
			div.remove();
			$('<span>You currently have no images uploaded.</span>').insertAfter(link);
		}
	});
}

