﻿$("window").ready(function () {
    var tabs = $(".po-site-tabs");

    if (IsPDFRequest()) {
        $("#po_master_topbar").hide();
        $("#po_taskbar").hide();
        $("body").css("background-image", "");
    }
    else {
        GetCurrentTaskbar();
    }

    SetCurrentFocusImage(GetSitePagePhotoURL());
    if (IgnoreFocus()) HideFocusText();

    $("#po_site_searchbox").smartFocus(SMARTFOCUS_SEARCHBOX);

    ShowSiteTooltips(false);

    $(window).trigger("resize");


    if (($(".po-dashboard-allowprint").length > 0 || $(".po-report-allowprint").length > 0) && $("#po_site_printbtn").length > 0) {
        $("#po_site_printbtn").css("visibility", "visible");

        if (($(".po-dashboard-allowmsword").length > 0 || $(".po-report-allowmsword").length > 0) && $("#po_site_printwordbtn").length > 0) {
            $("#po_site_printwordbtn").css("visibility", "visible");
        }

        if (($(".po-dashboard-allowexcel").length > 0 || $(".po-report-allowexcel").length > 0) && $("#po_site_printexcelbtn").length > 0) {
            $("#po_site_printexcelbtn").css("visibility", "visible");
        }
    }

    if (($(".po-dashboard-course").length > 0 || $(".po-report-course").length > 0) && $("#po_site_helpbtn").length > 0) {
        $("#po_site_helpbtn").css("visibility", "visible");
    }
});

function IsPDFRequest() {
    var o = $("#po_pdf");
    return o.length > 0 && o.val() == "true";
}

function HideFocusText() {
    var focus = $("#po_currentfocus");
    if (focus.length > 0) focus.text("");
}


function SetCurrentFocusImage(url)
{
    if (url==undefined || url==null) return;
    var focusimg = $("#po_currentfocus_img");
    if (focusimg.length>0)  focusimg.attr("src",url);
}

$(window).resize(function () {
    AdjustSite();
    ShowSiteTooltips(true);
});



function OpenSiteReportWindowById(rptid, title, params, w, h) {
    if (w==null || w==undefined) w = $(window).width() - 80;
    if (h == null || h == undefined) h = $(window).height() - 80;
    if (w > $(window).width() - 80) w = $(window).width() - 80;
    if (h > $(window).height() - 80) h = $(window).height() - 80;

    var url = "/Business/Report/Default.aspx?" + params;
    url = SetQueryString(url, "iid", rptid);
    ShowWindow('rpt_site_detail', url, title, null, w, h);
}


function OpenSiteReportWindowByRenderName(rendername, title, params, w, h) {
    if (w == null || w == undefined) w = $(window).width() - 80;
    if (h == null || h == undefined) h = $(window).height() - 80;
    if (w > $(window).width() - 80) w = $(window).width() - 80;
    if (h > $(window).height() - 80) h = $(window).height() - 80;

    var url = "/Business/Report/Default.aspx?" + params;
    url = SetQueryString(url, "rendername", rendername);
    ShowWindow('rpt_site_detail', url, title, null, w, h);
}

function AdjustSite() {
    
    if ($("#po_master").length == 0) return;
    if ($(".po-site-dashboard").length>0) AdjustDashboard();

    OnContextDialogResize(false);
}

function LoadSiteTopBarUserMenu(top, left) {
    var usermenu = $("#po_site_usermenu");
    if (usermenu.length == 0) {
        var url = "/Business/Site/UserMenu." + CurrentLCID() + ".htm";
        $.get(DynUrl(url), function (c) {
            $("#po_master").after(c);
            $("#po_site_usermenu").attr("show", "true").show().css("left", left).css("top", top);
            $(".po-site-usermenuitem").hover(function () { $(this).prev().children(":first").attr("src", "/Business/Site/Images/Arrow.gif"); },
            function () { $(this).prev().children(":first").attr("src", "/Images/PH_16x1.gif"); });

        });
    }
    else {
        if (usermenu.attr("show") == "true") {
            HideSiteTopBarUserMenu();
        }
        else {
            usermenu.attr("show", "true").show().css("left", left).css("top", top);
        }
    }
}

function HideSiteTopBarUserMenu() {
    $("#po_site_usermenu").attr("show", "false").hide();
}

