// other
jQuery(document).ready(function(){			  
	noSpam();
	smoothscroll();
	input(-3);
	jQuery('#gototop').topLink({fadeSpeed: 300});
	jQuery("#slider").easySlider();

	if(jQuery('a.guestbook').length > 0){
		jQuery('a.guestbook').fancybox({
				'overlayShow'	: true,
				'transitionIn'	: 'elastic',
				'transitionOut'	: 'elastic',
				'width'				: '50%',
				'height'			: '75%',

				'type' : 'iframe',
				'autoScale'			: false
		});
	}
	
	if(jQuery('a[rel^=lightbox]').length > 0){
		jQuery('a[rel^=lightbox]').fancybox({
				'overlayShow'	: true,
				'transitionIn'	: 'elastic',
				'transitionOut'	: 'elastic'
		});
	}
	
	jQuery('.inhalt').hide();
	jQuery('.click').click(function(){
			jQuery('.inhalt').slideUp(500);
		if(jQuery(this).next('.inhalt').css('display') == 'none'){
			jQuery(this).next('.inhalt').slideDown(500);
		}
	});

	
});

function input(topValue){
	jQuery('input[type=password], input[type=text]').each(function(){
		var $this = jQuery(this);
		if($this.attr('alt') !== '' && jQuery.trim($this.attr('value')) === ''){
			$this.after('<span class="label">'+$this.attr('alt')+'</span>');
			var $label = $this.next('.label');
			$label.css({
				'color':'#aaa',
				'padding':'10px',
				'position':'absolute',
				'top':$this.position().top+parseInt(topValue,10),
				'left':$this.position().left,
				'text-shadow':'none'
			});
			if($this.attr('value') !== ''){
				$this.next('.label').hide();
			}
		}
	});
	
	jQuery('.label').click(function(){
		jQuery(this).hide();
		jQuery(this).prev('input').focus();
	});
	
	jQuery('input[type=password], input[type=text]').focus(function(){
		if(jQuery(this).attr('alt') !== ''){
			jQuery(this).next('.label').hide();
		}
	});
	
	jQuery('input[type=password], input[type=text]').blur(function(){
		if(jQuery(this).attr('alt') !== '' && jQuery(this).attr('value') === ''){
			jQuery(this).next('.label').show();
		}
	});
}

// form_validation v. 2.1
//  ----------------------------
// | Euroweb Internet GmbH     |
//  ----------------------------
//	Last changed:	09. 09. 2009

