// (c) 2007 KystAtlas AS (Hans Martin Mohn)

gtAssert("gtEventHandler  != null");gtAssert("gtNavigateStack != null");gtAssert("gtUrl           != null");gtAssert("gtMatrix        != null");gtIgnoreHistory=1;function gtBeMapJS(m,tooltip_marker)
{m.onBeforeDrawMap=new gtEventHandler();m.onDrawStateChanged=new gtEventHandler();m.m_tool=null;m.m_url=null;m.m_rect=null;m.m_marker=null;m.m_matrix=null;m.m_inverse=null;m.m_coord_sys="";m.m_min_scale=200;m.m_max_scale=20000000;m.m_busy=false;m.m_save_cursor="";m.m_extent_user=null;m.m_extent_coord_sys=null;m.m_navigate_stack=new gtNavigateStack();m.m_transform_frame=null;m.m_tooltip_marker=tooltip_marker;m.m_padding=1;m.onMapLoaded=function(url_string,matrix_string,img,rect,marker,pan_urls,icon,coord_sys)
{this.m_url=new gtUrl(url_string);this.m_matrix=new gtMatrix(matrix_string);this.m_inverse=this.m_matrix.inverse();this.m_img=img;this.m_rect=rect;this.m_marker=marker;this.m_icon=icon;this.m_pan_urls=pan_urls;this.m_coord_sys=coord_sys;this.m_width=this.m_url.getParam("w");this.m_height=this.m_url.getParam("h");this.m_viewport_user=this.m_inverse.transformPointsString("0,0,"+this.m_width+",0,"+this.m_width+","+this.m_height+",0,"+this.m_height);this.m_marker.title=this.m_tooltip_marker;this.showMarker();addGTHandlers(img);img.m_map=this;img.gtClick=function(evt){if(this.m_map.m_tool)return this.m_map.m_tool.click(evt);}
img.gtMouseDown=function(evt){if(this.m_map.m_tool)return this.m_map.m_tool.buttonDown(evt);}
img.gtMouseUp=function(evt){if(this.m_map.m_tool)return this.m_map.m_tool.buttonUp(evt);}
img.gtMouseMove=function(evt){if(this.m_map.m_tool)return this.m_map.m_tool.mouseMove(evt);}
img.gtMouseOver=function(evt){if(this.m_map.m_tool)return this.m_map.m_tool.enter(evt);}
img.gtMouseOut=function(evt){if(this.m_map.m_tool)return this.m_map.m_tool.leave(evt);}
this.setBusy(false);if(this.m_tool!=null)
this.m_tool.update();this.onDrawStateChanged.raise("idle",this.m_url,this.m_viewport_user);};m.getViewportDevice=function()
{var w=gtGetWidth(this)-2*this.m_padding;var h=gtGetHeight(this)-2*this.m_padding;return"0,0 "+w+",0 "+w+","+h+" 0,"+h;};m.device2User=function(device)
{return this.m_inverse.transformPointsString(device);};m.user2Device=function(user)
{return this.m_matrix.transformPointsString(user);};m.setCursor=function(cursor_name)
{this.m_save_cursor=cursor_name;try
{if(!this.m_img||!this.m_img.style)
return;}
catch(e)
{return;}
if(!this.m_busy)
{this.m_img.style.cursor=cursor_name;this.m_rect.style.cursor=cursor_name;}}
m.setBusy=function(busy)
{this.m_busy=busy;try
{if(!this.m_img||!this.m_img.style)
return;}
catch(e)
{return;}
if(!busy)
{this.m_img.style.cursor=this.m_save_cursor;this.m_rect.style.cursor=this.m_save_cursor;}
else
{this.m_img.style.cursor="wait";this.m_rect.style.cursor="wait";}};m.selectToolByName=function(tool_name)
{switch(tool_name)
{case"zoom":this.selectTool(g_tool_zoom);break
case"pan":this.selectTool(g_tool_pan);break;}}
m.selectTool=function(tool)
{var prev=this.m_tool;if(this.m_tool)
this.m_tool.deselect();this.m_tool=tool;if(this.m_tool!=null)
this.m_tool.select(this);return prev;};m.showMap=function(url,history)
{if(!url)
url=this.m_url;this.onBeforeDrawMap.raise(url);if(history!=gtIgnoreHistory)
this.m_navigate_stack.addUrl(url);var w=gtGetWidth(this)-2*this.m_padding;var h=gtGetHeight(this)-2*this.m_padding;if(w>20&&h>20)
{this.setBusy(true);url.setParam("w",w);url.setParam("h",h);this.m_url=url;this.onDrawStateChanged.raise("drawing",this.m_url,this.m_viewport_user);this.refreshMap(url);}
else
{this.m_url=url;}};m.cmdNavigate=function(cmd)
{if(cmd=="f")
{if(this.m_navigate_stack.nextOk())
this.showMap(this.m_navigate_stack.getNext(),gtIgnoreHistory);return;}
else if(cmd=="b")
{if(this.m_navigate_stack.prevOk())
this.showMap(this.m_navigate_stack.getPrev(),gtIgnoreHistory);return;}
var cmds=new Array('in','out','n','ne','e','se','s','sw','w','nw');for(var i=0;i<cmds.length;i++)
{if(cmd==cmds[i])
{var url=new gtUrl(this.m_pan_urls[i]);this.showMap(url);return;}}}
m.refreshMap=function(url)
{url.setService("GetHTML");var t=url.getParam("t");if(t==undefined||t+""=="")
alert("Cannot get HTML without the t= parameter set!");this.src="http://"+window.location.hostname+"/GeoServe%3A"+url.toString();};m.setScale=function(scale)
{this.m_url.setParam("s",scale);this.showMap(this.m_url);};m.multiplyScaleByFactor=function(factor)
{var cur_scale=parseFloat(this.m_url.getParam("s"));this.m_url.setParam("s",factor*cur_scale);this.showMap(this.m_url);};m.resize=function(w,h)
{gtSetWidth(this,w);gtSetHeight(this,h);if(this.m_url)
{this.m_url.setParam("w",w);this.m_url.setParam("h",h);this.showMap(this.m_url);}};m.setMarker=function(extent_user,extent_coord_sys,transform_frame)
{this.m_extent_user=extent_user;this.m_extent_coord_sys=extent_coord_sys;this.m_transform_frame=transform_frame;this.showMarker();};m.showMarker=function()
{if(!this.m_extent_user)
{if(this.m_marker)
gtSetVisibility(this.m_marker,"hidden");return;}
var ext;overview_coord_sys=this.m_url.getParamDef("dst","TM84("+this.m_url.getParam("x")+")");if(this.m_extent_coord_sys!=overview_coord_sys)
{alert("Different coordinate systems in detail and overview map not supported!! ("+this.m_extent_coord_sys+" and "+overview_coord_sys+")\nUse UTM84(33) or another specific coordinate system...");}
else
{ext=this.user2Device(this.m_extent_user);}
var pts=ext.split(" ");var ll=pts[0].split(",");var ur=pts[1].split(",");var llx=parseFloat(ll[0]);var lly=parseFloat(ll[1]);var urx=parseFloat(ur[0]);var ury=parseFloat(ur[1]);var w=urx-llx;var h=lly-ury;gtSetWidth(this.m_marker,Math.round(w+0.5)+4);gtSetHeight(this.m_marker,Math.round(h+0.5)+4);gtSetLeft(this.m_marker,Math.floor(llx)-2);gtSetTop(this.m_marker,Math.floor(ury)-2);gtSetVisibility(this.m_marker,"visible");};m.raiseDrawStateChanged=function(state)
{switch(state)
{case"idle":if(this.m_tool!=null)
this.m_tool.update();break;}
if(this.onDrawStateChanged)
this.onDrawStateChanged.raise(state,this.m_url,this.m_viewport_user);};}