$(document).click(function (event) {
    if (!$(event.target).hasClass("po-site-userinfo")) {
        HideSiteTopBarUserMenu();
    }
});

$(window).ready(function () {
    var msg = $("#ctl00__exception").val();
    if (msg != "" && msg != MSG_SESSIONEXPIRED) {
        $("#po_master").hide();
        OnError($("#ctl00__exception").val());
        return;
    }

    var po_master = $("#po_master");
    if (po_master.attr("topbarbgimg") != undefined && po_master.attr("topbarbgimg") != "") {
        $("body").addClass("po-master-bgcolor");
        var bgcolor = $("body").css("background-color");
        $("body").css("background-image", "url(" + po_master.attr("topbarbgimg") + ")").css("background-repeat", "repeat-x").css("background-position", "top").css("background-color", bgcolor);
    }

    $(".fg-button-icon-right", $("#po_master_menu")).css("padding-right", "20px");

    $(".fg-button").css("margin-left", "0").css("margin-right", "3").hover(
            function () { $(this).removeClass('ui-state-default').addClass('ui-state-focus'); },
            function () { $(this).removeClass('ui-state-focus').addClass('ui-state-default'); }
        );

    if ($("#_userid").length == 0 || $("#_userid").val() == "") {
        var loginpagehtml = $("#po_site_loginpagehtml");
        if (loginpagehtml.length > 0 && $("#po_site_loginbg", loginpagehtml).length > 0) {
            $("body").append(loginpagehtml.children());
            var lc = $("#po_site_loginbg");
            var logindlgx = parseInt(lc.attr("logindlgx"));
            var logindlgy = parseInt(lc.attr("logindlgy"));

            if (logindlgx != undefined && logindlgy != undefined) {
                RenderLoginDialog(false, parseInt(logindlgx), parseInt(logindlgy),
                function () {
                    $(window).trigger("resize");
                });
            }
            else {
                RenderLoginDialog(false);
            }
        }
        else {
            po_master.html("");
            RenderLoginDialog();
        }

        $(window).trigger("resize");
    }

    if ($("#po_pdf").val() != "true") {
        $("#po_site_sidenote").click(function (e) {
            var noterename = $("#po_el_note_rename");
            var noteriid = $("#po_el_note_riid");
            if (noterename.length > 0 && noteriid.length > 0 && noteriid.val() != "" && noterename.val() != "") {
                ShowWindow("SideNotes", "/Business/ListPlayer/Player.aspx?ename=Note&laname=&rename=" + noterename.val() + "&riid=" + noteriid.val(), TITLE_MYNOTES, null, null, null, null, null, true);
            }
            else {
                ShowWindow("SideNotes", "/Business/ListPlayer/Player.aspx?ename=Note", TITLE_MYNOTES, null, null, null, null, null, true);
            }
        });
        $("#po_site_sidecal").click(function (e) {
            var w = $(window).width() - 40;
            var h = $(window).height() - 40;
            ShowWindow("SideCalendar", "/Business/Calendar/Calendar.aspx", TITLE_MYCALENDARS, null, w, h, null, null, true);
        });

        $("#po_site_sidequestion").click(function (e) {
            if (!jQuery.isFunction(OnSideQAClick)) {
                ShowWindow("SideQA", "/Business/Forum/Player.aspx?authorid=" + $("#_userid").val(), TITLE_MYQUESTIONS, null, null, null, null, null, true);
            }
            else {
                OnSideQAClick();
            }
        });


        $("#po_site_sideemail").click(function (e) {
            if (!jQuery.isFunction(OnSideEmailClick)) {
                var w = $(window).width() - 40;
                var h = $(window).height() - 40;
                ShowWindow("SideEmail", "/List.aspx?ename=Email", TITLE_MYEMAILS, null, w, h, null, null, true);
            }
            else {
                OnSideEmailClick();
            }

        });

        $("#po_site_searchbox").keypress(function (e) {
            if (e.which == 13) { $("#po_site_searchbox_btn").click(); return false; }
        });

        $("#po_site_searchbox_btn").click(function (e) {
            var keyword = encodeURIComponent($("#po_site_searchbox").val());
            if (SMARTFOCUS_SEARCHBOX != keyword && keyword.length > 0) {
                window.location = "/" + $("#_apppath").val() + "/Search.aspx?FreeTextSearch=" + keyword;
            }
        });
    }
    else {
        HideSideButtons();
    }

    $(".po-sidebtn").hover(
            function () { $(this).removeClass('ui-state-default').addClass('ui-state-focus'); },
            function () { $(this).removeClass('ui-state-focus').addClass('ui-state-default'); }
        );

    $(".po-sidebtn-ico").click(function () {
        $(this).prev().click();
    });

    $(".po-sidebtn-ico").hover(
            function () { $(this).prev().removeClass('ui-state-default').addClass('ui-state-focus'); },
            function () { $(this).prev().removeClass('ui-state-focus').addClass('ui-state-default'); }
        );

    InitMenus();


})