function validateForm(formular) {
	var error = 0;
	var currentLabel = '';
	var currentField = '';
	var klasse = '';
	var newClass = '';
	
	// Inspect all of the document's labels ...
	for (var i = 0; i < document.getElementsByTagName("label").length; i++) {
		currentLabel = document.getElementsByTagName("label")[i];
		if (currentLabel.htmlFor) {
			currentField = document.getElementById(currentLabel.htmlFor);
		}
		klasse = currentLabel.className;
		newClass = currentLabel.className.replace(/ error/, '');
		
		// Check if the current label belongs to the form we want to validate
		if (currentLabel.form == formular && currentField) {

			// ...  and if it is required at all, or has to be a number, or an e-mail address.
				
				// Rueckruf exists: Mark phone number field as required
				if (currentField.name == 'Rueckruf') {
					if (currentField.checked == true) {
						document.getElementById('label-telefon').className += ' required rueckruf';
					} else {
						var newTelClass = document.getElementById('label-telefon').className.replace(/ required rueckruf/, '');
						document.getElementById('label-telefon').className = newTelClass;
					}
				}
				

				// required field (but neither numeric nor an e-mail)
				if (klasse.match(/required/)) {
					if (currentField.tagName == 'SELECT') {
						if (currentField.childNodes[1].selected) {
							currentLabel.className = newClass;
							currentLabel.className += ' error';
							error = 1;
						} else {
							currentLabel.className = newClass;
						}
					} else if (currentField.type == 'checkbox') {
						if (currentField.checked == false) {
							currentLabel.className = newClass;
							currentLabel.className += ' error';
							error = 1;
						} else {
							currentLabel.className = newClass;
						}
					} else {
						if (currentField.value == '') {
							currentLabel.className = newClass;
							currentLabel.className += ' error';

							error = 1;
						} else {
							currentLabel.className = newClass;
						}
					}
					
				}
								
				// numeric field
				if (klasse.match(/number/)) {
					var numeric = isNumber(currentField);
					if (!numeric && !klasse.match(/required/) && currentField.value != '') {
						currentLabel.className = newClass;
						currentLabel.className += ' error';
						error = 1;
					} else {
						currentLabel.className = newClass;
					}
					if (!numeric && klasse.match(/required/)) {
						currentLabel.className = newClass;
						currentLabel.className += ' error';
						error = 1;
					} else {
						if (error == 0) {
							currentLabel.className = newClass;
						}
					}
				}
				
				// e-mail address
				if (klasse.match(/mail/)) {
					var valid = isMailValid(currentField);
					if (!valid && currentField.value != '') {
						currentLabel.className = newClass;
						currentLabel.className += ' error';
						error = 1;
					} else {
						currentLabel.className = newClass;
					}
					if (!valid && klasse.match(/required/)) {
						currentLabel.className = newClass;
						currentLabel.className += ' error';
						error = 1;
					} else {
						if (error == 0) {
							currentLabel.className = newClass;
						}
					}
				}


		}
		
	} // end for

	
	// Return TRUE and proceed sending the form if no errors occured.
	// Return FALSE in case of errors and display the error message,
	// then focus on the ID of the element containing the error message.
	
	// (The window.location.href call comes in handy if the page containing
	//	the form is as high as serveral screens,
	//  but can be safely removed without preventing the script to function properly).
	if(error === 0){
		return true;
	}else{
		var errorbox = document.getElementById("fehlermeldung");
		errorbox.style.display = 'block';
		errorbox.innerHTML = '<strong>Beim Abschicken sind Fehler aufgetreten.</strong> Sie haben eventuell nicht alle Pflichtfelder ausgef&uuml;llt. Die falsch ausgef&uuml;llten Punkte sind <strong class="error">rot</strong> gekennzeichnet.<br /><a href="javascript:;" id="errorclose">Diese Meldung schlie&szlig;en</a>';
		window.location.href = "#fehlermeldung";
		document.getElementById("errorclose").onclick = function (event) { errorbox.style.display = 'none'; return false; }
		return false;
	}
		
}


// Additional functions for numeric and e-mail validation
function isNumber(field) {
	var returnvar = (isNaN(parseInt(field.value)) == true) ? false : true;
	return returnvar;
}

function isMailValid(field) {
	var returnvar = (field.value.match(/^[\w\.\-]+@([\w\-]+\.)+[a-zA-Z]+$/)) ? true : false;
	return returnvar;
}

function noSpam(){
	jQuery('a.escape').each(function(){
		jQuery(this).find('span').first().html('@');
		jQuery(this).attr('href','mailto:'+jQuery(this).text());
	});
}

jQuery.fn.topLink = function(settings) {
	settings = jQuery.extend({fadeSpeed: 200}, settings);
		var scroll_timer;
		var displayed = false;
		var $message = jQuery(this);
		var $window = jQuery(window);
		var top = jQuery(document.body).children(0).position().top;
		$window.scroll(function () {
			window.clearTimeout(scroll_timer);
			scroll_timer = window.setTimeout(function () {
				if($window.scrollTop() <= top)
				{
					displayed = false;
					$message.fadeOut(settings.fadeSpeed);
				}
					else if(displayed == false) 
				{
					displayed = true;
					$message.stop(true, true).fadeIn(settings.fadeSpeed).click(function () { $message.fadeOut(settings.fadeSpeed); });
				}
			}, 100);
		});
};

