@*   Xafari Framework version : 17.2.3011.1336   *@

@using Xafari.Mvc
@using Xafari.Mvc.Templates

@model Xafari.Mvc.Controls.AutoRefreshControl

@{
	var id = this.ViewData.Frame().ControlId("AutoRefresh");
}
<script type="text/javascript" id="@Html.DXScriptId()">
	if (window.xafari && window.xafari.autoRefresh && window.xafari.autoRefresh.s@(id))
		window.xafari.autoRefresh.s@(id).stop();
	@if (this.Model.MvcAutoRefreshViewController.Active)
    {
    <text>
	if (window.xafari && !window.xafari.autoRefresh)
		window.xafari.autoRefresh = {};

	window.xafari.autoRefresh.s@(id) =
    {
    	windowId: "@(this.ViewData.Window().WindowId())",
    	autoRefreshTimeoutId: null,
    	timeout: @(this.Model.MvcAutoRefreshViewController.RefreshTimeout * 1000),
    	start:
            function() {
            	this.stop();
            	if (this.timeout > 0)
            		this.autoRefreshTimeoutId = window.setInterval(this.refreshCore, this.timeout, this);
            },
    	stop: function() {
    		if (this.autoRefreshTimeoutId)
    			window.clearTimeout(this.autoRefreshTimeoutId);
    		this.autoRefreshTimeoutId = null;
    	},
    	refreshCore: function(autoRefreshObj) {
    		autoRefreshObj.refresh();
    	},
    	refresh: function() {
    		if((window.activePopupControl && !window.activePopupControl.mvcShown) || this.windowId !== window.xafari.windowInfo.windowId){
    			this.stop();
    		}
    		else{
    			if(window.parent === window) {
    				var popupControl = GetActivePopupControl(window);
    				if (popupControl == null) {
    					window.clearTimeout(this.autoRefreshTimeoutId);
    					this.autoRefreshTimeoutId = null;
    					@Html.Raw(this.Model.MvcAutoRefreshViewController.RefreshControllerLink.RefreshAction.GetXafActionExecuteScript(this.ViewData.Window(), this.Ajax.JavaScriptStringEncode(this.Model.MvcAutoRefreshViewController.RefreshControllerLink.RefreshAction.ConfirmationMessage)));
    				}
    			}
    			else{
    				var popupControl = window.parent.GetActivePopupControl(window.parent);
    				if(popupControl === window.activePopupControl) {
    					window.clearTimeout(this.autoRefreshTimeoutId);
    					this.autoRefreshTimeoutId = null;
    					@Html.Raw(this.Model.MvcAutoRefreshViewController.RefreshControllerLink.RefreshAction.GetXafActionExecuteScript(this.ViewData.Window(), this.Ajax.JavaScriptStringEncode(this.Model.MvcAutoRefreshViewController.RefreshControllerLink.RefreshAction.ConfirmationMessage)));
    				}
    			}
    		}
    	}
    }
	</text>
    }
	@if (this.Model.MvcAutoRefreshViewController.Active && this.Model.MvcAutoRefreshViewController.IsStart)
    {
    <text>
	if (window.xafari && window.xafari.autoRefresh && window.xafari.autoRefresh.s@(id))
		window.xafari.autoRefresh.s@(id).start();
	</text>
    }
</script>
