	var empDialog = null;

	$(window).ready(function() {    
		if(typeof $("#slider").attr("id") != "undefined"){
			startSlider();
		}
	});
	$(document).ready(function(){
		
		$(window).resize(function() {
			var viewportHeight = window.innerHeight ? window.innerHeight : $(window).height();
			if($("#page_footer").hasClass("startseite")){
				if(viewportHeight > 716){
					$("#page_footer").css({"position":"absolute","bottom":"0","margin-top":"0"});
				}else{
					$("#page_footer").css({"position":"","bottom":"auto","margin-top":"100px"});
				}
			}else{
				realignFooter();
			}
		});		
		if($("#page_footer").hasClass("startseite")){
			var viewportHeight = window.innerHeight ? window.innerHeight : $(window).height();
			
			if(viewportHeight > 716){
				$("#page_footer").css({"position":"absolute","bottom":"0","margin-top":"0"});
			}
		}else{
			window.setTimeout(function(){realignFooter();}, 1000);
		}

       	$("ul.topnav li span").click(function() { //When trigger is clicked...

        	//Following events are applied to the subnav itself (moving subnav up and down)
        	$(this).parent().find("ul.subnav").slideDown('fast').show(); //Drop down the subnav on click

        	$(this).parent().hover(function() {
        	}, function(){
        		$(this).parent().find("ul.subnav").slideUp('slow'); //When the mouse hovers out of the subnav, move it back up
        	});

        	//Following events are applied to the trigger (Hover events for the trigger)
        	}).hover(function() {
        		$(this).addClass("subhover"); //On hover over, add class "subhover"
        	}, function(){	//On Hover Out
        		$(this).removeClass("subhover"); //On hover out, remove class "subhover"
        });
       	
       	$("#printPage").click(function(){window.print();});
       	$("#recoPage").click(function(){window.empfehlen();});
       	
		$('a').click(function(evt) {
			if(this.rel == "newWindow"){
				window.open(this.href, "","");
				return false;
			}
		});
		
		$("#newsletter-sign-up #email").click(function(){
			$("#newsletter-sign-up #email").val("");
		});
		
		$("#newsletter-sign-up #email").blur(function(){
			if($("#newsletter-sign-up #email").val() == ""){
				$("#newsletter-sign-up #email").val("E-Mailaddress");
			}
		});
		
			
		$("#newsletter-sign-up").bind("submit",function(){
			return submitForm();
		});
		
		$("#newsletter-sign-up #submit").click(function(){
			return submitForm();
		});
    });
	
	function is_array(input){
	    return typeof(input)=='object'&&(input instanceof Array);
	  }
    
	jQuery.extend({
		exists: function(obj, visible) {
			if(is_array(obj) && visible){
				var dirty;
				obj.each(function(ele){
					if($(ele).css("display") == "block"){
						dirty = true;
					}
				});
				return dirty;
			}else{
				if($(obj).length){
					return true;
				}
			}
			return false;
            	}
	});



	
	function submitForm(evt){
		var email = $("#email").val();

		var rege = /^([A-Za-z0-9_\-\.])+\@([A-Za-z0-9_\-\.])+\.([A-Za-z]{2,4})$/;
		if(rege.test(email)){
			$.ajax({
				  url: 'templates/ajax/saveNewsletter.php',
				  data: "email=" + email,
				  success: function(data) {
					if(data == "FALSE"){
						$('#newsletter').html(globalNewsletterSuccess);
					}else{
						$('#newsletter').html(globalNewsletterError);
					}
				  }
				});
		}else{
			alert("Leider koennen wir Ihre E-Mailadresse nicht in unseren Verteiler aufnehmen.\n\nBitte pruefen Sie Ihre E-Mailadresse.");
		}
		return false;
	}
	function startSlider(){
		$('#slider').nivoSlider({
			effect:'random', //Specify sets like: 'fold,fade,sliceDown'
			slices:10,
			animSpeed:700, //Slide transition speed
			pauseTime:5000,
			startSlide:0, //Set starting Slide (0 index)
			pauseOnHover:true, //Stop animation while hovering
			manualAdvance:false, //Force manual transitions
			captionOpacity:0.8, //Universal caption opacity
			beforeChange: function(){},
			afterChange: function(){},
			slideshowEnd: function(){} //Triggers after all slides have been shown
		});
	}
	
	
	function realignFooter(){
		if(!jQuery.exists("ui-dialog", true)){
			var sliderHeight = 0;
			
			if($("slider").height() == null){
				sliderHeight = 0;
			}
			var controlHeight = 0;
			
			if($("#page-info").attr("id")){
				var controlHeight = Math.round($("#page-info").offset().top + $("#page-info").height() + sliderHeight);
			}
		
    			var newHeight = Math.round($("#page-content").offset().top + $("#page-content").height());

		    	if($(".tabs").attr("id")){
					newHeight = Math.round($(".tabs").offset().top + $(".tabs").height() + sliderHeight);
		    	}
    			if(controlHeight > newHeight){
	    			newHeight = controlHeight;
    			}
    	
			var viewportHeight = window.innerHeight ? window.innerHeight : $(window).height();

    			if((newHeight + $("#page_footer").height()) < viewportHeight){
    				newHeight = viewportHeight - $("#page_footer").height();
    		
    				var footerTop = newHeight;
    				if(newHeight < 530){
    					footerTop = 530;
    					newHeight = 530;
    				}
    			}else{
    				var footerTop = newHeight+100;
    				newHeight = newHeight+100;
    				if(newHeight < 650){
    					footerTop = 650;
    					newHeight = 650;
    				}
    			}
    	
			$(".transparency").css({"height": newHeight});
    			$("#page-shadow-container").css({"height": newHeight});
    			$("#page_container_outter").css({"height": newHeight});
    			$("#page_footer").css({"top": footerTop,"position":"absolute","width":"100%"});
    	
    			window.pageHeight = newHeight;
		}
	}
	
	window.empfehlen = function(){
		var url = window.location.href;
		var $this = $(this);
		var horizontalPadding = 30;
		var verticalPadding = 30;
		
		empDialog = $('<iframe border="0" frameborder="0" id="externalSite" class="externalSite" src="empfehlen.html?url="'+url+' />').dialog({
			title: ($this.attr('title')) ? $this.attr('title') : 'Seite weiterempfehlen',
			autoOpen: true,
			width: 470,
			height: 530,
			modal: true,
			resizable: false,
			autoResize: false,
			overlay: {
				opacity: 0.8,
				background: "black"
			}
		}).width(440).height(500 - 30);	     
	}
	
	function parseUrl(id){
		var url1 = parent.location.href;
		var ele = document.getElementById(id);
		ele.value = url1;
	}
	
	
	function pageInclude(type){
		if(type == "empfehlen"){
			
				iwHighlighter.activateHighlighter = true;
				iwHighlighter.init();
			
				
				// Titel der Fehlermeldung
				h2fFormValidator.errorTitleString = " ";
				
				// 2. Titel der Fehlermeldung
				h2fFormValidator.errorTitleString2 = "<strong>Folgende Felder sind Pflichtfelder und m&uuml;ssen ausgef&uuml;llt werden:</strong><br />";
				
				// Formular, welches Validiert werden soll
				h2fFormValidator.attach2form = "empfehlenform";
																		
				// Container der Fehlermeldung
				h2fFormValidator.parseErrorToContainer = "fehlermeldung";												
				
				// Default-Konfiguration laden
				h2fFormValidator.config();			
				
				// Liste wird Kommagetrennt ausgegeben (True = Als Liste)
				h2fFormValidator.displayaslist = false;
			
				// &Uuml;bersetzungen der Input-Ids zu normalen Texten	
				h2fFormValidator.translateIDs.push(new Array("vorname","Vorname (Absender)"));
				h2fFormValidator.translateIDs.push(new Array("name","Nachname (Absender)"));
				h2fFormValidator.translateIDs.push(new Array("email","E-Mail (Absender)"));
				h2fFormValidator.translateIDs.push(new Array("vorname2","Vorname (Empf&auml;nger)"));
				h2fFormValidator.translateIDs.push(new Array("name2","Nachname (Empf&auml;nger)"));
				h2fFormValidator.translateIDs.push(new Array("email2","E-Mail (Empf&auml;nger)"));
				h2fFormValidator.translateIDs.push(new Array("nachricht","Nachricht"));
				
				h2fFormValidator.checkOneOrAnotherArray = new Array();
				
				// Setzen der Mindest- bzw. Maximalwerte bestimmter Felder
				h2fFormValidator.setMinMax = new Array();
						
				// Validator aktivieren
				h2fFormValidator.activateValidator = true;
				
				// IDs der Felder die validiert werden sollen
				h2fFormValidator.setValidateFields = new Array("vorname", "name", "email", "vorname2", "name2", "email2", "nachricht"); 
						
				// Abh&auml;ngigkeitscheck
				h2fFormValidator.dependencyCheck = false;
				
				// E-Mail-Validator aktivieren
				h2fFormValidator.activateMailValidator = true;
				
				// IDs der Felder die auf eine g&uuml;ltige E-Mailadresse validiert werden sollen
				h2fFormValidator.setMailValidatorArray = new Array("email","email2");
			
				h2fFormValidator.callBack = function(){showDialog("empfehlen");};

				h2fFormValidator.init();
		}
	}
	
	function showDialog(stat){
		var isParent = false;
		var aUrl = "";
		if(stat == "empfehlen"){
			aUrl = "empfehlenabsenden.php";
			isParent = true;
		}else{
			aUrl = "bestellungaufgeben.php";
		}

		var formData = "";
		if($("#kontaktform")){
			formData = $("#kontaktform").serialize();
		}else if($("#empfehlenform")){
			formData = $("#empfehlenform").serialize();
		}
		
		if(stat == "empfehlen"){
			formData = $("#empfehlenform").serialize();
		}

		
		$.ajax({
			data: formData,
        			method: "get", 
			url: aUrl,
            			success: function(html){ //so, if data is retrieved, store it in html
				
				empDialog = window.parent.empDialog;
            				if(stat == "empfehlen"){
            					alert("Ihre Empfehlung wurde erfolgreich versendet.");
            					empDialog.dialog("destroy");
            					
            				}else{
					if(html == "TRUE"){
						modal("#gut", isParent);
					}else{
						modal("#schlecht",isParent);
					}
				}
            			}
		});
		return false;
	}
		
		
	function modal(stat, isParent){
		var $this = $(this);
		var horizontalPadding = 30;
		var verticalPadding = 30;
		var sizex = 400;
		var sizey = 250;
		if(isParent){
			$("#gut", window.document.parent).dialog("destroy");
			$("#schlecht", window.document.parent).dialog("destroy");
								
			var gut = $("#gut").innerHTML;
			var schlecht = $("#schlecht").innerHTML;
			
			var pNode = $("site-container", window.document.parent);
			
			var nGut = document.createElement("div");
			var nSchlecht = document.createElement("div");
			nGut.innerHTML = gut;
			nSchlecht.innerHTML = schlecht;
			
			pNode.appendChild(nGut);
			pNode.appendChild(nSchlecht);
		}else{
			$("#gut").dialog("destroy");
			$("#schlecht").dialog("destroy");
		}
		
		var dialogOptions = {
			title: ($this.attr('title')) ? $this.attr('title') : 'Kontaktanfrage',
			autoOpen: true,
			width: sizex,
			height: 100,
			modal: true,
			resizable: false,
			autoResize: false,
			overlay: {
				opacity: 0.8,
				background: "black"
			},
			buttons: {
				'OK': function() {
					$(this).dialog('close');
				}
			}
		};
		if(isParent){
			$(stat, window.document.parent).dialog(dialogOptions).width(sizex - horizontalPadding).height(sizey - verticalPadding);	
		}else{
			$(stat).dialog(dialogOptions).width(sizex - horizontalPadding).height(sizey - verticalPadding);	
		}
		
	}
	

	function moveBy(id, mBy){           
		$('#' + id).animate({
		    opacity: 0.5,
		    left: '+='+mBy+'px'
		  }, 400, function() {
			  p = $('#imageContainer').css("left");
			  $("#" + id).animate({opacity: 1},400);
			  if(p == "auto"){p=0;}

		  });
	}

	function scrollMeLeft(){
		var p = $('#imageContainer').css("left");
		if(p == "auto"){p=0;}
		p = parseInt(p);
	    var slideBy = c;

	    if(c + p < 0){
	        slideImages(slideBy * (-1));
	    }else{
	        slideImages(p);
	    }
	}

	function scrollMeRight(){
		var p = $('#imageContainer').css("left");
		if(p == "auto"){p=0;}

		p = parseInt(p);
	    var slideBy = c;
	    
		var xy = m + p - c;
	    if( xy > c){
	        slideImages(slideBy);
	    }else{
	        slideImages(xy);
	    }
	}

	function slideImages(slideBy){
			moveBy("imageContainer", slideBy * (-1), 400);
	}
