(function($) {
    $.fn.diaporama = function(options) {

        var defaults = {
            delay: 3,
            animationSpeed: "normal",
            controls: true
        };

        var options = $.extend(defaults, options);

        this.each(function() {

            var obj = $(this);


            if ($(obj).find("li").length > 1) {
                var inter = setInterval(function() {
                    nextElt(options)
                }, (options.delay * 1000));
                var sens = "right";
                var pause = false;

                $(obj).find("li").hide();
                $(obj).find("li:first-child").addClass("active").fadeIn(options.animationSpeed);

                // Controls

                if (options.controls) {

                    $(obj).after("<div class='diaporama_controls'><div class='btns'><a href='#' class='prev'>Prec.</a><img class='imgfleche' src='../images/img_site/fche_bleu.gif'/> <a href='#' class='plusavis'>Voir plus d'avis</a><img class='imgfleche2' src='../images/img_site/fche_bleu.gif'/><a href='#' class='pause'>Lire la suite</a> <a href='#' class='next'>Suiv.</a></div></div>");

                    $(obj).siblings().find(".prev").click(function() {
                        clearInterval(inter);
                        prevElt(options);
                        if (!pause)
                            inter = setInterval(function() { prevElt(options) }, (options.delay * 1000));
                        sens = "left";
                        return false;
                    });

                    $(obj).siblings().find(".next").click(function() {
                        clearInterval(inter);
                        nextElt(options);
                        if (!pause)
                            inter = setInterval(function() { nextElt(options) }, (options.delay * 1000));
                        sens = "right";

                        return false;
                    });

                    $(obj).siblings().find(".plusavis").click(function() {
                        clearInterval(inter);
                        pause = true;
                        var id_feedback = $(obj).find("li.active").attr("id_feedback");
                        //$(obj).siblings().find(".plusavis").attr('href', "feedback_codepostal.aspx?id_feedback=" + id_feedback);
                        CallAjax("../Popups/feedbackliste.aspx?NA=" + Math.random() + "&id_feedback=" + id_feedback, "FeedBackClient");


                        $("#popupFeedBack").css({
                            "height": "400px",
                            "width": "700px"
                        });
                      
                        return false;
                    });


                    $(obj).siblings().find(".pause").click(
						function() {
						    clearInterval(inter);
						    pause = true;
						    var feedbackencour;
						    feedbackencour = $(obj).find("li.active").html();

						    //LOADING POPUP
						    //Click the button event!
						    $("#FeedBackClient").html(feedbackencour);
						    $("#FeedBackClient").find(".feedbackInfo").removeClass("feedbackInfo").addClass("feedbackInfoPopUp")
						    $("#FeedBackClient").find(".feedbackText").removeClass("feedbackText").addClass("feedbackTextPopUp")
						    var id_feedback = $(obj).find("li.active").attr("id_feedback");
						    $("#popupFeedBack").css({
						        "height": "384px",
						        "width": "408px"
						    });
						    CallAjax("../Popups/feedback.aspx?NA=" + Math.random() + "&id_feedback=" + id_feedback, "FeedBackClient");

						    //centering with css
						  

						    return false;

						},
						function() {
						    $(this).removeClass("play").addClass("pause");
						    inter = setInterval(function() { (sens == "right") ? nextElt(options) : prevElt(options) }, (options.delay * 1000));
						    pause = false;
						}
					);
                }

                // Affiche l'élément suivant

                function nextElt(options) {
                    $(obj).find("li.active").fadeOut(options.animationSpeed);
                    if (!$(obj).find("li.active").is(":last-child")) {
                        $(obj).find("li.active").next().addClass("active").prev().removeClass("active");
                        $(obj).find("li.active").fadeIn(options.animationSpeed);

                    }
                    else {
                        $(obj).find("li:first-child").addClass("active").fadeIn(options.animationSpeed);
                        $(obj).find("li:last-child").removeClass("active");
                    }
                    return false;
                }
                //Afficher popup
                function CallAjax(src, objId) {
                    var xhr_object = null;
                    if (window.XMLHttpRequest) // Firefox 
                        xhr_object = new XMLHttpRequest();
                    else if (window.ActiveXObject) // Internet Explorer 
                        xhr_object = new ActiveXObject("Microsoft.XMLHTTP");
                    else { // XMLHttpRequest non supporté par le navigateur renvoie false pour ouvrir une fenetre	   	
                        alert("Service non supporte par votre navigateur");
                    }
                    try {
                        //alert("début ajax");
                        var obj = document.getElementById(objId);
                        //obj.innerHTML = "OK";
                        xhr_object.open("GET", src, true);
                        xhr_object.onreadystatechange = function() {
                            if (xhr_object.readyState == 4) {
                                if (xhr_object.status == 200) {
                                    obj.innerHTML = xhr_object.responseText;
                                    //alert(obj.innerHTML);
                                    obj.style.visibility = 'visible';
                                    //alert("fin ajax");
                                    centerPopup();
                                    //load popup
                                    loadPopup();
                                }
                                else {
                                    // affichage si page trouvee
                                    alert("Service non disponible.");
                                }
                            }
                        }
                        xhr_object.send(null);
                    }
                    catch (e) { alert("Erreur:" + e); }
                }

                // Affiche l'élément précédent

                function prevElt(options) {
                    $(obj).find("li.active").fadeOut(options.animationSpeed);

                    if (!$(obj).find("li.active").is(":first-child")) {
                        $(obj).find("li.active").prev().addClass("active").next().removeClass("active");
                        $(obj).find("li.active").fadeIn(options.animationSpeed);

                    }
                    else {
                        $(obj).find("li:last-child").addClass("active").fadeIn(options.animationSpeed);
                        $(obj).find("li:first-child").removeClass("active");
                    }
                    return false;
                }

            }
            //SETTING UP OUR POPUP   
            //0 means disabled; 1 means enabled;
            var popupStatus = 0;

            //loading popup with jQuery magic!
            function loadPopup() {
                //loads popup only if it is disabled
                if (popupStatus == 0) {
                    $("#backgroundPopup").css({
                        "opacity": "0.7"
                    });
                    $("#backgroundPopup").fadeIn("slow");
                    $("#popupFeedBack").fadeIn("slow");
                    popupStatus = 1;
                }
            }

            //disabling popup with jQuery magic! 
            function disablePopup() {
                //disables popup only if it is enabled
                if (popupStatus == 1) {
                    $("#backgroundPopup").fadeOut("slow");
                    $("#popupFeedBack").fadeOut("slow");
                    popupStatus = 0;
                }
            }


            //centering popup
            function centerPopup() {
                //request data for centering
                var windowWidth = document.documentElement.clientWidth;
                var windowHeight = document.documentElement.clientHeight;
                var popupHeight = $("#popupFeedBack").height();
                var popupWidth = $("#popupFeedBack").width();
                //centering
                $("#popupFeedBack").css({
                    "position": "absolute",
                    "left": windowWidth / 2 - popupWidth / 2
                });
                //only need force for IE6
                $("#backgroundPopup").css({
                    "height": 3 * windowHeight
                });
            }
            $(document).ready(function() {
                //following code will be here
            });
            $(window).scroll(function() {
                $('#popupFeedBack').animate({
                    top: $(window).scrollTop() + 50 + "px"
                }
                 , { queue: false, duration: 350 });

            });


            //CLOSING POPUP
            //Click the x event!
            $("#popupFeedBackClose").click(function() {
                disablePopup();
                inter = setInterval(function() { (sens == "right") ? nextElt(options) : prevElt(options) }, (options.delay * 1000));
                pause = false;
            });
            //Click out event!
            $("#backgroundPopup").click(function() {
                inter = setInterval(function() { (sens == "right") ? nextElt(options) : prevElt(options) }, (options.delay * 1000));
                pause = false;
                disablePopup();
            });
            //Press Escape event!
            $(document).keypress(function(e) {
                if (e.keyCode == 27 && popupStatus == 1) {
                    disablePopup();
                }
            });
        });

        return this;
    };
})(jQuery);