function HideSideButtons() {
    $("#po_site_sidenote").hide();
    $("#po_site_sidecal").hide();
    $("#po_site_sideemail").hide();
    $("#po_site_sidequestion").hide();
    $(".po-sidebtn-ico").hide();
}



$(window).resize(function () {
    InitMenus();
})


function InitMenus() {
    if ($("#_isguest").length>0 && $("#_isguest").val().toLowerCase() == "true") {
        $("#po_menu_logout").hide();
    }
    else {
        $("#po_menu_login").hide();
    }

    $(".po-site-menu").each(
        function () {
            $(this).menu({
                content: $(this).next().html(), // grab content from this page
                flyOut: true,
                zIndex: 99999
            });
        });
    $(".po-site-menu").css("margin-right", 2);

   

    if ($("#po_site_usermenu").length > 0) $("#po_site_usermenu").hide();
}


function LogoutSite() {
    ShowMessageDialog("ProgressDlg", MSG_LOGOUT_TITLE, MSG_LOGOUT_WAIT, 300, 100, null);
    var command = PlatformCommand("Logout", "Context", "Logout", "void", true);
    ExecuteCommand(command, OnLogoutSiteSuccess, OnError);
}

function OnLogoutSiteSuccess(v) {
    window.location = $("#po_site_apppath").val() + "/Default.aspx?logout=true&AllowGuest=false";
}


function ChangeCurrentRole() {
  
    var command = BusinessCommand("RenderRoleListDialog", "AppSite", "RenderRoleListDialog", "System.String");
    AddCommandParameter(command, "width", "System.Int32",260);
    ExecuteCommand(command, function (v) {
        if (v == "") {
            ShowMessageDialog("WarningDlg", MSG_WARNING_TITLE, MSG_SINGLEROLEPROFILE);
        }
        else {
            ShowSimpleListDialog(MSG_SINGLEROLEPROFILE, v, function (o) {
                SetMenuProfile(o.attr("key"));
            })
        }
    }, OnError);
}

function SetMenuProfile(v) {
    
    if (v == undefined || v == "") { ShowMessageDialog("WarningDlg", MSG_WARNING_TITLE, MSG_SELECTROLEPROFILE_WARNING); return; }
    ShowMessageDialog("ProgressDlg", MSG_PROCESSING_TITLE, MSG_CHANGECURRENTROLE);
    var command = PlatformCommand("ChangeCurrentRole", "Context", "ChangeCurrentRole", "Void");
    AddCommandParameter(command, "roleId", "System.Guid", v);
    ExecuteCommand(command, function () {
        FlushUserSession(MSG_CHANGECURRENTROLE, '/' + $('#_apppath').val() + '/Default.aspx', '');
    }, OnError);

}

function ChangeCurrentTheme(name) {
    var command = PlatformCommand("ChangeCurrentTheme", "Context", "ChangeCurrentTheme", "Void");
    AddCommandParameter(command, "themeName", "System.String", name);
    ExecuteCommand(command, function () {
        FlushUserSession(MSG_CHANGETHEME, '/' + $('#_apppath').val() + '/Default.aspx', '');
    }, OnError);

}

function CustomizeHomePage() {
    window.location = $("#po_site_apppath").val() + "/Default.aspx?navid=home&customization=true";
}

