﻿var curCalloutId = '';

function ShowCallout(calloutId)
{
    curCalloutId = calloutId;

    // Internally intialise the callout
    Initialise(calloutId);
    
    // Intialise the controls on the callout
    InitCallout(calloutId);
    
    // Set the initial size/position of the callout and shield
    var callout = document.getElementById(curCalloutId);
    callout.style.display = 'block';
    ResizeCurrentCallout();

    // Resize/re-position shield every time the window changes size
    if (window.addEventListener) { window.addEventListener('resize', ResizeCurrentCallout, false); } else { window.attachEvent('onresize', ResizeCurrentCallout); }
}

function ResizeCurrentCallout()
{
    // Setup the layout of the shield and the callout panel itself
    var callout = document.getElementById(curCalloutId);

    // If we're trying to close the callout anyway there's no need continue
    if (callout.style.display != 'none')
    {    
        var shield = callout.parentNode;
        shield.style.position = 'absolute';
        shield.style.top = '0px';
        shield.style.left = '0px';

        var height; var width;
        width = document.documentElement.scrollWidth;
        height = document.documentElement.scrollHeight;
        if (width < document.documentElement.clientWidth) height = document.documentElement.clientWidth;
        if (height < document.documentElement.clientHeight) height = document.documentElement.clientHeight;
       
        shield.style.width = width+'px';
        shield.style.height = height+'px';
        shield.style.display = 'block';
        
        var left = (document.documentElement.clientWidth - callout.clientWidth) / 2 + document.documentElement.scrollLeft;
        var top = (document.documentElement.clientHeight - callout.clientHeight) / 2 + document.documentElement.scrollTop;
        callout.style.left = left + 'px';
        callout.style.top = top + 'px';
    }
}

function InitCallout(calloutId)
{
    // Call the InitCallout method within the <script> block on the user control, if available
    eval('if (window.'+calloutId+'_InitCallout)' + calloutId + '_InitCallout();');
}

function Initialise(calloutId)
{    
    if (document.getElementById(calloutId+'-loading-callout') == null)
    {    
        // "X" button
        var cmdCloseCallout = document.createElement("A");
        cmdCloseCallout.className = 'close-callout';
        cmdCloseCallout.id = "hlCloseCallout";
        if (window.addEventListener) { cmdCloseCallout.addEventListener('click', cmdCloseCallout_OnClick, false); } else { cmdCloseCallout.attachEvent('onclick', cmdCloseCallout_OnClick);}
        cmdCloseCallout.calloutId = calloutId;
        if (window.addEventListener) { cmdCloseCallout.addEventListener('mousedown', cmdCloseCallout_OnMousedown, false); } else { cmdCloseCallout.attachEvent('onmousedown', cmdCloseCallout_OnMousedown); }
        
        // AJAX loading panel
        var pnlLoading = document.createElement("DIV");
        pnlLoading.id = calloutId+'-loading-callout';
        pnlLoading.className = 'loading-callout';
        
        // Add this components to the callout's DOM
        var callout = document.getElementById(calloutId);        
        callout.insertBefore(pnlLoading, callout.firstChild);
        callout.insertBefore(cmdCloseCallout, callout.firstChild);
    }
}

function cmdCloseCallout_OnMousedown(e)
{
    if (window.event) {
        event.cancelBubble=true;
    } else {
        e.cancelBubble=true;
    }
    
    return false;
}

function cmdCloseCallout_OnClick(e)
{
    if (window.event) {
        CloseCallout(window.event.srcElement.calloutId);
    } else {
        CloseCallout(e.target.calloutId);
    }
}

function CloseCallout(calloutId)
{
    var callout = document.getElementById(calloutId);
    var shield = callout.parentNode;
    shield.style.display = 'none';
    callout.style.display = 'none';
    HideCalloutLoading(calloutId);
}

function CallServer(calloutId, arg, ctx)
{
    ShowCalloutLoading(calloutId);
    eval(calloutId + '_CallServer(arg,ctx);');
}

function ShowCalloutLoading(calloutId)
{
    var loading = document.getElementById(calloutId+'-loading-callout');
    loading.style.display = "block";
}
   
function HideCalloutLoading(calloutId)
{
    var loading = document.getElementById(calloutId+'-loading-callout');
    loading.style.display = "none";
}

function CloseWindow()
{
    window.opener = top;
    window.close();
}