var E_MAP_ADDMAPTYPE=12
var E_MAP_REMOVEMAPTYPE=13
var E_MAP_MAPTYPECHANGED=14
var E_MAP_ADDOVERLAY=15
var E_MAP_REMOVEOVERLAY=16
var E_MAP_CLEAROVERLAYS=17
var E_MAP_MOVESTART=18
var E_MAP_MOVE=19
var E_MAP_MOVEEND=20
var E_MAP_ZOOMEND=21
var E_MAP_INFOWINDOWOPEN=22
var E_MAP_INFOWINDOWCLOSE=23
var E_MAP_CLICK=24
var E_MAP_MOUSEMOVE=25
var E_MAP_MOUSEOVER=26
var E_MAP_MOUSEOUT=27
var E_MAP_DRAGSTART=28
var E_MAP_DRAG=29
var E_MAP_DRAGEND=30
var E_MAP_LOAD=31

var minZoom=0;
var maxZoom=19;

var smallMapControl=new GSmallMapControl();
var largeMapControl=new GLargeMapControl();
var smallZoomControl=new GSmallZoomControl();
var mapTypeControl= new GMapTypeControl();
var scaleControl=new GScaleControl();
var overviewMapControl=new GOverviewMapControl()
var gmap=null

var mapMapTypeSelect=null;
var mapZoomSelect=null;

function Map()
{
	this.width=480;
	this.height=440;
	this.zoom=1;//15;
	this.center=new GLatLng(0,0);//new GLatLng(50.98979, 12.96858);
	this.mapTypes=null;
	this.mapType=null;
	this.mapTypeIndex=1;
	this.mapTypeName='';
	
	this.smallMapControl=false
	this.largeMapControl=true;
	this.smallZoomControl=false;
	this.mapTypeControl=true;
	this.scaleControl=false;
	this.overviewMapControl=false;
	this.infoWindow=true;
	this.dblClickZoom=false;
	this.dragging=true;
	this.continousZoom=false;
}

var mapList=[]
var map=mapList[0]=new Map();

function mapSetup()
{
	gmap = new GMap2(document.getElementById("map"));
	gmap.setCenter(map.center,map.zoom);
	setMapMapTypes()
}

function mapEditSetup()
{
	putMapZoom()
	putMapMapTypes()
	putMapEdit()
	useMapSettings()
	mapEventsSetup()
}

function mapEventsSetup()
{
		GEvent.addListener(gmap, "addmaptype", mapAddMapType)
		GEvent.addListener(gmap, "removemaptype", mapRemoveMapType)
		GEvent.addListener(gmap, "maptypechanged", mapTypeChanged)
		GEvent.addListener(gmap, "addoverlay", mapAddOverlay)
		GEvent.addListener(gmap, "removeoverlay", mapAddOverlay)
		GEvent.addListener(gmap, "clearoverlays", mapClearOverlays)
		GEvent.addListener(gmap, "movestart", mapMoveStart)
		GEvent.addListener(gmap, "move", mapMove)
		GEvent.addListener(gmap, "moveend", mapMoveEnd)
		GEvent.addListener(gmap, "zoomend", mapZoomEnd)
		GEvent.addListener(gmap, "infowindowopen", mapInfoWindowOpen)
		GEvent.addListener(gmap, "infowindowclose", mapInfoWindowClose)
		GEvent.addListener(gmap, "click", mapClick)
		GEvent.addListener(gmap, "mousemove", mapMouseMove)
		GEvent.addListener(gmap, "mouseover", mapMouseOver)
		GEvent.addListener(gmap, "mouseout", mapMouseOut)
		GEvent.addListener(gmap, "dragstart", mapDragStart)
		GEvent.addListener(gmap, "drag", mapDrag)
		GEvent.addListener(gmap, "dragend", mapDragEnd)
		GEvent.addListener(gmap, "load", mapLoad)
		//mapInfoClickCloseHandle=GEvent.addListener(gmap.getInfoWindow(),'closeclick',new Function('','setTimeout("mapInfoClickClose()",0)'));
		mapInfoClickCloseHandle=GEvent.addListener(gmap.getInfoWindow(),'closeclick',mapInfoClickClose);
}

