var Attachments = new Array();
var ExternalImages = new Array();

function displayAttachments(postid){
	var atdisplay = $("#atdisplay" + postid);
	var pat = $("#pat" + postid);

	if (atdisplay.length == 0 || pat.length == 0) {
		return;
	}

	pat.css('display', '');
	atdisplay.css('display', 'none');

	for (var attid in Attachments[postid]) {
		var att = $("img#atimg" + attid);
  		if (att.length > 0) {
			att.attr('src', Attachments[postid][attid]);
		}
 	} 
}

function displayExternalImages(postid) {
	var ateidisplay = $("#ateidisplay" + postid);
	if (ateidisplay.length == 0) {
		return;
	}

	for (var atteiid in ExternalImages[postid]) {
		var attei = $("img#ateiimg_" + postid + "_" + atteiid);
  		if (attei.length > 0) {
			attei.attr('src', ExternalImages[postid][atteiid]);
			setExternalImageEvents(postid, attei);
		}
	}

 	ateidisplay.css('display', 'none');
}

function setExternalImageEvents(postid, picture) {
	$(picture).load(function() {
		$(this).toggleClass('externalImage');
		width = $(this).width();
		$(this).toggleClass('externalImage');

		if (width > 640) {
			var imageInfoId = $(this).attr('id') + '_info';
			var imageInfoFullId = imageInfoId + '_full';
			var imageInfoLinkId = imageInfoId + '_link';

			$(picture).mouseover(function() {
				$(this).css('cursor', 'ne-resize');
				$(this).css('opacity', '0.8');
			});

			$(picture).mouseleave(function() {
				$(this).css('opacity', '');
				$(this).css('cursor', '');
			});
	
			$(this).click(function() {
				postMessageBoxWidth = $('div#post_message_' + postid).width();
				var totalMaxWidth = postMessageBoxWidth + 'px';

				if ($(this).css('maxWidth') == totalMaxWidth) {
					$(this).animate({maxWidth: '640px'}, 300);
				} else {
					$(this).animate({maxWidth: totalMaxWidth}, 300);
				}

				return false;
			});
		}	
	});
}

function displayAllAttachments(){
	for (var postid in Attachments) {
		displayAttachments(postid);
	}
}

function displayAllExternalImages(){
	for (var postid in ExternalImages) {
		displayExternalImages(postid);
		
	}
} 