function AddWebPart() {
    var cot = GetQueryStringValue(window, "customization");
    if (cot != null && cot == "true") {
        ShowMessageDialog("ConfirmationDlg", MSG_SAVECONTENT_TITLE, "Do you want to save your customization first ?", 300, 200,
        { "No": function () { HideMessageDialog(); window.location = $("#po_site_apppath").val() + "/Default.aspx?navid=home&customization=true&addwebpart=true"; },
            "Yes": function () { HideMessageDialog(); SaveCustomizeHomePage(function () { window.location = $("#po_site_apppath").val() + "/Default.aspx?navid=home&customization=true&addwebpart=true"; }); }
        });
    } else {
        window.location = $("#po_site_apppath").val() + "/Default.aspx?navid=home&customization=true&addwebpart=true";
    }
}

function ShowAddWebPartForm() {
    var command = BusinessCommand("RenderWebPartListDialog", "AppSite", "RenderWebPartList", "System.String");
    AddCommandParameter(command, "appPath", "System.String", $("#po_site_apppath").val());
    AddCommandParameter(command, "width", "System.Int32", 260);
    ExecuteCommand(command, function (v) {
        if (v == "") {
            ShowMessageDialog("WarningDlg", MSG_WARNING_TITLE, MSG_SINGLEROLEPROFILE);
        } else {
            ShowSimpleListDialog('Add Web Part', v, function (webPart) {
                var webPartName = webPart.text();
                var webPartUrl = webPart.attr("key");
                if (webPartName != null && webPartName.length > 0) {

                    var dashboard = $("#dashboard");
                    var theWebPartNotExist = true;
                    dashboard.find("#parttitle").each(function () {
                        var h3 = $(this);
                        if (h3.attr("name") == webPartName) {
                            theWebPartNotExist = false;
                        }
                    });
                    if (theWebPartNotExist) {
                        dashboard.find("div").each(function () {
                            var div = $(this);
                            if (div.attr("sequence") == "0") {
                                var firstWebPart = $(div.find("div")[0]);
                                var customization = $(firstWebPart.find("#parttitle")[0]).attr("customization");
                                var width = firstWebPart.width();
                                div.prepend("<div class=\"po-site-webpart ui-widget-content  ui-corner-all\" style=\"width:" + width + "px;height:200px;\">" +
                                       "<div id='parttitle' customization=\"" + customization + "\" name=\"" + webPartName + "\" class=\"ui-widget-header po-site-webpart-header ui-corner-all po-site-webpart-header-nohide\">" + webPartName + "</h3>" +
                                       "<div id='partcontent' class=\"po-site-webpart-content\"><iframe class=\"po-iframe\" frameborder=\"0\" src=\"" + webPartUrl + "\"></iframe></div>" +
                                       "</div>");
                                InitWebPartHeader($(div.find("#parttitle")[0]));
                                MakeWebPartResizable(div.children(":first"));
                                AdjustWebPartContent($(".po-site-webpart-content", div.children(":first")));
                            }
                        });
                    }
                }
            })
        }
    }, OnError);
}

function SaveCustomizeHomePage(fun) {
    var xml = "";
    var dashboard = $("#dashboard");
    xml += "<Dashboard Width=\"" + dashboard.width() + "\">";
    dashboard.children().each(function () {
        var wpw = $(this);
        xml += "<WebPartWrapper Width=\"" + wpw.width() + "\">";

        wpw.children().each(function () {
            var wp = $(this);
            var h3 = $(wp.find("h3")[0]);
            var iframe = $(wp.find("iframe")[0]);
            var hideHeader = !h3.hasClass("po-site-webpart-header-nohide");

            xml += "<WebPart Height=\"" + wp.height() + "\" HideHeader=\"" + hideHeader + "\" HeaderCaption=\"" + h3.attr("name") + "\" ContentURL=\"" + iframe.attr("src") + "\" />";
        });
        xml += "</WebPartWrapper>";
    });
    xml += "</Dashboard>";

    var customXml = CreateXmlDoc(xml);
    var xmlDoc = CreateXmlDoc("<SystemUser><SystemUserId>" + $("#_userid").val() + "</SystemUserId></SystemUser>");

    xml = xml.replace(/\&/g, "&amp;");

    var xmlElem = xmlDoc.createElement("DashboardXml");
    if (jQuery.browser.msie) {
        xmlElem.text = xml;
    }
    else {
        xmlElem.textContent = xml;
    }
    xmlDoc.documentElement.appendChild(xmlElem);

    var command = GenericAPICommand("UpdateDashboardXml", "Update", "System.Guid");
    AddCommandParameter(command, "xmlDocData", "System.Xml.XmlDocument", GetXmlDocContent(xmlDoc));
    if (fun == null) {
        ExecuteCommand(command, function () { window.location = $("#po_site_apppath").val() + "/Default.aspx?navid=home&guid" + Guid(); }, OnError);
    } else {
        ExecuteCommand(command, fun, OnError);
    }
}

