﻿// musste im Sortable Script die Stelle this.helper.css("position", "absolute") gegen this.helper.css("position", "fixed") austauschen
var newItem;
var contenOfNewItem;


// Main Sortable und Left Sortable
$(function () {
    $(".SortableMain, .SortableLeft").sortable({
        forcePlaceholderSize: true,
        placeholder: "moving",
        forceHelperSize: true,
        axis: 'y',
        cursor: 'pointer',
        opacity: 0.6,
        handle: 'a.lnkMove',
        helper: 'clone',
        //        connectWith: ".SortableMain, .SortableLeft",
        start: function (event, ui) {
        },
        update: function (event, ui) {
            UpdatePriority($(this).attr("lang") + "&templateID=" + $(newItem).attr("template"), $(this).sortable('toArray'));
            if (newItem != null) {
                newItem.before(contenOfNewItem).remove();
                ShowEditButtons(); //FrontendUtils
                InitEditor(); //FrontendUtils
                InitColorbox();
                InitTooltips();
            }
        },
        receive: function (event, ui) {
            ui.item.clone().prependTo($("div[lang=" + ui.item.attr("lang") + "]"));
            newItem = ui.item.addClass("pnlModuleWrapper").removeClass("pnlModule").css("background", "0").html("Modul wird hinzugefügt. Einen Moment bitte ...");
            return true;
        },
        over: function (event, ui) {
            ui.placeholder.addClass("movingModuleOver");
            ui.placeholder.removeClass("movingModule");
            return true;
        }
    });

    $("#FrontendAnyWhereEditor_0, #FrontendFlexSlideGalleries_0, #FrontendFlexAudio_0").click(function () {

        // create AnyWhereModule
        var newItem = $(this).clone();
        newItem.insertBefore(".homeLink")
            .css("background", "transparent");

        var targetContainerAttrLang = $(".pnlHeader").attr("lang");

        UpdatePriority(targetContainerAttrLang, newItem.attr("id"));

        if (newItem != null) {
            newItem.before(contenOfNewItem).remove();
            ShowEditButtons(); //FrontendUtils
            InitEditor(); //FrontendUtils
            InitColorbox();
            InitTooltips();
        }
    });

    // draggable for Flex Module
    $("#pnlPage .pnlAnyWhereContentModule, #pnlPage .pnlFlexSlideGallery, #pnlPage .pnlFlexAudioModule").live("mouseover", function () {
        var item = $(this);
        item.draggable({
            handle: ".pEditHeaderAnyWhereModule .lnkMove",
            containment: "#pnlPage",
            stop: function (event, ui) {
                var moduleName = item.attr("id")
                var zIndex;
                item.hasClass("flexInBackground") ? zIndex = -1 : zIndex = item.css("z-index");
                var itemID = item.attr("id").substr(item.attr("id").indexOf("_") + 1);
                var top = parseInt(ui.position.top, 10);
                var left = parseInt(ui.position.left, 10);
                var width = parseInt(item.css("width"), 10);
                var height = parseInt(item.height(), 10);

                // call ajax function and save css values
                SetCss(itemID, top, left, zIndex, width, height, moduleName);

                // set z-index without reload (SetCss cares for DB)
                item.attr("z-index", zIndex);
            }
        });
    });

    // resizable for Flex Module
    $(".pnlAnyWhereContentModule").live("mouseover", function () {
        var item = $(this);
        item.resizable({
            ghost: true,
            minHeight: 5,
            helper: "ui-resizable-helper",
            stop: function (event, ui) {
                var moduleName = item.attr("id");
                var itemID = item.attr("id").substr(item.attr("id").indexOf("_") + 1);
                var zIndex = item.css("z-index");
                var top = parseInt(item.css("top"), 10);
                var left = parseInt(item.css("left"), 10);
                item.css("min-height", parseInt(item.css("height"), 10));
                //item.css("height", "auto");

                // call ajax function and save css values
                SetCss(itemID, top, left, zIndex, ui.size.width, ui.size.height, moduleName);
            }
        });
    });

});

// Set Css of AWC Module
function SetCss(id, topPosition, leftPosition, zIndex, width, height, moduleName) {
    $.ajax({
        url: "/Module/Modules/SetCss?id=" + id + "&top=" + topPosition + "&left=" + leftPosition + "&zindex=" + zIndex + "&width=" + width + "&height=" + height + "&moduleName=" + moduleName
    });
}

// AdminPanelSortable (Items die in die SortableMain gezogen werden können)
$(function () {

    $("div.MainContainer").sortable({
        placeholder: " ",
        cursor: 'pointer',
        opacity: 0.6,
        helper: 'clone',
        connectWith: ".SortableMain",
        over: function (event, ui) {
            ui.placeholder.addClass("movingModule");
            ui.placeholder.removeClass("movingModuleOver");
            return true;
        }
    });

    $("div.LeftContainer").sortable({
        placeholder: " ",
        cursor: 'pointer',
        opacity: 0.6,
        helper: 'clone',
        connectWith: ".SortableLeft",
        over: function (event, ui) {
            ui.placeholder.addClass("movingModule");
            ui.placeholder.removeClass("movingModuleOver");
            return true;
        }
    });

    $("div.MainContainer.LeftContainer").sortable({
        placeholder: " ",
        cursor: 'pointer',
        opacity: 0.6,
        helper: 'clone',
        connectWith: ".SortableMain, .SortableLeft",
        over: function (event, ui) {
            ui.placeholder.addClass("movingModule");
            ui.placeholder.removeClass("movingModuleOver");
            return true;
        }
    });
});

function UpdatePriority(url, itemArray) {
    $.ajax({
        url: url + '&items=' + itemArray.toString(), // /Module/Modules/UpdatePriority
        async: false,
        success: function (data) {
            contenOfNewItem = data;
        }
    });
}
