﻿
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().parent();
    dock.addClass('loading');

    //do connect to webservice
    //$(document).ready(function() {  //had to comment this due to some strange behaviour in AddDockSelector.ascx
    $.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',
        success: function(msg) {
            //remove loading class and set html output to control
            dock.removeClass('loading');
            element.removeClass('error').html(msg.d);
            //make ie8 redraw
            ReDrawZones();

        },
        error: function(xhr) {
            dock.removeClass('loading');
            element.addClass('error').html('<h1>Failure</h1><p>This component failed to load</p><p style="display:none;">' + xhr.status + ' ' + 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");
    }

});



//add jquery extensions used by DockSelector
$("head").append("<script type='text/javascript' src='/templates/SSP.Dock/js/jquery.corner.js'><\/script>");
$("head").append("<script type='text/javascript' src='/templates/SSP.Dock/js/jqModal.js'><\/script>");
//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
    $("#" + id + " INPUT[type='checkbox']").attr('checked', !$("#" + id + " INPUT[type='checkbox']").attr('checked'));
}