function ShowDialogForNewIssue() {
    var url = "/Form.aspx?ename=ProductIssue&NoRibbon=true&nonav=true";
    ShowWindow("CreateIssue", url, "Submit an issue", { 'Submit': function () {
        $("#ifadlgCreateIssue")[0].contentWindow.SaveRecord(null, function (iid) { OnSubmitNewIssue(iid); });
    }
}, null, null, null, null, true);
}

function OnSubmitNewIssue(iid) {
    $("#dlgCreateIssue").dialog("close");
    ShowMessageDialog("ConfirmationDlg", MSG_CONFIRMDEL_TITLE, "Your issue has been submitted successfully", 300, 200,
    {
        "I'm done": function () { HideMessageDialog(); },
        "Show Issue List": function () {
            HideMessageDialog();
            ShowWindow("IssueList", "/List.aspx?ename=ProductIssue", "Issue List");
        }
    });
}


function IsFullPageSite() {
    var fp = $("#po_site_fullpage");
    if (fp.length == 0) return false;
    return fp.val().toLowerCase() == "true" || $(".po-site-dpartwrapper-winwadj").length > 0;
}



var _changeSiteFocusPostFunc = null;
var _changeSiteFocusCode = null;
var _changeSiteFocusParamName = null;
var _changeSiteFocusURL = null;

function ChangeSiteFocusRedirect(focus, url) {
    _changeSiteFocusURL = SetQueryString(url,"focus",focus);
    ChangeSiteFocus(focus, null, function () { window.location = _changeSiteFocusURL; });
}

function ChangeSiteFocus(focus, pname, func) {
    _changeSiteFocusCode = focus;
    _changeSiteFocusParamName = pname;
    if (func == null || func == undefined) {
        _changeSiteFocusPostFunc = function () {
            window.location = "/" + $("#_apppath").val() + "/Default.aspx?focus=" + _changeSiteFocusCode + "&" + _changeSiteFocusParamName + "=" + $("#_cur" + _changeSiteFocusParamName).val();
        }
    }
    else {
        _changeSiteFocusPostFunc = func;
    }

    $("#po-site-changefocus-ico").attr("src", "/Business/Site/Images/SiteTopBarUnSelected.jpg");
    $("#po_site_changefocus_ico_" + focus).attr("src", "/Business/Site/Images/SiteTopBarSelected.jpg");


    $.get("/Default.aspx?Focus=" + focus + "&FlushUserSession=true", function (v) {
        $.get("/" + $("#_apppath").val() + "/WebCalls/ChangeFocus.aspx?Focus=" + _changeSiteFocusCode, function (v) {
            _changeSiteFocusPostFunc();
        });
    });
 
}

function GoSiteHome() {
    window.location = "/" + $("#_apppath").val() + "/Default.aspx";
}

function ShowDisclaimerDialog(disclaimerid) {
    if (disclaimerid == undefined || disclaimerid == null) disclaimerid = "190A7925-576E-4712-AC4F-0F3FCB145F41";
    ShowWindow("GlobalDisclaimer", "/Business/Report/Default.aspx?iid=A3E26E37-6228-413B-8C38-B495F63C7B9A&DisclaimerId=" + disclaimerid, "Disclaimer", null, null, null, null, null, true);
}

function GetCurrentFocusId() {
    var o = $("#_curFocusInstanceId");
    if (o.length == 0) {
        return "";
    }
    else {
        return $("#_curFocusInstanceId").val();
    }
}



function IgnoreFocus() {
    var ignorefocus = false;
    if ($(".po-dashboard-ignorefocus").length > 0) {
        ignorefocus = true;
    }
    else {
        if ($(".po-site-dashboard").length == 0) {
            if ($(".po-rpt-ignorefocus").length > 0) ignorefocus = true;
        }
    }
    return ignorefocus;
}

