var out, cur

window.onload = function(){

 	if( document.getElementById ){

 		oAreas = document.getElementById('Areas').getElementsByTagName('AREA')
		oSelect = document.getElementById('Select')
		oActive = document.getElementById('Active')

		oShowAll = document.getElementById('ShowAll')
		oRegion = document.getElementById('Region')
		oCentral = document.getElementById('Central')
		oServices = document.getElementById('Services')
		oOffices = document.getElementById('Offices')
		oRepresentatives = document.getElementById('Representatives')
		oDealers = document.getElementById('Dealers')

//		HighlightInit(oServices)
//		HighlightInit(oOffices)
//		HighlightInit(oDealers)

		oShowAll.onclick = ShowAll

		for( var i = 0; i < oAreas.length; i++ ) {
			oAreas[i].onmouseover = ShowArea
			oAreas[i].onmouseout = HideArea
			oAreas[i].onclick = SetArea
		}

		document.getElementById('Wrap').style.display = ''

		$("A.onthemap").fancybox({"scrolling":"no"}).click( function(){ ShowMap(this) })

	}
}

window.onunload = function(){ GUnload() }

function ShowArea(e){

//	это не выход за пределы карты
	clearTimeout(out)

	oArea = window.event ? window.event.srcElement : e.currentTarget

	oActive.style.left = ( -oArea.getAttribute('offset') * 472 ) + 'px'

}

function HideArea(){

//	предполагаем выход за пределы карты
	out = setTimeout("oActive.style.left = '472px'", 50)

}

function SetArea(e){

	oArea = window.event ? window.event.srcElement : e.currentTarget

	oSelect.style.left = ( -oArea.getAttribute('offset') * 472 ) + 'px'

	cur = oArea.id

//	показываем ссылку "вся Украина"
	oShowAll.style.display = ''

//	в заголовке показываем выбранную область
	oRegion.innerHTML = ', ' + oArea.title

//	скрываем/показываем блок "Главный офис"
	oCentral.style.display = oCentral.className == cur ? '' : 'none'

//	фильтруем списки сервисов, представителей, дилеров
	Filter(oServices)
	Filter(oOffices)
	Filter(oRepresentatives)
	Filter(oDealers)

	return false

}

function Filter(obj,renew){
	var show = false
	var nodes = obj.firstChild.nextSibling.childNodes

	for ( var i = 0; i < nodes.length; i++ )
		if( nodes[i].nodeType == 1 ){
			nodes[i].style.display = nodes[i].className == cur || renew ? '' : 'none'
			show = nodes[i].className == cur || renew ? true : show
		}

//	если в списке ни одной записи - список не показываем вообще
	obj.style.display = show ? '' : 'none'

}

function ShowAll(){

//	показываем блок "Главный офис"
	oCentral.style.display = ''

//	из заголовка убираем область
	oRegion.innerHTML = ''

//	восстанавливаем списки сервисов, представителей, дилеров
	Filter(oServices,true)
	Filter(oOffices,true)
	Filter(oDealers,true)

	oSelect.style.left = '472px'

	oShowAll.style.display = 'none'

	return false

}

function HighlightInit(obj){
	var nodes = obj.firstChild.nextSibling.childNodes

	for ( var i = 0; i < nodes.length; i++ )
		if( nodes[i].nodeType == 1 ){
			nodes[i].onmouseover = function(){ this.bgColor = '#FFFFFF' }
			nodes[i].onmouseout =  function(){ this.bgColor = '' }
		}
}

function initGMap(lat,lng) {
	if( GBrowserIsCompatible() ){
		var map = new GMap2(document.getElementById("OnTheMap"))

		var customUI = map.getDefaultUI()
		customUI.controls.scalecontrol = false
		customUI.controls.largemapcontrol3d = false
		customUI.controls.maptypecontrol = true
		customUI.maptypes.physical = false
		customUI.maptypes.satellite = false
		map.setUI(customUI)

		var point = new GLatLng(lat,lng)
		map.setCenter(point,12)

		var marker = new GMarker(point)
		map.addOverlay(marker)
	}
}

function ShowMap(a){
	var lat = $(a).attr("lat")
	var lng = $(a).attr("lng")
	initGMap(lat,lng)
}
