﻿window.onload = centerDialogs;
$addHandler(window, "resize", centerDialogs);

// Center dialog if open
function centerDialogs() {
    var bg = $get('ModalBackground');
    if (bg)
        if (bg.style.display == 'block') positionModalBackground(false);
    
    var tipInfoDialog = $get('TipDialog');
    if (tipInfoDialog)
        if (tipInfoDialog.style.display == 'block') centerTipDialog(false);

    var loginDialog = $get('LoginDialog');
    if (loginDialog)
        if (loginDialog.style.display == 'block') centerLoginDialog(false);
    
    var articleInfoDialog = $get('ArticleInfoDialog');
    if (articleInfoDialog) 
        if (articleInfoDialog.style.display == 'block') centerArticleInfoDialog(false, 0);

    var checkoutDialog = $get('CheckoutDialog');
    if (checkoutDialog)
        if (checkoutDialog.style.display == 'block') centerCheckoutDialog(false, checkoutItems);

    var accountApplicationDialog = $get('AccountApplicationDialog');
    if (accountApplicationDialog)
        if (accountApplicationDialog.style.display == 'block') showAccountDialog();

    var myAccountDialog = $get('MyAccountDialog');
    if (myAccountDialog)
        if (myAccountDialog.style.display == 'block') showMyAccountDialog();

    var settingsDialog = $get('SettingsDialog');
    if (settingsDialog)
        if (settingsDialog.style.display == 'block') showSettingsDialog();
    }

// Position the modal background
function positionModalBackground(show) {
    var div = $get('ModalBackground');

    if (Sys.Browser.agent == Sys.Browser.InternetExplorer && Sys.Browser.version < 7)
        div.style.position = 'absolute'

    div.style.width = parseFloat(screen.availWidth > document.body.scrollWidth ? screen.availWidth : document.body.scrollWidth) + 'px';
    div.style.height = parseFloat(screen.availHeight > document.body.scrollHeight ? screen.availHeight : document.body.scrollHeight) + 'px';


    if (show) {
        isDialogActive = true;
        div.style.display = 'block';
    }
}

// Hide the modal background
function hideModalBackground() {
    var bg = $get('ModalBackground');
    if (bg) {
        isDialogActive = false;
        bg.style.display = 'none';
    }
}

// Direct link
function toggleDirectLinkVisibility() {
    var popup = $get('DirectLink');
    if (popup.style.display == 'block')
        popup.style.display = 'none';
    else
        popup.style.display = 'block';
}

// Notify ScriptManager that this is the end of the script
if (typeof (Sys) !== 'undefined') Sys.Application.notifyScriptLoaded();


