$(function(){
	//
	var offsets = 0;
	
	function eventNavigation() {
		//show activity indicator
		$('span#activityIndicator').show();
		
		//get next event through AJAX
		var offsets = parseInt($('input#eventPageNumber').attr('value'));
		
		var action;
		var navID = $(this).attr('id');
		
		if (navID == 'nextEvent') {
			action = "next";
			offsets = offsets + 1;
		} else {
			action = "previous";
			offsets = offsets - 1;
		}
		
		if (offsets < 0) offsets = 0;
		
		//alert(offsets);
		$('input#eventPageNumber').val(offsets);
		
		//alert($('input#offset').val());
		
		data = "action=" + action + "&offset=" + offsets;
		$.getJSON(templateDirectory + "/ajax/processors/ajax.getEvent.php?" + data + "&callback=?", {rnd: Math.random()}, 
						function(data){
							//update event title and description
							$('h2#eventTitle').html(data.key.eventTitle);
							$('p#eventDescription').html(data.key.eventDescription);
							
							//update event month and date (begins)
							$('span#month').html(data.key.eventMonthBegin);
							$('span#date').html(data.key.eventDateBegin);
							
							if (offsets > parseInt(data.key.eventCount) - 1)
								offsets = parseInt(data.key.eventCount) - 1;
							
							$('input#eventPageNumber').val(offsets);
							
							if (data.key.showNext == "1") {
								$('span#nextEventWrapper').html("");
								$('span#nextEventWrapper').append('| <a href="#" id="nextEvent" class="eventNavigation">NEXT</a>');
							} else {
								$('span#nextEventWrapper').html("");
							}
							
							if (data.key.showPrev == "1") {
								$('span#prevEventWrapper').html("");
								$('span#prevEventWrapper').append('<a href="#" id="prevEvent" class="eventNavigation">PREV</a>');
							} else {
								$('span#prevEventWrapper').html("");
							}
							
							$('a.eventNavigation').click(eventNavigation);
							//hide activity indicator
							$('span#activityIndicator').hide();
						});
		/*
		$.ajax({
			url: templateDirectory + "/ajax/processors/ajax.getEvent.php",
			beforeSend: function(x) {
			  if(x && x.overrideMimeType) {
			   x.overrideMimeType("application/j-son;charset=UTF-8");
			  }
			 },
			data: "action=" + action + "&offset=" + offsets,
			success: function(data) {
				alert(data);
				var jSON = eval("(" + data + ")");
				alert(jSON.result[0].eventTitle);
		
				//update event title and description
				$('h2#eventTitle').html(jSON.result[0].eventTitle);
				$('p#eventDescription').html(jSON.result[0].eventDescription);
				
				//update event month and date (begins)
				$('span#month').html(jSON.result[0].eventMonthBegin);
				$('span#date').html(jSON.result[0].eventDateBegin);
				
				if (offsets > parseInt(jSON.result[0].eventCount) - 1)
					offsets = parseInt(jSON.result[0].eventCount) - 1;
				
				$('input#eventPageNumber').val(offsets);
				
				//hide activity indicator
				$('span#activityIndicator').hide();
			},
			error: function(err) {
				alert(err);
			},
			dataType: "json"
		});*/
		
		return false;
	}
	
	$('a.eventNavigation').click(eventNavigation);
	//$('input#eventPageNumber').attr('value','0');
	
});