$(document).ready(function() {

    // add thickbox to images on the entry details page
    $('#more img.mt-image-left').each(function() {
    	var thisWidth = $(this).width();
    	if (thisWidth == 75) {
	   		var thumbSrc = $(this).attr('src');
	   		var regex = /\/[^/]*\.jpg$/;
	   		thumbSrc = thumbSrc.match(regex).toString();
	   		regex = /.*(?=-thumb)/;
	   		var thumbSrc = thumbSrc.match(regex).toString();
	   		thumbSrc = '/content/photo-gallery' + thumbSrc + '.jpg';
	   		$(this).parent().removeAttr("onclick").attr("rel", "thisGallery").attr("class", "thickbox").attr('href',thumbSrc);
	   		
	   		// do captions
	   		regex = /[^/]*$/
	   		var descSrc = thumbSrc.match(regex).toString();
	   		descSrc = $("p.lightbox-desc:contains("+descSrc+")").attr('title');
	   		$(this).parent().attr('title',descSrc);
   		}
   	});
   	
	// make the excerpt thumbnails link to the article
	
	$('div.changethehref').each(function() {
		var hrefID = $(this).attr('id');
   		var regex = /[^-][0-9]*$/;
   		hrefID = hrefID.match(regex).toString();
   		var ourAnchor = $('#title-' + hrefID).attr('href');
   		$('#excerpt-' + hrefID + ' form a').removeAttr('onclick').attr('href', ourAnchor);
	});
	
	// add captions to all article images
	$('div.asset-body img').each(function() {
		var imglink = $(this).parent();
		if($(this).parent().parent().hasClass('mt-enclosure mt-enclosure-image') ==0) {
			$(imglink).wrap('<span class="mt-enclosure mt-enclosure-image" style="display: inline;" />');
		}
  });
	
	$('div.asset-body img').each(function() {
		var imgWidth = $(this).width();
		if (imgWidth > 200) {
			var imgSrc = $(this).attr('alt');
			var imgDesc = $('p.lightbox-desc:contains('+imgSrc+')').attr('title');
			var imgWidth = $(this).width();
			imgWidth = imgWidth + 8;
			if (imgDesc && imgDesc.length > 0) {
				var toAppend = '<p class="imgDesc" style="width: ' +imgWidth+ 'px;">'+imgDesc+'</p>';
				var thisFloat = $(this).css('float');
				if($(this).parent().parent('.mt-enclosure-image').length > 0) {
					$(this).css({'margin-bottom': '0', 'float': 'none'}).parent().parent('.mt-enclosure-image').append(toAppend).css({'display': 'block', 'float': 'right', 'margin-right': '20px', 'width': imgWidth});
				} else {
					$(this).css({'margin-bottom': '0', 'float': 'none'}).parent('.mt-enclosure-image').append(toAppend).css({'display': 'block', 'float': 'right', 'margin-right': '20px', 'width': imgWidth});
				}
			}
		}
		
	});
	
	//add some margin to all object tags in the blogs
	$('object').each(function(){
		var el = $(this);
		var side = el.attr('align');
		if (side == 'left')
			el.attr('style', 'margin: 10px 20px 10px 0; display: block; float: left;');
		else
			el.attr('style', 'margin: 10px 0px 10px 20px; display: block; float: right;');
		el.removeAttr('align');
	});
	
	//if it's floated, make it actually float correctly
	$('img.mt-image-right').each(function() {
		$(this).attr('style', 'float: right; margin-left: 20px; display: inline;');
	});
	$('img.mt-image-left').each(function() {
		$(this).attr('style', 'float: left; margin-right: 20px; display: inline;');
	});

});