/********************************************
*  iJax Clipping Extension by Innotive Inc. *
********************************************/


/*
This extension allow users to select a rectangular area of IPQ with mouse dragging.

browser.Execute("clip","") starts selecting mode. When an area is selected, 
"clipsave" event is fired, where param1 is in form of "x,y,width,height".
*/

function ClipExtension	()
{
	var self = this;

	var layer = null;
	var browser = null;

	var board = null;

	var func = null;
	this.initialize = function(parent)
	{
		browser = parent;
		layer = browser.GetLayer();
	}

	this.onLoad = function()
	{
		if (board != null) self.onUnload();

		board = document.createElement("div");
		board.style.position = "absolute";
		board.style.left = board.style.top = 0;
		board.style.width = board.style.height = "100%";
		board.style.cursor = "crosshair";
		board.style.display = 'none';

		var drawing = false;
		board.onmousedown = function(e)
		{
			if (drawing) return;
			drawing = true;
			if (!e) var e = window.event;
			var bx = (window.opera ? (e.clientX - layer.offsetLeft) : (e.x ? e.x : e.clientX - layer.offsetLeft));
			var by = (window.opera ? (e.clientY - layer.offsetTop) : (e.y ? e.y : e.clientY - layer.offsetTop));
			var cx, cy;
	
			var rect = document.createElement("div");
			rect.style.position = "absolute";
			rect.style.left = bx;
			rect.style.top = by;
			rect.style.border = "red 1px solid";
			board.appendChild(rect);

			board.onmousemove = function(e)
			{
				if (!e) var e = window.event;
				cx = (window.opera ? (e.clientX - layer.offsetLeft) : (e.x ? e.x : e.clientX - layer.offsetLeft));
				cy = (window.opera ? (e.clientY - layer.offsetTop) : (e.y ? e.y : e.clientY - layer.offsetTop));

				rect.style.left = Math.min(bx, cx);
				rect.style.top = Math.min(by, cy);
				rect.style.width = Math.abs(cx - bx); 
				rect.style.height = Math.abs(cy - by);
			}

			board.onmouseup = function()
			{
				board.removeChild(rect);
				board.onmousemove = board.onmouseup = null;
				board.style.display = "none";
				drawing = false;

				// transform the viewport coordinate into ipq coordinate.
				
				var ratio = browser.GetMapWidth(browser.GetLevelCount()) / browser.GetMapWidth(browser.GetCurrentLevel());
				var left = (Math.min(bx, cx) + browser.GetViewportX()) * ratio;
				var top = (Math.min(by, cy) + browser.GetViewportY()) * ratio;
				var width = Math.abs(bx - cx) * ratio;
				var height = Math.abs(by - cy) * ratio;
				browser.ProcessEvent("event", func, left + "," + top + "," + width + "," + height);
			}
		}

		if ((document.all) && (!window.opera)) 
		{
			board.innerHTML = '<table width="100%" height="100%"><tr><td></td></tr></table>'
		}
		layer.appendChild(board);
	}

	this.onUnload = function()
	{
		if (board)
		{
			try
			{
				board.parentNode.removeChild(board);
			} catch (e) {}
		}
		board = null;
	}

	this.onBrowserEvent = function(ev, param1, param2)
	{
		return false;
	}

	this.Execute = function(action, param)
	{
		func = param;
		if (action == "clip")
		{
			board.style.display = '';
			return true;
		}
		return false;
	}

	return this;
}