function smoothscroll(){
	jQuery('a[href*=#]').click(function() {
		if (location.pathname.replace(/^\//,'') == this.pathname.replace(/^\//,'') && location.hostname == this.hostname) {
			var $target = jQuery(this.hash);
			$target = $target.length && $target || jQuery('[name=' + this.hash.slice(1) +']');
			if ($target.length) {
				var targetOffset = $target.offset().top;
				jQuery('html,body').animate({scrollTop: targetOffset}, 1000);
				return false;
			};
		};
	});
};

// EASYSLIDER
/*
 * 	Easy Slider 1.5 - jQuery plugin
 *	written by Alen Grakalic	
 *	http://cssglobe.com/post/4004/easy-slider-15-the-easiest-jquery-plugin-for-sliding
 *
 *	Copyright (c) 2009 Alen Grakalic (http://cssglobe.com)
 *	Dual licensed under the MIT (MIT-LICENSE.txt)
 *	and GPL (GPL-LICENSE.txt) licenses.
 *
 *	Built for jQuery library
 *	http://jquery.com
 *
 */

(function($) {

	$.fn.easySlider = function(options){
	  
		// default configuration properties
		var defaults = {			
			prevId: 		'prevBtn',
			prevText: 		'Previous',
			nextId: 		'nextBtn',	
			nextText: 		'Next',
			controlsShow:	true,
			controlsBefore:	'',
			controlsAfter:	'',	
			controlsFade:	true,
			firstId: 		'firstBtn',
			firstText: 		'First',
			firstShow:		false,
			lastId: 		'lastBtn',	
			lastText: 		'Last',
			lastShow:		false,				
			vertical:		false,
			speed: 			1500,
			auto:			false,
			pause:			2000,
			continuous:		true
		}; 
		
		var options = $.extend(defaults, options);  
				
		this.each(function() {  
			var obj = $(this); 				
			var s = $("li", obj).length;
			var w = $("li", obj).width(); 
			var h = $("li", obj).height(); 
			obj.width(w); 
			obj.height(h); 
			obj.css("overflow","hidden");
			var ts = s-1;
			var t = 0;
			$("ul", obj).css('width',s*w);			
			if(!options.vertical) $("li", obj).css('float','left');
			
			if(options.controlsShow){
				var html = options.controlsBefore;
				if(options.firstShow) html += '<span id="'+ options.firstId +'"><a href=\"javascript:void(0);\">'+ options.firstText +'</a></span>';
				html += ' <span id="'+ options.prevId +'"><a href=\"javascript:void(0);\">'+ options.prevText +'</a></span>';
				html += ' <span id="'+ options.nextId +'"><a href=\"javascript:void(0);\">'+ options.nextText +'</a></span>';
				if(options.lastShow) html += ' <span id="'+ options.lastId +'"><a href=\"javascript:void(0);\">'+ options.lastText +'</a></span>';
				html += options.controlsAfter;						
				$(obj).after(html);										
			};
	
			$("a","#"+options.nextId).click(function(){		
				animate("next",true);
			});
			$("a","#"+options.prevId).click(function(){		
				animate("prev",true);				
			});	
			$("a","#"+options.firstId).click(function(){		
				animate("first",true);
			});				

			$("a","#"+options.lastId).click(function(){		
				animate("last",true);				
			});		
			
			function animate(dir,clicked){
				var ot = t;				
				switch(dir){
					case "next":
						t = (ot>=ts) ? (options.continuous ? 0 : ts) : t+1;						
						break; 
					case "prev":
						t = (t<=0) ? (options.continuous ? ts : 0) : t-1;
						break; 
					case "first":
						t = 0;
						break; 
					case "last":
						t = ts;
						break; 
					default:
						break; 
				};	
				
				var diff = Math.abs(ot-t);
				var speed = diff*options.speed;						
				if(!options.vertical) {
					p = (t*w*-1);
					$("ul",obj).animate(
						{ marginLeft: p }, 
						speed
					);				
				} else {
					p = (t*h*-1);
					$("ul",obj).animate(
						{ marginTop: p }, 
						speed
					);					
				};
				
				if(!options.continuous && options.controlsFade){					
					if(t==ts){
						$("a","#"+options.nextId).hide();
						$("a","#"+options.lastId).hide();
					} else {
						$("a","#"+options.nextId).show();
						$("a","#"+options.lastId).show();					
					};
					if(t==0){
						$("a","#"+options.prevId).hide();
						$("a","#"+options.firstId).hide();
					} else {
						$("a","#"+options.prevId).show();
						$("a","#"+options.firstId).show();
					};					
				};				
				
				if(clicked) clearTimeout(timeout);
				if(options.auto && dir=="next" && !clicked){;
					timeout = setTimeout(function(){
						animate("next",false);
					},diff*options.speed+options.pause);
				};
				
			};
			// init
			var timeout;
			if(options.auto){;
				timeout = setTimeout(function(){
					animate("next",false);
				},options.pause);
			};		
		
			if(!options.continuous && options.controlsFade){					
				$("a","#"+options.prevId).hide();
				$("a","#"+options.firstId).hide();				
			};				
			
		});
	  
	};

})(jQuery);
