﻿
function SavedDockLayout() { $("#DockLayoutSaving").hide(); }
function ClientDockPositionChanged(obj, e) { $("#DockLayoutSaving").show(); }

function fetchContent(id, virtualPath, pageLink, languageBranch, uniquename, autoRefresh, clientid, donotpersistchanges) {
    //get element
    var element = $('#' + id);
    //add loading to css class
    var dock = element.parent().parent();

    //do connect to webservice

    $(document).ready(function () {
        $.ajax({
            type: 'POST',
            url: '/SSP.Dock/Services/DockService.asmx/GetUserControl',
            data: "{'virtualPath':'" + virtualPath + "', 'pageLink': '" + pageLink + "', 'languageBranch':'" + languageBranch + "','uniquename':'" + uniquename + "', 'clientid':'" + clientid + "','donotpersistchanges':'" + donotpersistchanges + "'}",
            contentType: 'application/json; charset=utf-8',
            dataType: 'json',
            beforeSend: function () {
                dock.addClass('loading');
            },
            success: function (data, status, xhr) {
                //remove loading class and set html output to control
                dock.removeClass('loading');
                element.removeClass('error').html(data.d);
                //make ie8 redraw
                ReDrawZones();
            },
            timeout: 90000,
            error: function (xhr, status, error) {
                dock.removeClass('loading');
                if (status == "timeout") {
                    element.addClass("error").html('<h1>Timeout</h1><p>This component took to long to execute. </p><p style="font-size:xx-small;">.ajax error timeout</p>');
                }
                else {
                    element.addClass('error').html('<h1>Failure</h1><p>This component failed to load</p><p style="font-size:xx-small;white-space:pre;">xhr-status:' + xhr.status + '<br />xhr-response:' + xhr.responseText + '</p>');
                }
            }
        });

    });

    //reload
    if (autoRefresh != 0) {
        window.setTimeout(function () { fetchContent(id, virtualPath, pageLink, languageBranch, uniquename, autoRefresh, clientid, donotpersistchanges); }, autoRefresh);
    }
}

function openAddDocks() {
    //fetchContent("addDocks", "~/templates/SSP.Dock/Units/DockSelector.ascx", pageLink, "", 0);
    $("#addDocks").slideDown(1000);
}
function closeAddDocks() {
    $("#addDocks").slideUp(500);
    //setTimeout(function() { $("#addDocks").html(""); }, 500);
}

$.fn.check = function (mode) {
    var mode = mode || 'on'; // if mode is undefined, use 'on' as default
    return this.each(function () {
        switch (mode) {
            case 'on':
                this.checked = true;
                break;
            case 'off':
                this.checked = false;
                break;
            case 'toggle':
                this.checked = !this.checked;
                break;
        }
    });
};

function toggleVisibility(id1, id2) {
    if (id1) $("#" + id1).slideToggle('fast', ReDrawZones);
    if (id2) $("#" + id2).slideToggle('fast', ReDrawZones);
}
function ReDrawZones() {
    //make zones redraw
    $(".raddockzone").hide().show();
}



$(document).ready(function () {
    // if in editmode - set the .rdCommand width to auto (some kind of Telerik bug)
    if (typeof (isEditMode) == "function" && isEditMode()) {
        $(".rdCommands").attr("style", "width: auto");
    }

});



//round corners in dockselector
$(document).ready(function () {
    $(".DockSelectorDockItem").corner(".5%").click(function () { toggleCheck(this.id); });

    //move the help panels to an outer div (problem with positioning after introducing "corner")
    $(".DockSelectorDockItemHelp").appendTo($("body")).jqm({ overlay: 40, overlayClass: 'DockSelectorDockItemHelpOverlay' });
});

//check helper for DockSelector
function toggleCheck(id) {
    //will find a decendant checkbox and revert the "checked" status
    var checkBox = $("#" + id + "checkbox");
    checkBox.attr('checked', !checkBox.attr('checked'));
    UpdateSelectedDockItems(checkBox.attr('title'), checkBox.attr('checked'));
}

/// Will update textbox 'idOfInput' with a comma-separated list of dockItems. If checked=true it will add, false will remove
function UpdateSelectedDockItems(dockItem, checked) {
    var txtSelected = $('.SelectedDockItems');
    var original = txtSelected.val();
    var dockItems = original.split(",");
    if (checked == false) {
        var found = false;
        var i = 0;
        for (var i = 0; found == false && i < dockItems.length; i++) {
            if (dockItems[i] == dockItem) {
                dockItems.splice(i, 1);
                found = true;
            }
        }
    }
    else {
        dockItems.push(dockItem);
    }
    txtSelected.val(dockItems.join(","));
}





$(document).ready(function () {

    $(".rdContent input[onclick*='EPi.CreatePageBrowserDialog']").mousedown(function () {
        //override the default EPiServer behavoiur to position the dialog.
        //Something goes wrong in some cases that causes the dialog to be positioned outside the visible area on the desktop
        //This solution just positions the dialog 100px from the top and 150px from the left
        _p.top = 100;
        _p.left = 150;
    });

});

/* Fix RADDock bug */

Telerik.Web.UI.RadDock.prototype._fixEmWidth = function () { /*do nothing*/return; }