function GetSitePagePhotoURL() {
    if (IgnoreFocus()) {
        var photourl = "/Images/PH_1x1.gif";
        var o = $(".po-dashboard-webpagephoto");
        if (o.length > 0) {
            photourl = o.val();
        }
        else {
            o = $(".po-rpt-webpagephoto");
            if (o.length == 0) {
                photourl = o.val();
            }
        }
        return photourl;
    }
    return null;
}


function GetFocusRelatedURL(url) {
    if (!IgnoreFocus()) {
        url = url + "&CurrentFocusInstanceId=" + GetCurrentFocusId();
        url = url + "&CurrentFocusInstanceEntityName=" + $('#_curFocusInstanceEntityName').val();
        url = url + "&CurrentFocusInstanceDisplay=" + encodeURIComponent($("#po_currentfocus").text());
    }

    return url;
}

function SetCurrentFocus(iid, ename, text) {
    $('#_curFocusInstanceId').val(iid);
    $('#_curFocusInstanceEntityName').val(ename);
    $("#po_currentfocus").text(text);
    $("#po_currentfocus").attr("iid",iid);
}


function ShowSiteTooltips(resize) {

    $(".po-site-tooltip").each(function () {
        var m = $("#po_master");
        var width = parseInt($(this).attr("w"));
        var trace = parseInt($(this).attr("trace"));
        var height = parseInt($(this).attr("h"));
        var center = $(this).attr("center") == "true";
        var ico = $(this).attr("ico") == "true";
        var modal = $(this).attr("modal") == "true";
        var left = parseInt($(this).attr("l")) + m.position().left;
        var top = parseInt($(this).attr("t"));
        var iid = $(this).attr("iid");
        var c = $(this).html();
        var loc = parseInt($(this).attr("loc"));
        var id = "po_site_tooltip_" + iid;

        if (!resize) {

            if (width < 250) width = 250;
            if (height < 150) height = 150;

            var tc = "<div id='" + id + "_wrapper'>" +
            "<table id='" + id + "_tbl' cellspacing='0' cellpadding='0' border='0' style='width:100%;height:100%'><tr id='" + id + "_tbl_tr'><td style='vertical-align:top;'><div id='" + id + "_content'  class='ui-widget ui-widget-content ui-corner-all' style='border-width:3px;'>" +
            "<table cellspacing='0' border='0' cellpadding='0' style='width:100%;'><tr>";
            if (ico) {
                tc = tc + "<td style='vertical-align:top;' rowspan='2'><img src='/Content/Dialogs/Images/Tooltips.jpg' style='width:64px;height:64px;'></td>";
            }
            tc = tc + "<td style='padding:2px;overflow:hidden;'>" + c + "</td></tr>" +
            "<tr style='height:20px;'><td style='padding-right:5px;'><div class='ui-button ui-widget ui-corner-all ui-button-text-only ui-state-default' style='padding:2px;font-weight:normal; float:right;' onclick='CloseSiteTooltip($(this));' iid='" + iid + "' trace='" + trace + "'>Close</div>" +
            "<div class='ui-button ui-widget ui-corner-all ui-button-text-only ui-state-default' style='padding:2px;font-weight:normal;float:right;' onclick='HideSiteTooltip($(this));' iid='" + iid + "' trace='" + trace + "'>Don't show again</div></td></tr></table></div></td></tr></table>";


            $("body").append(tc);
            var tpwrapper = $("#" + id + "_wrapper");

            var cw = width;
            width = width + 20;
            var tr = $("#" + id + "_tbl_tr");
            switch (loc) {
                case 0:
                    {
                        top = top - 6;
                        tr.before("<tr><td style='text-align:left;height:16px;'><img style='margin-left:5px;' src='/CSS/Themes/" + CurrentTheme() + "/Images/po-tt.gif' /></td></tr>");
                        break;
                    }
                case 1:
                    {
                        top = top - height + 48;
                        tr.children(0).css("vertical-align", "bottom");
                        tr.after("<tr><td style='text-align:left;height:16px;'><img style='margin-left:5px;' src='/CSS/Themes/" + CurrentTheme() + "/Images/po-bb.gif' /></td></tr>");
                        break;
                    }
                case 2:
                    {
                        top = top - 20;
                        left = left + 16;
                        tr.children(0).before("<td style='width:16px;vertical-align:top;'><img style='margin-top:5px;' src='/CSS/Themes/" + CurrentTheme() + "/Images/po-bb-h.gif' /></td>");
                        break;
                    }
                case 3:
                    {
                        top = top - 20;
                        left = left - width;
                        tr.children(0).css("text-align", "right");
                        tr.children(0).after("<td style='width:16px;vertical-align:top;'><img style='margin-top:5px;' src='/CSS/Themes/" + CurrentTheme() + "/Images/po-tt-h.gif' /></td>");
                        break;
                    }
            }
            var dlg = null;
            if (center) {
                tpwrapper.dialog({ bgiframe: true, autoOpen: true,
                    resizable: false, draggable: false, height: height, width: width,
                    modal: modal, title: false, position:"center"
                }).css("overflow", "hidden").css("padding", 0);
                $("#" + id + "_content").width(cw).height(height - 80);
                dlg = tpwrapper.parent();
            }
            else {
                tpwrapper.dialog({ bgiframe: true, autoOpen: true,
                    resizable: false, draggable: false, height: height, width: width,
                    modal: modal, title: false, position: [left, top]
                }).css("overflow", "hidden").css("padding", 0);
                $("#" + id + "_content").width(cw).height(height - 80);
                dlg = tpwrapper.parent();
                dlg.css("top", top).css("left", left);
            }

            dlg.removeClass("ui-widget").removeClass("ui-widget-content").removeClass("ui-corner-all");

            $(".ui-dialog-titlebar", dlg).remove();
        }
        else {
            if (!center) $("#" + id + "_wrapper").parent().css("top", top).css("left", left);
        }
    });
}

