﻿//
// LINK LIST
//
var _LinkListCurrentFieldID;
var _LinkListCurrentLinkIndex;
var _LinkListTmpHtml;
var _CurrentLinkListModal;

function editLinkList(pageUrl, wpGuid) {

    var dialogHtmlObj = document.getElementById("lnd_linklistdialog");
    var data = { "a": "render", "pageUrl": pageUrl, "wpGuid": wpGuid, "r": Math.floor(Math.random() * 10000) };

    jQuery.ajax({
        type: "GET",
        async: false,
        url: "/_layouts/lindabgroup/services/LinkListService.ashx",
        data: data,
        success: function (data) {
            $("#lnd_linklistdialog").html(data);
        }
    });

    var dialogOptions = {
        html: dialogHtmlObj,
        title: 'Links',
        allowMaximize: false,
        showClose: true,
        width: 500,
        height: 500,
        dialogReturnValueCallback: editLinkListClose
    };
    _CurrentLinkListModal = SP.UI.ModalDialog.showModalDialog(dialogOptions);
}

function editLinkListClose(result, target) {


    //lägg tillbaks elementet så dialogen kan öppnas igen
     $("#lnd_linklistwrapper").prepend("<div id='lnd_linklistdialog'></div>");
     _CurrentLinkListModal = null
    //: ladda om content i WP
    var fullUrl = document.location.href
    var data = { "a": "renderList", "pageUrl": fullUrl, "wpGuid": result, "r": Math.floor(Math.random() * 10000) };
    jQuery.ajax({
        type: "GET",
        async: true,
        url: "/_layouts/lindabgroup/services/LinkListService.ashx",
        data: data,
        success: function (data) {
            $("#lnd-linklist-container").html(data);
        }
    });
}

function editLinkListField(area, pageUrl, wpGuid) {

    var index = $(area).parent().parent().attr("id").replace("link_", "");
    var txtLabel = document.getElementById("linkLabel_" + index);
    var txtUrl = document.getElementById("linkUrl_" + index);
    var chkOpenInNewWindow = document.getElementById("linkOpenInNewWindow_" + index);

    var data = { "a": "text", "pageUrl": pageUrl, "wpGuid": wpGuid, "linkLabel": txtLabel.value, "linkUrl": txtUrl.value, "linkOpenInNewWindow": chkOpenInNewWindow.checked, "index": index, "r": Math.floor(Math.random() * 10000) };

    jQuery.ajax({
        type: "GET",
        async: true,
        url: "/_layouts/lindabgroup/services/LinkListService.ashx",
        data: data,
        success: function (data) {
            $("#lnd_linklistdialog").html(data);
        }
    });
}

function saveWpTitle(area, pageUrl, wpGuid) {

    var wpTitle = area.value;
    var data = { "a": "saveWpTitle", "pageUrl": pageUrl, "wpGuid": wpGuid, "text": wpTitle, "r": Math.floor(Math.random() * 10000) };

    jQuery.ajax({
        type: "GET",
        async: true,
        url: "/_layouts/lindabgroup/services/LinkListService.ashx",
        data: data,
        success: function (data) {
        }
    });
}

function saveWpDescription(area, pageUrl, wpGuid) {

    var wpDescription = area.value;
    var data = { "a": "saveWpDescription", "pageUrl": pageUrl, "wpGuid": wpGuid, "text": wpDescription, "r": Math.floor(Math.random() * 10000) };

    jQuery.ajax({
        type: "GET",
        async: true,
        url: "/_layouts/lindabgroup/services/LinkListService.ashx",
        data: data,
        success: function (data) {
        }
    });
}

function editLinkListBrowse() {
    var dialogUrl = '/_layouts/AssetPortalBrowser.aspx?DisplayLookInSection=true';
    var features = 'resizable: yes; status: no; scroll: no; help: no; dialogWidth:840px; dialogHeight:720px;';
    var rv = commonShowModalDialog(dialogUrl, features, editLinkListAddLinkCallback);
}

function editLinkListAddLinkCallback(obj) {
    if (obj != null) {
        var linkUrl = getRelativeUrl(obj['AssetUrl']);
        var txtUrl = document.getElementById("newLinkUrl");
        txtUrl.value = linkUrl;
    }
}

