/**
 * usage:
 * <input type="button" value="OFF" onclick="javascript: FLW.off();" />
 * <input type="button" value="ON"  onclick="javascript: FLW.on();" />
 */

/*
 * We should invoke ContextMenuModule.stop() for fixing problem with context menu hiding under IE8
 * (Please see Bug # 3330: Deleting the last document version should redirect to parent page).
 *
 * JS exception occurs because AJAX request and context menu hiding with "scroll up" effect process
 * simultaneously. This causes some DOM elements are null in JS. For preventing bug we perform
 * immediate hiding of context menu without any effect (function ContextMenuModule.stop())
 * before AJAX request will be invoked.
 */
var FLW = {
    on: function() {
        if (typeof contextMenu != 'undefined' && contextMenu) {
            contextMenu.stop();
        }
        AjaxIndicator.setDisplayMode(AjaxIndicator.DISPLAY_ON);
    },

    off: function() {
        AjaxIndicator.setDisplayMode(AjaxIndicator.DISPLAY_OFF);
    }
};
var AjaxIndicator = {

	TOP_PADDING		:	10,
	RIGHT_PADDING	:	10,

	CONTROL_ELEMENT	:	null,

	DISPLAY_ON		:	"block",
	DISPLAY_OFF		:	"none",

    SWITCH_ON_COUNT : 0,
    /** displays  the indicator in the right-top corner
	*	according to the scroller Use in IE6 only
	*/

	indicate		: 	function() {
		var pos=document.documentElement.scrollTop ?
        	      document.documentElement.scrollTop : document.body.scrollTop;
		AjaxIndicator.CONTROL_ELEMENT.style.top=pos.toString()  + "px";
		},

    /**
	 * @param overlay_id Overlay ID
	 *
	 * NOTE: Overlay container must have already setup CSS rules
	 */
	setup			:	function(overlay_id) {
		AjaxIndicator.CONTROL_ELEMENT = (typeof overlay_id == "object")
			? overlay_id
			: document.getElementById(overlay_id);
		if(typeof AjaxIndicator.CONTROL_ELEMENT != "object")
			AjaxIndicator.CONTROL_ELEMENT = null;
		else
			this.indicate();
	},


	isAvailable		: 	function() {
		return (typeof AjaxIndicator.CONTROL_ELEMENT == "object");
	},

	/**
	 * @param mode one of AjaxIndicator.DISPLAY_ON or AjaxIndicator.DISPLAY_OFF
	 */
	setDisplayMode	:	function(mode) {
		if(AjaxIndicator.isAvailable()) {
            if (mode == AjaxIndicator.DISPLAY_ON) {
                AjaxIndicator.SWITCH_ON_COUNT = AjaxIndicator.SWITCH_ON_COUNT + 1;
            } else {
                AjaxIndicator.SWITCH_ON_COUNT = AjaxIndicator.SWITCH_ON_COUNT - 1;
                if (AjaxIndicator.SWITCH_ON_COUNT  > 0) {
                    return;
                } else {
                    AjaxIndicator.SWITCH_ON_COUNT = 0;
                }
            }
            AjaxIndicator.CONTROL_ELEMENT.parentNode.style.display = mode;
		};
	}
};
