//###############################
// Basic Config
//###############################
var browser = $.browser
$(document).ready(function () {
    $.ajax({
        url: "scripts/loadfiles.php",
        dataType: "json",
        success: function (json) {
            phpfile.items.menu_file = json[0].load_menuphp_file;
            phpfile.items.kategorie_file = json[0].load_Katphp_file,
            phpfile.items.previewer_file = json[0].load_previewphp_file,
            phpfile.items.node_file = json[0].load_nodephp_file,
            phpfile.items.home_file = json[0].load_home_file,
            phpfile.items.comment_file = json[0].load_comment_file,
            phpfile.items.slider_file = json[0].load_slider_file,
            phpfile.items.set_comment_file = json[0].set_comment_file,
            phpfile.items.ucheck = json[0].load_userinfo,
            phpfile.items.links = json[0].load_links,
			phpfile.items.news = json[0].load_new

            autoloader.install()

            //###############################
            // Höhe des Contentbereichs je nach 
            // Browserengine (Mozilla, Webkit und IE)
            //###############################

            if ($.browsercheck()) {
                var height_cell = parseInt($(window).height()) - parseInt($("#header").height());
                $("#content").css("min-height", height_cell - 14 + "px");
                $("#node").css("min-height", height_cell - 14 + "px");
                $("#sub_menu").css("min-height", height_cell - 14 + "px");
                $("#menu-entry-div").css("height", height_cell - 25 + "px");
                $("#pager").css("min-height", "10px");
            }
            else {
                var height_cell = parseInt($(window).height()) - parseInt($("#header").height());
                $("#content").css("height", height_cell - 28 + "px");
                $("#node").css("height", height_cell - 28 + "px");
                $("#node-text").css("height", height_cell - 28 + "px");

                $("#sub_menu").css("height", height_cell - 28 + "px");
                $("#menu-entry-div").css("height", height_cell - 28 + "px");
            }

            //###############################
            // IE < 8.0 Warnung
            //###############################

            //#Region Bei altem IE Warnung anzeigen
            if (!$.browsercheck()) {
                $("body").append("<div style='height:100px; width:100%; z-index:800; display:none; border-bottom:1px solid #FBF0B7;' class='Menulinkdiv oldie' id='oldIE' />");
                $("body").append("<div style='height:100px; width:100%; position:absolute; top:0px; z-index:801; display:none;' class='oldie' id='browsercheck' />");

                $.get("fehler/oldbrowser.htm", function (data) {
                    version = browser.version.slice(0, 1);
                    IE7 = 2006;
                    IE6 = 2001;
                    var jetzt = new Date();
                    this_year = jetzt.getFullYear();

                    if (version == 7) {
                        IE_LifeSpan = this_year - IE7;
                    }
                    else if (version == 6) {
                        IE_LifeSpan = this_year - IE6;
                    }
                    else {
                        IE_LifeSpan = this_year - IE6;
                        IE_LifeSpan = "mehr als " + IE_LifeSpan
                    }

                    datastream = data.replace("XXXX", IE_LifeSpan + " Jahre");
                    $("#browsercheck").html(datastream);
                    $("#browsercheck").append("<span id='warningaway' class='cur_pointer no_hash_edit' style='position:absolute; bottom:5px; right:5px; z-index:802; color:#FF9900;'><b>schließen</b></span>");

                    $("#warningaway").live("click", function () {
                        $(".oldie").fadeOut("slow");
                    });

                    if ($.browser.msie && version < 8) {
                        $(".oldie").fadeIn("slow");
                    }
                });
            }
            //#EndRegion

            //###############################
            // Hover Events
            //###############################	

            $(".Menulink_hover").live("mouseover", function () {
                $(this).find(".Menulinkdiv").css({ "opacity": .80, "filter": "Alpha(Opacity=80)" });
                $(this).find(".Menulinktext").find("td").css("color", "#FFCC00");
            });
            $(".Menulink_hover").live("mouseout", function () {
                $(this).find(".Menulinkdiv").css({ "opacity": .30, "filter": "Alpha(Opacity=30)" });
                $(this).find(".Menulinktext").find("td").css("color", "#fbf0b7");
            });

            $(".Menulink_home").live("mouseover", function () {
                $(this).find(".Menulinkdiv_home").css({ "opacity": .80, "filter": "Alpha(Opacity=80)" });
                $(this).find(".Menulinktext").find("td").css("color", "#FFCC00");
            });
            $(".Menulink_home").live("mouseout", function () {
                $(this).find(".Menulinkdiv_home").css({ "opacity": .30, "filter": "Alpha(Opacity=30)" });
                $(this).find(".Menulinktext").find("td").css("color", "#fbf0b7");
            });

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

            // Vorschau nach Kategoriewechsel
            $("input[name=Radio-Menu]").live("click", function () {
                $("#pager").html("");
                $("#node-text").html("<img id='loading_gif' src='IMG/loading_content.gif' />");

                me = "#" + $(this).val();
                var Titel = $(this).val().split("-");
                var Titel = Titel[0];

                $(".menu-entry").fadeOut("fast", function () {
                    setTimeout(function () {
                        $(me).fadeIn("slow");
                    }, 500);
                });

                $("#menu-entry-div").fadeOut("fast", function () {
                    $("#menu-entry-div").css({ "background": "url(IMG/" + Titel + ".png) no-repeat top right" }).fadeIn("fast");
                });

                $("._marker").html("");
                $("._marker").parents(".Menulink").find(".Menulinktext").css({ "font-weight": '' });
                $("#Main-Kat").html($(this).attr("alt"));

                var Kategorie = $(this).val().split("-");

                if ($.browsercheck()) {
                    if ($("#Main-Kat").html() != "Home") {
                        $("title").html("Khyrindas diary > " + $(this).attr("alt"));
                    }
                    else {
                        $("title").html("Khyrindas diary");
                    }
                }

                $("#Node-Titel").html("");

                var Titel_Sub = Titel + "-Sub";


                if ($("#Main-Kat").html() != "Home") {
                    global.parahash.kat_slug = Kategorie[0].toLowerCase();
                    global.parahash.titel = "";
                    global.parahash.seite = "";

                    $.ajax({
                        url: phpfile.items.previewer_file + "?titel=" + Titel, // + "&Source=private",
                        dataType: "json",
                        success: function (json) {
                            var insert_this = "";
                            if (json.length == 0) {
                                var NODE_TXT = "derzeit gibt es keine Beschreibung dieser Kategorie";
                            }
                            else {
                                var NODE_TXT = json[0].NODE_TXT;
                            }

                            NODE_TXT = NODE_TXT.replace("<p><!--more--></p>", "").replace("<p>&nbsp;</p>", "");
                            if (NODE_TXT.indexOf("<!-- next -->") > 0) {
                                var insert_text = NODE_TXT.split("<!-- next -->");

                                for (i = 0; i < insert_text.length; i++) {
                                    page = i + 1;

                                    insert_this += "<div id='p" + page + "' style='display:none;' class='_pages'>" + insert_text[i] + "</div>";
                                };
                            }
                            else if (NODE_TXT.indexOf("<p><!--nextpage--></p>") > 0) {
                                var insert_text = NODE_TXT.split("<p><!--nextpage--></p>");

                                for (i = 0; i < insert_text.length; i++) {
                                    page = i + 1;

                                    insert_this += "<div id='p" + page + "' style='display:none;' class='_pages'>" + insert_text[i] + "</div>";
                                };
                            }
                            else {
                                insert_this = NODE_TXT;
                            }

                            $("#node-text").html(insert_this);
                        },
                        error: function (data, Errortext) {
                            //alert(Errortext + " - " + data.status)
                            $("#node-text").html(Errortext + " - " + data.status);
                            $("#Node-Table").fadeIn("slow");
                        },
                        complete: function () {
                            PluginsLoader.run()  //.call()

                            var url_para = location.href.split("?");
                            if (url_para[1]) {
                                location.hash = "!/" + Kategorie[0].toLowerCase();
                            }
                            else {
                                url_para = location.hash.split("#");

                                if ($.browsercheck() && history.pushState) {
                                    history.pushState("", Kategorie[0].toLowerCase(), url_para[0] + "#!/" + Kategorie[0].toLowerCase());
                                }
                                else {
                                    location.href = url_para[0] + "#!/" + Kategorie[0].toLowerCase();
                                }
                            }
                        }
                    });
                }
                else {
                    var HomeText = "";
                    var HomeList = "<span style='color: #ffbd08;'><strong>&Auml;ltere Beitr&auml;ge:</strong></span>";

                    global.parahash.kat_slug = "";
                    global.parahash.titel = "";
                    global.parahash.seite = "";

                    $.ajax({
                        url: phpfile.items.home_file,
                        dataType: "json",
                        success: function (json) {
                            for (i = 0; i < 5; i++) {
                                var NODE_TXT = json[i].NODE_TXT;

                                if (NODE_TXT.indexOf("<p><!--more--></p>") >= 0) {
                                    var NODE_TXT = json[i].NODE_TXT.split("<p><!--more--></p>");
                                    var NODE_TXT = NODE_TXT[0]
                                }

                                HomeText += "<div class='_Home_Node_DIV' style='font-size:0.9em;'>" +
										"<table style='width:100%; position:relative;' cellpadding='0' cellspacing='0'><tr>" +
										"<td align='left' style='width:50% border-top:1px solid #542008; border-bottom:1px solid #542008;' class='_Home_Node_Titel'>" +
										"<font style='font-size:0.9em; color:#ffbd08;'><b>" + json[i].NODE_TITEL + "</b></font></td>" +
										"<td align='right' style='width:50% border-top:1px solid #542008; border-bottom:1px solid #542008;' class='_Home_Node_Date'>" +
										"<font style='font-size:0.9em; color:#ffbd08;'>" + json[i].NODE_DATE_WEEKDAY + "</font></td></tr>" +
										"<tr><td style='height:5px;' colspan='2'></td></tr>" +
										"<tr><td colspan='2'>" + NODE_TXT +
										"<p style='margin-top:20px; margin-bottom:20px;' align='center'><a style='cursor:pointer; color:#ffbd08;' titel='" + json[i].NODE_ID + "' class='Menulink_more Menulink_hover'><b>Weiterlesen &gt;</b></a></p>" +
										"</td></tr></table></div>";
                            }

                            $("#node-text").html(HomeText);
                            $("#pager").html("");

                            if (!$.browsercheck()) {
                                var table_Class = "Menulinkstyle_IE";
                            }
                            else {
                                var table_Class = "Menulinkstyle";
                            }

                            for (i = 5; i < json.length; i++) {
                                if ($.browsercheck()) {
                                    var Date_Insert = "<span style='float:right;'>" + json[i].NODE_DATE_DAY + "</span>";
                                }
                                else {
                                    var Date_Insert = "<span style='position:absolute; right:0px;'>" + json[i].NODE_DATE_DAY + "</span>";
                                }

                                HomeList += "<a style='cursor:pointer;' rel='" + json[i].NODE_SLUG + "' titel='" + json[i].NODE_ID + "' class='Menulink_more Menulink_home Menulink_hover'>" +
										"<table style='margin-bottom:0.9em; position:relative; padding-right:0.5em;'><tr><td valign='middle'>" +
										"<div style='position:relative;'><div class='Menulinkdiv_home ui-corner-all'>" +
										"</div><table class='Menulinktext " + table_Class + "'><tr>" +
										"<td valign='middle'>" + json[i].NODE_TITEL + Date_Insert +
										"</td></tr></table></div></td></tr></table></a>";
                            }

                            if ($.browsercheck()) {
                                $("#Home-Menu").find("p").css({ "position": "relative", "top": "-15px" });
                            }

                            $("#Home-Menu").html(HomeList);

                        },
                        error: function (data, Errortext) {
                            $("#node-text").html(Errortext + " - " + data.status);
                            $("#Node-Table").fadeIn("slow");
                        },
                        complete: function () {
                            PluginsLoader.run() //.call()
                            location.hash = "";
                        }
                    });
                }
            });

            //###############################
            // Einblenden des Content
            //###############################

            $(".Menulink").live("click", function () {
                $("#pager").html("");
                $("#node-text").html("<img id='loading_gif' src='IMG/loading_content.gif' />");                

                $("._marker").html("");
                $("._marker").parents(".Menulink_hover").find(".Menulinktext").css({ "font-weight": '' });

                $("._selected").removeClass("_selected");
                $(this).addClass("_selected");

                $(this).find("._marker").html("<font style='color:#FFCC00;'><b>&lt;</b></font>");
                $(this).find(".Menulinktext").css({ "font-weight": "bold" });

                if ($(this).parents(".menu-entry").length > 0) {
                    var kategorie = $(this).parents(".menu-entry").attr("id").split("-");
                }
                else if ($(this).parents(".menu-entry-news").length > 0) {
                    var kategorie = $(this).parents(".menu-entry-news").attr("id").split("-");
                }

                var this_id = $(this).attr("id");

                $.ajax({
                    url: phpfile.items.node_file + "?id=" + this_id,
                    dataType: "json",
                    success: function (json) {
                        // Node_Import(json[0].NODE_TXT, json[0].NODE_TITEL)                
                        var insert_this = "";
                        if (json[0].NODE_TXT.indexOf("<!-- next -->") >= 0) {
                            var insert_text = json[0].NODE_TXT.replace("<p><!--more--></p>", "").split("<!-- next -->");

                            for (i = 0; i < insert_text.length; i++) {
                                page = i + 1;

                                insert_this = insert_this + "<div id='p" + page + "' style='display:none;' class='_pages'>" + insert_text[i] + "</div>";
                            };
                        }
                        else if (json[0].NODE_TXT.indexOf("<p><!--nextpage--></p>") > 0) {
                            var insert_text = json[0].NODE_TXT.replace("<p><!--more--></p>", "").split("<p><!--nextpage--></p>");

                            for (i = 0; i < insert_text.length; i++) {
                                page = i + 1;

                                insert_this = insert_this + "<div id='p" + page + "' style='display:none;' class='_pages'>" + insert_text[i] + "</div>";
                            };
                        }
                        else {
                            insert_this = json[0].NODE_TXT.replace("<p><!--more--></p><br />", "");
                        }

                        $("#node-text").html(insert_this);

                        if (json[0].NODE_COMMENT == "open") {
                            comment.install({ id: this_id })
                        }

                        $("#Node-Titel").html(" <font style='color:#FFCC00;'><b>&gt;</b></font> " + json[0].NODE_TITEL);

                        global.parahash.kat_slug = kategorie[0].toString().toLowerCase().replace(/\s/g, "-");
                        global.parahash.titel = json[0].NODE_SLUG;  //json[0].NODE_TITEL.toString().toLowerCase().replace(/\s/g, "-").replace(/&uuml;/g, "ü").replace(/&Uuml;/g, "Ü").replace(/&auml;/g, "ä").replace(/&Auml;/g, "Ä").replace(/&ouml;/g, "ö").replace(/&Ouml;/g, "Ö").replace(/&szlig;/g, "ß");
                        global.parahash.seite = "";

                        //var kat_hash = kategorie[0].toString().toLowerCase().replace(/\s/g, "-");
                        //var titel_hash = json[0].NODE_TITEL.toString().toLowerCase().replace(/\s/g, "-").replace(/&uuml;/g, "ü").replace(/&Uuml;/g, "Ü").replace(/&auml;/g, "ä").replace(/&Auml;/g, "Ä").replace(/&ouml;/g, "ö").replace(/&Ouml;/g, "Ö").replace(/&szlig;/g, "ß");

                        var url_para = location.href.split("?");
                        if (!url_para[1]) {
                            location.hash = "!/" + global.parahash.kat_slug + "/" + global.parahash.titel;
                        }
                        else {
                            if ($.browsercheck() && history.pushState) {
                                history.pushState("", titel_hash, url_para[0] + "#!/" + global.parahash.kat_slug + "/" + global.parahash.titel);
                            }
                            else {
                                location.href = url_para[0] + "#!/" + global.parahash.kat_slug + "/" + global.parahash.titel;
                            }
                        }
                    },
                    error: function (data, Errortext) {
                        $("#Node-Titel").html(" <font style='color:#FFCC00;'><b>&gt;</b></font> AJAX " + Errortext + " - " + data.status);
                        $("#node-text").html(Errortext + " - " + data.status);
                    },
                    complete: function (json) {
                        //hash Wert in ajax-Request success gesetzt
                        PluginsLoader.run() //.call()
                    }
                });
            });

            $(".Menulink_more").live("mouseover", function () {
                $(this).css("color", "#FF9900");
            });

            $(".Menulink_more").live("mouseout", function () {
                $(this).css("color", "#ffbd08");
            });

            $(".Menulink_more").live("click", function () {
                global.parahash.kat_slug = "";
                global.parahash.titel = parseInt($(this).attr("titel"));
                global.parahash.seite = "";

                directLink.load();
            });

            $(".internlink").live("click", function () {
                var link = $(this).attr("titel").split("/");
                global.parahash.kat_slug = link[0];
                global.parahash.titel = link[1];
                if (link[2] != undefined) {
                    global.parahash.seite = link[2];
                }
                else {
                    global.parahash.seite = "";
                }

                directLink.load();
            });

            $("#iRadio-Menu-0").attr("checked", true);
            $(".menu-button-set").buttonset("update");

            //###############################
            // Tooltip bei externen Links
            //###############################
            $("body").append("<div id='link_tooltip' style='color:#FF9900; padding:0.5em; width:auto; height: auto; display:none; border:1px solid #FBF0B7; position:absolute; background:#542008; z-index:100;' />");

            $("a").live("mouseenter", function (e) {
                if ($(this).attr("target") == "_blank") {
                    if ($(this).attr("rel") != "") {
                        $("#link_tooltip").html($(this).attr("rel"));
                    }
                    else {
                        $("#link_tooltip").html($(this).attr("href").substr(0, 50));
                    }
                    //alert($(this).css("left"))
                    if ($(this).css("left") == "") {
                        var offsetY = $(this).offset().left + $(this).width() + 5; //e.offsetX - $(this).offset().top;
                        var offsetX = $(this).offset().top - 10; // e.offsetY - $(this).offset().left;
                    }
                    else {
                        var offsetY = parseInt($(this).css("left").substr(0, $(this).css("left").length - 2)) + $(this).width() + 5
                        var offsetX = parseInt($(this).css("top").substr(0, $(this).css("top").length - 2)) - 10;
                    }

                    $("#link_tooltip").css({ "top": offsetX + "px", "left": offsetY + "px" });
                    $("#link_tooltip").fadeIn("slow");
                }
            });

            $("a").live("mouseleave", function () {
                if ($(this).attr("target") == "_blank") {
                    $("#link_tooltip").fadeOut("slow");
                }
            });

            //###############################
            // Seite anzeigen nachdem 
            // alles fertig geladen wurde
            //###############################
            $("#Body_div").fadeIn("fast");
        },
        error: function (data, Errortext) {
            $("#Node-Titel").html(" <font style='color:#FFCC00;'><b>&gt;</b></font> AJAX " + Errortext + " - " + data.status);
            $("#node-text").html(Errortext + " - " + data.status);
        }
    });
});
