window.onload = init;

$(document).ready(function() {
	$('form#form_route').hide()
	$('a#a_route').click(function() {
		$('form#form_route').slideToggle()
		return false
	});
	var in_suche = 'Suche...';
	$('#q').focus(function() { $(this).removeClass('error'); if (this.value==in_suche) this.value='' })
	$('#q').blur(function() { if (this.value=='') this.value=in_suche })
	$('form.suche').submit(function() {
		if ($('#q').val() == in_suche) { $('#q').addClass('error'); $('input.submit').blur(); return false; }
	})

	$('div#div_info').hide()
	var cl = $('map#organigramm').children()
	if (cl.length > 0) {
		for(var i=0;i<cl.length;i++) {
			cl[i].onmouseover = function() {
				$('div#div_info').show()
				$('div#div_info').html(this.getAttribute('alt'))
			}
			cl[i].onmouseout = function() {
				$('div#div_info').hide()
			}
		}
	}
	var alphabet = $('div.alphabet a')
	if (alphabet.length > 0) {
		var buchstaben = new Array();
		for(var i=0;i<alphabet.length;i++) {
			var bs = $(alphabet[i]).attr('name')
			if (bs.length == 1) {
				buchstaben.push(bs)
			}
		}
		if (buchstaben.length > 0) {
			$('h1').after('<strong>Nutzen Sie die alphabetische Sortierung:</strong><ul class="alphabet"></ul><br class="clear">')
			var li = ''
			for(var i=65;i<=90;i++) {
				if (jQuery.inArray(String.fromCharCode(i).toLowerCase(),buchstaben) != -1) {
					li += '<li><a href="#'+String.fromCharCode(i).toLowerCase()+'">'+String.fromCharCode(i)+'</a></li>'
				}
				else {
					li += '<li><span>'+String.fromCharCode(i)+'</span></li>'
				}
			}
			$('ul.alphabet').html(li)
		}
	}
	var anchor=$(location).attr('hash');
	anchor = anchor.substr(1);
	if (anchor.length > 1) {
		var ea = $('a[name='+anchor+']');
		if (ea.val() == '' && $.browser.msie) { ea.prepend('&nbsp;'); }
		ea.addClass('ankerpfeil');
		setTimeout('scroll_anchor()',400);
	}

	$('form').submit(function() {
		var foc = false;
		$(':input',this).each(function() {
			if ($(this).is(':visible')) {
				var prev = $(this).prev();
				if (prev.attr('alt') == 'pflicht') {
					var err = false;
					if ($(this).val() == false) { err = true }
					else if (this.name == 'teilnehmer') {
						var tl = this.value.split("\n");
						var c=0;
						for(var i=0;i<tl.length;i++) {
							if (tl[i]) { c++ }
						}
						if (c > 4) {
							err = true
						}
					}
					if (err == true) { if (foc == false) { this.focus(); foc = true } prev.attr('class','error') }
					else { prev.attr('class',''); }
				}
			}
		})
		return foc == false ? true : false;
	})
	$('div#button_drucken').html('<a href="javascript:window.print()" class="butt">Drucken</a>');
	$('input.submit').hover(function() { $(this).css("cursor","pointer") })
})

function scroll_anchor() {
	var scroll_top = $(window).scrollTop();
	var tol = scroll_top - 20;
	$('html, body').animate({scrollTop:tol}, 'slow');
}
function init() {
	loaded = true;
	if (gid('input_focus') != null) {
		gid(gid('input_focus').innerHTML).focus();
	}
	if (gid('googlemap') != null) {
		google_maps_load();
	}
	var height_document = $(document).height()
	$('div#inner').height(height_document-275)
	$('div#left').height(height_document-195)
	$('div#right').height(height_document-225)
}
function gid(id) { return document.getElementById(id) }

if (window.location.href.indexOf("/ihr-lav/struktur") != -1) {
	var loaded = false;
	var div_move = false;
	document.onmousemove = getMousePosition;
}
var mouse_x_pos = 0;
var mouse_y_pos = 0;
function getMousePosition(e) {
	if (e == undefined) e = window.event;
	if (e.pageX || e.pageY) {
		mouse_x_pos = e.pageX;
		mouse_y_pos = e.pageY;
	}
	else if (e.clientX || e.clientY) {
		mouse_x_pos = e.clientX + document.body.scrollLeft + document.documentElement.scrollLeft;
		mouse_y_pos = e.clientY + document.body.scrollTop + document.documentElement.scrollTop;
	}
	if (document.getElementById) {
		if (loaded==true) {
			gid('div_info').style.top  = mouse_y_pos+20+'px';
			gid('div_info').style.left = mouse_x_pos-15+'px';
		}
	}
	return true;
}

function google_maps_load() {
	if (GBrowserIsCompatible()) {
		var map = new GMap2(document.getElementById('googlemap'));
		map.setCenter(new GLatLng(49.995597,8.272786), 15);
		map.addControl(new GSmallMapControl());
		map.addControl(new GMapTypeControl());
		var point1 = new GPoint(8.272786,49.995597);
		var html1 = '<p><strong>Apothekerverband Rheinland-Pfalz e. V. - LAV</strong><br>Weißliliengasse 3<br>55116 Mainz</p>';
		var sw = new GMarker(point1);
		GEvent.addListener(sw, 'click', function() {sw.openInfoWindowHtml(html1)});
		map.addOverlay(sw);
	}
}

