﻿var OKCommon_ShowProcessing_ajaxWaitMessageTimer;
var OKCommon_ShowProcessing_ajaxWait_Timeout = 500;
var requestStatus = 0; //0: not running, 1:running, 2:completed
var isVisible = false;


function showProcessing()
{
	osjs(function($) {

		if(osjs.browser.msie && osjs.browser.version == '6.0')
		{
			var wd = document.body.offsetWidth > document.documentElement.clientWidth ? document.body.offsetWidth : document.documentElement.clientWidth; 
			var hd = document.body.offsetHeight > document.documentElement.clientHeight ? document.body.offsetHeight: document.documentElement.clientHeight;
			document.getElementById('divIndicatorBackground').style.width = wd;
			document.getElementById('divIndicatorBackground').style.height = hd;
			document.getElementById('divIndicatorBackground').style.display = 'block';
			osjs("select")["hide"]();
		}

		else
		{

			document.getElementById('divIndicatorBackground').style.width = '100%';
			document.getElementById('divIndicatorBackground').style.height = '100%';
			document.getElementById('divIndicatorBackground').style.display = 'block';
		}
	});
}


function showLoading()
{
	if (isVisible && requestStatus==2)
	{
		document.getElementById('divIndicatorBackground').style.display = 'none';
		
		if(osjs.browser.msie && osjs.browser.version == '6.0')
			osjs("select")["show"]();
		
		isVisible = false;
		requestStatus = 0;
		clearTimeout(OKCommon_ShowProcessing_ajaxWaitMessageTimer);
	}

	if (requestStatus == 1 && !isVisible)
	{
		if(osjs.browser.msie && osjs.browser.version == '6.0')
		{
			var wd = document.body.offsetWidth > document.documentElement.clientWidth ? document.body.offsetWidth : document.documentElement.clientWidth; 
			var hd = document.body.offsetHeight > document.documentElement.clientHeight ? document.body.offsetHeight: document.documentElement.clientHeight;
			document.getElementById('divIndicatorBackground').style.width = wd;
			document.getElementById('divIndicatorBackground').style.height = hd;
			document.getElementById('divIndicatorBackground').style.display = 'block';
			osjs("select")["hide"]();
		}
	
		else
		{
			document.getElementById('divIndicatorBackground').style.width = '100%';
			document.getElementById('divIndicatorBackground').style.height = '100%';
			document.getElementById('divIndicatorBackground').style.display = 'block';
		}

		isVisible = true;
		OKCommon_ShowProcessing_ajaxWaitMessageTimer = setTimeout(showLoading, OKCommon_ShowProcessing_ajaxWait_Timeout);
	}
}


function OKCommon_ShowProcessing_ajaxWait_init()
{
	osjs(function($) {

		osAjaxBackend.BindBeforeAjaxRequest(function(){
			requestStatus = 1;
			OKCommon_ShowProcessing_ajaxWaitMessageTimer = setTimeout(showLoading, OKCommon_ShowProcessing_ajaxWait_Timeout);
		});

		osAjaxBackend.BindAfterAjaxRequest(function(){
			requestStatus = 2;
			OKCommon_ShowProcessing_ajaxWaitMessageTimer = setTimeout(showLoading, OKCommon_ShowProcessing_ajaxWait_Timeout);
		});

	});
}