﻿// (c) Copyright Advanced Solutions International.
// All rights reserved.

// This gives us a client-side way of determining if something has
// changed on the page.
var IsDirty = false;

//addLoadEvent(  MasterPageBase_Init);

//
//
//
function MasterPageBase_AsyncInit()
{
    MasterPageBase_Init(true);
}

var progressDivInitialWidth;
var progressDivInitialHeight;
var progressDivInitialMarginLeft;
//
//
//
function MasterPageBase_Init()
{
    // populate initial values for progress div
    var progressDiv = $get('FillProgressDiv');
    if (progressDiv != null)
    {
        progressDivInitialWidth = 166; //progressDiv.offsetWidth;
        progressDivInitialHeight = 30; //progressDiv.offsetHeight;
        progressDivInitialMarginLeft = '45%'; //progressDiv.style.marginLeft;
    }

    var manager = Sys.WebForms.PageRequestManager.getInstance();
    if (manager != null)
    {
        // tell the page you want to run MasterPageBase_EndRequest whenever an async postback ends
        //manager.add_endRequest(MasterPageBase_EndRequest);
        
        // tell the page you want to run MasterPageBase_BeginRequest whenever an async postback begins
        manager.add_beginRequest(MasterPageBase_BeginRequest);
    }

    // Set the page IsDirty bit - we don't allow setting this back to false, because in some cases (esp. ones with an embedded Object Browser or other control that does AJAX postbacks)
    // it was incorrectly resetting back to false on those postbacks.
    if (!IsDirty)
    {
        IsDirty = eval($get('PageIsDirty').value);
        var radWindow = GetRadWindow();
        if (radWindow != null)
        {
            radWindow.IsDirty = IsDirty;
        }
    }
}

//
//
//
function MasterPageBase_BeginRequest(sender, args) 
{
    var postBackElement = args.get_postBackElement();

    // Show Progress Div
    var progressDiv = $get('FillProgressDiv');
    if (progressDiv != null)
    {
        // reset progress div to initial size and position, it'll get moved in the 
        // script that follows if need be.
        progressDiv.style.width = progressDivInitialWidth + 'px';
        progressDiv.style.height = progressDivInitialHeight + 'px';
        progressDiv.style.marginLeft = progressDivInitialMarginLeft;
        progressDiv.style.top = 0;
        progressDiv.style.left = 0;
        
//            var progressDivClones = new Array();

        var ajaxUpdatedControlID = postBackElement.getAttribute('ajaxUpdatedControlID');
        // If control is on a smartcontrol then the ajaxUpdatedControlID might be set on the parent, so check that.
        if (ajaxUpdatedControlID == null)
            ajaxUpdatedControlID = getParent(postBackElement).getAttribute('ajaxUpdatedControlID');
        
        if (ajaxUpdatedControlID != null && ajaxUpdatedControlID.length > 0)
        {
            var updatedElementIDs = ajaxUpdatedControlID.split(',');
            for (var i=0; i<updatedElementIDs.length; i++)
            {
                var updatedElement = $get(updatedElementIDs[i]);
//                if (updatedElement == null)
//                    updatedElement = $get('OuterContentPanel');
//                if (updatedElement == null)
//                    updatedElement = document.body;
                    
                if (updatedElement != null)
                {
                    progressDiv.style.display = 'block';
                    progressDiv.style.marginLeft = '0';

                    Cover(updatedElement, progressDiv, false);
                }   
                
//                var coords = findAbsoluteCoords(updatedElement);
//                var progressDiv2 = progressDiv;
////                    if (i > 0)
////                    {
////                        // if more than one control to update, then clone the fill div
////                        progressDiv2 = progressDiv.cloneNode(true);
////                        progressDiv.parentNode.appendChild(progressDiv2);
////                    }
//                
//                progressDiv2.style.left = coords.absoluteLeft + 'px';
//                progressDiv2.style.top = coords.absoluteTop + 'px';
//                progressDiv2.style.width = coords.width + 'px';
//                progressDiv2.style.height = coords.height + 'px';
            }
        }
        else
        {
//            var mainPanelElement = $get('MainPanel');
//            if (mainPanelElement == null)
//                mainPanelElement = document.body;
//            
//            setTimeout('Cover(mainPanelElement, progressDiv, false)', 2000);
    
//            progressDiv.style.display = 'none';
        }
    }
}
//
//
//
//function MasterPageBase_EndRequest(sender, args)
//{
////    var progressDiv = $get('FillProgressDiv');
////    if (progressDiv != null)
////    {
////        progressDiv.style.width = progressDivPrevWidth + 'px';
////        progressDiv.style.height = progressDivPrevHeight + 'px';
////    }
//}
//
//
//
function PostCommitScript(customPostCommitScript)
{
    // only execute the post commit script if there are no messages to display
    // and the button clicked was a commit button (save, ok)
    //if (IsDirty)
    {
        var commitButtonClickedHiddenInput = $get('CommitButtonClicked');
        var commitButtonClicked = commitButtonClickedHiddenInput != null && eval(commitButtonClickedHiddenInput.value);
        if (commitButtonClicked && customPostCommitScript != null && customPostCommitScript.length > 0)
        {
            var messagesPanelID = $get('TemplateUserMessagesID').value;
            var messagesPanel = messagesPanelID.length > 0 ? $get(messagesPanelID) : null;
            if (messagesPanel == null)
            {
                eval(customPostCommitScript);
            }
        }
    }
    
    commitButtonClickedHiddenInput.value = 'false';
}
//
//
//
function CancelButton_Click(url)
{
    var dialogWindow = GetRadWindow();
    if (dialogWindow != null)
    {
        CloseRadWindow();
    }
    else
    {
        window.location.href = url;
    }

    return CancelEvent();
}

