// adding toggler
$.fn.toggle = function() {
	this.each(function() {
		
		var t = $(this); // togglebox
				
		
		// get height of closed toggler, it's always the height of the first p elem
		var tHeight = t.height() + 'px';
		var paragraphs = $(this).find('p');
		var moreChildren = $(this).children('*').not('h2');
			
		var firstParagraph = $(paragraphs[0]);
		var h2Height = t.children('h2').outerHeight(true);
		var closedHeight = firstParagraph.outerHeight(true) + h2Height;			
		closedHeight = closedHeight + 'px';			
		
		
		
		if(paragraphs.length > 1 || moreChildren.length > 1) {
			t.append('<a href="#" class="more">mehr infos</a>');			
			t.wrapInner('<div class="wrapper" />');
		}			
		
		// check if anchor in url equals id in toggler, then open it
		var hashes = window.location.href.slice(window.location.href.indexOf('?') + 1).split('#');		
		if(t.attr('id') == hashes[1])
			t.addClass('opened');
		else {
			t.addClass('closed');
			t.children('.wrapper').css({
				'height': closedHeight						
			})
		}		
		
		
		var toggleLink = t.find('.more');
		var defaultLinkText = toggleLink.text();		
		
		toggleLink.bind('click', function(e) {
			e.preventDefault();
			if(t.hasClass('closed')) {
				t.removeClass('closed');
				t.addClass('opened');
				t.children('.wrapper').animate({				   
				    height: tHeight
				  }, 500, function() {
					  toggleLink.text('weniger Infos');
				  });	
			} else {
				t.addClass('closed');
				t.removeClass('opened');
				t.children('.wrapper').animate({				   
				    height: closedHeight
				  }, 500, function() {
					  toggleLink.text(defaultLinkText);
				  });				
			}
		});
		
	});
}

$.fn.inputValueSwitcher = function() {
	this.each(function() {
		var el = $(this);
		var value = el.val();
    var hasError = el.hasClass('error');   
    
		el.focus(function() {
		if (el.val() === value) {
			el.val('');  
      if(hasError == true) { el.removeClass('error'); }
    }
		}).focusout(function() {
		if (el.val() === '') {
      el.val(value);
      if(hasError == true) { el.addClass('error'); }
    }
		});
	});
	return this;
};


$(document).ready(function() {
	$('.toggle').toggle();
	$('.fText').inputValueSwitcher();
	$('.show > a').lightBox();
});