function putMapEdit()
{
	if(innerHTMLFix | !mapMapTypeSelect)putMapMapTypes()
	if(innerHTMLFix | !mapZoomSelect)putMapZoom()
	mapMapTypeSelect.selectIndex(map.mapTypeIndex);
	mapZoomSelect.selectIndex(map.zoom);
	
	putMapCenter()
	
	document.getElementById('mapWidth').value=map.width;
	document.getElementById('mapHeight').value=map.height;
	document.getElementById('setSmallMapControl').checked=map.smallMapControl
	document.getElementById('setLargeMapControl').checked=map.largeMapControl
	document.getElementById('setSmallZoomControl').checked=map.smallZoomControl
	document.getElementById('setMapTypeControl').checked=map.mapTypeControl
	document.getElementById('setScaleControl').checked=map.scaleControl
	document.getElementById('setOverviewMapControl').checked=map.overviewMapControl
	//document.getElementById('setMapInfoWindow').checked=map.infoWindow
	document.getElementById('setMapDblClickZoom').checked=map.dblClickZoom
	document.getElementById('setMapDragging').checked=map.dragging;
	document.getElementById('setMapContinuousZoom').checked=map.continousZoom;
	document.getElementById("setMapDblClickZoom").disabled=!map.dragging;
	document.getElementById("setMapContinuousZoom").disabled=!map.dragging;
	document.getElementById("setMapDblClickZoom").disabled=!map.dragging;
	document.getElementById("setMapContinuousZoom").disabled=!map.dragging;
}

function useMapSettings()
{
	setMapSize(map.width,map.height)
	gmap.setZoom(map.zoom)
	
	setMapMapType(map.mapTypeIndex)
	gmap.setCenter(map.center)
	
	if(map.infoWindow)gmap.enableInfoWindow()
	else gmap.disableInfoWindow()
	
	if(map.dblClickZoom)gmap.enableDoubleClickZoom()
	else gmap.disableDoubleClickZoom()

	if(map.dragging)gmap.enableDragging()
	else gmap.disableDragging()
	
	if(map.continousZoom)gmap.enableContinuousZoom()
	else gmap.disableContinuousZoom()


	if(map.smallMapControl)gmap.addControl(smallMapControl)
	else gmap.removeControl(smallMapControl)
	
	if(map.largeMapControl)gmap.addControl(largeMapControl)
	else gmap.removeControl(largeMapControl)
	
	if(map.smallZoomControl)gmap.addControl(smallZoomControl)
	else gmap.removeControl(smallZoomControl)
	
	if(map.mapTypeControl)gmap.addControl(mapTypeControl)
	else gmap.removeControl(mapTypeControl)
	
	if(map.scaleControl)gmap.addControl(scaleControl)
	else gmap.removeControl(scaleControl)
	
	if(map.overviewMapControl)gmap.addControl(overviewMapControl)
	else gmap.removeControl(overviewMapControl)
}

function setMapMapType(index)
{
	if(typeof index=='undefined')index=0
	map.mapType=map.mapTypes[index]
	map.mapTypeIndex=index
	map.mapTypeName=map.mapType.getName()
	gmap.setMapType(map.mapType)
}

function setMapMapTypes(mapTypes)
{
	if(typeof mapTypes!='undefined')map.mapTypes=mapTypes;
	else map.mapTypes=gmap.getMapTypes();
	for(x in map.mapTypes)
	{		
		minZoom=Math.min(minZoom,map.mapTypes[x].getMinimumResolution())
		maxZoom=Math.max(maxZoom,map.mapTypes[x].getMaximumResolution())
	}
}

function putMapMapTypes()
{
	var x;
	if(!mapMapTypeSelect)
	{
		mapMapTypeSelect=new SelectBox('mapMapType','mapEdit',function(option){setMapMapType(option.value)})
		mapMapTypeSelect.setOptions(optionsMapTypes)
	}
	mapMapTypeSelect.selectedIndex=map.mapTypeIndex;
	mapMapTypeSelect.draw()
}

function putMapZoom()
{
	var x;
	if(!mapZoomSelect)
	{
		mapZoomSelect=new SelectBox('mapZoom','mapEdit',function(option){setMapZoom(option.value)})
		mapZoomSelect.setOptions(optionsZoom) 
	}
	mapZoomSelect.selectedIndex=map.zoom;
	mapZoomSelect.draw()
}

function setMapControl(name,on)
{
	if(on)
	{
		gmap.addControl(window[name])	
		map[name]=true
	}
	else
	{
		gmap.removeControl(window[name])	
		map[name]=false
	}
}

function setMapZoom(zoom)
{
	if(typeof zoom!='object')setTimeout('gmap.setZoom('+zoom+')',0)
}




function mapZoomEnd(oldLevel, newLevel) 
{
	mapZoomSelect.selectIndex(newLevel)
	nodesShowHideOnZoom(newLevel)
}

function mapMoveStart()
{

}

function mapMove()
{

}

function mapRemoveMapType(type)
{
	minZoom=Number.MAX_VALUE;
	maxZoom=Number.MIN_VALUE;
	setMapMapTypes(mapTypes)
	map.mapTypes=gmap.getMapTypes();
	for(x in map.mapTypes)
	{		
		minZoom=Math.min(minZoom,map.mapTypes[x].getMinimumResolution())
		maxZoom=Math.max(maxZoom,map.mapTypes[x].getMaximumResolution())
	}
}

