﻿
__Popup_ShowPopup = false;

// ****************************************************************
// PUBLIC

// Show popup window
function ShowPopupWindow(control, parameters)
{
    submitRequest = false;

    __Popup_ShowPopup = true;
    var hidPopupData = document.getElementById(document.getElementById('hidPopupDataID').value);
    hidPopupData.value = control + ' ' + parameters;
    __doPostBack(document.getElementById('hidPopupPostback').value, '');
}

function CleanFCKEditorState()
{
    // remove FCKeditor's event handler of form.submit
    var form = document.getElementById('hidPopupDataID').form;
    if (typeof(form.submit) == 'function' && form._FCKOriginalSubmit)
    {
        form.submit = form._FCKOriginalSubmit;
        form._FCKOriginalSubmit = null;

        for (var name in FCKeditorAPI.__Instances)
	    {
            //alert('name = ' + name);
    
	        var oEditor = FCKeditorAPI.__Instances[name];
		    if (oEditor.GetParentForm && oEditor.GetParentForm() == form && oEditor.UpdateLinkedField)
		    {
    		    //alert('oEditor.UpdateLinkedField()');
		        oEditor.UpdateLinkedField();
		    }
        }
    }
}

// Close popup window
function ClosePopupWindow()
{
    popupRequestDone = false;
    submitRequestDone = false;

    CleanFCKEditorState();
    
    hidePopup();
    var hidPopupData = document.getElementById(document.getElementById('hidPopupDataID').value);
    hidPopupData.value = 'Default' + ' ';
    __doPostBack(document.getElementById('hidPopupPostback').value, '');
}

// Resize popup window
function ResizePopup()
{
	var objPopup = document.getElementById('popup');
	if (objPopup.style.display != "none")
	{
	    var arrayPageSize = GetPageSizeArray();
	    var arrayPageScroll = GetPageScrollArray();

    	var objShadow = document.getElementById('shadow');
    	objShadow.style.height = (arrayPageSize[1] + 'px');
    	objShadow.style.width = arrayPageSize[0] + 'px';
	    
       	var hidDisablePopupScroll = GetServerObject('hidDisablePopupScrollID');

	    if (hidDisablePopupScroll.value != '1')
	    {
            var widthBox = objPopup.clientWidth;
            var heightBox = objPopup.clientHeight;
            var popupTop = arrayPageScroll[1] + ((arrayPageSize[3] - heightBox) / 2);
            var popupLeft = arrayPageScroll[0] + ((arrayPageSize[2] - widthBox) / 2);
            objPopup.style.top = (popupTop < 0) ? "0px" : popupTop + "px";
            objPopup.style.left = (popupLeft < 0) ? "0px" : popupLeft + "px";
	    }
	    else
    		hidDisablePopupScroll.value = '0';
	}
}

// ****************************************************************
// Hook up Application event handlers

Sys.Application.add_init(Popup_Page_OnInit);
Sys.Application.add_load(Popup_Page_OnLoad);
$addHandler(window, "resize", Popup_OnPageResize);

// on page load
function Popup_Page_OnLoad(sender, args)
{
    // Hide popup on page load
    var hidHidePopup = GetServerObject('hidHidePopupID');
    if (hidHidePopup.value != '')
    {
        hidHidePopup.value = '';
        ClosePopupWindow();
    }
    
    // Restore popup(after full postback)
    var hidRestorePopup = GetServerObject('hidRestorePopupID');
    if (hidRestorePopup.value != '')
    {
        RestoreScrollPosition();
        hidRestorePopup.value = '';
        showPopup();
    }
}

// on page init
function Popup_Page_OnInit(sender)
{
  var prm = Sys.WebForms.PageRequestManager.getInstance();
  if (!prm.get_isInAsyncPostBack())
  {
      prm.add_endRequest(Popup_OnAjaxEndRequest);
  }
}

// on ajax request finished
function Popup_OnAjaxEndRequest(sender, args)
{
    if (__Popup_ShowPopup)
    {
        showPopup();
        __Popup_ShowPopup = false;
    }
    ResizePopup();
}

//on page resize
function Popup_OnPageResize(eventElement)
{
    ResizePopup();
}

// ****************************************************************
// IMPLEMENTATION

// Show Popup
function showPopup()
{
	// get objects
	var objShadow = document.getElementById('shadow');
	var objPopup = document.getElementById('popup');
	
	var arrayPageSize = GetPageSizeArray();
	var arrayPageScroll = GetPageScrollArray();

	// set height of Shadow
	objShadow.style.height = (arrayPageSize[1] + 'px');
	objShadow.style.width = arrayPageSize[0] + 'px';
	objShadow.style.display = 'block';

    objPopup.style.display = 'block';

    var widthBox = objPopup.clientWidth;
    var heightBox = objPopup.clientHeight;
    var popupTop = arrayPageScroll[1] + ((arrayPageSize[3] - heightBox) / 2);
    var popupLeft = arrayPageScroll[0] + ((arrayPageSize[2] - widthBox) / 2);
    objPopup.style.top = (popupTop < 0) ? "0px" : popupTop + "px";
    objPopup.style.left = (popupLeft < 0) ? "0px" : popupLeft + "px";

   	selects = document.getElementsByTagName("select");
    for (i = 0; i != selects.length; i++) 
    {
        selects[i].style.visibility = "hidden";
    }
    
	arrayPageSize = GetPageSizeArray();
	objShadow.style.height = (arrayPageSize[1] + 'px');
	
	window.setTimeout(fnShowSelects, 250);
}

function fnShowSelects()
{
	var objPopup = document.getElementById('popup');
		selects = document.getElementById('popup').getElementsByTagName("select");
		for (i = 0; i != selects.length; i++) 
		{
			selects[i].style.visibility = "visible";
		}
}

//
// hidePopup()
//
function hidePopup()
{
	// get objects
	objShadow = document.getElementById('shadow');
	objPopup = document.getElementById('popup');

	// hide popup and shadow
	objShadow.style.display = 'none';
	objPopup.style.display = 'none';

	// make select boxes visible
	selects = document.getElementsByTagName("select");
    for (i = 0; i != selects.length; i++) 
	{
		selects[i].style.visibility = "visible";
	}
}

// ****************************************************************
if(typeof(Sys) != 'undefined') Sys.Application.notifyScriptLoaded();