function str_replace(haystack, needle, replacement) { 
	var temp = haystack.split(needle); 
	return temp.join(replacement); 
}

function makelink(str){
	str = str_replace(str, '#', '');
	str = str_replace(str, '.', '');
	str = str_replace(str, ',', '');
	str = str_replace(str, ':', '');
	str = str_replace(str, ';', '');
	str = str_replace(str, '?', '');
	str = str_replace(str, '!', '');
	str = str_replace(str, ' ', '-');
	str = str_replace(str, '_', '-');
	str = str_replace(str, ' - ', '-');
	str = str_replace(str, ' / ', '-');
	str = str_replace(str, '/', '-');
	str = str_replace(str, '\'', '');
	str = str_replace(str, '"', '');
	str = str_replace(str, '`', '');
	str = str_replace(str, '~', '');
	str = str_replace(str, '’', '');
	str = str_replace(str, '&amp;', 'and');
	str = str_replace(str, '&', 'and');
	return str.toLowerCase();
}

$(function() { 
	$.getJSON("http://www.tickethookups.com/json/event-list.action?profileId=20&callback=?",
	function(data) {
		if (data.events.length > 0) {
			var idx = 0;
			var key = data.events.length-1;
			var ttt = new Array();
			$.each(data.events, function(i, event) {
				var x = event.startDateTime.split(' ');
				var d = x[3].substring(0,3) + '. ' + x[4];
				var table = '\
				<a href="/event/' + makelink(event.title) + '_' + event.id + '/"><span class="date">' + d + '</span><span class="name">' + event.title + '</span></a>\
				';
				table = table + '';
				ttt[key] = table;
				key = key - 1;
			});
			for(idx = 0; idx<data.events.length; idx++){
				if(idx <= 3){
					$("#shorteventslist").append(ttt[idx]);
				}
			}
			$("#shorteventslist").append('<a href="/calendar/" class="full">View Full Calendar &raquo;</a>');
		} else {
			$("#shorteventslist").append('<i>No upcoming events at this moment.<br />Please stay in touch!</i>');
		}
	});
});
