var isZoomed = false;

function hovermap(area) {
	if (isZoomed) return;
	unhovermap();
	$('hovermap').set('style', 'background-image: url(/img/map/hover/'+area+'.png)');
	$('mapnavi_'+area).addClass("active");
}

function unhovermap() {
	if ($('hovermap'))
		$('hovermap').set('style', 'background-image: none');
	
	if (!isZoomed)
		$$('#naviactive ul li a').each(function(el) {
			el.removeClass("active");
		});
}

function zoommap(area) {
	isZoomed = true;
	$('map').fade('out');
	$$('#zoommap img').each(function(el) {
		el.fade('out');
	});
	$$('#naviactive ul li a').each(function(el) {
		el.removeClass("active");
	});
	
	var zoomareamap = $('zoomareamap_'+area);
	if (!zoomareamap) {
		zoomareamap = new Element('img').set('src', '/img/map/zoom/'+area+'.png').set('border', '0').set('id', 'zoomareamap_'+area);
		$('zoommap').adopt(zoomareamap);
		if (laenderdaten[area]) laenderdaten[area].each(function(item){
			$('zoommap').adopt(new Element('a').set('href', item.baseUrl).adopt(new Element('img').set('class', 'zoomareamap_'+area).set('id', 'zoomareamap_'+item.id).store('id',item.id).set('src', '/img/map/mapitem.png').set('title', item.name.replace('<br />', " - ")).setStyles({'position':'absolute', 'left':(item.x-5)+'px', 'top':(item.y-5)+'px','visibility':'hidden'})));
			$('zoomareamap_'+item.id).addEvent('mouseover', function() {hoverLocation(this.retrieve('id'))});
			$('zoomareamap_'+item.id).addEvent('mouseout', function() {unHoverLocation(this.retrieve('id'))});
		});
	}
	$$('.zoomareamap_'+area).each(function(el) {
		el.fade('in');
	});
	addRightLocations(area, laenderdaten[area]);
	
	zoomareamap.fade('in');
	$('zoommapback').fade('in'); 

	$('mapnavi_'+area).addClass("active");
}

function unzoommap() {
	isZoomed = false;
	unhovermap();
	$('zoommapback').fade('out');
	$$('#zoommap img').each(function(el) {
		el.fade('out');
	});
	
	$('map').fade('in');
	
	if ($('locright'))
		$('locright').destroy(); 
}

function addRightLocations(area, locations) {
	if ($('locright')) $('locright').destroy(); 
    var divright = new Element('div').set('class', 'locations').set('id', 'locright');
    divright.adopt(new Element('h2').set('html', 'Beratungsstellen'+(area!=null?' in '+laender[area]+'':' im Umkreis von '+$('plzinput').get('value'))));
    var ulright = new Element('ul');
    divright.adopt(ulright);
    $('right').adopt(divright);
    if (locations) locations.each(function (location) {
    	ulright.adopt(new Element('li').adopt(new Element('a').set('html', location.name).set('href',location.baseUrl).set('id', 'locationright_'+location.id).store('id', location.id)).adopt(new Element("span").set('html', (location.distance?'<br />ca. '+location.distance+'km':''))));    	
    	$('locationright_'+location.id).addEvent('mouseover', function() {hoverLocation(this.retrieve('id'))});
    	$('locationright_'+location.id).addEvent('mouseout', function() {unHoverLocation(this.retrieve('id'))});
    });
    if (!locations) divright.adopt(new Element('p').set('text', 'Leider haben wir noch keine Beratungsstellen in '+laender[area]))
}

function hoverLocation(id) {
	if ($('locationright_'+id)) $('locationright_'+id).setStyle('background-color', '#C1C2C6');
	if ($('zoomareamap_'+id)) $('zoomareamap_'+id).set('src', '/img/map/mapitem_active.png');
}

function unHoverLocation(id) {
	if ($('locationright_'+id)) $('locationright_'+id).setStyle('background-color', 'inherit');
	if ($('zoomareamap_'+id)) $('zoomareamap_'+id).set('src', '/img/map/mapitem.png');
}

function requestLocationSearch(form) {
	unzoommap();
	new Request.JSON({url: form.get('action')+'/'+$('plzinput').get('value'), onSuccess: function(locations, debug){
		addRightLocations(null, locations.items);
	}}).get();
	
}
