// JavaScript Document


jQuery(function($){
	$.fn.fadeImages = function(time, fade, first) {
		var items = this;
		time = time || 5000;
		fade = fade || 2000;
		first = typeof first == 'undefined';
		
		if(first) {
			items.css('position', 'absolute').filter(':not(:first)').hide();
		}
		
		setTimeout(function() {
			var current = items.filter(':visible');
			var next = current.index() == (items.length - 1) ? items.filter(':first') : items.filter(':eq(' + (current.index() + 1) + ')')
		
			current.fadeOut(fade);
			next.fadeIn(fade);
		
			items.fadeImages(time, fade, true);
		}, time)
	}
		
})



jQuery.cookie = function (key, value, options) {

    // key and value given, set cookie...
    if (arguments.length > 1 && (value === null || typeof value !== "object")) {
        options = jQuery.extend({}, options);

        if (value === null) {
            options.expires = -1;
        }

        if (typeof options.expires === 'number') {
            var days = options.expires, t = options.expires = new Date();
            t.setDate(t.getDate() + days);
        }

        return (document.cookie = [
            encodeURIComponent(key), '=',
            options.raw ? String(value) : encodeURIComponent(String(value)),
            options.expires ? '; expires=' + options.expires.toUTCString() : '', // use expires attribute, max-age is not supported by IE
            options.path ? '; path=' + options.path : '',
            options.domain ? '; domain=' + options.domain : '',
            options.secure ? '; secure' : ''
        ].join(''));
    }

    // key and possibly options given, get cookie...
    options = value || {};
    var result, decode = options.raw ? function (s) { return s; } : decodeURIComponent;
    return (result = new RegExp('(?:^|; )' + encodeURIComponent(key) + '=([^;]*)').exec(document.cookie)) ? decode(result[1]) : null;
};


$(document).ready(function(){

	var $togglers = $(".toggle-section"),
		categoryToggle = function($elem){
			var catid = $elem.data("catid");
			if(isNaN(catid) && !catid) {
				$msghead = $elem;
			} else {
				$msghead = $("#d_cat_" + catid);
			}
			$msghead.toggleClass("open")
				.next(".msg_body").slideToggle(200);
	};
	
	
	$togglers.click(function(){
		categoryToggle($(this));
	});

	//hide the all of the element with class msg_body
  	$(".msg_body").hide();
  	//toggle the componenet with class msg_body
  	$(".msg_head").click(function()
  	{
   		categoryToggle($(this));
	});
	
	$(".prod_detail_atrb_02 table:contains('DN/OD')").each(function(){
		$(".prod_detail_atrb_02").after($("<p class='star-text'>*DN/OD = Außendurchmesser (mm). </p>"));
	});
	$(".prod_detail_atrb_02 table:contains('Rohrlänge')").each(function(){
		$("p.star-text").after($("<p class='star-text'>**Weitere Rohrlängen sind auf Anfrage möglich. </p>"));
	});
	
	
	
	$(".prod_detail_atrb_02 table:contains('Anstauhöhe')").each(function(){
		$(".prod_detail_atrb_02").after($("<p class='star-text'>*Bei Anstauhöhen von 35-50 mm ist ein Kürzen der Gewindestifte der Gullybauteile erforderlich.</p>"));
	});

	var $table = $(".prod_detail_atrb_02 table");
	var $allThs = $table.find("th");
	
	var header = ["Material","Art","Modell"];
	
	$allThs.each(function(){
	    var $this = $(this);
	    var t = $this.text();
	    if($.inArray(t,header) >= 0) {
	        var idx = $allThs.index($this);
	        $table.find("tr td:nth-child("+(idx+1)+")").css("text-align","left");
	        $this.css("text-align","left");
	    }
	});
		
	var $checkedBauart = $("input[name=bauart]:checked");
	if(!$checkedBauart.length) {
	    $("input[name=bauart]:eq(0)").attr('checked', true);
	}

	var checkAnstau = function($elem) {

		if($elem.val() === '') {
			$("#belastung-checkbox").attr("checked","checked");
			$("#dachaufbaumaxmoglicheranstaudinmm, #leitungsfuehrunghohendifferenzzursammelanschlussleitungstandard").val("<strong>ja</strong>");
		/*
			$('.radio_hide').show();
			$('.radio_hide').attr('selected','selected');
		*/
		} else {
			$("#belastung-checkbox").removeAttr("checked");
			$("#dachaufbaumaxmoglicheranstaudinmm, #leitungsfuehrunghohendifferenzzursammelanschlussleitungstandard").val("nein");
			//$('.radio_hide').hide();
		}
	};
	
	var $anstau = $('#dachaufbaumaxmoglicheranstaumm');
	
	$anstau.keyup(function(){
		checkAnstau($(this));
	});

	checkAnstau($anstau);

});

var Sita = {};

