(function($){

    $.fn.extend({

        //pass the options variable to the function
        js_pager: function(options) {


            //Set the default values, use comma to separate the settings, example:
            var defaults = {
                prev_page: 'Vorherige Seite',
                next_page: 'Nächste Seite',
                openTag : '<li>',
                closeTag : '</li>',
                units: 3
            }

            var option =  $.extend(defaults, options);

            return this.each(function() {
                var o = option;
                var zahl;
                var content_elements = "<li id='prev'>"+o.prev_page+o.closeTag;
                $(".pager_content div:eq(0)").show();
                $('.pager_content .page').each(function(index){
                    index++;
                    zahl = index;
                    content_elements += "<li>"+index+o.closeTag;
                });
                
                content_elements += "<li id='next' >"+o.next_page+o.closeTag;
               $('.js_pager').append(content_elements);
               $('.js_pager li').attr("class",'aktive');
               $('.js_pager li:lt(2)').attr("class",'inaktive');
               $('.js_pager li:eq(1)').attr("id",'current');
                var len = $(".js_pager").children().size();
                len = zahl;
               $('.js_pager li').click(function(){
                   var type = $(this).text();
                   //console.log(type);
                   if(type != o.prev_page && type != o.next_page){
                       var index =$("#current").text();
                       console.log(index);
                       $(".pager_content div").hide();
                       $(".pager_content div.page:eq("+index+")").show();
                       $('.js_pager li').attr('class', 'aktive');
                       $(this).attr('class', 'inaktive');
                       if(type == 1){
                           $('#prev').attr('class', 'inaktive');
                           $('#next').attr('class', 'aktive');
                           $("#current").attr('id','');
                           $(this).attr('id','current');
                        }
                        if(type == len){
                           $('#next').attr('class', 'inaktive');
                           $('#prev').attr('class', 'aktive');
                           $("#current").attr('id','');
                           $(this).attr('id','current');
                        }
                        if(type < len && type > 1){
                            $('#next').attr('class', 'aktive');
                            $('#prev').attr('class', 'aktive');
                            $("#current").attr('id','');
                           $(this).attr('id','current');
                        }
                        if(index == len){
                            //alert("JETZT");
                            $('#next').attr('class', 'aktive');
                            $('#prev').attr('class', 'aktive');
                            $("#current").attr('id','');
                           $(this).attr('id','current');
                            $(".pager_content div.page:eq("+type+")").show();
                        }
                   }else{
                            var clicked = $(this).attr("id");
                            var $currentElement = $("#current");
                            var current = $('#current').text();
                            var prev = $("#current").prev().text();
                            var next = $("#current").next().text();

                            if(clicked == "prev"){
                                  if(prev == o.prev_page){
                                        $("#prev").attr('class', 'inaktive');
                                  }
                                  if(prev >= 1){
                                      $("#current").prev().attr("id", "current");
                                      $currentElement.attr("id", "");
                                      current = $('#current').text();$('.js_pager li').attr('class', 'aktive');
                                      $("#prev").attr('class', 'aktive');
                                  }
                                  if(prev == 1){
                                      $("#prev").attr("class", "inaktive");
                                  }
                            }
                            if(clicked == "next"){
                                  if(next == o.next_page){
                                        $("#next").attr('class', 'inaktive');
                                  }
                                  if(next <= len){
                                      $("#current").next().attr("id", "current");
                                      $currentElement.attr("id", "");
                                      current = $('#current').text();$('.js_pager li').attr('class', 'aktive');
                                      $("#next").attr('class', 'aktive');

                                  }
                                  if(next == len){
                                      $("#next").attr("class", "inaktive");
                                  }
                            }
                           $("#current").attr("class", "inaktive");
                           $(".pager_content div").hide();
                           $(".pager_content div:eq("+(current-1)+")").show();
                   }

               });
            });
        }
    });

})(jQuery);


