$(document).ready(function(){

$('div#ergebnis img').fadeTo('fast', 0.3);

$.extend({
getUrlVars: function() {
	var vars = {};
	var parts = window.location.href.replace(/[?&]+([^=&]+)=([^&]*)/gi, function(m,key,value) { vars[key] = value; });
	return vars;
	}
});

var myauswahl = $.getUrlVars()['auswahl'];
var mygewerb = 0;
var myradio = 0;
var myart = 3;

var breite = '';
var tiefe = '';
var hoehe = '';
var volumen = '';

var mytemp = new Array();
var mytemp2 = '';


$("input[name=gewerb]").click(function () {    

	if (myauswahl == 'Steuer') {
		$("#cover3").fadeOut('slow', function() {
			$("#cover3").css({'display-style' : 'none'});
		});
		
		$("#groesse").fadeOut("slow", function() {
			// Animation complete.
			$("#cover2").remove();
			$(this).remove();
		});
		
	} else {
		$("#cover2").fadeOut('slow', function() {
			$("#cover2").css({'display-style' : 'none'});
		});
	}
	 
	mygewerb = this.value;
	show_products(myauswahl,mygewerb,myradio,myart);
    });

$("input[name=myradiobutton]").click(function () {
      $("#cover3").fadeOut('slow', function() {
		$("#cover3").css({'display-style' : 'none'});
	  });
	  
	  myradio = this.value;
	  show_products(myauswahl,mygewerb,myradio,myart);
    });

$("input[name=sauna_art]").click(function () {
	  myart = this.value;
	  show_products(myauswahl,mygewerb,myradio,myart);
	  $("#mail").fadeIn("slow");
	  $("#emailform").validate({
		rules: {
			myemail: {
				required: true,
				email: true
			}
		},
		messages: {
			myemail: {
				required: " ",
				email: "eMail Adresse nicht valide"
			}
		}
		});
    });
	
function show_products(auswahl, gewerb, radio, art) {
	$('div#ergebnis img').fadeTo('fast', 0.3);
	$.getJSON("ajax2.php",{auswahl: auswahl, gewerb: gewerb, radio: radio, art: art}, function(data){
        
		mytemp = new Array();
		
		if (data.length > 0) {
		
        $.each(data, function(i,item){
		  mytemp.push(item.bez);
		  mytemp2 = item.sql;
		  $('#prod-'+item.bez).fadeTo('slow', 1);
        });
		
		} else {
			$('div#ergebnis img').fadeTo('fast', 0.3);
		}
		
		//$("#mytemp").val( mytemp.join(", ") );
		$("#formauswahl").val( mytemp.join(" ") );
		$("#formauswahl2").val( mytemp.join(" ") );
		//$("#mytemp2").val(mytemp2);
		//$("#debug").html(mytemp2);
				
		/*
		if (mytemp.length > 0) {
			$("#auswahl").fadeIn("fast");
			$("#auswahl").html( "" + mytemp.join(" | ") + "" );
		} else { $("#auswahl").fadeOut("fast"); }
		*/
		
    });
}

$('#radio_up').click(function () {
	$('#radio_up').fadeTo('slow', 1);
	$('#radio_down').fadeTo('slow', 0.5);
});

$('#radio_down').click(function () {
	$('#radio_up').fadeTo('slow', 0.5);
	$('#radio_down').fadeTo('slow', 1);
	
	if (volumen > 0) {
		myradio = volumen;
		show_products(myauswahl,mygewerb,myradio,myart);
	}
});

$("#radio_down input:text").change(function () {
	breite = $("input[name=breite]").val();
	tiefe = $("input[name=tiefe]").val();
	hoehe = $("input[name=hoehe]").val();
	
	breite = calc(breite);
	tiefe = calc(tiefe);
	hoehe = calc(hoehe);
	
	volumen = breite * tiefe * hoehe;
	
	volumen = Math.round(volumen*100)/100;
	
	if(volumen > 0) {
		$("#volumen").text(volumen+'m³');
		
		$("#cover3").fadeOut('slow', function() {
			$("#cover3").css({'display-style' : 'none'});
		});
		
		myradio = volumen;
		show_products(myauswahl,mygewerb,myradio,myart);
	} else { $("#volumen").text(''); }
    });
	
function calc (zahl) {
	zahl = zahl.replace(/,/, ".");
	zahl = parseFloat(zahl);
	return zahl;
}

});

