﻿var widget_ActiveWidgetFlag = null;
var widget_Locked = false;
var LoginControlClientID = '';

function GetMasterPagerRoot(AppendID) {
    return "cphBody_" + AppendID;
}

function SetLoginControlRoot(ID) {
    LoginControlClientID = ID;
}

function GetLoginControlRoot(AppendID) {
    return LoginControlClientID + AppendID;
    
}

function widget_Initialize(WidgetContainerID, WidgetName, xPos, yPos, Width, Height, AllowResize, AllowMove, VisibleTitleBar, StaticTitlebar, cssTitlebar, cssFooterbar, Center, Snap, Visible, DropShadow, Border, CloseIconImagePath) {

    widget_InitWidgetTitlebar(WidgetName, cssTitlebar, VisibleTitleBar);
    widget_InitWidgetFooterbar(WidgetName, cssFooterbar, VisibleTitleBar);

    if (AllowMove) $("#" + WidgetName).draggable({ containment: "#" + WidgetContainerID, snap: Snap, cursor: 'move' });

    if (AllowResize) $("#" + WidgetName + '_Body').resizable({ animate: true });

    $("#" + WidgetName).mouseenter(function () {

        widget_ActiveWidgetFlag = WidgetName;
        widget_ResetZIndex();
        $("#" + WidgetName).css("z-index", "501");

        if (!StaticTitlebar) {

            $("#" + WidgetName + "_Header").fadeIn('fast');
            $("#" + WidgetName + "_Footer").fadeIn('fast');

        }

    });

    $("#" + WidgetName).mouseleave(function () {

        widget_ActiveWidgetFlag = null;
        if (!StaticTitlebar) setTimeout('if (widget_ActiveWidgetFlag != "' + WidgetName + '") $("#' + WidgetName + '_Header").fadeOut(); $("#' + WidgetName + '_Footer").fadeOut();', 500);

    });

    if (DropShadow) {

        $("#" + WidgetName + '_Body').css("box-shadow", '0px 0px 13px #000');
        $("#" + WidgetName + '_Body').css("-webkit-box-shadow", '0px 0px 13px #000');
        $("#" + WidgetName + '_Body').css("-moz-box-shadow", '0px 0px 13px #000');

    }

    if (Border) {
        $("#" + WidgetName + '_Body').css("border-width", Border);
        $("#" + WidgetName + '_Body').css("border-style", 'solid');
        $("#" + WidgetName + '_Body').css("border-color", 'black'); 
    }

    if (CloseIconImagePath) {

        widget_CreateWidgetIcon(WidgetName + '_Header', CloseIconImagePath, 'right', 0, 8, 10, 0, WidgetName + '_close');
        $('#' + WidgetName + '_close').click(function () {

            $("#" + WidgetName).fadeOut('slow', function () { widget_UnloadSource(WidgetName) });

        });

    }

    widget_SetCoordinateDefaults(WidgetContainerID, WidgetName, xPos, yPos, Width, Height, Center, Border);

    if (Visible == false) { $("#" + WidgetName).css("display", "none"); }
    else { $("#" + WidgetName).css("display", ""); }
}

function widget_SetCoordinateDefaults(WidgetContainerID, WidgetID, xPos, yPos, Width, Height, Center, Border) {

    if (!Border) { Border = 0; }

    if (xPos) $("#" + WidgetID).css("left", xPos);
    if (yPos) $("#" + WidgetID).css("top", yPos);
    if (Width) $("#" + WidgetID + '_Body').css("width", Width);
    if (Height) $("#" + WidgetID + '_Body').css("height", Height);
    if (Width) $("#" + WidgetID + '_Header').css("width", (Width + (Border * 2)));
    if (Height) $("#" + WidgetID + '_Footer').css("width", (Width + (Border * 2)));
    //if (Height) $("#" + WidgetID).css("filter", 'alpha(opacity=90)');
    //if (Height) $("#" + WidgetID).css("opacity", '0.90');
   
    if (Center) { $("#" + WidgetID).center({ inside: "#" + WidgetContainerID }); }
    $('#' + WidgetID + '_imgprogress').center({ inside: '#' + WidgetID + '_Body' });
}

