// JavaScript Document
//http://church.princeofpeace.org/wbsvcbiblestudieslist/

$(document).ready(function()
{
	var list_type = $('.bible-study-list').attr('list-type');
	
	if($('.bible-study-list'))
		getEvents(list_type);
	
});

function getEvents(list_type)
{
	var json = list_type == null ? false : '{"type":"'+list_type+'"}';
	
	$.post('http://church.princeofpeace.org/wbsvcbiblestudieslist/', { data: json },
		function(data)
		{
			$('.bible-study-list').empty().html('<table><thead><tr><th colspan="5" id="date_range"><h3>Bible Studies</h3></th></tr><tr><th>Day</th><th>Time</th><th>Topic</th><th>Location</th><th>Organizer</th></tr></thead><tbody></tbody></table>');
			insertStudies(data);
			setupEffects();
		}, "json");
}

function setupEffects()
{
	var days = ['Sunday','Monday','Tuesday','Wednesday','Thursday','Friday','Saturday'],
			style = 'light_grey',
			current = 'Sunday';
	
	for(var i = 0;i < days.length;i++)
	{
		if(days[i] != current && $('.bible-study-list TABLE TBODY TR.'+days[i]).size() > 0)
			style = style == 'light_grey' ? 'dark_grey' : 'light_grey';
			
		current = days[i];
		
		$('.bible-study-list TABLE TBODY TR.'+days[i]+':first').addClass(style).siblings('.'+days[i]).each(function()
		{
			$(this).addClass(style).children('TD:first').empty();
		});
	}
	//alert('setting up effects...');
}

/* inserts the returned events into the calendar */
function insertStudies(data)
{
	var info = '',
			num = data.events.length,
			rows = new Array(),
			$obj = $('.bible-study-list TABLE TBODY');
	
	$('#date_range H3').empty().html('Bible Studies from '+data.start+' to '+data.end);
	
	for(var i = 0;i < num;i++)
	{
		var ev = data.events[i],
				id = ev.id,
				name = decodeURIComponent(ev.friendly_name),
				url = decodeURIComponent(ev.url),
				time = decodeURIComponent(ev.time),
				day = ev.day,
				day_value = ev.day_value,
				desc = decodeURIComponent(ev.short_description) != 'undefined' ? decodeURIComponent(ev.short_description) : '';
				address = decodeURIComponent(ev.address),
				study_location = decodeURIComponent(ev.study_location),
				coordinator = ev.coordinator,
				email = ev.email && ev.email != 'undefined' ? ev.email : '',
				html = '<tr class="'+day+'" value="'+day_value+'"><td>'+day+'</td><td>'+time+'</td><td class="topic" title="'+desc+'"><a href="'+url+'">'+name+'</a></td><td class="location" title="'+address+'">'+study_location+'</td><td><div>'+coordinator+'</div><div>'+email+'</div></td></tr>';
				//html = '<div class="inside_event'+tags+'" event_id="e_'+ev.id+'" title="'+title+'"><div class="event_name"><a href="'+ev.url+'">'+decodeURIComponent(ev.name)+'</a></div></div>';
		
			//$obj.append(html);
		if(day)
			rows[i] = new rowSortConstructor(day_value,encodeURIComponent(html));
	}
	
	rows.sort(function(a,b)
	{
		var x = a.day_value,
				y = b.day_value;
		
		return ((x < y) ? -1 : (x > y ? 1 : 0));
	});
	
	for(var i = 0;i < rows.length;i++)
		$obj.append(decodeURIComponent(rows[i].html));
}

function rowSortConstructor(day_value,html)
{
	this.day_value = day_value,
	this.html = html;
}