function HideSiteTooltip(o) {
    var command = GenericAPICommand("HideTooltip", "TraceRecentRecord", "void");
    AddCommandParameter(command, "typeCode", "System.Int32", o.attr("trace"));
    AddCommandParameter(command, "instanceId", "System.Guid", o.attr("iid"));
    AddCommandParameter(command, "entityName", "System.String", "Tooltip");
    AddCommandParameter(command, "url", "System.String", window.location);
    AddCommandParameter(command, "enforceTrace", "System.Boolean", true);
    
    CloseSiteTooltip(o);
    ExecuteCommand(command, function () { }, OnError);
  
   
}

function CloseSiteTooltip(o) {
    var dlg = $("#po_site_tooltip_" + o.attr("iid") + "_wrapper").parent();
    dlg.remove();
}

function RenderGenericList(ename, euiid, title, subtitle, filtertype, raname, rename, params) {
    var focus = $('#po_currentfocus');
    var url = "/" + $("#_apppath").val() + "/Default.aspx?DashboardRenderName=GenericList";

    if (params != null && params != undefined) {
        url = url + "&" + params;
    }

    url = SetQueryString(url, "EntityName", ename);

    if (filtertype != null && filtertype != undefined) {
        url = SetQueryString(url, "FilterType", filtertype);
    }

    if (euiid != null && euiid != undefined && euiid != "") {
        url = SetQueryString(url, "euiid", euiid);
    }

    if (title != null && title != undefined && title != "") {
        url = SetQueryString(url, "WebPageTitle", title);
    }
    if (subtitle != null && subtitle != undefined && subtitle != "") {
        url = SetQueryString(url, "WebPageSubtitle", subtitle);
    }

    if (focus.length > 0) {
        url = SetQueryString(url, "CurrentFocusId", GetCurrentFocusId());
    }

    var paramCurFocusId = GetQueryStringValue(window.location + "", "CurrentFocusId");
    
    if (paramCurFocusId != null && paramCurFocusId != "") {
        url = SetQueryString(url, "CurrentFocusId", paramCurFocusId);
    }

    var paramRegardingId = GetQueryStringValue(window.location + "", "RegardingId");

    if (paramRegardingId != null && paramRegardingId != "") {
        url = SetQueryString(url, "RegardingId", paramRegardingId);
    }

    if (rename != null && rename != undefined && rename != "") {
        url = SetQueryString(url, "RegardingEntityName", rename);
    }

    if (raname != null && raname != undefined && raname != "") {
        url = SetQueryString(url, "RegardingAttributeName", raname);
    }
    window.location = url.replace(/&&/g,"&");
}