function widget_InitWidgetFooterbar(ID, CssClass, Visible) {
    if ($("#" + ID) != null) {

        var TitleContainerID = ID + '_Footer_Container';
        var TitleID = ID + '_Footer';
        var elTitle = document.getElementById(TitleID);
        var elTitleContainer = document.getElementById(TitleContainerID);

        elTitleContainer.style.height = elTitle.offsetHeight + 'px';
        if (!Visible) { elTitle.style.display = 'none'; elTitleContainer.style.display = 'none'; }
        else { elTitle.style.display = ''; elTitleContainer.style.display = ''; }
    }

}

function widget_InitWidgetTitlebar(ID, CssClass, Visible) {

    if ($("#" + ID) != null) {

        var LoadingDivID = ID + '_loading';
        var TitleContainerID = ID + '_Header_Container';
        var TitleID = ID + '_Header';
        var elTitle = document.getElementById(TitleID);
        var elTitleContainer = document.getElementById(TitleContainerID);
        var elLoading = document.getElementById(LoadingDivID);
        elTitleContainer.style.height = elTitle.offsetHeight + 'px';
        if (!Visible) { elTitle.style.display = 'none'; elTitleContainer.style.display = 'none'; }
        else { elTitle.style.display = ''; elTitleContainer.style.display = ''; }

   }

}

function widget_CreateWidgetIcon(TitleBarID, IconPath, Align, Left, Top, Right, Bottom, ID) {

    if ($('#' + TitleBarID) != null) {

        var elIcon = document.createElement('img');

        elIcon.src = IconPath;
        elIcon.style.position = 'relative';
        elIcon.style.marginTop = Top + 'px';
        elIcon.style.marginLeft = Left + 'px';
        elIcon.style.marginRight = Right + 'px';
        elIcon.style.marginBottom = Bottom + 'px';
        elIcon.style.cursor = 'pointer';
        elIcon.align = Align;        
        elIcon.id = ID;

        $('#' + TitleBarID).append(elIcon);
  
    }
}

function widget_ResetZIndex(WidgetContainer) {

    $("div").each(function (index, domEle) {

        if (domEle.getAttribute('IsWidget')) {

            $(domEle).css("z-index", "500");

        }


    });

}

function widget_getCss(styleSheet, selector, property) {
    var i, r, s = document.styleSheets && document.styleSheets[styleSheet]; if (s) {
        r = s.rules ? s.rules : s.cssRules; if (r) {
            i = r.length; while (i--) {
                if (r[i].selectorText.toLowerCase() === selector.toLowerCase()) {
                    return (r[i].style[property]);
                }
            }
        }
    }
    return null;
}

function widget_HideLoadingDiv(WidgetID) {

        $('#' + WidgetID + '_loading').fadeOut('slow');
    
}

function widget_LoadWidget(Animate, WidgetID, AppSRC, LoadInterval) {

    $(document).ready(function () {
        var LoggedIn = $('#' + GetLoginControlRoot('login_LoginState')).val();
        var OpenSessions = $('#' + GetLoginControlRoot('login_LoadingState')).val();


        if (LoggedIn == 'true') {

            if (Animate) { $("#" + WidgetID).fadeIn(); }
            $('#' + WidgetID + '_imgprogress').center({ inside: '#' + WidgetID });

            widget_LoadIFrame(WidgetID, AppSRC);

        }
        else {

            $('#' + GetLoginControlRoot('login_LoadingState')).val(OpenSessions + WidgetID + ',');

            widget_UnloadSource(WidgetID);

            login_ShowLogin(true);

            if (Animate) { $("#" + WidgetID).fadeIn(); }
        }
    });

}

    function widget_UnloadSource(WidgetID) {

        $('#' + WidgetID + '_loading').css("display", '');
        $('#' + WidgetID + '_imgprogress').center({ inside: '#' + WidgetID });
        $('#' + WidgetID).find('iframe').attr('src', '');

    }

    function widget_LoadIFrame(WidgetID, url, callback) {

        $('#' + WidgetID).find('iframe').attr('src', url);
        $('#' + WidgetID).find('iframe').load(function () {

            $('#' + WidgetID + '_loading').fadeOut('slow');

        });

    }


