(function ($, undefined) {
    var methods = {

        //###############################
        // Initalisierung der Elemente, 
        // generieren der Seitezähler
        //###############################

        init: function (method) {
            method = $.extend({
                first: 1, // Mit welcher Seite wird begonnen 
                pager_id: "pager" // wohin soll der Seitenzähler etc = ID!                           
            }, method);

            //Abändern des Permalinks um direkte Seitenaufrufe zu ermöglichen
            function hashpage(page) {                
                global.parahash.seite = page;
                //alert(global.parahash.titel)
                if (global.parahash.titel != "") {
                    location.hash = "!/" + global.parahash.kat_slug + "/" + global.parahash.titel + "/" + global.parahash.seite;
                }
                else {
                    location.hash = "!/" + global.parahash.kat_slug + "//" + global.parahash.seite;
                }
            }

            var last_pages = $("._pages").length; //Null Indixiert!

            //Erste Seite einblenden und als "aktuell" markieren
            $(this).find("#p" + method.first).addClass("_aktuell").fadeIn("slow");

            //Am Ende aller Texte ein "zurück / weiter" einbinden (mit Ausnahme der ersten und letzten Seite)
			
            if (!$.browsercheck()) {
                var tilde = "<img src='IMG/tilde.png' />";
                var slash = "<img src='IMG/slash.png' />";
                var next = "<img src='IMG/next.png' id='next' />";
                var back = "<img src='IMG/back.png' id='back' />";
                var font = "";
            }
            else {
                var tilde = "<img src='IMG/tilde.png' />";
                var slash = "<img src='IMG/slash.png' />";
                var next = "<span id='next'>weiter</span>";
                var back = "<span id='back'>zur&uuml;ck</span>";
                var font = "font-family:Merlinn,arial; font-size:2.5em;";
            }

            if ($(".page_bottom_icon").length == 0) {
                $(this).find("._pages").each(function (index) {
                    if (index == 0) {
                        $(this).append("<p align='center' style='margin:0.2em; padding:0.2em; margin-top:30px; " + font + "'><span>" + tilde + "</span> <span style='cursor:pointer;' class='next_page page_bottom_icon'> " + next + "</span> <span>" + tilde + "</span></p>");
                    }
                    else if (index == last_pages - 1) {
                        $(this).append("<p align='center' style='margin:0.2em; padding:0.2em; margin-top:30px; " + font + "'><span>" + tilde + "</span> <span style='cursor:pointer;' class='prev_page page_bottom_icon'>" + back + " </span> <span>" + tilde + "</span></p>");
                    }
                    else {
                        $(this).append("<p align='center' style='margin:0.2em; padding:0.2em; margin-top:30px; " + font + "'><span>" + tilde + "</span> <span style='cursor:pointer;' class='prev_page page_bottom_icon'>" + back + " </span><span>" + slash + "</span><span style='cursor:pointer;' class='next_page'> " + next + "</span> <span>" + tilde + "</span></p>");
                    }
                });
            }

            $("#node-text").find("._pages").each(function () {
                $(this).children("br:first-child").remove();
            });

            //Seitenzähler einbauen
            //#Region Seitenzähler UI 
            if ($(this).find("._pages").length >= 1) {
                var pager = "";
                for (i = 0; i < last_pages; i++) {
                    var thispage = i + 1;
                    pager += "<span class='page_click ui-corner-all' id='per" + thispage + "'>" + thispage + "</span>";
                }

                if ($.browsercheck()) {
                    var pagerbody = "<table style='border:none; height:50px; position:relative; margin:auto; font-family:Merlinn,arial; font-size:1.6em;'><tr><td style='width:25px;' valign='middle' align='left'>" +
									  "<span style='cursor:pointer;' id='first_page'>start</span></td><td>&nbsp;<span id='back_kl' style='cursor:pointer;' class='prev_page'>&lt;&lt;</span></td>" +
									  "<td align='center'><div style='display:table; border-spacing:1px;'>" + pager + "</div></td><td>" +
									  "<span id='next_kl' style='cursor:pointer;' class='next_page'>&gt;&gt;</span>&nbsp;</td><td style='width:25px;' align='right'>" +
									  "<span style='cursor:pointer;' id='end_page'>ende</span></td></tr></table>" +
									  "<table style='border:none; width:505px; height:67px; position:relative; margin:auto; background:url(IMG/trennlinie.png) center center no-repeat;'><tr>" +
									  "<td>&nbsp;</td>" +
									  "</tr></table>";
                }
                else {
                    var pagerbody = "<table style='border:none; height:50px; position:relative; margin:auto;'><tr><td style='width:25px;' valign='middle' align='left'>" +
									  "<img src='IMG/first.png' style='cursor:pointer;' id='first_page' /></td><td>&nbsp;<img id='back_kl' src='IMG/back_kl.png' style='cursor:pointer;' class='prev_page' /></td>" +
									  "<td align='center'><div style='display:table; border-spacing:1px;'>" + pager + "</div></td><td>" +
									  "<img id='next_kl' src='IMG/next_kl.png' style='cursor:pointer;' class='next_page' />&nbsp;</td><td style='width:25px;' align='right'>" +
									  "<img src='IMG/last.png' style='cursor:pointer;' id='end_page' /></td></tr></table>" +
									  "<table style='border:none; width:505px; height:67px; position:relative; margin:auto; background:url(IMG/trennlinie.png) center center no-repeat;'><tr>" +
									  "<td>&nbsp;</td>" +
									  "</tr></table>";
                }

                $("#" + method.pager_id).html(pagerbody);

                $("#per" + method.first).addClass("aktuell_page");

                //###############################
                // Click Events
                //###############################

                $("#first_page").click(function () {
                    $("._aktuell").fadeOut("slow", function () {
                        $("._aktuell").removeClass("_aktuell");
                        $("#p" + method.first).addClass("_aktuell").fadeIn("slow");
                    });

                    $(".page_click").removeClass("aktuell_page");
                    $("#per" + method.first).addClass("aktuell_page");

                    hashpage(method.first)
                });

                $("#end_page").click(function () {
                    $("._aktuell").fadeOut("slow", function () {
                        $("._aktuell").removeClass("_aktuell");
                        $("#p" + last_pages).addClass("_aktuell").fadeIn("slow");
                    });

                    $(".page_click").removeClass("aktuell_page");
                    $("#per" + last_pages).addClass("aktuell_page");

                    hashpage(last_pages)
                });

                $(".prev_page").click(function () {
                    var this_page = parseInt($("._aktuell").attr("id").substr(1, $("._aktuell").attr("id").length - 1));
                    if (this_page > method.first) {
                        var prev_page = this_page - 1;

                        $("._aktuell").fadeOut("slow", function () {
                            $("._aktuell").removeClass("_aktuell");
                            $("#p" + prev_page).addClass("_aktuell").fadeIn("slow");
                        });

                        $(".page_click").removeClass("aktuell_page");
                        $("#per" + prev_page).addClass("aktuell_page");

                        hashpage(prev_page)
                    }
                });

                $(".next_page").click(function () {
                    var this_page = parseInt($("._aktuell").attr("id").substr(1, $("._aktuell").attr("id").length - 1));
                    if (this_page < last_pages) {
                        var next_page = this_page + 1;
                        //alert(this_page + " + 1 " + next_page);
                        $("._aktuell").fadeOut("slow", function () {
                            $("._aktuell").removeClass("_aktuell");
                            $("#p" + next_page).addClass("_aktuell").fadeIn("slow");
                        });

                        $(".page_click").removeClass("aktuell_page");
                        $("#per" + next_page).addClass("aktuell_page");

                        hashpage(next_page)
                    }
                });

                $(".page_click").click(function () {
                    var newpage = $(this).html();
                    $("._aktuell").fadeOut("slow", function () {
                        $("._aktuell").removeClass("_aktuell");
                        $("#p" + newpage).addClass("_aktuell").fadeIn("slow");
                    });

                    $(".page_click").removeClass("aktuell_page");
                    $(this).addClass("aktuell_page");

                    hashpage(newpage)
                });

                //###############################
                // Mouseover/Mouseout Events
                //###############################

                $(".page_click").live("mouseover", function () {
                    //$(".page_click").removeClass("mover_pager");
                    $(this).addClass("mover_pager");
                    $(this).css("color", "#000");
                });

                $(".page_click").live("mouseout", function () {
                    //$(".page_click").removeClass("mover_pager");
                    $(this).removeClass("mover_pager");
                    $(this).css("color", "#fbf0b7");
                });

                //###############################

                $("#end_page").live("mouseover", function () {
                    if (!$.browsercheck()) {
                        $(this).attr("src", "IMG/mo_last.png");
                    }
                    else {
                        $(this).css("color", "#ffcc00");
                    }
                });

                $("#end_page").live("mouseout", function () {
                    if (!$.browsercheck()) {
                        $(this).attr("src", "IMG/last.png");
                    }
                    else {
                        $(this).css("color", "");
                    }
                    //$(".page_click").removeClass("mover_pager");
                    //
                });

                //###############################

                $("#first_page").live("mouseover", function () {
                    if (!$.browsercheck()) {
                        $(this).attr("src", "IMG/mo_first.png");
                    }
                    else {
                        $(this).css("color", "#ffcc00");
                    }
                });

                $("#first_page").live("mouseout", function () {
                    //$(".page_click").removeClass("mover_pager");
                    if (!$.browsercheck()) {
                        $(this).attr("src", "IMG/first.png");
                    }
                    else {
                        $(this).css("color", "");
                    }
                });

                //###############################

                $("#next_kl").live("mouseover", function () {
                    if (!$.browsercheck()) {
                        $(this).attr("src", "IMG/mo_next_kl.png");
                    }
                    else {
                        $(this).css("color", "#ffcc00");
                    }
                });

                $("#next_kl").live("mouseout", function () {
                    //$(".page_click").removeClass("mover_pager");
                    if (!$.browsercheck()) {
                        $(this).attr("src", "IMG/next_kl.png");
                    }
                    else {
                        $(this).css("color", "");
                    }
                });

                //###############################

                $("#back_kl").live("mouseover", function () {
                    if (!$.browsercheck()) {
                        $(this).attr("src", "IMG/mo_back_kl.png");
                    }
                    else {
                        $(this).css("color", "#ffcc00");
                    }
                });

                $("#back_kl").live("mouseout", function () {
                    if (!$.browsercheck()) {
                        $(this).attr("src", "IMG/back_kl.png");
                    }
                    else {
                        $(this).css("color", "");
                    }
                });

                //###############################

                $("#back").live("mouseover", function () {
                    if (!$.browsercheck()) {
                        $(this).attr("src", "IMG/mo_back.png");
                    }
                    else {
                        $(this).css("color", "#ffcc00");
                    }
                });

                $("#back").live("mouseout", function () {
                    if (!$.browsercheck()) {
                        $(this).attr("src", "IMG/back.png");
                    }
                    else {
                        $(this).css("color", "");
                    }
                });

                //###############################

                $("#next").live("mouseover", function () {
                    if (!$.browsercheck()) {
                        $(this).attr("src", "IMG/mo_next.png");
                    }
                    else {
                        $(this).css("color", "#ffcc00");
                    }
                });

                $("#next").live("mouseout", function () {
                    if (!$.browsercheck()) {
                        $(this).attr("src", "IMG/next.png");
                    }
                    else {
                        $(this).css("color", "");
                    }
                });
            }
            //#EndRegion			

            //Wenn in Permalink die Seite enhalten ist, wird diese angeklickt
            if (global.parahash.seite != "") {
                $("#per" + global.parahash.seite).click();
            }
        }
    };

    //###############################
    // Aufruf der Funktion und Überprüfung ob eine Mehtode
    // angegeben wurde und wenn ja ob diese definiert ist.
    // Wenn diese nicht definiert wurde, wird eine Fehlermeldung
    // im Log (FF) bzw. Messagebox ausgegeben:
    // 
    // "Method 'XXX' does not exist on jQuery.pagethis".
    //###############################

    $.fn.pagethis = function (method) {
        if (methods[method]) {
            return methods[method].apply(this, Array.prototype.slice.call(arguments, 1));
        } else if (typeof method === 'object' || !method) {
            return methods.init.apply(this, arguments);
        } else {
            $.error(alert('Method ' + method + ' does not exist on jQuery.pagethis'));
        }
    }
})(jQuery);