function mapAddMapType(type)
{
	setMapMapTypes(mapTypes)
	map.mapTypes=gmap.getMapTypes();
	for(x in map.mapTypes)
	{		
		minZoom=Math.min(minZoom,map.mapTypes[x].getMinimumResolution())
		maxZoom=Math.max(maxZoom,map.mapTypes[x].getMaximumResolution())
	}
}

function mapAddOverlay(overlay)
{

}

function mapRemoveOverlay(overlay)
{

}

function mapClearOverlays()
{

}

function mapTypeChanged()
{
	var name=gmap.getCurrentMapType().getName()
	for(var x=0;x<map.mapTypes.length;x++)
	{
		if(map.mapTypes[x].getName()==name)
		{
			mapMapTypeSelect.selectIndex(x);
			map.mapType=map.mapTypes[x]
			map.mapTypeIndex=x
			map.mapTypeName=name
			break;
		}
	}
}

function mapDragStart()
{

}

function mapDrag()
{
	map.center=gmap.getCenter();
	putMapCenter()
}
function mapDragEnd()
{
	map.center=gmap.getCenter();
	putMapCenter()
}

function mapLoad()
{
}

function mapMoveEnd()
{
	var obj
	var size=gmap.getSize()
	if(obj=document.getElementById('mapWidth'))obj.value=map.width=size.width;
	if(obj=document.getElementById('mapHeight'))obj.value=map.height=size.height;
	map.center=gmap.getCenter();
	putMapCenter()
	map.zoom=gmap.getZoom()
}

function mapMouseOut(latlng)
{
	sections.map.putStatusText('')
}


function mapMouseOver(latlng)
{
}

function mapMouseMove(latlng)
{
	if(overlayDragging)overlayDrag(overlayDragging,gmap.fromLatLngToDivPixel(latlng))
	sections.map.putStatusText(formatLat(latlng.lat())+' '+formatLng(latlng.lng()))
}

function mapClick(overlay,point)
{
	if(!overlay && lastMarkerOpened)
	{
		lastMarkerOpened.infoOpen=0;
		lastMarkerOpened=null;
	}
	else if(lastMarkerOpened && overlay && overlay.getSelectedTab )
	{
		markerInfoTabbedClicked(overlay.getSelectedTab(),overlay,lastMarkerOpened.uid)
	}
	//sections.map.parent.toFront()
}

var mapInfoClickCloseHandle=null

function mapInfoClickClose()
{
	//if(typeof currentNode!='object')var nodeObj=mapNodes[currentNode];
	//else var nodeObj=markerPreset;
	nodeObj=mapNodes[lastMarkerOpened.uid]
	nodeObj.infoOn=null;
	nodeObj.infoOpen=0;
	lastMarkerOpened=null;
}


function mapInfoWindowOpen()
{

}


function mapInfoWindowClose()
{


}

function setMapSize(width,height)
{
	if(width)
	{
		width=map.width=parseInt(width)
		sections.map.parent.size.width=width+sizes.section.outerAllWidth;
	}
	if(height)
	{
		height=map.height=parseInt(height)
		sections.map.parent.size.height=height+sizes.section.outerAllHeight+barsHeightNoTabs;
	}
	sections.map.parent.showSize()
	gmap.checkResize()
}


function setMapCenter(lat,lng)
{
	if(typeof lat!='object' && typeof lat!='undefined' && isNaN(lat=parseLatLng(lat)))
	{
		alert('Ivalid Latitude Value!');
		return
	}
	if(typeof lng!='object' && typeof lng!='undefined' && isNaN(lng=parseLatLng(lng)))
	{
		alert('Ivalid Longitude Value!');
		return
	}
	if(typeof lat=='object' || typeof lat=='undefined')lat=map.center.lat()
	if(typeof lng=='object' || typeof lng=='undefined')lng=map.center.lng()
	gmap.setCenter(map.center=new GLatLng(lat,lng))
}



function putMapCenter()
{
	document.getElementById('mapCenterLat').value=(map.center)?formatLat(map.center.lat()):'';
	document.getElementById('mapCenterLng').value=(map.center)?formatLng(map.center.lng()):'';
}



function setMapInfoWindow(onOff)
{
	if(onOff)gmap.enableInfoWindow()
	else gmap.disableInfoWindow()
	map.infoWindow=onOff
}

function setMapDblClickZoom(onOff)
{
	if(onOff)gmap.enableDoubleClickZoom()
	else gmap.disableDoubleClickZoom()
	map.dblClickZoom=onOff
}

function setMapDragging(onOff)
{
	if(onOff) gmap.enableDragging()
	else gmap.disableDragging()
	document.getElementById("setMapDblClickZoom").disabled=!onOff;
	document.getElementById("setMapContinuousZoom").disabled=!onOff;
	map.dragging=onOff
}

function setMapContinuousZoom(onOff)
{
	if(onOff)gmap.enableContinuousZoom()
	else gmap.disableContinuousZoom()
	map.continousZoom=onOff
}