


function SvgClass(drawing_layer, image_frame)
{
	dojo.require("dojox.gfx");
	
	var me = this;

	this.draw_mode = false;

	
	var svg_layer = document.createElement("div");
	svg_layer.style.position = "absolute";
	svg_layer.style.zIndex = 2;
	//svg_layer.style.top = "50px";
	svg_layer.style.border = "2px solid red";
	//drawing_layer.style.border = "1px solid green";
	//svg_layer.style.backgroundColor = "green";
	drawing_layer.appendChild(svg_layer);

	svg_layer.style.top = "-1000px";
	svg_layer.style.left = "-1000px";


	var svg_surface = dojox.gfx.createSurface(svg_layer, 6000, 6000);


	image_frame.ondblclick = function(){me.ClearAll();};

	
	if (document.addEventListener) // ff
	{
		image_frame.addEventListener("mousedown", MouseDown, false);
		document.addEventListener("mouseup", MouseUp, false);
		document.addEventListener("mousemove", MouseMove, false);
	} 
	else if (document.attachEvent) // ie
	{
		image_frame.attachEvent("onmousedown", MouseDown);
		document.attachEvent("onmouseup", MouseUp);
		document.attachEvent("onmousemove", MouseMove);
	}



	var line_data_array = new Array();
	var line_objects = new Array();
	var line_base_objects = new Array();
	var active_line_data_array = new Array();
	var active_line_object = null;
	var active_line_base_object = null;
	var drawing_bussy = false;

	
	var mouse_down = false;
	function MouseDown(event)
	{
		if (me.draw_mode == false) return;

		mouse_down = true;
	}

	function MouseUp(event)
	{
		mouse_down = false;

		if (active_line_data_array.length > 0)
		{
			line_data_array.push(active_line_data_array);
			active_line_data_array = new Array();
		}

		if (active_line_object != null)
		{
			line_objects.push(active_line_object);
			active_line_object = null;
		}

		if (active_line_base_object != null)
		{
			line_base_objects.push(active_line_base_object);
			active_line_base_object = null;
		}
	}


	function MouseMove(event)
	{
		if (me.draw_mode == false) return;
		if (mouse_down == false) return;
		if (drawing_bussy == true) return; // kamēr zīmē iepriekšējo līniju, jauna nezīmēsim

		// iegūst un pieglabā kursora sākuma koordinātes
		var pointer_x = pointerX(event) - findPosX(E('pan_area')) - ObjectLeft(svg_layer);
		var pointer_y = pointerY(event) - findPosY(E('pan_area')) - ObjectTop(svg_layer);


		// zīmē jaunu līniju tikai no iepriekšējā punkta
		if (active_line_data_array.length > 0)
		{
			drawing_bussy = true;

			//var tmp_line_data = [line_data_array[line_data_array.length-1], {x: pointer_x, y: pointer_y}];
			//svg_surface.createPolyline(tmp_line_data).setStroke({ color: 'blue', width: 22});


			// novāc iepriekšējo līniju
			if (active_line_object != null){ svg_surface.remove(active_line_object, true); }
			if (active_line_base_object != null){ svg_surface.remove(active_line_base_object, true); }

			active_line_data_array.push({x: pointer_x, y: pointer_y});
			active_line_base_object = svg_surface.createPolyline(active_line_data_array).setStroke({ color: 'red', width: 8});
			active_line_object = svg_surface.createPolyline(active_line_data_array).setStroke({ color: 'blue', width: 6});

			setTimeout(function(){drawing_bussy = false;}, 10);
		}
		else
		{
			active_line_data_array.push({x: pointer_x, y: pointer_y});
		}
	}



	this.ClearAll = function()
	{
		line_data_array = new Array();
		active_line_data_array = new Array();

		if (active_line_object != null){ svg_surface.remove(active_line_object, true); }
		if (active_line_base_object != null){ svg_surface.remove(active_line_base_object, true); }
		active_line_object = null;
		active_line_base_object = null;

		for (var i=0; i<line_objects.length; i++)
		{
			svg_surface.remove(line_objects[i], true);
		}

		for (var i=0; i<line_base_objects.length; i++)
		{
			svg_surface.remove(line_base_objects[i], true);
		}
		
	}



}


