// converts titles of img.imgcap eles to captions

img_eater = {
	// setup onload handlers for imgs
	watch_imgs: function() {
		var contentdiv = document.getElementById('content');
		
		$('img.imgcap', contentdiv).one('load', function() {
			img_eater.swallow_tag(this);
		}).each(function() {
			if(this.complete || (jQuery.browser.msie && parseInt(jQuery.browser.version) == 6))
				$(this).trigger('load');
		});;

		$('img', contentdiv).one('load', function() {
			img_eater.make_clickable(this);
		}).each(function() {
			if(this.complete || (jQuery.browser.msie && parseInt(jQuery.browser.version) == 6))
				$(this).trigger('load');
		});
	},

	// add onclick event to large images
	make_clickable: function(img) {
		if(img.width <= 600) return;

		var cdiv = $(img).wrap('<div />').parent();
		$(cdiv).append('Click for full-size image (' + img.width + 'x' + img.height + ')');
		$(cdiv).css({ 'font-size': '10px', 'cursor': 'pointer', 'text-align': 'right' });
		$(img).css('width', $(cdiv).parent().width() + 'px').css('height', 'auto');

		var asub = function() { window.open(img.src, 'saimg_fullsize', 'menubar=0,resizable=1,width='+(img.width + 50)+',height='+(img.height + 50)); };
		$(img).click(asub);
		$(cdiv).click(asub);
	},

	// pass img node, creates div with img and caption div as children
	swallow_tag: function(img) {
		var title = $(img).attr('title');
		if(title.length < 1) return; // only wrap images that have a title set

		var cdiv = $(img).wrap('<div />').parent();
		$(cdiv)
			.append($('<div />').append(title))
			.attr('class', $(img).attr('class'))
			.css('width', img.offsetWidth + 'px');

		$(img).removeClass('imgcap');

		var c = $(img).attr('class');
		var styles = (c == "") ? [] : c.split(/\s+/g);

		for(var j = 0; j < styles.length; j++) {
			switch(styles[j]) {
				case 'right': $(cdiv).css({ marginLeft:'8px', clear:'both' }); break;
				case 'left': $(cdiv).css({ marginRight:'8px', clear:'both' }); break;
				case 'center':
					break;
			}
		}
	}
};

$(document).ready(function() {
	img_eater.watch_imgs();
});


