$(document).ready(function () {
	var sections = ['.news', '.blogs', '.research', '.view'];

	function fix_section(section) {
		$(section + ' .htmlresults > li').each(function (i, e) {
			if ($('.media_thumbnail', e).length == 0 || $('.media_thumbnail img', e).get(0).width != 75) {
				$(e).append('<ul class="media_thumbnail"><li><img src="http://static.flickr.com/79/213744007_a62501c378_s.jpg" width="75" height="75"></li></ul>');
			}

			if ($.trim($('p', e).text()) == '') {
				$('p', e).text($('h4 a', e).text());
			}
		});
	}

	function make_initial_capital(section) {
		var first_li = $(section + ' li').get(0);
		var parent = $(first_li).parent();

		$(parent).before('<div class="cleared mount"><img class="initial-capital" src="' + $('img', first_li).attr('src').replace(/_s\.jpg$/, '_m.jpg') + '"/><h3><a href="' + $('h4 a', first_li).attr('href') + '">' + $('h4', first_li).text() + '</a></h3><p>' + $('p:eq(0)', first_li).html() + '</p><p>' + $('p:eq(1)', first_li).html() + '</p><div style="background:url(http://www.gnucitizen.org/styles/images/background.content.png)">&nbsp;</div></div>');

		$(first_li).remove();
	}

	for (var i = 0; i < sections.length; i++) {
		try {
			var section = sections[i];

			fix_section(section);

			if (section != '.view') {
				make_initial_capital(section);
			}
		} catch (e) {
			try { console.log(e) } catch (e) {}
		}
	}

	$('h4:contains(No results)').each(function (i, e) {
		$(e).css({'text-transform': 'lowercase', 'padding-left': '20px'}).html('<p><em>Oops! Something just went wrong.</em></p>');
	});

	$('.exploits li').each(function (i, e) {
		var t = $('p', e).text();

		$('h4 a', e).attr('title', t ? t : $('h4 a', e).text());
	});

	jQuery('[title]').Tooltip({showURL: false, showBody: false});

	$('.htmlresults h4 a').attr('target', '_blank');
});
