$(document).ready(function() {
	
	var i = 0;
	$('div.group-by-date div.item').each(function(){
	
		i++;
		
		var parent = $(this).parent();
		
		var title			= $(this).find('h5 a');
		var hour			= $(this).find('p.listview-date-start');
		var date			= $(this).parent().find('h4');
		var place			= $(this).find('p.listview-place');
		var theme			= $(this).find('p.listview-theme');
		var img 			= $(this).find('img');
		var content			= $(this).find('div.listview-content');
		var content_detail	= $(this).find('div.listview-content-details');
		var html 			= $(this).find('div.listview-content-html');
		var css				= $(this).attr('class');
		
		// After !!!
		if ($(hour).text() == '23h55')
		{
			$(hour).text('After');
		}
		var c_html 				= $(html).html() != null ? $(html).html() : '';
		var c_content 			= $(content).html() != null ? $(content).html() : ''; 
		var c_content_details 	= $(content_detail).html() != null ? $(content_detail).html() : ''; 
		
		var c_sep = $(hour).text() == '' ? '&#149; ' : ' : ';
		
		// Link in the box
		var link	= 
			'<h5 class="' + css + '"><a href="#TB_inline?height=490&width=720&inlineId=inlineContent'+i+'" class="thickbox">'+
			'<span class="hour">' + $(hour).text() + '</span>' +
			c_sep +
			$(title).text()+
			'</a></h5>';
			
		$(this).remove();
		
		// Contruct content for Thickbox
		var construct =	
		'<p class="theme ' + $(theme).text() + '">' + $(theme).text() + '</p>' + 
		'<p class="date">' + $(date).text() + '</p>' + 
		'<p class="place hour ' + css + '">' + $(hour).text() + c_sep + $(place).text() + '</p>' +
		'<p class="img"><img src="' + $(img).attr('src') + '" /></p>' + 
		'<div class="content"><h5>' + $(title).text()  + '</h5>' + c_content + '<div class="content-details">' + c_content_details + '</div>' + c_html + '</div>'
		'<div class="clearer"></div>';
		
		// Append DOM
		var div 	= $('<div>').hide().html(construct).attr('id', 'inlineContent' + i).attr('class', css);
		$(parent).append(link).append(div);

	});
	
	// init thickbox after DOM rebuild
	tb_init('a.thickbox, area.thickbox, input.thickbox');
	

	var i = 0;
	$('div.group-by-date').each(function(){
		i++;
		if (i == 3 || i == 6 || i == 9 || i == 12)
		{
			$(this).addClass('last');
		}
	});
	
});
