function MultiSelect(IsInline)
{
	var div = null, x1 = 0, y1 = 0, x2 = 0, y2 = 0;
	var last = []
	var active = false
	var changed = false
	var self = this
	var inline = IsInline

	this.SetIsInline = function(IsInline) {
		inline = IsInline
	}

	function reCalc() {
		var x3 = Math.min(x1,x2);
		var x4 = Math.max(x1,x2);
		var y3 = Math.min(y1,y2);
		var y4 = Math.max(y1,y2);
		Object.assign(div.style, {
			left: x3 + 'px',
			top: y3 + 'px',
			width: x4 - x3 + 'px',
			height: y4 - y3 + 'px'
		});
		if(x4 - x3 > 10 && y4 - y3 > 10 && active)
			$(div).show();
		else
			$(div).hide();
		$("body").toggleClass("multiselect-active", active)
	}

	function RectangleSelect(selector, x1, y1, x2, y2) {
		var elements = [];
		var { pageXOffset, pageYOffset } = window;
		document.querySelectorAll(selector).forEach((el) => {
			var rect = el.getBoundingClientRect();
			var x = rect.left + pageXOffset;
			var y = rect.top + pageYOffset;
			var w = rect.width;
			var h = rect.height;

			if (!(x+w<x1 || x2<x || y+h<y1 || y2<y))
			{
				var add = "0"
				if(inline)
				{
					add = el.parentNode.getAttribute("task-id")
				}else
				{
					add = el.getAttribute("task-id")
				}
				
				if(add != "0")
			    	elements.push(add);
			}
		});
		return elements;
	}

	function FindSelectedActions()
	{
		var tx1, tx2, ty1, ty2;
		if(x1 > x2)
		{
			tx1 = x2
			tx2 = x1
		}else
		{
			tx1 = x1
			tx2 = x2
		}
		if(y1 > y2)
		{
			ty1 = y2
			ty2 = y1
		}else
		{
			ty1 = y1
			ty2 = y2
		}

		var selector = inline ? ".taskcontainer .toolcontent": ".taskcontainer"

		var nxt = RectangleSelect(selector,tx1,ty1,tx2,ty2)

		var remove = $(last).not(nxt).get()
		var add = $(nxt).not(last).get()
		if(add.length > 0)
		{

			add.forEach(function(id){

				var task = FindTaskById(parseInt(id))

				if(task)
				{

					if(!task.get("is_selected"))
					{
						changed = true
						_MainView.currentTargetId = IndexOf(parseInt(id))
						_MainView.ToggleSelection()
					}
				}

			})

			

		}
		if(remove.length > 0)
		{

			remove.forEach(function(id){

				var task = FindTaskById(parseInt(id))

				if(task)
				{
					if(task.get("is_selected"))
					{
						changed = true
						_MainView.currentTargetId = IndexOf(parseInt(id))
						_MainView.ToggleSelection()
					}
				}

			})				
		}
			
		last = nxt
	}

	this.Update = function(clientX,clientY, Pressed)
	{
		if($('.modal.in').length == 0 && Pressed)
		{
			x2 = clientX + document.documentElement.scrollLeft;
			y2 = clientY + document.documentElement.scrollTop;
			if(y2 > document.documentElement.scrollHeight - 1)
				y2 = document.documentElement.scrollHeight - 1
			if(x2 > document.documentElement.scrollWidth - 1)
				x2 = document.documentElement.scrollWidth - 1
			if(y2 < 0)
				y2 = 0
			if(x2 < 0)
				x2 = 0
			if(active)
			{
				FindSelectedActions();
				reCalc();
			}
		}
	}

	this.IsActive = function()
	{
		return $(div).is(":visible"); 
	}

	this.Init = function()
	{

		

		$( document ).ready(function() {
			


			div = document.getElementById('multiselect')
			$("html").mousedown(function(e) {

				if($('.modal.in').length == 0 && e.button == 0)
        		{
        			var target = $(e.target)
        			if(
						target.hasClass("main") || 
						target.hasClass("tool-margin") || 
						(target.prop("tagName") == "HTML" && e.clientX < document.documentElement.offsetWidth) ||
						inline && ( target.hasClass("tool-div"))
					 )
        			{
        				BrowserAutomationStudio_StopSaveToUndoManager = true
						active = true
						changed = false
						
						last = []

						if(!e.shiftKey)
							_MainView.ClearSelection()

						
						x1 = e.clientX + document.documentElement.scrollLeft;
						y1 = e.clientY + document.documentElement.scrollTop;
						x2 = x1;
						y2 = y1;
						
						reCalc();
					}
				}
			});
			$("html").mousemove(function(e) {
				self.Update(e.clientX, e.clientY, e.button == 0)
			});

			$("html").mouseup(function(e) {
				if($('.modal.in').length == 0 && e.button == 0)
        		{
					active = false
					$(div).hide();
					$("body").removeClass("multiselect-active")


					BrowserAutomationStudio_StopSaveToUndoManager = false
					if(changed)
					{
						BrowserAutomationStudio_SaveToUndoManager()
					}
					changed = false;
				}
			});

			
		});
	}


}