// ############################################################################
// Functions for resizing content to fit window
// ############################################################################
var topPanel; 
var titlePanel;
var sideBarPanel;
var contentPanel;
var buttonPanel;
var footPanel;
var oldTopPanelHeight;
var oldSideBarPanelWidth;

//
// Call this function from a .master implementation to setup all master page components
// for use in the resizing functions.
//
function MasterPageBase_SetupComponents(topPanelId, titlePanelId, sideBarPanelId, contentPanelId, buttonPanelId, footPanelId)
{
    if (topPanelId != null && topPanelId.length > 0)
        topPanel = $get(topPanelId);
    if (titlePanelId != null && titlePanelId.length > 0)
        titlePanel = $get(titlePanelId);
    if (sideBarPanelId != null && sideBarPanelId.length > 0)
        sideBarPanel = $get(sideBarPanelId);
    if (contentPanelId != null && contentPanelId.length > 0)
        contentPanel = $get(contentPanelId);
    if (buttonPanelId != null && buttonPanelId.length > 0)
        buttonPanel = $get(buttonPanelId);
    if (footPanelId != null && footPanelId.length > 0)
        footPanel = $get(footPanelId);
}

//addLoadEvent(Window_Load);
function Window_Load()
{
    addResizeEvent(Window_Resize);

    // When you hide document.body's overflow then any time there is a onmouseover
    // script inside Desktop Client the whole page disappears.  No clue what is going 
    // on, but we just don't hide the body overflow when inside Desktop Client.
    // 
    // [Update JG 7/1/2008] We only need to do it for FireFox browser
    if (BrowserDetect.browser != 'Explorer') // window.location.href.indexOf('/CS/') == -1)
        document.body.style.overflow = 'hidden';

    if (topPanel != null)
    {
        // set width on top panel collapsible panel's div for FireFox
        if (BrowserDetect.browser != 'Explorer')
        {
            topPanel.firstChild.style.width = '100%';
            topPanel.firstChild.style.overflow = 'hidden';
        }
        
        // Register resize event for top panel
        topPanel.onresize = TopPanel_Resize;
    }

    Window_Resize();
    setTimeout("ScrollToFragment()", 500);
}
function ScrollToElement(theElement)
{
    var selectedPosX = 0;
    var selectedPosY = 0;

    while (theElement != null && theElement != contentPanel)
    {
        //selectedPosX += theElement.offsetLeft;
        selectedPosY += theElement.offsetTop;
        theElement = theElement.offsetParent;
    }

    if (contentPanel != null)
        contentPanel.scrollTop = selectedPosY;
    else
        window.scrollTo(selectedPosX, selectedPosY);
}
function ScrollToFragment()
{
    // location.hash is automatically set to the URL fragment value
    if (location.hash != null && location.hash != '')
    {
        var element = $get(location.hash.substring(1));
        if (element == null)
            element = $get(location.hash);
        if (element != null)
            ScrollToElement(element);
    }
}

//
//
//
function Window_Resize()
{
    setTimeout("ResizeContentPanel()", 300);
    //ResizeContentPanel();
}

//
//
//
function TopPanel_Resize(e)
{
    var topPanelHeight = topPanel.offsetHeight;
    if (topPanelHeight != oldTopPanelHeight)
    {
        oldTopPanelHeight = topPanelHeight;
        ResizeContentPanel();  
    }
}

