var embedWidth = parseInt(590);var embedHeight = parseInt(190);var svgns = "http://www.w3.org/2000/svg";var imagens = "http://www.w3.org/1999/xlink";var imagerLoaded = false;var mouseMove = null;var doc = null;function translateEvt(evt, trans) {if(trans == null){/* Performing the translation is really slow, so avoid it if possible. * Unfortunately this is necessary on some platforms * (MSIE if I remember correctly). */return evt;}try {evt.clientX = add(evt.clientX, trans.x);evt.clientY = add(evt.clientY, trans.y);evt.pageX = add(evt.pageX, trans.x);evt.pageY = add(evt.pageY, trans.y);} catch(E) {}return evt;}function tools_MouseMove(evt, trans) {if(mouseMove != null) {mouseMove(translateEvt(evt, trans));}}function getPositionX(evt) {var ret;if(evt.pageX != undefined) {ret = evt.pageX;} else {ret = evt.clientX;}return ret;}function getPositionY(evt) {var ret;if(evt.pageY != undefined) {ret = evt.pageY;} else {ret = evt.clientY;}return ret;}function msleep(m) {var then = new Date(new Date().getTime() + m);while (new Date() < then) {}}function clearAttribute(obj, attribute) {obj.setAttribute(attribute, 'none');obj.removeAttribute(attribute);}function SVG_OK() {if(!imagerLoaded)return -1;return !(doc == null);}function clearImage() {if(SVG_OK()){freehand_Clear('local');}}function handleKeyboard(evt) {var key = evt.keyCode ? evt.keyCode : evt.charCode;if(key == 0) {}else if(key == 8) {tools_SendCommand('delete', null);}else if(key == 46) {tools_SendCommand('delete', null);}else if(key == 27) {tools_SendCommand('escape', null);}else if(key == 37) {tools_SendCommand('left', null);}else if(key == 39) {tools_SendCommand('right', null);}else if(key == 38) {tools_SendCommand('up', null);}else if(key == 40) {tools_SendCommand('down', null);}}function touchDown(evt) {if(evt.touches.length == 1){var touch = evt.touches[0];var event = {'stopPropagation' : 0,'button' : 0,'clientX' : touch.clientX,'clientY' : touch.clientY,'pageX' : touch.pageX,'pageY' : touch.pageY};tools_StartDrawing(event);}evt.preventDefault();}function touchMove(evt) {if(evt.touches.length == 1){var touch = evt.touches[0];var event = {'stopPropagation' : 0,'button' : 0,'clientX' : touch.clientX,'clientY' : touch.clientY,'pageX' : touch.pageX,'pageY' : touch.pageY};tools_MouseMove(event);}evt.preventDefault();}function touchUp() {tools_StopDrawing();}function initImage(sync, server) {var all = null, local = null, remote = null, bglayer = null, bg = null;var image = document.getElementById('image');try {doc = image.getSVGDocument();} catch(E) {return;}all = doc.getElementById('all');bglayer = doc.createElementNS(svgns, 'g');bglayer.setAttribute('id', 'bglayer');bglayer.setAttribute('opacity', '0.2');all.appendChild(bglayer);local = doc.createElementNS(svgns, 'g');local.setAttribute('id', 'local');all.appendChild(local);remote = doc.createElementNS(svgns, 'g');remote.setAttribute('id', 'remote');all.appendChild(remote);doc.onmousedown = preventDrag;doc.onmousemove = preventDrag;doc.onmouseup = preventDrag;doc.ongesturestart = preventDrag;doc.ongesturestart = preventDrag;doc.ongesturechange = preventDrag;doc.ongestureend = preventDrag;doc.ontouchstart = touchDown;doc.ontouchmove = touchMove;doc.ontouchend = touchUp;/*doc.onmousedown = tools_StartDrawing;doc.onmouseup = tools_StopDrawing;*/document.onmouseup = tools_StopDrawing; /* stop drawing if the user releases the mouse off of the drawing area */doc.onkeypress = handleKeyboard;if(sync != '') {initSync(sync, remote);/* Load the TICC logo :) */bg = doc.createElementNS(svgns, 'background');bg.setAttribute('x', 0);bg.setAttribute('y', 0);bg.setAttribute('width', '0');bg.setAttribute('height', '0');bglayer.appendChild(bg);logo = doc.createElementNS(svgns, 'image');logo.setAttributeNS(imagens, 'xlink:href', '/csm/survey/imager/ticc.png');logo.setAttribute('x', embedWidth-150);logo.setAttribute('y', embedHeight-100);logo.setAttribute('width', '150');logo.setAttribute('height', '100');bglayer.appendChild(logo);}tools_SendCommand('initialize', local);setTool('0');}function getSVGCode() {var xml;try {var serializer = new XMLSerializer();xml = serializer.serializeToString(doc);} catch(E) {try {var image = document.getElementById('image');xml = image.window.printNode(doc);} catch(E) {alert('Failed to generate SVG image.');}}return xml;}function getGroup(parentName) {return doc.getElementById(parentName);}function getGroupAttr(parentName, attribName) {var parent = getGroup(parentName);return parent.getAttribute(attribName);}function setGroupAttr(parentName, attribName, attribValue) {var parent = getGroup(parentName);parent.setAttribute(attribName, attribValue);}function getGroupChild(parentName, childName) {var parent = getGroup(parentName);if(parent != null) {var children = parent.childNodes;for(var i = 0; i < children.length; i++) {var child = children.item(i);if(child.id == childName) {return child;}}}return null;}function preventDrag(evt) {/* disallow drag and drop (only needed in firefox) */if (evt.stopPropagation) {evt.stopPropagation();evt.preventDefault();}return false;}function setCursor(cursor, x, y) {var svg = doc.getElementById('svg');if(!cursor) {svg.setAttribute('cursor', 'auto');} else {svg.setAttribute('cursor', 'url(/csm/survey/imager/'+cursor+') '+x+' '+y+', auto');}}var currentTool = null;var lastIndex = 0;function tools_StartDrawing(evt, trans) {var func = tools[currentTool][4];/* disallow drag and drop (only needed in firefox) */if (evt.stopPropagation) {evt.stopPropagation();evt.preventDefault();}evt = translateEvt(evt, trans);if(evt.button == 0) {func('start-drawing', evt);}}function tools_StopDrawing() {var func = tools[currentTool][4];func('stop-drawing', null);mouseMove = preventDrag;}function redrawTools() {for(i=0;i<7;i++)offTool(i);}function tools_SendCommand(c,v) {var i;tools_ServerSync(c,v,-1);for(i=0;i<7;i++) {func = tools[i][4];func(c,v,-1);}}function setTool(id) {var toolObject = document.getElementById('tool-'+id);var lastTool = currentTool;var func;if(id == null || currentTool == id) {return;}if(toolObject.src == tools[id][3].src) {return;}if(currentTool != null){func = tools[currentTool][4];func('unset-tool');}func = tools[id][4];currentTool = id;redrawTools();func('set-tool', lastTool);}function overTool(id) {var toolObject = document.getElementById('tool-'+id);if(toolObject.src != tools[id][2].src && toolObject.src != tools[id][3].src && currentTool != id) {toolObject.src = tools[id][2].src;}}function disableTool(id) {var toolObject = document.getElementById('tool-'+id);toolObject.src = tools[id][3].src;}function offTool(id) {var toolObject = document.getElementById('tool-'+id);if(toolObject.src == tools[id][3].src) {return;}if(currentTool == id) {toolObject.src = tools[id][1].src;} else {toolObject.src = tools[id][0].src;}}function showObject(object) {var obj = document.getElementById(object);obj.style.visibility = 'visible';}function hideObject(object) {var obj = document.getElementById(object);obj.style.visibility = 'hidden';}function getWidth(obj) { return obj.offsetWidth;}function getLeft(obj) {var curleft = 0;if (obj.offsetParent) {while (obj.offsetParent) {if(obj != "[object HTMLDivElement]") {curleft += obj.offsetLeft;}obj = obj.offsetParent;}}else if(obj.x) {curleft += obj.x;}return curleft;}function getTop(obj) {var curtop = 0;if (obj.offsetParent) {while (obj.offsetParent) {if(obj != "[object HTMLDivElement]") {curtop += obj.offsetTop;}obj = obj.offsetParent;}} else if(obj.y) {curtop += obj.y;}curtop -= 1;return curtop;}function getHeight(obj) { return obj.offsetHeight;}function xmlRequest_New() {var xmlhttp = false;if(window.XMLHttpRequest){xmlhttp = new XMLHttpRequest();if (xmlhttp.overrideMimeType){xmlhttp.overrideMimeType('text/xml');}}else if(window.ActiveXObject){try {xmlhttp = new ActiveXObject("Msxml2.XMLHTTP");} catch(e) {try {xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");} catch (e) {}}}return xmlhttp;}var send_id = 0;function xmlGetData(command, object, dataHandler) { var xmlhttp = xmlRequest_New(); if(xmlhttp != false) { xmlhttp.open("GET", command, true); xmlhttp.onreadystatechange=function() { if(xmlhttp.readyState==4) { dataHandler(object, xmlhttp.responseText); } }; xmlhttp.send(null); } else { dataHandler(object, "ERROR");}}function xmlPostData(command, data, object, dataHandler) {var objHTTP, strResult;var xmlhttp = xmlRequest_New();if(xmlhttp != false) {xmlhttp.open('POST', command, true);xmlhttp.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded');if(dataHandler != null) {xmlhttp.onreadystatechange=function() {if(xmlhttp.readyState==4) {dataHandler(object, xmlhttp.responseText);}};}xmlhttp.send(data.toString());/*if(dataHandler != null) {dataHandler(object, xmlhttp.responseText);}*/} else {alert("Error creating XMLHttpRequest.");}}var serverSync = null;/* Use arrayInUse as a NEAR-mutex, * a better way should be implemented */var lineArray = Array();var arrayLength = 0;var arrayInUse = false;function postImage(server) {var data = 'request=post&image='+getSVGCode();xmlPostData('/csm/survey/imager/sync.php', data, null, null);setTimeout("postImage(\'"+server+"\')", 1000);}function initSync(sync, remote) {serverSync = sync;if(sync == 'client') {disableTool('5');disableTool('6');tools_RemoteSend('initialize', remote);xmlGetData_Thread();tools_ServerRender();} else {xmlPostData_Thread();}}var testIndex = -1;function xmlPostData_Thread() {var lines = null;var data = null;while(arrayInUse) { }arrayInUse = true;lines = lineArray;lineArray = Array();arrayLength = 0;arrayInUse = false;for(var i=0;i i){/* only add the item if something follows */seglist.insertItemBefore(pt, i);}return true;}function SVG_RemoveSegment(seglist, i) {if(seglist.removeItem != undefined) {seglist.removeItem(i);} else {var oldlist = seglist;var k=0;seglist = Array();for(j=0;j 0) {seglist.removeItem(0);}} else {seglist = Array();}return seglist;}function SVG_GetSegment(seglist, i) {var ret;try {ret = seglist.getItem(i);} catch(E) {ret = seglist[i];}return ret;}function SVG_GetSegListLen(seglist) {var maxitems;if(seglist.numberOfItems != undefined) {maxitems = seglist.numberOfItems;} else {maxitems = seglist.length;}return maxitems;}function freehand_MouseDown(evt){if(!freehand_drawing) {return false;}freehand_SendCommand('add-pixel',evt,0);return false;}function freehand_AddPixel(parent, evt) {var path = doc.getElementById(getGroupAttr(parent, 'active-path'));var x = getPositionX(evt);var y = getPositionY(evt);SVG_AddPathPoint(path, x, y);}function freehand_StopDrawing(parent) {if(parent == 'local') {/*doc.onmousemove = preventDrag;*/freehand_drawing = false;}}function freehand_Initialize(parent) {var fh = doc.createElementNS(svgns, "g");if(navigator.userAgent.search('Safari') >0 ){/* Safari horribly violates the W3C DOM specification, * so unfortunately we are forced to special case it */safari = true;}fh.setAttribute("id", 'container-freehand');parent.appendChild(fh);setTimeout('freehand_PointThread()', 100);}var id = 0;function freehand_PointThread() {var points = null;var data = null;while(freehand_arrayInUse) { }freehand_arrayInUse = true;points = freehand_pointArray;freehand_pointArray = Array();freehand_arrayLength = 0;freehand_arrayInUse = false;for(var i=0;i 0) {freehand_id--;obj = doc.getElementById('path-'+parent+'-'+freehand_id);if(obj != null) {fh.removeChild(obj);}}}function add(a,b) {var c = parseInt(a) + parseInt(b);return c;}function freehand_ErasePoints(fh, path, circle) {var seglist = SVG_GetPathSegments(path);var maxitems = SVG_GetSegListLen(seglist);var x = parseInt(circle.x);var y = parseInt(circle.y);var r = parseInt(circle.r);var ox = SVG_GetSegment(seglist, 0).x;var oy = SVG_GetSegment(seglist, 0).y;/* This is when the first item needs to be removed, * removing the first item is more difficult since it holds the abs position */if(Math.sqrt(Math.pow(ox-x, 2)+Math.pow(oy-y, 2)) <= r) {/* one or less points is no good */if(maxitems <= 2) {fh.removeChild(path);} else {var nx = SVG_GetSegment(seglist, 1).x;var ny = SVG_GetSegment(seglist, 1).y;/* try the easy way */if(!SVG_ChangeToMove(path, 0)){/* recreate the entire list manually */var segments = new Array(maxitems);segments[0] = SVG_SetPathStart(path, nx, ny);for(i=2;i w) || (oy < y) || (oy > h))return;for(var i=1;i w) || (oy < y) || (oy > h))return;}path.setAttribute('stroke-dasharray', '2,2');path.setAttribute('selected', 'true');}function freehand_DeselectAll(parent) {var fh = getGroupChild(parent, 'container-freehand');var obj = fh.firstChild;while(obj != null) {if(obj.getAttribute('selected') == 'true') {clearAttribute(obj, 'stroke-dasharray');clearAttribute(obj, 'selected');}obj = obj.nextSibling;}}function freehand_CheckErase(parent, circle) {var fh = getGroupChild(parent, 'container-freehand');var obj = fh.firstChild;while(obj != null) {freehand_ErasePoints(fh, obj, circle);obj = obj.nextSibling;}}function freehand_DeleteSelected(parent) {var fh = getGroupChild(parent, 'container-freehand');var obj = fh.firstChild;while(obj != null) {if(obj.getAttribute('selected') == 'true') {var del = obj;obj = obj.nextSibling;fh.removeChild(del);} else {obj = obj.nextSibling;}}}function freehand_SelectContained(parent, rect) {var fh = getGroupChild(parent, 'container-freehand');var obj = fh.firstChild;while(obj != null) {freehand_SelectObject(obj, rect);obj = obj.nextSibling;}}function freehand_MoveObject(path, point) {var seglist = SVG_GetPathSegments(path);var maxitems = SVG_GetSegListLen(seglist);for(i=0;i 0) {activeSelect.setAttribute("width", x-startX);activeSelect.setAttribute("x", startX);} else {activeSelect.setAttribute("width", startX-x);activeSelect.setAttribute("x", x);}if(y-startY > 0) {activeSelect.setAttribute("height", y-startY);activeSelect.setAttribute("y", startY);} else {activeSelect.setAttribute("height", startY-y);activeSelect.setAttribute("y", y);}}function select_StartDrawing(evt) {tools_SendCommand('escape', null);startX = getPositionX(evt);startY = getPositionY(evt);activeSelect = doc.createElementNS(svgns, "rect");activeSelect.setAttribute("id", 'select-0');activeSelect.setAttribute("fill-opacity", "0");activeSelect.setAttribute("stroke", "black");activeSelect.setAttribute("stroke-width", "1");activeSelect.setAttribute("stroke-linecap", "butt");activeSelect.setAttribute("stroke-linejoin", "miter");activeSelect.setAttribute("stroke-miterlimit", "4");activeSelect.setAttribute("stroke-dasharray", "2,2");activeSelect.setAttribute("stroke-dashoffset", "0");activeSelect.setAttribute("x", startX);activeSelect.setAttribute("y", startY);activeSelect.setAttribute("width", 0);activeSelect.setAttribute("height", 0);sel.appendChild(activeSelect);mouseMove = select_Resize;}function select_StopDrawing() {if(activeSelect != null) {var rect = {'type' : 'rect','x' : activeSelect.getAttribute("x"),'y' : activeSelect.getAttribute("y"),'width' : activeSelect.getAttribute("width"),'height' : activeSelect.getAttribute("height")};tools_SendCommand('select-contained', rect);sel.removeChild(activeSelect);}activeSelect = null;}function select_SendCommand(command, variable) {if(command == 'initialize') {sel = variable;} else if(command == 'start-drawing') {select_StartDrawing(variable);} else if(command == 'stop-drawing') {select_StopDrawing();}}var pallete = null;var lastTool = null;function color_SetColor(parent, color) {setGroupAttr(parent, 'global-color', color);pallete.style.visibility = 'hidden';setTool(lastTool);}function color_HidePallete(v) {var obj = document.getElementById('tool-2');lastTool = v;pallete.style.visibility = 'hidden';}function color_ShowPallete(v) {var p = document.getElementById('imagediv');var obj = document.getElementById('tool-2');lastTool = v;pallete.style.left = getLeft(obj)-getLeft(p);pallete.style.top = getTop(obj)+getHeight(obj)-getTop(p);pallete.style.visibility = 'visible';}function color_Initialize(parent) {setGroupAttr(parent, 'global-color', 'black');pallete = document.getElementById('color-select');}function color_SendCommand(c,v,s) {var p = 'local';if(s == -2) {p = 'remote';}if(c == 'initialize') {color_Initialize(p);} else if(c == 'set-tool') {color_ShowPallete(v);} else if(c == 'unset-tool') {color_HidePallete();} else if(c == 'set-color') {color_SetColor(p,v);}}var move_startX = null;var move_startY = null;function move_AdjustObjects(evt) {var point = {'type' : 'point','x' : getPositionX(evt)-move_startX,'y' : getPositionY(evt)-move_startY};tools_SendCommand('adjust-position', point);move_startX = getPositionX(evt);move_startY = getPositionY(evt);}function move_StartDrawing(evt) {move_startX = getPositionX(evt);move_startY = getPositionY(evt);mouseMove = move_AdjustObjects;}function move_StopDrawing() {move_startX = null;move_startY = null;}function move_SendCommand(c,v,s) {if(s != -1) {tools_ServerSync(c,v,3);}if(c == 'start-drawing') {move_StartDrawing(v);} else if(c == 'stop-drawing') {move_StopDrawing();}}var circleRadius = 10;var activeErase = null;var erase;function delete_SetTool(lastTool) {setCursor('eraser.png', 7, 12);}function delete_UnsetTool() {setCursor(null);}function delete_StartDrawing(evt) {tools_SendCommand('escape', null);startX = getPositionX(evt);startY = getPositionY(evt);/* TODO: Report to the user that they need to wait */if(activeErase) {return;}activeErase = doc.createElementNS(svgns, "circle");activeErase.setAttribute("id", 'erase-0');activeErase.setAttributeNS(null, "cx", startX);activeErase.setAttributeNS(null, "cy", startY);activeErase.setAttributeNS(null, "r", circleRadius);activeErase.setAttributeNS(null, "fill", "white");activeErase.setAttribute("stroke", "black");activeErase.setAttribute("stroke-width", "1");erase.appendChild(activeErase);mouseMove = function(evt) {setTimeout("delete_EraserThread('"+getPositionX(evt)+"','"+getPositionY(evt)+"','"+circleRadius+"')", 0);}}function delete_EraserThread(x,y,r) {var circle = {'type' : 'circle','x' : x,'y' : y,'r' : r};tools_SendCommand('erase',circle);}var moveLock = false;function delete_UseEraser(circle) {if(activeErase){while(moveLock)msleep(1);moveLock = true;activeErase.setAttribute("cx", circle.x);activeErase.setAttribute("cy", circle.y);moveLock = false;}}function delete_StopDrawing() {if(activeErase) {erase.removeChild(activeErase);}activeErase = null;}function delete_SendCommand(c, v) {if(c == 'initialize') {erase = v;} else if(c == 'erase') {delete_UseEraser(v);} else if(c == 'set-tool') {delete_SetTool(v);} else if(c == 'unset-tool') {delete_UnsetTool();} else if(c == 'start-drawing') {delete_StartDrawing(v);} else if(c == 'stop-drawing') {/* spawn a new thread, for some reason deleting SVG objects * during mouse-up causes JavaScript to have difficulty * handling the mouse outside of the SVG document */setTimeout("delete_StopDrawing()", 0);}}var image_LastTool = null;function image_SetTool(lastTool) {/*var imageDialog = document.getElementById("image-dialog");image_LastTool = lastTool;imageDialog.style.left = document.body.clientWidth/2 - imageDialog.offsetWidth/2; imageDialog.style.top = document.body.clientHeight/2 - imageDialog.offsetHeight/2;showObject('image-dialog');*//*alert('The background image tool is not yet ready!');*/saveImage();setTool(lastTool);}function image_SetBackground() {setTool(image_LastTool);hideObject('image-dialog');}function image_CancelBackground() {setTool(image_LastTool);hideObject('image-dialog');}function image_SendCommand(c, v) {if(c == 'set-tool') {image_SetTool(v);}}function newpage_Create() {var holder = document.getElementById('image-holder');var image = document.getElementById('image');var svg = doc.getElementById('svg');var h = svg.getAttribute('height');var w = svg.getAttribute('width');svg.setAttribute('height',parseInt(h)+embedHeight);image.style.width = embedWidth+20;image.style.height = parseInt(h)+embedHeight;holder.style.height = embedHeight;}function newpage_SetTool(lastTool) {tools_SendCommand('new-page', null);setTool(lastTool);}function newpage_SendCommand(c, v) {if(c == 'set-tool') {newpage_SetTool(v);} else if(c == 'new-page') {newpage_Create();}}