function SpeedTest() {
    window.location = "/" + $("#_apppath").val() + "/iframe.aspx?ename=SystemSite&iid=" + $("#_siteid").val() + "&aname=SpeedTestURL";
}

function InitSiteiFrame() {
    $("#po_site_iframe_tabs").tabs();
    $("iframe").attr("src", $("iframe").attr("url"));
    var a = $("#ctl00__cphCenter__tabTitle");
    if (a.text() == "") {
        a.parent().parent().remove();
    }
    AdjustSiteiFrame();
        
}

function AdjustSiteiFrame() {
    var ifa = $("iframe");
    if (ifa.attr("height") != undefined && ifa.attr("height") != "") {
        ifa.height(parseInt(ifa.attr("height")));
    }
    else {
        var adj = 183;
        if ($("#ctl00__cphCenter__tabTitle").length == 0) adj = 150;
        ifa.height($(window).height() - adj);
        $("body").css("overflow", "hidden");
    }

}

function GotoForumListOfCurFocus() {
    var v = GetCurrentFocusId();
    if (v != "") {
        window.location = "/" + $("#_apppath").val() + "/Report.aspx?ReportRenderName=ForumList&InstanceId=" + v;
    }
    else {
        window.location = "/" + $("#_apppath").val() + "/Report.aspx?ReportRenderName=ForumList";
    }
}

function GotoDashboard(dashboardname, params) {
    var url =  "/" + $("#_apppath").val() + "/Default.aspx?DashboardRenderName=" + dashboardname;
    url = GetFocusRelatedURL(url);
    if (params != undefined && params != null) url = url + "&" + params;
    window.location = url;
}


function SetWebPartSubElemRelativePosition(o, x, y) {
    var p = GetParentWebPart(o).position();
    o.css("position", "absolute").css("left", p.left + x).css("top", p.top + y);
}

function RenderHelpBtn() {
    var course = $('.po-dashboard-course');
    if (course.length == 0) {
        course = $('.po-report-course');
    }
    if (course.length > 0) {
        var courseid = course.attr('courseid');
        var contentid = course.attr('coursecontentid');
        var url = '/Business/Dashboard/Default.aspx?DashboardRenderName=KM.Knowledge.CourseContent&preview=true';
        if (courseid != null || contentid != null) {
            if (courseid != null && contentid != null) {
                url = url + '&CourseId=' + courseid + '&CourseContentId=' + contentid;
            }
            else {
                url = url + '&CourseId=' + courseid;
            }

            queryxml = '<Query EntityName=\'Course\'><Attributes><Attribute Name=\'displaytypecode\'/><Attribute Name=\'displaywidth\'/><Attribute Name=\'displayheight\'/><Attribute Name=\'NavigationTypeCode\'/></Attributes><Where><Condition Type=\'AND\' SourceAttributeName=\'courseid\' Operator=\'equal\' TargetValue=\'' + courseid + '\'/></Where></Query>';
            command = PlatformCommand("Query", "GenericAPIs", "Query", "System.String");
            AddCommandParameter(command, "queryXml", "System.String", queryxml);
            ExecuteCommand(command, function (xml) {
                xmlDoc = $.parseXML(xml);
                $xml = $(xmlDoc);
                $type = $xml.find('DisplayTypeCode');
                $height = $xml.find('DisplayHeight');
                $width = $xml.find('DisplayWidth');
                $nav = $xml.find('NavigationTypeCode');

                switch ($nav.text()) {
                    case '-1':
                        url = url + '&Mode=2&NoButtons=1';
                        break;
                    case '1':
                        url = url + '&NoButtons=1';
                        break;
                    case '2':
                        url = url + '&Mode=2';
                        break;
                }
                switch ($type.text()) {
                    case '0':
                        ShowWindow('HelpDialog', url, 'Help', null, $width.text(), $height.text(), null, null, false);
                        break;
                    case '1':
                        window.open(url, 'HelpDialog', 'width=' + $width.text() + ', height=' + $height.text() + ', location=no');
                        break;
                    case '2':
                        window.open(url, 'HelpDialog', 'fullscreen=yes, scrollbars=auto');
                        break;
                }
            }, OnError);
        }
    }
    else {
        alert('There is no help docuemnt linked to this page!');
    }
}
