﻿function AllPages() {
    jQuery.timeago.settings.allowFuture = true;
    $(".timeago").timeago();
    //$("textarea.Resizable:not(.processed)").TextAreaResizer();
    $(".CloseButton").click(function () {
        $(this).parent().slideUp("fast");
    });
}
function Access() {
    $(".Site").click(function () {
        $(this).parent().siblings().find(":checkbox").attr("checked", this.checked);
    });
    $("ul>li>ul>li>label>input:checkbox").click(function () {
        var checkedAll = true;
        $(this).parent().parent().parent().find(":checkbox").each(function () {
            if (!this.checked) {
                checkedAll = false;
            }
        });
        $(this).parent().parent().parent().siblings().find(".Site").attr("checked", checkedAll);
    });
    $("ul>li>ul>li>label>input:checkbox").each(function () {
        var checkedAll = true;
        $(this).parent().parent().parent().find(":checkbox").each(function () {
            if (!this.checked) {
                checkedAll = false;
            }
        });
        $(this).parent().parent().parent().siblings().find(".Site").attr("checked", checkedAll);
    });
}
function AddEditor() {
    Access();
    $("#Name").keyup(function () {
        var user = $(this).val();
        $(".UserName").each(function () {
            $(this).html(user);
        });
    });
    $("#Message").keyup(function () {
        $(".MessageContent").html($(this).val().replace(new RegExp("(.*)\n([^#*\n].*)", "g"), "$1<br />$2"));
    });
}
function GetWebsiteName() {
    $("#Url").blur(function () {
        $.ajax({
            type: "GET",
            url: "/site/getname",
            data: "path=" + $(this).val(),
            success: function (msg) {
                $("#Name").val(msg);
            }
        });
    });
}
function AddPage(path) {
    //GetWebsiteName();
    $("#Directories").html("<li><img src='" + path + "loader.gif'></li>");
    Directories($("#Directories"), "", "", "");
    var baseUrl = $("#BaseUrl").html();

    $("#Directories a").live('click', function (e) {
        $(this).addClass("Current");
        var node = $(this).html();
        var classes = "";
        var current = $(this).parent();
        var spath = node;
        while (true) {
            current = current.parent();
            if ($(current).attr("id") == "Directories") {
                break;
            }
            current = current.parent();
            spath = current.children().html() + "/" + spath;
        }
        $("#Path").val(spath);
        $("#Url").val(baseUrl + "/" + spath);
        $("#Directories a").each(function () {
            if (node != $(this).html()) {
                $(this).removeClass("Current");
            }
        });
        if (!$(this).hasClass("File")) {
            $(this).parent().append("<ul><li><img src='" + path + "loader.gif'></li></ul>");
            Directories($(this).parent(), "<a href='#' class='Current'>" + node + "</a><ul>", "</ul>", $("#Path").val());
        }
    });
    function Directories(container, before, after, File) {
        var url = "/site/listfiles";
        var data = "idsite=" + $("#IDSite").val() + "&Directory=" + $("#ServerPath").val() + File;
        var callback = function (data, textStatus) {
            var result = before;
            if (data !== "") {
                result += data;
            }
            else {
                result += "<li>brak podfolderów</li>";
            }
            result += after;
            container.html(result);
        };
        jQuery.post(url, data, callback, "application/json");
    }
}
function AddWebSite(path) {
    //GetWebsiteName();
    $("#Test").live("click", function () {
        var cont = $(this).parent();
        cont.html("<img src='" + path + "loader.gif'>");
        $.ajax({
            type: "POST",
            url: "/site/testconnection",
            data: "server=" + $("#Server").val() + "&User=" + $("#UserName").val() + "&Password=" + $("#Password").val(),
            success: function (msg) {
                if (msg == "True") {
                    cont.html("<img src='" + path + "accept.png'>");
                }
                else {
                    cont.html("<img src='" + path + "delete.png'> <a href='#' id='Test'>Test</a>");
                }
            }
        });
    });
    var pathInput;
    $(".Browse").click(function () {
        pathInput = $(this).siblings("input");
        pathInput.val("");
        $("#Directories").parent().show();
        $("#Directories").html("<li><img src='" + path + "loader.gif'></li>");
        $("#Directories a").each(function () {
            $(this).removeClass("Current");
        });
        Directories($("#Directories"), "", "");
    });
    $("#Directories a").live('click', function (e) {
        $(this).addClass("Current");
        var node = $(this).html();
        var classes = "";
        var current = $(this).parent();
        var spath = node;
        while (true) {
            current = current.parent();
            if ($(current).attr("id") == "Directories") {
                break;
            }
            current = current.parent();
            spath = current.children().html() + "/" + spath;
        }
        pathInput.val(spath);
        $("#Directories a").each(function () {
            if (node != $(this).html()) {
                $(this).removeClass("Current");
            }
        });
        if (!$(this).hasClass("File")) {
            $(this).parent().append("<ul><li><img src='" + path + "loader.gif'></li></ul>");
            Directories($(this).parent(), "<a href='#' class='Current'>" + node + "</a><ul>", "</ul>");
        }
    });
    function Directories(container, before, after) {
        var url = "/site/listdirectory";
        var data = "server=" + $("#Server").val() + "&User=" + $("#UserName").val() + "&Password=" + $("#Password").val() + "&Directory=" + pathInput.val();
        var callback = function (data, textStatus) {
            var result = before;
            if (data !== "") {
                result += data;
            }
            else {
                result += "<li>brak podfolderów</li>";
            }
            result += after;
            container.html(result);
        };
        jQuery.post(url, data, callback, "application/json");
    }
}
function DetailPage() {
    $("textarea.Resizable").ckeditor(function () { }, {
        baseHref: baseHref
    }); 
    PreviewPage();
    CloseWindow();
    RevisionToogle();
    $(".DeleteItem").live("click", function () {
        var Block = $(this).attr("rel");
        $(this).parent().parent().html("");
        $.scrollTo('#Block' + Block, 800);
    });
    $(".AppendItem").click(function (e) {
        e.stopPropagation();
        var Block = $(this).attr("rel");
        var Item = AddItem(Block);
        $(this).parent().parent().find(".RepeatBlock").append(Item);
        $.scrollTo('#Block' + Block + '_Item' + Items[Block], 800);
        $('#Block' + Block + '_Item' + Items[Block]).find("textarea").each(function () {
            $(this).ckeditor();
        });
    });
    $(".PrependItem").click(function (e) {
        e.stopPropagation();
        var Block = $(this).attr("rel");
        var Item = AddItem(Block);
        $(this).parent().parent().find(".RepeatBlock").prepend(Item);
        $.scrollTo('#Block' + Block + '_Item' + Items[Block], 800, { offset: -20 });
        $('#Block' + Block + '_Item' + Items[Block]).find("textarea").each(function () {
            $(this).ckeditor();
        });
    });
    function AddItem(Block) {
        Items[Block]++;
        var Item = Blocks[Block].split("${ItemCount}").join(Items[Block]);
        return Item;
    }
    $(".Publish").live("click", function () {
        $.scrollTo("#Scroll", 800);
    });
    $(".BlockStrip").click(function (e) {
        $(this).siblings().toggle();
    });
    $(".Edits h4").live("click", function () {
        $(this).siblings().toggle();
    });
}
function RevisionToogle() {
    $(".RevisionHeader").live("click", function (e) {
        $(this).siblings().toggle();
    });
}
function PreviewPage() {
    $(".Preview").click(function (e) {
        e.stopPropagation();
        e.preventDefault();
        LaunchWindow("#PagePreview");
        $("#PagePreviewContent").html("<img src='" + loadPath + "' alt='wczytywanie' />");
        $.post($(this).parents().find("form").attr("action") + "?call=ajax", $(this).parents().find("form").serialize(),
            function (data) {
                $("#PagePreviewContent").html("<iframe src='" + data + "' width='100%' height='540'><div class=\"Info Message\"><div class=\"InfoContainer\">Twoja przegladarka nie obsługuje ramek. Kliknij aby zaobaczyć podląd strony. <a href='" + data + "' target='_blank'>Zobacz</a>.</div></div></iframe>");
            });
    });
}
function Start() {
    $(".ModalBox").click(function () {
        LaunchWindow("#" + $(this).attr("rel"));
    });
    CloseWindow();
}
function CloseWindow() {
    $('.Window .Close').click(function (e) {
        e.preventDefault();
        $('#Mask, .Window').hide();
    });
    $('#Mask').click(function () {
        $(this).hide();
        $('.Window').hide();
    });
}
function LaunchWindow(id) {
    var maskHeight = $(document).height();
    var maskWidth = $(window).width();

    $('#Mask').css({ 'width': maskWidth, 'height': maskHeight });

    $('#Mask').fadeIn(500);
    $('#Mask').fadeTo("fast", 0.8);

    var winH = $(window).height();
    var winW = $(window).width();

    $(id).css('top', winH / 2 - 300 + $(window).scrollTop());
    $(id).css('left', winW / 2 - $(id).width() / 2);

    $(id).fadeIn(1000);
}
function Customization() {
    $("#PrimaryColor").keyup(function () {
        var color = $(this).val();
        var length = color.length;
        if (length == 4 || length == 7) {
            $("#Header").css('background-color', color);

            var NewColor = "#";
            for (var i = 1; i < length; ++i) {
                if (color[i] >= 0 && color[i] < 9) {
                    NewColor += color[i] + 1;
                }
                else if (color[i] == '9') {
                    NewColor += 'a';
                }
                else if (color[i] == 'a' || color[i] == 'A') {
                    NewColor += '9';
                }
                else if (color[i] == 'b' || color[i] == 'B') {
                    NewColor += 'a';
                }
                else if (color[i] == 'c' || color[i] == 'C') {
                    NewColor += 'b';
                }
                else if (color[i] == 'd' || color[i] == 'D') {
                    NewColor += 'c';
                }
                else if (color[i] == 'e' || color[i] == 'E') {
                    NewColor += 'd';
                }
                else if (color[i] == 'f' || color[i] == 'F') {
                    NewColor += 'e';
                }
            }
            $(".Box2").css('border-color', NewColor);
            $(".Box2 h4").css('background-color', NewColor);
            $(".Box2 h4").css('border-bottom-color', NewColor);
            $("#Menu a").css('background-color', NewColor);
        }
    });
    $("#SecondaryColor").keyup(function () {
        var color = $(this).val();
        var length = color.length;
        if (length == 4 || length == 7) {
            $("a").css('color', color);
            $("#Header").css('border-bottom-color', color);
        }
    });
    $("#TextColor").keyup(function () {
        var color = $(this).val();
        var length = color.length;
        if (length == 4 || length == 7) {
            $("#Menu a").css('color', color);
            $(".Box2 h4").css('color', color);
        }
    });
}
