﻿// Shout box images/links
function shoutImages(makeScroll,toTop){
	var tmp, shoutWidget = $('.gpWidget-shout'),
	rex1 = /(\{(\w+?)\}:)?(http\:\/\/[\w\-\.]+\.[a-zA-Z]{2,3}(?:\/\S*)?(?:[\w])+)/g,
	rex2 = /(http\:\/\/[\w\-\.]+\.[a-zA-Z]{2,3}(?:\/\S*)?(?:[\w])+\.(?:jpg|png|gif|jpeg|bmp))/g;

	if (makeScroll) {
		shoutWidget.find('span[id*=xmlShouts]').addClass('scrollableShout');
	}
	if (toTop) {
		tmp = $('div[id*="_pnlNewShout"]');
		tmp.parent().prepend( tmp.html() + "<br><br>" );
		tmp.empty();
	}
	shoutWidget.find('.NormGridRow, .AltGridRow').each(function(j){
		var last = ' ', txt, shout = $(this);
		shout
		.find('div.pTop').addClass('shoutBody body'+j)
		.html(function(i,h){
			var txt, url = h.split(' '), image = h.match(rex2);
			console.log([h,image]);
			if (image !== null) {
				shout.addClass('shoutWithImage');
				$.each(image, function(i,img){
					h = h.replace(img, '<img src="' + img + '" alt="" />');
				});
			} else {
				$.each(url, function(j, u) {
					if (rex1.test(u)) {
						txt = u.split(':')[0] || ' ';
						if (txt.indexOf('}') >= 0) {
							u = u.replace(txt + ':', '');
							var x = 1;  // find open bracket (spaces in link name)
							while (txt.indexOf('{') < 0 && j-x > 0) {
								txt = url[j-x] + ' ' + txt;
								url[j-x] = '';
								x++;
							}
							txt = txt.replace(/[\{\}:]/g, '');
						} else {
							txt = '';
						}
						url[j] = '<a href="' + u + '">' + ((txt === '') ? 'link' : txt) + '</a>';
					}
				});
				h = url.join(' ');
			}
			return h;
		});
	}).find('.shoutBody img').click(function(){
		$.facebox({ image: $( this ).attr("src") });
	});
}