function editLinkListAddLink(pageUrl, wpGuid) {
    var txtLabel = document.getElementById("newLinkLabel");
    var txtUrl = document.getElementById("newLinkUrl");
    var chkOpenInNewWindow = document.getElementById("newLinkOpenInNewWindow");

    var data = { "a": "add", "pageUrl": pageUrl, "wpGuid": wpGuid, "linkLabel": txtLabel.value, "linkUrl": txtUrl.value, "linkOpenInNewWindow": chkOpenInNewWindow.checked, "r": Math.floor(Math.random() * 10000) };

    jQuery.ajax({
        type: "GET",
        async: false,
        url: "/_layouts/lindabgroup/services/LinkListService.ashx",
        data: data,
        success: function (data) {
            $("#lnd_linklistdialog").html(data); //Get the html from service!
        }
    });
}

function editLinkListButtonClick(obj, pageUrl, wpGuid) {

    var action = $(obj).attr("class");
    var index = $(obj).parent().parent().parent().attr("id").replace("link_", "");

    if (action == "delete") {
        $(obj).css("background-position-y", "-20px");
        if (confirm("Do you want to remove this link from the list?")) {
            var data = { "a": "delete", "pageUrl": pageUrl, "wpGuid": wpGuid, "index": index, "r": Math.floor(Math.random() * 10000) };
            jQuery.ajax({
                type: "GET",
                async: true,
                url: "/_layouts/lindabgroup/services/LinkListService.ashx",
                data: data,
                success: function (data) {
                    $(obj).css("background-position-y", "0px");
                    $("#link_" + index).slideUp("fast", function () {
                        $("#link_" + index).remove();
                        //reset indexes
                        $("#linkListContainer").children().each(function (i) {
                            $(this).attr("id", "link_" + i);
                            $(this).children().first().children().first().attr("id", "linkLabel_" + i);
                            $(this).children().last().children().first().attr("id", "linkUrl_" + i);
                            $(this).children().last().children().last().attr("id", "linkOpenInNewWindow_" + i);
                        });
                    });
                }
            });
        }
    }
    if (action == "moveUp" && index != "0") {
        $(obj).css("background-position-y", "-20px");
        var data = { "a": "moveUp", "pageUrl": pageUrl, "wpGuid": wpGuid, "index": index, "r": Math.floor(Math.random() * 10000) };
        jQuery.ajax({
            type: "GET",
            async: true,
            url: "/_layouts/lindabgroup/services/LinkListService.ashx",
            data: data,
            success: function (data) {
                $("#link_" + index).prev().before($("#link_" + index));
                $(obj).css("background-position-y", "0px"); //reset indexes
                $("#linkListContainer").children().each(function (i) {
                    $(this).attr("id", "link_" + i);
                    $(this).children().first().children().first().attr("id", "linkLabel_" + i);
                    $(this).children().last().children().first().attr("id", "linkUrl_" + i);
                    $(this).children().last().children().last().attr("id", "linkOpenInNewWindow_" + i);
                });
            }
        });
    }
    if (action == "moveDown" && $("#link_" + index).next().attr("id") != null) {
        $(obj).css("background-position-y", "-20px");
        var data = { "a": "moveDown", "pageUrl": pageUrl, "wpGuid": wpGuid, "index": index, "r": Math.floor(Math.random() * 10000) };
        jQuery.ajax({
            type: "GET",
            async: true,
            url: "/_layouts/lindabgroup/services/LinkListService.ashx",
            data: data,
            success: function (data) {
                $("#link_" + index).next().after($("#link_" + index));
                $(obj).css("background-position-y", "0px"); //reset indexes
                $("#linkListContainer").children().each(function (i) {
                    $(this).attr("id", "link_" + i);
                    $(this).children().first().children().first().attr("id", "linkLabel_" + i);
                    $(this).children().last().children().first().attr("id", "linkUrl_" + i);
                    $(this).children().last().children().last().attr("id", "linkOpenInNewWindow_" + i);
                });
            }
        });
    }
}


function getRelativeUrl(url) {
    var urlStart = location.protocol + "//" + location.host;
    url = url.replace(urlStart, "");
    var hashindex = (url.indexOf("#") == -1) ? 10000 : url.indexOf("#");
    var queryindex = (url.indexOf("?") == -1) ? 10000 : url.indexOf("?");
    var index = Math.min(hashindex, queryindex);
    if (index < 10000) {
        url = url.substr(0, index);
    }
    return url;
}
