var HgMap=Class.create({initialize:function(b,a){this._params=b;this._map=a;this._map.show()},geo2pix:function(a){return this._map.geo2scr(this._geo2coord(a))},pix2geo:function(b){var a=this._map.scr2geo(b);return{lat:a.getLat()/1000000,lon:a.getLon()/1000000}},transformCoords:function(b,a){if(!a){var d=this._geo2coord(b);b.lat=d.getLat()/1000000;b.lon=d.getLon()/1000000}else{if(b.lat>90||b.lon>180){b.lat=b.lat/1000000;b.lon=b.lon/1000000}}return b},_geo2coord:function(a){if(a.lat<=90||a.lon<=180){return new CCoord({lat:a.lat*1000000,lon:a.lon*1000000})}else{if(a.lat<1000000||a.lon<1000000){return new CCoord({swissy:a.lat,swissx:a.lon})}else{return new CCoord({lat:a.lat,lon:a.lon})}}},_coord2geo:function(a){return{lat:a.getLat()/1000000,lon:a.getLon()/1000000}},evt2pix:function(a){var b=$("sbbdiv").cumulativeOffset();return{x:Event.pointerX(a)-b.left,y:Event.pointerY(a)-b.top}},centerToPix:function(b){var a=this._map.scr2geo(b);this._map.increaseZoomInternal();this._map.centerToGeo(a)},createContent:function(a){if(a.pos){a.coord=this._geo2coord(a.pos)}if(a.onDragEnd){a.ondragend=function(b,c){a.onDragEnd(this._coord2geo(c))}.bind(this)}return new HgMap._Content(a,this._map)}});HgMap._Content=Class.create({initialize:function(b,a){this._params=b;this._map=a;this._handle=a.createContent(b);this.show()},finalize:function(){this.hide();this._map.removeContent(this._handle)},update:function(a){this.finalize();this.initialize(a,this._map)},params:function(){return this._params},show:function(){if(!this._isVisible){this._map.showContent(this._handle)}this._isVisible=true},hide:function(){if(this._isVisible){this._map.hideContent(this._handle)}this._isVisible=false},toTop:function(){this._map.contentToTop(this._handle)},toBottom:function(){this._map.contentToBottom(this._handle)}});var HgObject=Class.create({initialize:function(a,b){this._type=a;this._type._instances.push(this)},finalize:function(){this._type._instances=this._type._instances.without(this)},type:function(){return this._type}});HgObject.ClassMembers={initialize:function(){this._instances=new Array()},instances:function(){return this._instances},visitAll:function(a){this._instances.each(a)},clearAll:function(a){this._instances.each(function(b){b.finalize()});this._instances=new Array()}};var HgNode=Class.create(HgObject,{initialize:function($super,a){$super(HgNode);this._nid=this.type().instances().length;this._pos={lat:a.lat,lon:a.lon};this._edges=new Array();this._createMapContent()},finalize:function($super){this._removeMapContent();$super()},destroy:function(){if(HgNode._selected==this){HgNode._selected=null}var a=this._edges;if(a.length==2){var c=a[0].otherNode(this);var b=a[1].otherNode(this);a[0].destroy();a[1].setSrcNode(c);a[1].setTgtNode(b);a[1].redraw()}else{this._edges.each(function(d){d.destroy()})}this.finalize()},select:function(){HgNode._selected=this},nid:function(){return this._nid},pos:function(){return this._pos},data:function(){return{nid:this._nid,lat:this._pos.lat,lon:this._pos.lon}},mapIcon:function(){return hgImagePath+"himgeo/map/station.gif"},edges:function(){return this._edges},setData:function(a){this._nid=a.nid;this._pos=a.pos;hgNodeList[this._nid]=this},setPos:function(a){this._pos=a},_addRef:function(a){this._edges.push(a)},_delRef:function(a){this._edges=this._edges.without(a)},_createMapContent:function(){if(hgMapParams.showNodeMarkers){var b=Object.clone(hgLocParamsStation8);b.pos=this._pos;b.onclick=function(c){Event.stop(c)}.bindAsEventListener(this);b.oncontextmenu=this._showPopup.bindAsEventListener(this);b.draggable=true;b.onDragEnd=a.bind(this);this._icon=hgMap.createContent(b)}function a(c){this.setPos(c);this.edges().each(function(d){d.redraw()})}},_removeMapContent:function(){if(this._icon){this._icon.finalize();this._icon=null}},_showPopup:function(c){var e=hgMap.evt2pix(c);var b=hgMapCtrl.createPopup(c);if(HgNode._selected&&HgNode._selected!=this){var d=new Element("input",{type:"button",value:hgTxt.createEdge,className:"button"}).setStyle({display:"block",width:"15ex"});b.appendChild(d);Event.observe(d,"click",function(f){hgMapCtrl.closePopup();new HgEdge(HgNode._selected,this)}.bindAsEventListener(this))}var a=new Element("input",{type:"button",value:hgTxt.deleteNode,className:"button"}).setStyle({width:"15ex"});b.appendChild(a);Event.observe(a,"click",function(f){hgMapCtrl.closePopup();this.destroy()}.bindAsEventListener(this));Event.stop(c)}});Object.extend(HgNode,HgObject.ClassMembers);HgNode.initialize();var HgEdge=Class.create(HgObject,{initialize:function($super,c,b,a){$super(HgEdge);this._srcNode=c;this._tgtNode=b;this._color=a||"red";c._addRef(this);b._addRef(this);this._createMapContent()},finalize:function($super){this._removeMapContent();$super()},destroy:function(){this.setSrcNode(null);this.setTgtNode(null);this.finalize()},redraw:function(){this._removeMapContent();this._createMapContent()},data:function(){return{src:this._srcNode.nid(),tgt:this._tgtNode.nid(),color:this._color}},srcNode:function(){return this._srcNode},tgtNode:function(){return this._tgtNode},otherNode:function(a){return this._srcNode!=a?this._srcNode:this._tgtNode},pos:function(){var b=this._srcNode.pos();var a=this._tgtNode.pos();return{lat:(b.lat+a.lat)/2,lon:(b.lon+a.lon)/2}},mapIcon:function(){return hgImagePath+"himgeo/map/stationLink.gif"},edgesOnLine:function(){var b=[this];a(this,this._srcNode);a(this,this._tgtNode);return b;function a(e,d){var f=d.edges();if(f.length==2){var c=f[0]!=e?f[0]:f[1];b.push(c);a(c,c.otherNode(d))}}},setSrcNode:function(a){if(this._srcNode){this._srcNode._delRef(this)}this._srcNode=a;if(a){a._addRef(this)}},setTgtNode:function(a){if(this._tgtNode){this._tgtNode._delRef(this)}this._tgtNode=a;if(a){a._addRef(this)}},_createMapContent:function(){if(hgMapParams.showEdgeMarkers){var b=Object.clone(hgLocParamsStationLink8);b.pos=this.pos();b.onclick=function(e){Event.stop(e)}.bindAsEventListener(this);b.oncontextmenu=this._showPopup.bindAsEventListener(this);b.draggable=true;b.onDragEnd=a.bind(this);this._icon=hgMap.createContent(b)}var d=this.srcNode().pos();var c=this.tgtNode().pos();this._poly=hgMap.createContent({type:"polyline",coords:[hgMap._geo2coord(d),hgMap._geo2coord(c)],color:this._color,opacity:0.7,width:4});function a(g){var e=new HgNode(g);var f=new HgEdge(e,this.tgtNode(),this._color);this.setTgtNode(e);e.edges().each(function(h){h.redraw()})}},_removeMapContent:function(){if(this._icon){this._icon.finalize();this._icon=null}this._poly.finalize();this._poly=null},_showPopup:function(c){var d=hgMap.evt2pix(c);var b=hgMapCtrl.createPopup(c);var a=["red","green","blue","yellow","cyan","purple"];a.each(function(e){var f=new Element("input",{type:"button",value:"  ",className:"button"}).setStyle({backgroundColor:e});b.insert(f);Event.observe(f,"click",function(g){this.edgesOnLine().each(function(h){hgMapCtrl.closePopup();h._color=e;h.redraw()})}.bindAsEventListener(this))},this);Event.stop(c)}});Object.extend(HgEdge,HgObject.ClassMembers);HgEdge.initialize();var HgSign=Class.create(HgObject,{initialize:function($super,a){$super(HgSign);this._pos=a.pos;this._src=a.src;this._num=a.num;this._createMapContent()},finalize:function($super){this._removeMapContent();$super()},pixPos:function(){return hgMap.geo2pix(this.pos())},pos:function(){return this._pos},num:function(){return this._num},data:function(){return{pos:this._pos}},setPos:function(a){this._pos=a},show:function(){this._icon.show();this._caption.show()},hide:function(){this._icon.hide();this._caption.hide()},showPopup:function(){showContainer(this._num-1,this.pixPos())},hidePopup:function(){hideContainer(this._num-1)},toTop:function(){this._caption.toTop();this._icon.toTop()},_createMapContent:function(){var a=this;var d=true;var e=false;var c=hgMapParams.draggableSigns;this._icon=hgMap.createContent({type:"location",text:"",pos:this._pos,imageurl:this._src,imagewidth:16,imageheight:16,hotspot:{x:-2,y:7},onmousedown:function(f){if(c){a._caption.hide();a.hidePopup();d=false}}.bindAsEventListener(this),onmouseup:function(f){if(c){a._caption.show();d=true}}.bindAsEventListener(this),onmouseover:function(f){if(!e){a.toTop();if(d){a.showPopup()}}e=true;Event.stop(f)}.bindAsEventListener(this),onmouseout:function(f){a.hidePopup();e=false;Event.stop(f)}.bindAsEventListener(this),onclick:function(f){$("messageTableRowForMessageNumber_"+a._num).scrollTo();Event.stop(f)}.bindAsEventListener(this),oncontextmenu:this._showPopup.bindAsEventListener(this),draggable:c,onDragEnd:b.bind(this)});this._caption=hgMap.createContent({type:"container",pos:this._pos,content:'<div class="iconNr">'+this._num+"</div>",hotspot:{x:0,y:-11}});function b(f){this.setPos(f);this._updateMapContent()}},_removeMapContent:function(){this._icon.finalize();this._icon=null;this._caption.finalize();this._caption=null},_updateMapContent:function(){this._removeMapContent();this._createMapContent()},_showPopup:function(a){hgMapCtrl.closePopup();Event.stop(a)}});Object.extend(HgSign,{highlightByNum:function(a){this._instances.each(function(b){if(a==b.num()){var c=b.pixPos()}highlightMessageIcon(c.x+2,c.y-7)})}});Object.extend(HgSign,HgObject.ClassMembers);HgSign.initialize();var HgRegion=Class.create(HgObject,{initialize:function($super,a){$super(HgRegion);this._pos=a.pos;this._radius=a.radius;this._createMapContent()},finalize:function($super){this._removeMapContent();$super()},_createMapContent:function(){var a=this;this._circle=hgMap.createContent({type:"circle",pos:this._pos,radius:this._radius*1000,color:"red",opacity:0.7,width:3})},_removeMapContent:function(){this._circle.finalize();this._circle=null}});Object.extend(HgRegion,HgObject.ClassMembers);HgRegion.initialize();var HgMapController=Class.create({initialize:function(){},initFromPolygon:function(b){var h=b.lat;var j=b.lon;for(var e=0;e<h.length;e++){var g=h[e]*1000000;var a=j[e]*1000000;var d=new HgNode({lat:g,lon:a});var f;if(f){var c=new HgEdge(f,d)}f=d}},initFromJson:function(a){var b=new Hash();a.nodes.each(function(c){hgMap.transformCoords(c,a.version);var d=new HgNode(c);b.set(c.nid,d)});a.edges.each(function(e){var d=b.get(e.src);var c=b.get(e.tgt);var f=new HgEdge(d,c,e.color)})},initFromMessageData:function(a){if(a.sign&&a.sign.pos){var b=Object.clone(a.sign);if(a.json&&a.json.sign){b.pos=a.json.sign.pos;hgMap.transformCoords(b.pos,a.json.version)}new HgSign(b)}if(a.region){new HgRegion(a.region)}else{if(a.json){this.initFromJson(a.json)}else{this.initFromPolygon(a.poly)}}},initAll:function(){hgMapData.each(function(a){this.initFromMessageData(a)},this)},run:function(){hgMap=new HgMap(hgMapParams,new CSBBMap("sbbdiv",{coord:new CCoord({swissx:118467,swissy:499969}),minzoom:1,maxzoom:hgMapParams.editMode?3:2,zoomonclick:false,centeronclick:false}));this.initAll();var a=$("sbbdiv");Event.observe(a,"click",function(b){if(!this.closePopup()){hgMap.centerToPix(hgMap.evt2pix(b))}b.stop()}.bindAsEventListener(this));Event.observe(a,"contextmenu",function(b){this.closePopup();if(hgMapParams.editMode){this._showPopup(b)}b.stop()}.bindAsEventListener(this));Event.observe(a,"mouseover",function(b){this.closePopup();b.stop()}.bindAsEventListener(this))},onClick:function(){alert("onClick")},onContextMenu:function(){alert("onContextMenu")},save:function(){var b={version:"1.1",nodes:[],edges:[]};HgNode.visitAll(function(c){b.nodes.push(c.data())});HgEdge.visitAll(function(c){b.edges.push(c.data())});HgSign.visitAll(function(c){b.sign=c.data()});var a=Object.toJSON(b);$("mapJson").value=a;return true},reset:function(){this.clear();hgMapData.each(function(a){a.json=null},this);this.initAll()},clear:function(){HgNode.clearAll();HgEdge.clearAll();HgSign.clearAll();HgRegion.clearAll()},showAllOfType:function(a){this.clear();hgMapData.each(function(b){if(a==null||a==b.sign.type){this.initFromMessageData(b)}},this)},showMessageByNumber:function(a){hgMapData.each(function(b){if(b.sign&&a==b.sign.num){this.initFromMessageData(b)}},this)},createPopup:function(c){this.closePopup();var a=$("map");var d=hgMap.evt2pix(c);var b=new Element("div",{id:"hgPopup"}).setStyle({position:"absolute",top:d.y-5+"px",left:d.x-5+"px",backgroundColor:"white",border:"solid black thin",zIndex:100});a.insert(b);return b},closePopup:function(){var a=$("hgPopup");if(a){a.remove()}return a?true:false},_showPopup:function(b){var d=hgMap.evt2pix(b);var a=this.createPopup(b);var c=new Element("input",{type:"button",value:hgTxt.createNode,className:"button"});a.insert(c);Event.observe(c,"click",function(e){a.remove();node=new HgNode(hgMap.pix2geo(d));node.select()}.bindAsEventListener(this))}});