//
//
//
function SideBarPanel_Resize()
{
    if (BrowserDetect.browser == 'Explorer' && BrowserDetect.version < 7)
        return;
        
    var sideBarPanelWidth = sideBarPanel.offsetWidth; // + $get(sideBarPanel.id + 'Divider').offsetWidth;
    if (sideBarPanelWidth != oldSideBarPanelWidth)
    {
        oldSideBarPanelWidth = sideBarPanelWidth;
        ResizeContentPanel();  
    }
}

//
//
//
function ResizeContentPanel()
{
    var pageHeight = livePageHeight();
    var pageWidth = livePageWidth();
    
    // get height
    var headHeight = 0;
    if (topPanel != null)
        headHeight += topPanel.offsetHeight + $get(topPanel.id + 'Divider').offsetHeight;
    if (titlePanel != null)
        headHeight += titlePanel.offsetHeight;

    var footHeight = 0; 
    if (buttonPanel != null)
        footHeight += (BrowserDetect.browser!='Explorer'&&buttonPanel.offsetHeight==0)?23:buttonPanel.offsetHeight; //buttonPanel.offsetHeight;
    if (footPanel != null)
        footHeight += footPanel.offsetHeight;
        
    var mainHeight = pageHeight - headHeight - footHeight;

    // subtract more for general padding and stuff (i really don't know why this is needed)
    if (BrowserDetect.browser == 'Explorer' && BrowserDetect.version > 7)
        mainHeight -= 36;
    else
        mainHeight -= 14;
    
    if (mainHeight <= 99)
    {
        // min height of 100 px
        mainHeight = 100;
    }

//    // get width
//    var mainWidth = pageWidth;
//    if (sideBarPanel != null)
//    {
//        mainWidth -= (sideBarPanel.offsetWidth + 20);
//        //mainWidth -= $get(sideBarPanel.id + 'Divider').offsetWidth;
//    }
//    
//    if (mainWidth < 0)
//        mainWidth = 0;
    
    if (contentPanel != null)
    {
        contentPanel.style.height = mainHeight + 'px';
        //contentPanel.style.width = mainWidth + 'px';
    }
    
//    if (titlePanel != null)
//        titlePanel.style.width = mainWidth + 'px';
//    if (buttonPanel != null)
//        buttonPanel.style.width = mainWidth + 'px';
    
    if (sideBarPanel != null)
    {
        sideBarPanel.style.height = mainHeight + 'px';
    }

    // invoke the resize functions that have been registered
    if (contentPanel != null)
    {
        var contentWidth = contentPanel.offsetWidth;
        //var contentHeight = contentPanel.offsetHeight;
        contentResized(contentWidth, mainHeight);
    }
}

function TopPanel_Click()
{
    // set cookie so we remember top panel collapsed state on nav change
    var collapsed = readCookie('TopPanelCollapsed');
    if (collapsed != null)
        collapsed = eval(collapsed);
    else
        collapsed = false;
        
    collapsed = !collapsed;
    createCookie('TopPanelCollapsed', (collapsed) + '', 1);
    
    if (BrowserDetect.browser != "Explorer")
        ResizeHeightForMozilla();
    else
    {
        // have to hide the menu since we have absolute positioning in there with the auto-scroll feature
        setTimeout("$get('innerSpan').style.display = " + collapsed + "? 'none' : 'block'", 300);
    }
}

var ResizeHeightIntervalID = 0;
var ResizeHeightLastHeight = 0;
function ResizeHeightForMozilla()
{
    ResizeHeightLastHeight = topPanel.offsetHeight;
    ResizeHeightIntervalID = setInterval("ResizeHeightInterval()", 250);
}
function ResizeHeightInterval()
{
    var topPanelHeight = topPanel.offsetHeight;
    if (topPanelHeight == ResizeHeightLastHeight)
    {
        clearInterval(ResizeHeightIntervalID);
        ResizeContentPanel();
    }
    ResizeHeightLastHeight = topPanelHeight;
}

// ############################################################################
// Functions to help use the Rad tool suite (RadWindow, RadAjax, etc...)
// ############################################################################


//
// Get a handle for the current RadWindow (dialog).
//
function GetRadWindow()
{
    var oWindow = null;
    if (window.radWindow) oWindow = window.radWindow; //Will work in Moz in all cases, including clasic dialog
    else if (window.frameElement && window.frameElement.radWindow) oWindow = window.frameElement.radWindow;//IE (and Moz az well)
    return oWindow;
}
//
// Close the current RadWindow (dialog).
//
function CloseRadWindow()
{
    var oWindow = GetRadWindow();
    if (oWindow != null)
        oWindow.Close();
}
//
// Refreshes the parent of the current RadWindow (dialog).
//
function RefreshRadWindowParent()
{
    var oWindow = GetRadWindow();
    if (oWindow != null && oWindow.BrowserWindow != null)
        oWindow.BrowserWindow.location.reload();
} 
