$(document).ready(function () {
    var secondesInterval = 5000;

    //when the user click
    var clickToAction = false;
    var inProcess = false;

    $.ajax({
        type: "GET",
        url: "/home/controlinscriptionmanager.aspx",
        success: function (data) {
            $('#controlInscriptionManager').html(data);
        }
    });

    var timer = $.timer(secondesInterval, function (timer) {
        $('#annonce_inscription').loadAnnonce('timer');
    });

    $('body')
    .delegate('.ai_controller_play, .ai_controller_right, .ai_controller_left', 'click', function () {
        clickToAction = true;
    })
	.delegate('.ai_controller_play', 'click', function () {
	    $('#annonce_inscription').loadAnnonce('timer');
	    $(this).removeClass('ai_controller_play').addClass('ai_controller_pause');
	})
	.delegate('.ai_controller_pause', 'click', function () {
	    timer.stop();
	    $(this).removeClass('ai_controller_pause').addClass('ai_controller_play');
	})

	.delegate('.ai_controller_right', 'click', function () {
	    timer.stop();
	    $('.ai_controller_pause').removeClass('ai_controller_pause').addClass('ai_controller_play');
	    $('#annonce_inscription').loadAnnonce('1');
	})
	.delegate('.ai_controller_left', 'click', function () {
	    timer.stop();
	    $('.ai_controller_pause').removeClass('ai_controller_pause').addClass('ai_controller_play');
	    $('#annonce_inscription').loadAnnonce('-1');
	})
    .delegate('#imgPasserelle', 'click', function () {
        timer.stop();
        $('.ai_controller_pause').removeClass('ai_controller_pause').addClass('ai_controller_play');
    });


    var ajaxLoadAnnonce;
    var serverError = false;

    jQuery.fn.loadAnnonce = function (mode) {

        var element = $(this[0]);
        //        if (mode == 'changementRegion') {
        //            timer.stop();
        //        }
        if (mode == 'passerelle') {
            timer.stop();
        }

        if (mode == 'timer') {
            ajaxAnnonce();
        }
        else {
            element.parent().addClass('loadingContent').find('.ai_controller').fadeOut(500).end().end()
				.fadeOut(500, function () {
				    ajaxAnnonce();
				});
        }


        function ajaxAnnonce() {

            /*if (typeof ajaxLoadAnnonce != 'undefined' && ajaxLoadAnnonce != null) {
            ajaxLoadAnnonce.abort();
            }*/
            if (inProcess) {
                return;
            }

            timer.stop();

            inProcess = true;

            ajaxLoadAnnonce = $.get(lienObtenirAnnonce + '?' + '&mode=' + mode, function (response, status, xhr) {
                /*if (status == "error") {
                //element.loadAnnonce(mode);

                }*/

                inProcess = false;

                if (mode == 'timer') {
                    timer.reset(secondesInterval);
                    element.parent().addClass('loadingContent').find('.ai_controller').fadeOut(500).end().end()
					.fadeOut(500, function () {
					    element.html(response);
					});
                }
                else {
                    if (mode == 'changementRegion') {
                        $('a.togglePlay').removeClass('ai_controller_play ai_controller_pause').addClass('ai_controller_pause');
                        timer.reset(secondesInterval);
                    }
                    element.html(response);
                }
                element.parent().removeClass('loadingContent').end().fadeIn(500).next().fadeIn(500);
            });
        }
    }
});
