/* Setting up the custom event */
var CustomEvent = function() {
	//name of the event
	this.eventName = arguments[0];
	var mEventName = this.eventName;

	//function to call on event fire
	var eventAction = null;

	//subscribe a function to the event
	this.subscribe = function(fn) {
		eventAction = fn;
	};

	//fire the event
	this.fire = function(sender, eventArgs) {
		this.eventName = mEventName;
		if(eventAction != null) {
			eventAction(sender, eventArgs);
		}
	};
};

var MinimizedOrBlurred = new CustomEvent("MinimizedOrBlurred");

/* Detecting and firing the event */
var current_focus = document;
var blur_count = 0;

setInterval(function() { 
	if ((window.screenX == -32000 && window.screenY == -32000) || current_focus == null)
	{
		blur_count = blur_count + 1;
	} else {
		blur_count = 0;
	}
	
	if (blur_count == 1) {
		MinimizedOrBlurred.fire(null);
	}
}, 1000);


// check for Internet Explorer
if (/*@cc_on!@*/false) { 
	document.onfocusin = onFocus;
	document.onfocusout = onBlur;
} else {
	window.onfocus = onFocus;
	window.onblur = onBlur;
}

function onBlur()
{
    current_focus = null;    
}

function onFocus()
{
    current_focus = this;    
}

function SetFocusAchieved()
{
    //window.focus();
    current_focus = document;
    blur_count = 0;
}