$(document).ready(function() {
	
	$('.gebaeudedaten').not(":first").hide();
    $(".next").click(function(){
        var $nextGebaeudedaten = $(this).closest(".gebaeudedaten").nextAll(".gebaeudedaten:first");        
		$nextGebaeudedaten.show();
        $nextGebaeudedaten.find(".closeButton").show();
        $(this).hide();
        return false;
    });
 
	$(".closeButton").click(function(){
		$(this).closest(".gebaeudedaten").hide();
		$(this).closest(".gebaeudedaten").prevAll(".gebaeudedaten:first").find(".next").show();
	});
	
	
	Sita.createErrorPopover = function() {
		
		// Fehler aus den DOM auslesen und in Popover packen
	 	var $errorMessages = "<h2>Es sind Fehler aufgetreten</h2><ul>";
	 	var errorOccured = false;
	 	$(".validation-error").each(function() {
	 		errorOccured = true;
	 		$errorMessages += "<li>"+$(this).text()+"</li>";
	 		$(this).remove();
	 	});
	 	$errorMessages += "</ul><div style=\"text-align: center;\"><input type=\"submit\" value=\"Meldung ausblenden\" onclick=\"$('#validation-error-overview').hide(); $('#validation-error-overlay').hide();\" id=\"dismiss-error-popover\"></div>";
	 	
	 	if (errorOccured == true) {
	 		$('#validation-error-overview').html($errorMessages);
	 		$('#validation-error-overview').css("display","block");
	 		$('#validation-error-overlay').css("display","block");
	 	}
	 	
	}
	
	Sita.createErrorPopover();
 
	
	Sita.checkIfFlaechenNotEmpty = function(){
		var allesOk = true;		
	 	var $gebaeudeDaten = $(".gebaeudedaten:visible");
	 	
	 	var checkboxCheck = function($elem){
	 	
	 		var $checkboxes = $elem.find("input[type='checkbox']:checked");
	 	
			if($checkboxes.length < 1) {
	 			allesOk = false;
	 			var errorMsg = $elem.find(".checkboxError");
	 			if(!errorMsg.length) {
	 				$elem.prepend('<ul><li class="checkboxError validation-error">Mind. eine Checkbox auswählen!</li></ul>');
	 			}
	 		} else {
	 			$elem.find(".checkboxError").remove();
	 		}
		};
		
		$gebaeudeDaten.each(function(){
		
			var $flaeche = $(this).find("input[name$='flachem2']");
			
			if($flaeche.val() === "") {
				// FEHLER
				allesOk = false;
				
	 			var $errorMsg = $flaeche.closest(".ctrlHolder").find("li");
				if(!$errorMsg.length){
					$flaeche.css("border","1px solid #E58E29");
			 		$flaeche.before($("<li class=\"validation-error\">*Pflichtfeld</li>"));
			    }
			 } else {
				// ALLES GUT
		 		$flaeche.css("border","1px solid #AAAAAA");
		 		$flaeche.closest(".ctrlHolder").find("li").remove();
	 		}
		});
		
		var $checkboxMinimum = $("fieldset.checkbox-group:visible");
		
		$checkboxMinimum.each(function(){
			checkboxCheck($(this));
		});	
		
		Sita.createErrorPopover();
		
		return allesOk;
	}
 
 	//$("button[type='submit']").click(Sita.checkIfFlaechenNotEmpty);
 	
 	// Resolves #699
	$('form[name="tx_indexedsearch"]').submit(function() {
	  $('form[name="tx_indexedsearch"]').submit();
	});
	
	
	
	// Logos per Zufall anzeigen -de 
	var $logodivs = $("#c512,#c121,#c626,#c629,#c673,#c675"),
		zufallszahl = Math.round(Math.random());
	if($logodivs.length === 2) {
		$logodivs.eq(zufallszahl).show();
	}
	
	
	$('.startseite .tv_page_2col_2 .csc-textpic-imagewrap .csc-textpic-imagerow').hide();
	$('.startseite .tv_page_2col_2 .csc-textpic-imagewrap .csc-textpic-imagerow:first-child').show();
	
	$('.startseite #c655 .csc-textpic-imagewrap div.csc-textpic-imagerow').fadeImages(5000);
	
	setTimeout(function(){
		$('.startseite #c681 .csc-textpic-imagewrap div.csc-textpic-imagerow').fadeImages(5000);
	}, 2500);
	/*
	
	var timer = setInterval(fadeImage, 6000);
	var timer = setInterval(fadeImage2, 6000);
	
	function fadeImage() {
		$('.startseite #c588 .csc-textpic-imagewrap .csc-textpic-imagerow').each(function () {
			if ($(this).is(':visible')) {
				$(this).fadeOut('slow', function() {
					if ($(this).hasClass('csc-textpic-imagerow-last')) {
						$('.startseite #c588 .csc-textpic-imagewrap .csc-textpic-imagerow:first-child').fadeIn();
					} else {
						$(this).next().fadeIn();
					}
				});
			}
		});
	};
	
	function fadeImage2() {
		$('.startseite #c622 .csc-textpic-imagewrap .csc-textpic-imagerow').delay(3000).each(function () {
			if ($(this).is(':visible')) {
				$(this).fadeOut('slow', function() {
					if ($(this).hasClass('csc-textpic-imagerow-last')) {
						$('.startseite #c622 .csc-textpic-imagewrap .csc-textpic-imagerow:first-child').fadeIn();
					} else {
						$(this).next().fadeIn();
					}
				});
			}
		});
	};
	*/
	
	
	
	// $('.startseite .csc-textpic-imagerow:gt(0)').hide();
	// setInterval(function(){
      // $('.csc-textpic-imagerow :first-child').fadeOut()
         // .next('img').fadeIn()
         // .end().appendTo('.csc-textpic-imagerow');
       // }, 1000
       // );
       
       
       $('#c685 .csc-textpic-single-image a').addClass('overlay_grund');
	
});





	
	 



