jQuery(document).ready(function() {

    /* Funkcja obsługująca błędy */
	function handleErrorFunction(response, errorBoxId)
	{
		var errMessage;
		if(response[0] === undefined)
		{
			errMessage = response.body.innerHTML;
			errMessage = '<table><tr><td class="Bold Error">Podczas zmiany hasła wystąpił niespodziewany błąd.<br />Proszę o pilny kontakt z administratorem.</td></tr><tr><td>' + errMessage +'</td></tr></table>';
		}
		else
		{
			if(typeof(response) === 'string')
			{
				errMessage = '<table><tr><td class="Bold Error">' + response +'</td></tr></table>';
			}
			else
			{
				if(response[1] === undefined)
				{
					errMessage = 'Brak odpowiedzi z serwera. Najprawdopodobniej został przekroczony dozwolony czas na załadowanie obrazka na serwer.';
				}
				else
				{
					errMessage = response[1];
				}
			}
			errMessage = '<table><tr><td class="Bold Error">' + errMessage +'</td></tr></table>';
		}
        var errorBox = $(errorBoxId);
		errorBox.html(errMessage);
		errorBox.show();
	}


    /* Kolejne wydarzenie */
	$('#nextEvent').live("click", function(){
        var nextEventId = $(this).attr('rel');

        if(nextEventId == 0)
            return;

        $.post("index.php?p=event&action=getJson",
            { id : nextEventId },
            function(response) {
                if(response[0] === undefined)
                    {
                        $('#changePasswordSuccessBox').hide();
                        handleErrorFunction(response, '#nextPrevEventErrorBox');
                        return;
                    }
                    else
                    {
                        if(response[0] === "success")
                        {
                            $('#nextPrevEventErrorBox').hide();

                            $('#nearestEvent').fadeOut('slow', function(){
                                $('#nearestEventDateBegin').html(response[1][0]['date_begin']);
                                $('#nearestEventTimeBegin').html(response[1][0]['time_begin']);
                                $('#nearestEventTitle').html(response[1][0]['title']);
                                $('#nearestEventSeeMore').attr('href', 'event,' + response[1][0]['id_event'] + '.html');
                                $('#nearestEvent').fadeIn();

                                var nearestEventEdit = $('#nearestEventEdit');
                                var nearestEventDelete = $('#nearestEventDelete');

                                if(nearestEventEdit.length)
                                    nearestEventEdit.attr('href', 'event,edit,' + response[1][0]['id_event'] + '.html');

                                if(nearestEventDelete.length)
                                    nearestEventDelete.attr('href', 'event,delete,' + response[1][0]['id_event'] + '.html');
                            });
                            
                            if(response[3] != 0)
                            {
                                $('#prevEvent').attr('rel', '' + response[3] + '');
                                $('#prevEvent').show();
                            }
                            else
                            {
                                $('#prevEvent').hide();
                                $('#prevEvent').attr('rel', '0');
                            }

                            if(response[4] != 0)
                            {
                                $('#nextEvent').attr('rel', '' + response[4] + '');
                                $('#nextEvent').show();
                            }
                            else
                            {
                                $('#nextEvent').hide();
                                $('#nextEvent').attr('rel', '0');
                            }
                        }
                        else
                        {
                            handleErrorFunction(response, '#nextPrevEventErrorBox');
                            return;
                        }
                    }
            },
            "json");

    });


    /* Poprzednie wydarzenie */
	$('#prevEvent').live("click", function(){
        var nextEventId = $(this).attr('rel');

        if(nextEventId == 0)
            return;

        $.post("index.php?p=event&action=getJson",
            { id : nextEventId },
            function(response) {
                if(response[0] === undefined)
                    {
                        $('#changePasswordSuccessBox').hide();
                        handleErrorFunction(response, '#nextPrevEventErrorBox');
                        return;
                    }
                    else
                    {
                        if(response[0] === "success")
                        {
                            $('#nextPrevEventErrorBox').hide();

                            $('#nearestEvent').fadeOut('slow', function(){
                                $('#nearestEventDateBegin').html(response[1][0]['date_begin']);
                                $('#nearestEventTimeBegin').html(response[1][0]['time_begin']);
                                $('#nearestEventTitle').html(response[1][0]['title']);
                                $('#nearestEventSeeMore').attr('href', 'event,' + response[1][0]['id_event'] + '.html');
                                $('#nearestEvent').fadeIn();
                            });

                            var nearestEventEdit = $('#nearestEventEdit');
                            var nearestEventDelete = $('#nearestEventDelete');

                            if(nearestEventEdit.length)
                                nearestEventEdit.attr('href', 'event,edit,' + response[1][0]['id_event'] + '.html');

                            if(nearestEventDelete.length)
                                nearestEventDelete.attr('href', 'event,delete,' + response[1][0]['id_event'] + '.html');

                            if(response[3] != 0)
                            {
                                $('#prevEvent').attr('rel', '' + response[3] + '');
                                $('#prevEvent').show();
                            }
                            else
                            {
                                $('#prevEvent').hide();
                                $('#prevEvent').attr('rel', '0');
                            }

                            if(response[4] != 0)
                            {
                                $('#nextEvent').attr('rel', '' + response[4] + '');
                                $('#nextEvent').show();
                            }
                            else
                            {
                                $('#nextEvent').hide();
                                $('#nextEvent').attr('rel', '0');
                            }
                        }
                        else
                        {
                            handleErrorFunction(response, '#nextPrevEventErrorBox');
                            return;
                        }
                    }
            },
            "json");

    });
		
});
