﻿window.roadBlocker = {
	_initialize: function() {
		this.enabled = false;
		Event.observe(window, 'load', this._onLoad.bind(this));
	},

	_onLoad: function() {
		this.initLinks();
		Sys.WebForms.PageRequestManager.getInstance().add_pageLoaded(this._updatePanelLoadedHandler.bind(this));
	},

	_updatePanelLoadedHandler: function(sender, args) {
		var updatedPanels = args.get_panelsUpdated();
		for (i = 0; i < updatedPanels.length; i++) {
			this.initLinks(updatedPanels[i].id);
		}
	},

	init: function(enabled) {
		this.enabled = enabled;
	},

	initLinks: function(areaId) {
		var t = this;
		var areaSelector = areaId ? '#' + areaId + ' .linkWithSignUp' : '.linkWithSignUp';
		var res = $$(areaSelector).each(function(el) {
			var isSUT = false;
			el.className.split(' ').each(function(cl) {
				var clArray = cl.split('_');
				if (clArray.length == 2 && clArray[0] == 'SUT' && !isNaN(parseInt(clArray[1]))) {
					t.bindSignUpSUTElement(el, clArray[1]);
					isSUT = true;
					return;
				}
			})
			if (!isSUT) {
				t.bindSignUpElement(el);
			}
			el.removeClassName('linkWithSignUp');
		});

		if (this.userIsBlocked()) {
			areaSelector = areaId ? '#' + areaId + ' .linkWithRoadBlocking' : '.linkWithRoadBlocking';

			res = $$(areaSelector).each(function(el) {
				t.bindElement(el);
				el.removeClassName('linkWithRoadBlocking');
			});
		}
	},

	userIsBlocked: function() {
		return this.enabled;
	},

	_runRoadBlocker: function() {
		Flux.createWidget('Authentication', { containerId: null },
		function(widget) {
			widget.performRoadBlockerCheck();
		});
		// Flux.createWidget('Context', {}, function(context) { context.performRoadBlockerCheck(false); });
	},

	_runSignUp: function() {
		Flux.createWidget('Authentication', { containerId: null },
		function(widget) {
			widget.showSignUpForm()
		});
	},

	_runSignUpSUT: function(sutId) {
		Flux.createWidget('Authentication', { containerId: null },
		function(widget) {
			widget.showSutSignUpForm(sutId)
		});
	},

	bindSignUpSUTElement: function(element, userTypeId, eventName) {
		this._bindElementEvent(element, eventName, this._runSignUpSUT, userTypeId);
	},

	bindSignUpElement: function(element, eventName) {
		this._bindElementEvent(element, eventName, this._runSignUp);
	},

	bindElement: function(element, eventName) {
		if (!this.userIsBlocked()) return;

		this._bindElementEvent(element, eventName, this._runRoadBlocker);
	},

	_bindElementEvent: function(element, eventName, func, param) {
		if (eventName == undefined) eventName = 'click';

		Event.observe(element, eventName, function(event) {
			event.stop();
			func(param);
		});

		var eventAttibuteName = 'on' + eventName;
		if (element[eventAttibuteName]) {
			element.removeAttribute(eventAttibuteName);
			element[eventAttibuteName] = null;
		}
	},

	_bindElementEventWithParam: function(element, eventName, func, param) {
		if (eventName == undefined) eventName = 'click';

		Event.observe(element, eventName, function(event) {
			event.stop();
			func(param);
		});

		var eventAttibuteName = 'on' + eventName;
		if (element[eventAttibuteName]) {
			element.removeAttribute(eventAttibuteName);
			element[eventAttibuteName] = null;
		}
	},

	actionIfNeeded: function() {
		if (this.userIsBlocked()) this._runRoadBlocker();
	},

	safeAction: function(func) {
		if (!this.userIsBlocked()) return func();
		this._runRoadBlocker();
	},

	onClickAction: function() {
		if (!this.userIsBlocked()) return true;
		this._runRoadBlocker();
		return false;
	}
}
window.roadBlocker._initialize();

// [AtlasScript]

if(typeof(Sys)!="undefined")
	Sys.Application.notifyScriptLoaded();

// [/AtlasScript]
/**/