document.write('<script src="http://maps.google.com/maps?file=api&amp;v=2&amp;sensor=false&amp;key=ABQIAAAAlO6W5guyF8ilekAfI4Mj1RTztwdoqvbpDeVHV9JYRas4vtn1WBT_OqAk3h1qcGpb2KZ2AIcRep9_PQ" type="text/javascript"></script>');

var w3cDOMsupport	= (document.getElementById && document.createElement) ? true : false;
var entryCat;
var entryCats = new Array();
var letters   = new Array('A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', 'K', 'L', 'M', 'N', 'O', 'P', 'Q', 'R', 'S', 'T', 'U', 'V', 'W', 'X', 'Y', 'Z');
var indexes   = new Array();

function openMailDialogue(obj)
{
	if (w3cDOMsupport) {
		var m = obj.innerHTML;
		m = m.replace(/\s+at\s+/, '@');
		document.location = 'mailto:'+m;
	}
};

$(document).ready(function(){
	
	$('div#content a[rel="new"]').attr('target', '_blank');
	
	$('table.zebra tr:even, #top-nav li:even, table#prices tr:odd').addClass('even');

	$('a.prev').html('zur&uuml;ck');
	$('a.next').html('vor');
	
	//$('dd.false').each(function() {
		//$(this).prev().hide();
		//$(this).hide();
	//});

	if ($('div#entry div#description').length > 0) {
		$.getScript('/js/jquery.expander.mod.js', function(){
			setTimeout(function(){
				$('div#entry div#description').expander({
					slicePoint:			300,
					expandText:			'[...]',
					expandPrefix:		'',
					expandEffect:		'slideDown',
					userCollapseText:	'[^]'
				});
			}, 200);
		});
	}

	if ($('.gallery').length) {
		$(".gallery a").overlay({
			target: '#gallery',
			expose: '#FBF3DD'
		}).gallery({
			speed: 800,
			template: '<strong>${title}</strong> <span>Bild ${index} von ${total}</span>'
		});
	}

	$('#entry div.toggable, #entry dl.toggable, body.lexikon #content div.toggable, #route').hide();

	if ($('#googlemap').length >0 ) {
		$.getScript('/js/jquery.googlemaps1.01.js', function(){
			setTimeout(function(){
				$('#googlemap').css('position', 'absolute');
				$('#googlemap').css('left', '-999em');
				$('#googlemap').css('width', '480px');
				$('#googlemap').css('height', '350px');
				$('#googlemap').css('border', '1px solid #7D0020');
				$('#googlemap').css('overflow', 'hidden');

				$('#googlemap').googleMaps({
					controls: {
						mapType: [{
							remove: 'G_SATELLITE_MAP'
						}, {
							remove: 'G_NORMAL_MAP'
						}]
					},
					markers: {
						geocode: $('#address').html().replace(/<br>/i, ', ').replace(/D\-/, '').replace(/straße/i, 'str.'),
						info: {
							layer: '#map-pop-up',
							popup: true,
							addAddress: true,
							addRouteToLink: true
						}
					}

				});

				$('#showMap').click(function() {
					if ($('#googlemap').css('position') == 'absolute') {
						$('#googlemap').css('position', 'relative');
						$('#googlemap').css('left', 'auto');
						if ($('#route table').length) {
							$('#route').show();
						}
					} else {
						$('#googlemap').css('position', 'absolute');
						$('#googlemap').css('left', '-999em');
						if ($('#route').length) {
							$('#route').hide();
						}
					}
					return false;
				});
			}, 200);
		});	
	}

	$('div.scrollable').scrollable({ size: 3});

	$('#entry h2.toggable, #entry h3.toggable, body.lexikon #content h2').click(function() {
		$(this).toggleClass('toggled');
		if ($.browser.msie && !$('body').hasClass('lexikon')) {
			// IE needs a re-draw of all headlines
			$(this).next('div.toggable, dl.toggable').slideToggle('fast', function() {
				$('#entry h2.toggable, #entry h3.toggable, body.lexikon #content h2').css('display', 'inline');
				$('#entry h2.toggable, #entry h3.toggable, body.lexikon #content h2').css('display', 'block');
			});
		} else {
			$(this).next('div.toggable, dl.toggable').slideToggle();
		} 
	});
	
	if ($('body').hasClass('lexikon')) {
		
		$('#content h2, #content div.toggable').hide();
		$('#content h1').after('<ul id="index"></ul>');
		
		$('#content h2').each(function() {
			if (!$.browser.msie) {
				var letter = $(this).text().trim().toString().substr(0,1);
			} else {
				var letter = $(this).text().substr(0,1);
			}
			if (jQuery.inArray(letter, indexes) < 1) {
				indexes.push(letter);
			}
			$(this).addClass('letter-'+letter);
			$(this).next().addClass('letter-'+letter);
		});		
		
		for(i=0; i<letters.length; i++) {
			if (jQuery.inArray(letters[i], indexes) >= 0) {
				$('ul#index').append('<li id="index-'+letters[i]+'" class="clickable">'+letters[i]+'</li>');
			} else {
				$('ul#index').append('<li id="index-'+letters[i]+'">'+letters[i]+'</li>');
			}
		}
		
		$('body.lexikon ul#index li.clickable').click(function() {
			$('#content h2, #content div.toggable').hide();
			var l = $(this).attr('id').substr($(this).attr('id').length-1, 1);
			$('#content h2.letter-'+l).removeClass('toggled');
			$('#content h2.letter-'+l).show();
		});
		
		$('#content h2.letter-A').show();
	
		$('body.lexikon a').click(function() {
			var glossarId = $(this).attr('href').replace(/#/, '');
			$('h2#'+glossarId).toggleClass('toggled');
			if ($.browser.msie) {
				$('h2#'+glossarId).next('div.toggable').slideDown('fast', function() {
					$('body.lexikon #content h2').css('display', 'inline');
					$('body.lexikon #content h2').css('display', 'block');			
				});
			} else {
				$('h2#'+glossarId).next('div.toggable').slideDown();
			}
			$('h2#'+glossarId).next('div.toggable').slideDown();
		});
	}
	
	//li:nth-child(2)
	$('dl.true-false dt:even').each(function() {
		$(this).css('clear', 'left');
	});

	$('a.email-obfuscated').each(function() {
		el = $(this);
		var mail = el.text().replace('[at]','@').replace('[dot]','.');
		var subject = '';
		el.each(function(){
			if (el.attr('title')){
				subject = '?subject='+el.attr('title');
			}
			el.attr('href','mailto:' + mail+subject);
			el.html(mail);
		});
	});

	/*
	if ($('body').hasClass('wir-ueber-uns')) {
		listItems = $('#nav ul li').html();
		$('#nav ul li').remove();
		$('#news-list li a').each(function() {
			$('#nav ul').append('<li><a href="'+$(this).attr('href')+'">'+$(this).text()+'</a></li>');
		});
		$('#nav ul').append(listItems);
	}
	*/

	if ($('body').hasClass('eintrag-einrichten') || $('body').hasClass('eintrag-bearbeiten')) {
		createEntrySelector();
		$('#select-entry-type label#tip-standard').tooltip({ tip: 'div.tip-standard', relative: true, position: 'bottom center', offset: [15, 30]});
		$('#select-entry-type label#tip-standard-plus').tooltip({ tip: 'div.tip-standard-plus', relative: true, position: 'bottom center', offset: [15, 0]});
		$('#select-entry-type label#tip-premium').tooltip({ tip: 'div.tip-premium', relative: true, position: 'bottom center', offset: [15, 0]});
		$('#select-entry-type label#tip-premium-plus').tooltip({ tip: 'div.tip-premium-plus', relative: true, position: 'bottom center', offset: [15, -20]});
	}
	
	$('body.suchen div.note').hide();
	$('body.suchen #search-entries input.text').focus(function() {
		$('body.suchen div.note').slideDown();
	});
});

function showRoute(source, destination, map)
{
	$('#googlemap').animate({
		width: '280px',
		marginLeft: '200px'
	}, 500, function() {
		$('#route').html('');
		directionsPanel = document.getElementById('route');
		directions = new GDirections(map, directionsPanel);
		directions.clear();

		GEvent.addListener(directions, "load", function() {
			$('#route').show('slow');
			//$('#map-route #route').append('<p><a href="http://maps.google.com/maps?saddr='+source+'&daddr='+destination+'" target="_blank">im neuen Fenster darstellen</a></p>');
			$('#route p.error').hide();
			$('#showMap').removeClass('dont-print');
		});

		GEvent.addListener(directions, "error", function() {
			$('#route').show('slow');
			$('#route').append('<p class="error">Leider keine Route von <b>'+source+'</b> gefunden</p>');
		});

		directions.load('from: '+source+' to: '+destination);
	});
	return false;
};

function createEntrySelector()
{
	if (!$('body').hasClass('eintrag-bearbeiten')) {
		$('form#create-entry div#entry-ambulantepflege,' +
		  'form#create-entry div#entry-altenheim,' +
		  'form#create-entry div#entry-tagesnachtpflege,' + 
		  'form#create-entry div#entry-ambulantepalliativpflege,' +
		  'form#create-entry div#entry-rehabilitation,' +
		  'form#create-entry div#entry-hausnotruf,' +	  
		  'form#create-entry fieldset#entry-type, form#create-entry p#entry-submit').hide();

		$('form#create-entry select#entry-cat option').each(function() {
			option = $(this).val().replace(/\-\d*/, '');
			if (option != 0 && jQuery.inArray(option, entryCats) == -1) {
				entryCats.push(option);
			}
		});

		$('form#create-entry select#entry-cat').change(function() {
			entryCat = $('form#create-entry select#entry-cat option:selected').val().replace(/\-\d*/, '');
			jQuery.each(entryCats, function(index, value) {
				if (value != entryCat) {
					$('form#create-entry div#entry-'+value).hide();
				}
			});
			$('form#create-entry input').attr('checked', '');
			$('form#create-entry fieldset#entry-type').fadeIn();
		});
	} else {
		entryType = $('form#create-entry p#select-entry-type input:checked').val();
		switch (parseInt(entryType)) {
			case 0:
				$('form#create-entry div.entry-standard-plus, form#create-entry div.entry-premium, form#create-entry div.entry-premium-plus').hide();
				break;
			case 1:
				$('form#create-entry div.entry-premium, form#create-entry div.entry-premium-plus').hide();
				break;
			case 2:
				$('form#create-entry div.entry-premium-plus').hide();
				break;
		}
	}

	$("form#create-entry input[name='entry_type']").click(function(){
		if (!$('body').hasClass('eintrag-bearbeiten')) {
			switch (parseInt($(this).val())) {
				case 0:
					$('div#entry-'+entryCat+', div#entry-'+entryCat+' div.entry-standard, p#entry-submit').fadeIn();
					$('form#create-entry div.entry-standard-plus, form#create-entry div.entry-premium, form#create-entry div.entry-premium-plus').hide();
					break;
				case 1:
					$('div#entry-'+entryCat+', div#entry-'+entryCat+' div.entry-standard, div#entry-'+entryCat+' div.entry-standard-plus, p#entry-submit').fadeIn();
					$('form#create-entry div.entry-premium, form#create-entry div.entry-premium-plus').hide();
					break;
				case 2:
					$('div#entry-'+entryCat+', div#entry-'+entryCat+' div.entry-standard, div#entry-'+entryCat+' div.entry-standard-plus, div#entry-'+entryCat+' div.entry-premium, p#entry-submit').fadeIn();
					$('form#create-entry div.entry-premium-plus').hide();
					break;
				case 3:
					$('div#entry-'+entryCat+', div#entry-'+entryCat+' div.entry-standard, div#entry-'+entryCat+' div.entry-standard-plus, div#entry-'+entryCat+' div.entry-premium, div#entry-'+entryCat+' div.entry-premium-plus, p#entry-submit').fadeIn();
					break;
			}
		} else {
			switch (parseInt($(this).val())) {
				case 0:
					$('div.entry-standard, p#entry-submit').fadeIn();
					$('form#create-entry div.entry-standard-plus, form#create-entry div.entry-premium, form#create-entry div.entry-premium-plus').hide();
					break;
				case 1:
					$('div.entry-standard, div.entry-standard-plus, p#entry-submit').fadeIn();
					$('form#create-entry div.entry-premium, form#create-entry div.entry-premium-plus').hide();
					break;
				case 2:
					$('div.entry-standard, div.entry-standard-plus, div.entry-premium, p#entry-submit').fadeIn();
					$('form#create-entry div.entry-premium-plus').hide();
					break;
				case 3:
					$('div.entry-standard, div.entry-standard-plus, div.entry-premium, div.entry-premium-plus, p#entry-submit').fadeIn();
					break;
			}		
		}
	});
};


