/**
 * @author Christoph Gentsch / Ironshark Entertainment 2008
 */
 
 //globale variable, workaround für callbacks
 var conf_instance;
 
 
 //TODO: debug-funktion!
 function shortcut() {
 	conf_instance.config = ["opt_000_01", "opt_010_03", "opt_020_01", "opt_021_02", "opt_030_02", "opt_040_02", "opt_050_02", "opt_060_00", "opt_070_03", "opt_080_02", "opt_090_02"]
 	conf_instance.page= 4;
 	conf_instance.mystep = "100";
 	conf_instance.updateAll();
 	conf_instance.showPage(4);
 }
 
 //main class
 function Configurator() {
 
	this.init = function() {
		conf_instance = this;
		
		this.config = [];
		this.page = 1;
		this.mystep;
		this.maxpage = 1;
		this.prodarray = {};
		this.hiddenSteps = [];
		this.widgetClicked = "";
		this.pageReadyCount = 0;
		this.calc = 0;
		
		$("body > div").append("<div id='preload' style='display:none'></div>")
		$("body > div").append("<div id='infoBox' class='infoBox' style='display:none'></div>")
		
		for(var i=1;i<7;i++) {
			$("#preload").append("<div id='ppage"+i+"'></div>");
			$("#ppage"+i).load(location.pathname+"content/page"+i+".htm", null, this.pageReady );
		}
		
		$("#conPreview").empty();
	};
 
	this.pageReady = function() {
		conf_instance.pageReadyCount++;
		if(conf_instance.pageReadyCount==6)
			conf_instance.showPage(1);
	};
	
	this.showPage = function(num) {
		this.page = num;
		$("#conConfig").html( $("#ppage"+num).html() );
		switch(num) {
			case 1:
			case 2:
			case 3:
				this.setupPage()
				break;
			case 4:
				this.setupProductPage();
				break;
			case 5:
				this.setupCalcPage();
				break;
			case 6:
				this.setupContactPage();
				break;
		}
	};
	 
	this.setupContactPage = function() {
		this.contact = new Contact();
		this.contact.init();
	}
	
	this.setupCalcPage = function() {
		$("#pagination").hide();
		this.calc = new Calculator();
		this.calc.init(this.prodarray);
	}
	
	this.setupPage = function() {
	 
	 	//"nächste Seite.." verstecken
	 	$("#pagination").hide();
	 	$("#lknextStep").text("> Nächster Schritt");
	 	
	 	//alle steps verstecken
	 	$(".stepRow").hide();
	 	
		conf_instance.updateBreadCrumbs();
		
	 	//wurde die seite schon (teilweise) konfiguriert?
	 	if( conf_instance.isPageTouched(conf_instance.page) ) {
	 		//ja: alte konfig laden
	 		conf_instance.loadConfig();
	 		conf_instance.gotoNextStep();
	 		if(conf_instance.getConfigPos("060")!=-1) 
				conf_instance.prepareStep6();
	 	}	
	 	else {
	 		//nein: ersten step der page aktivieren
	 		conf_instance.setPageDefaultStep();
	 		conf_instance.maxpage = conf_instance.page;
	 	}
	 	
	 	conf_instance.applyMouseOver();
	 	
	 	//clickhandler für alle opts registrieren
		$("#conConfig .stepRow img").each(
			function() { $(this).click( function() { conf_instance.clickEventHandler(this) } ) }
		);
		var nextpage = conf_instance.page+1;
		$("#pagination").unbind(); 
		$("#pagination").click( function() { conf_instance.showPage( nextpage ) } );
		
		if(conf_instance.page==1) {
			conf_instance.addToHS("021");
		}
		else if(conf_instance.page==3) {
			conf_instance.addToHS("091");
		}
		
	};
	
	this.setupProductPage = function() {
		var prodarray = conf_instance.prodarray;
		$(".productRow").hide(); $("#lknextStep").text("> Berechnen");
		
		$("#pagination").unbind(); 
		$("#pagination").click( function() { conf_instance.showPage( 5 ) } );
		$("#pagination").fadeIn();
		
		
		//fbhz
		if(conf_instance.getOptInStep("080")!=-1 && conf_instance.getOptInStep("080")!="00") {
			
			var fbhzMap = {"01":"ww","02":"el"};
			var fbhz = fbhzMap[conf_instance.getOptInStep("080")];
			
			$("#fbhz .headline div").text(Configurator.products.fbhz.products[fbhz].name);
			var img = Configurator.products.fbhz.products[fbhz].icon;
			if(img=="") { $("#fbhz .imageLeft").remove(); }
			else { $("#fbhz .imageLeft").attr("src","images/planer/select/"+img); }
			$("#fbhzText1").html(Configurator.products.fbhz.products[fbhz].text1);
			$("#fbhz .link_datasheet")[0].href=Configurator.products.fbhz.products[fbhz].link_datasheet;
				
			$("#fbhz").fadeIn();
		}
		
		//dp
		if(conf_instance.prodarray.dp.length>0 && conf_instance.prodarray.dp!="xx") {
			conf_instance.updateProductPage("dp");
			
			$(conf_instance.prodarray.adp).each ( function() {
				$("#dp select").append("<option>"+Configurator.products.dp.products[this].name+"</option>");
			});
				
			if(conf_instance.prodarray.adp.length>0) {
				$("#dp select").change(function () {
					if($("#dp select :selected")[0].index>0) {
						var test = conf_instance.prodarray.adp[$("#dp select :selected")[0].index-1];
						conf_instance.prodarray.dp = [test];
						conf_instance.updateProductPage("dp");
					}
				});
			}
			else
				$("#dp select").hide();
	
			$("#dp").fadeIn("slow");
		}
		//////////
		
		//ep		
		if( conf_instance.prodarray.ep.length>0 ) {
			conf_instance.updateProductPage("ep");
		
			conf_instance.prodarray.aep = conf_instance.prodarray.aep.concat(conf_instance.prodarray.ep).unique();
			$(conf_instance.prodarray.aep).each ( function() {
				$("#ep select").append("<option>"+Configurator.products.ep.products[this].name+"</option>");
			});
		
			if( conf_instance.prodarray.aep.length>0) {
				$("#ep select").change(function () {
					if($("#ep select :selected")[0].index>0) {
						var temp = conf_instance.prodarray.aep[$("#ep select :selected")[0].index-1];
						conf_instance.prodarray.ep = [temp];
						conf_instance.updateProductPage("ep");
					}
				});
			}
			else
				$("#ep select").hide();	
			
			$("#ep").fadeIn("slow");
		}
		else
			$("#ep").hide();
		//////////
		
		//zp
		if(conf_instance.prodarray.zp!=undefined) {
			$("#zp .headline div").text(Configurator.products.zp.products[prodarray.zp].name);
			var img = Configurator.products.zp.products[prodarray.zp].icon;
			if(img=="") { $("#zp .imageLeft").remove(); }
			else { $("#zp .imageLeft").attr("src","images/planer/select/"+img); }
			$("#zpText1").html(Configurator.products.zp.products[prodarray.zp].text1);
			$("#zp .link_datasheet")[0].href=Configurator.products.zp.products[prodarray.zp].link_datasheet;
			
			$("#zp").fadeIn("slow");
		}			
		
		conf_instance.maxpage = conf_instance.page;
		conf_instance.updateBreadCrumbs();
		
		//make preview interactive
				
		if(conf_instance.prodarray.zp!=undefined) {
			$("#prevStep020").mouseover( function() {
				$("#zp").addClass("active");
				var msg = Configurator.products.zp.products[conf_instance.prodarray.zp].name;
				var off = $(this).offset();
				conf_instance.showInfoBox(msg, off.left+$("#prevStep020 img")[0].width-40, off.top); //$("#prevStep020 img")[0].x+$("#prevStep020 img")[0].width-40, $("#prevStep020 img")[0].y)
			}).mouseout( function(){ 
				$("#zp").removeClass("active"); 
				conf_instance.hideInfoBox();
			});
		}
		
		if(conf_instance.prodarray.dp.length>0 && conf_instance.prodarray.dp!="xx") {
			$("#prevStep030").mouseover( function() {
				$("#dp").addClass("active");
				var msg = Configurator.products.dp.products[conf_instance.prodarray.dp[0]].name;
				var off = $(this).offset();
				conf_instance.showInfoBox(msg, off.left+$("#prevStep060 img")[0].width-40, off.top)
			}).mouseout( function(){ 
				$("#dp").removeClass("active"); 
				conf_instance.hideInfoBox();
			});
		}
		
		$("#prevStep060").mouseover( function() {
			$("#ep").addClass("active");
			var msg = Configurator.products.ep.products[conf_instance.prodarray.ep[0]].name;
			var off = $(this).offset();
			conf_instance.showInfoBox(msg, off.left+$("#prevStep060 img")[0].width-40, off.top)
		}).mouseout( function(){ 
			$("#ep").removeClass("active"); 
			conf_instance.hideInfoBox();
		});
			
		if(conf_instance.getOptInStep("080")!=-1 && conf_instance.getOptInStep("080")!="00") {
			$("#prevStep080").mouseover( function() {
				$("#fbhz").addClass("active");
				var fbhzMap = {"01":"ww","02":"el"};
				var fbhz = fbhzMap[conf_instance.getOptInStep("080")];
				var msg = Configurator.products.fbhz.products[fbhz].name;
				var off = $(this).offset();
				conf_instance.showInfoBox(msg, off.left+$("#prevStep080 img")[0].width-40, off.top);
			}).mouseout( function(){ 
				$("#fbhz").removeClass("active"); 
				conf_instance.hideInfoBox();
			});
		}
		
		//viceversa
		if(conf_instance.prodarray.dp.length>0 && conf_instance.prodarray.dp!="xx") {
			$("#dp").mouseover( function() {
				$("#dp").addClass("active");
				var msg = Configurator.products.dp.products[conf_instance.prodarray.dp[0]].name;
				var off = $("#prevStep030 img").offset();
				if (off!=null)
				{
					conf_instance.showInfoBox(msg, off.left+238-40, off.top)
				}
			}).mouseout( function(){ 
				$("#dp").removeClass("active");
				conf_instance.hideInfoBox();
			});
		}
		
		if( conf_instance.prodarray.ep.length>0 ) {
			$("#ep").mouseover( function() {
				$("#ep").addClass("active");
				var msg = Configurator.products.ep.products[conf_instance.prodarray.ep[0]].name;
				var off = $("#prevStep060 img").offset();
				conf_instance.showInfoBox(msg, off.left+$("#prevStep060 img")[0].width-40, off.top)
			}).mouseout( function(){ 
				$("#ep").removeClass("active"); 
				conf_instance.hideInfoBox();
			});
		}
		
		if(conf_instance.prodarray.zp!=undefined) {
			$("#zp").mouseover( function() {
				$("#zp").addClass("active");
				var msg = Configurator.products.zp.products[conf_instance.prodarray.zp].name;
				var off = $("#prevStep020 img").offset();
				conf_instance.showInfoBox(msg, off.left+$("#prevStep020 img")[0].width-40, off.top)
			}).mouseout( function(){ 
				$("#zp").removeClass("active");
				conf_instance.hideInfoBox();
			});
		}
		
		if(conf_instance.getOptInStep("080")!=-1 && conf_instance.getOptInStep("080")!="00") {
			$("#fbhz").mouseover( function() {
				$("#fbhz").addClass("active");
				var fbhzMap = {"01":"ww","02":"el"};
				var fbhz = fbhzMap[conf_instance.getOptInStep("080")];
				var msg = Configurator.products.fbhz.products[fbhz].name;
				var off = $("#prevStep080 img").offset();
				conf_instance.showInfoBox(msg, off.left+$("#prevStep080 img")[0].width-40, off.top)
			}).mouseout( function(){ 
				$("#fbhz").removeClass("active"); 
				conf_instance.hideInfoBox();
			});
		}
	};
	
	this.updateProductPage = function(what) {
		var prodarray = conf_instance.prodarray;
		switch(what) {
			case "dp":
				$("#dp .headline div").text(Configurator.products.dp.products[prodarray.dp[0]].name);
				var img = Configurator.products.dp.products[prodarray.dp[0]].icon;
				if(img=="") { $("#dp .imageLeft").remove(); }
				else { $("#dp .imageLeft").attr("src","images/planer/select/"+img); }
				$("#dpText1").html(Configurator.products.dp.products[prodarray.dp[0]].text1);
				$("#dp .link_datasheet")[0].href=Configurator.products.dp.products[prodarray.dp[0]].link_datasheet;
				
				var img_map = { "ho":"3", "xp":"2", "ep":"1", "so":"5", "mw":"4" ,"au":"5","ge":"5"};
				var size_map = { "01":"01","02":"05","03":"16" };
				var size = size_map[conf_instance.getOptInStep("030")];
				$("#prevStep030").css("background-image","url(images/planer/preview/step/C"+img_map[prodarray.dp[0]]+size+".gif)");
				break;
				
			case "ep":
				$("#ep .headline div").text(Configurator.products.ep.products[prodarray.ep[0]].name);
				var img = Configurator.products.ep.products[prodarray.ep[0]].icon;
				if( img=="" ) { $("#ep .imageLeft").remove(); }
				else { $("#ep .imageLeft").attr("src","images/planer/select/"+img); }
				$("#epText1").html(Configurator.products.ep.products[prodarray.ep[0]].text1);
				$("#ep .link_datasheet")[0].href=Configurator.products.ep.products[prodarray.ep[0]].link_datasheet;
				if (prodarray.ep[0]=='22' || prodarray.ep[0]=='se' || prodarray.ep[0]=='te')
				{
					$("#ep .link_insulant")[0].href=Configurator.products.ep.products[prodarray.ep[0]].link_insulant;
					$("#ep .li_insulant").show();
				}
				else
				{
					$("#ep .li_insulant").hide();
				}
				var img = Configurator.products.ep.products[prodarray.ep[0]].prevImg;
				$("#prevStep060 img")[0].src = "images/planer/preview/step/"+img;
				break;
		}
	};
	///////////////////////
	this.clickEventHandler = function( widget ) {
		 
		var type = widget.id.substr(0,3);
		
		switch(type) {
			case "opt":
				this.widgetClicked = widget.id;
				var newstep = widget.id.split("_")[1];
				
				//letzten step bestimmen
				var tempStep = $("#stepRow"+newstep).attr("next");
			
				if( $.inArray(tempStep,this.hiddenSteps)!=-1 ) { 
					tempStep = $("#stepRow"+tempStep).attr("next");
				}
				var isLastStep = tempStep == this.mystep;
			
				//nutzer hat auf aktuellen step geklickt
				//verarbeitung normal 
				if(newstep==this.mystep) { 
					this.saveSelection();
				}
				//nutzer ist mehrere steps zurück gegangen -> alles bis hier löschen, vorher fragen
				else if(newstep<this.mystep && !isLastStep) {
					Boxy.ask("Bisherige Auswahl verwerfen?", ["OK", "Abbrechen"], function(val) { conf_instance.saveOverSelection(val);}, {title: "Aktion bestätigen"}); 
				}
				else { //nutzer ist EINEN step zurückgegangen -> nicht fragen
					this.saveOverSelection("OK");
				}
				break;
		};
	};
	
	//verwirft alte config und speichert neue auswahl
	this.saveOverSelection = function(val) {
		if(val=="OK") {
			var opt = this.widgetClicked.split("_");;
			var newstep = opt[1]; 
			
			$(".selectList img").removeClass("active"); //alle opts abwählen
						
			var pos = this.getConfigPos( newstep );
						
			this.config = this.config.slice(0,pos); //alles bis hier aus konfig raushauen
			this.pushConfig(this.widgetClicked); //aktuelle opt in konfig pushen
			this.loadConfig(); //alle in der konfig verbliebenen opts wieder anwählen
						
			$("#stepRow"+newstep+" img.active").removeClass("active"); //alle steps abwählen
			$("#"+this.widgetClicked).addClass("active"); //aktuelle opt anwählen
						
			//nächsten step highlighten
			this.mystep = newstep;
			this.maxpage = this.page;
			this.updateBreadCrumbs();
			
			this.updateAll();
		}
	}
	
	//fügt neue auswahl an bestehende config an
	this.saveSelection = function() {
		var opt = this.widgetClicked.split("_");
		var newstep = opt[1]; 
		
		$("#stepRow"+newstep+" img.active").removeClass("active");
		$("#"+this.widgetClicked).addClass("active");
						
		this.pushConfig(this.widgetClicked);
		
		this.updateAll();
	}
	
	this.updateAll = function() {
		this.updateProductRec();
		this.updateAddProdRec();	
		this.gotoNextStep();
		this.updatePresenter();
		this.updateOverview();
		this.updateMouseOver();
	}
	
	this.applyMouseOver = function() {
		//steps
		$(".planerFrame .stepRow").each( function() {
			var thisStep = this.id.substr(7,3);
			
			if( Configurator.mouseOver[thisStep].hint!="" ) {
				$("#"+this.id+" .infoIcon").mouseover( function() {
					var step = this.parentNode.parentNode.parentNode.id.substr(7,3);
					var off = $(this).offset();
					conf_instance.showInfoBox(Configurator.mouseOver[step].hint,off.left+20,off.top)
				});
			}
			else {
				$("#"+this.id+" .infoIcon").hide();
			}
			
			$("#"+this.id+" .infoIcon").mouseout( function() {
				conf_instance.hideInfoBox();
			});
			
			$("#"+this.id+" .selectList img").each( function() {
				$(this).mouseover( function() {
					var thisOpt = this.id.split("_")[2]; var thisStep = this.id.split("_")[1];
					var off = $(this).offset();
					conf_instance.showInfoBox(Configurator.mouseOver[thisStep].opts[thisOpt].name,off.left-10,off.top+60);
				});
				$(this).mouseout( function() {
					conf_instance.hideInfoBox();
				});
			});			
		});
		
		this.updateMouseOver();
	}
	
	this.updateMouseOver = function() {
		//overview
		$(".config ul img").each( function() {
			$(this).mouseover( function() {
				var thisStep = this.id.substr(8,3);
				var thisOpt = conf_instance.getOptInStep(thisStep); 
				var off = $(this).offset();
				conf_instance.showInfoBox(Configurator.mouseOver[thisStep].opts[thisOpt].name, off.left-10, off.top+60);
			});
			$(this).mouseout( function() {
				conf_instance.hideInfoBox();
			});
		});
	}
	
	this.updateBreadCrumbs = function() {
		//aktuellen breadcrumb markieren
	 	$(".breadCrumb a.active").removeClass("active");
	 	$("#bcpage"+this.page).addClass("active");
	 	
	 	//breadcrumbs von vorgängerseiten aktivieren
	 	for(var i=1;i<5;i++) {
	 		$("#bcpage"+i).unbind();
	 		$("#bcpage"+i).css("cursor","pointer");
	 	}

	 	$("#bcpage"+1).click( function() {conf_instance.showPage(1)});
	 	$("#bcpage"+2).click( function() {conf_instance.showPage(2)});
	 	$("#bcpage"+3).click( function() {conf_instance.showPage(3)});
	 	$("#bcpage"+4).click( function() {conf_instance.showPage(4)}); 
	 	
	 	for(var i=this.maxpage+1;i<5;i++){
	 		$("#bcpage"+i).unbind();
	 		$("#bcpage"+i).css("cursor","default");
	 	}
	};
	
	this.showInfoBox = function(msg,x,y) {
		$("#infoBox").html(msg);//text(msg);
		$("#infoBox").css("top",y); $("#infoBox").css("left",x);
		$("#infoBox").show();
	};
	this.hideInfoBox = function() {
		$("#infoBox").hide();
	};
	
	//wurde eine seite schon bearbeitet?
	this.isPageTouched = function( page ) {
		switch(page) {
			case 1:
				if( this.getConfigPos("000")!=-1 )
					return true;
				break;
			case 2:
				if( this.getConfigPos("030")!=-1 )
					return true;
				break;
			case 3:
				if( this.getConfigPos("070")!=-1 )
					return true;
				break;
			case 4:
				break;
		}	
		return false;
	};
	
	this.setPageDefaultStep = function() {
		switch(this.page) {
			case 1:
				this.mystep = "000";
				break;
			case 2:
				this.mystep = "030";
				break;
			case 3:
				this.mystep = "070";
				break;
		}
		$("#conConfig div.stepRow").removeClass("active");
		$("#stepRow"+this.mystep).addClass("active");
		$("#stepRow"+this.mystep).fadeIn();
	};
	
	this.updateOverview = function() {
		$(".previewList").empty();
		for(var i=0;i<conf_instance.config.length;i++) {
			var thisOpt = conf_instance.config[i]; var thisStep = thisOpt.split("_")[1]; var thisO = thisOpt.split("_")[2];
			var title=Configurator.mouseOver[thisStep].opts[thisO].name;
			$(".previewList").append('<li><a><img alt="'+title+'" class="" id="prevConf'+thisStep+'" src="images/planer/select/'+thisOpt+'.gif"/></a></li>');
			
			var page = conf_instance.getPageForStep(thisStep);
			switch(page) {
				case 1: $("#prevConf"+thisStep).click( function(){ 
							conf_instance.showPage(1); var step = this.id.substr(8,3);
							if(conf_instance.maxpage!=1)
								$("#stepRow"+step).addClass("active"); 
						} ); break;
				case 2: $("#prevConf"+thisStep).click( function(){ 
							conf_instance.showPage(2); var step = this.id.substr(8,3);
							if(conf_instance.maxpage!=2)
								$("#stepRow"+step).addClass("active"); 
						} ); break;
				case 3: $("#prevConf"+thisStep).click( function(){ 
							conf_instance.showPage(3); var step = this.id.substr(8,3);
							if(conf_instance.maxpage!=3)
								$("#stepRow"+step).addClass("active"); 
						} ); break;
			}
		}
	};
	
	this.gotoNextStep = function() {
	
		if(this.getConfigPos("020")!=-1) {
			if(this.getOptInStep("020")!="00") {
				this.removeFromHS("021");
			}
			else
				this.addToHS("021");
		}
		else {
			this.addToHS("021");
		}
		
		if(this.getConfigPos("090")!=-1) {
			if(this.getOptInStep("090")=="07")
				this.removeFromHS("091");
			else
				this.addToHS("091");
		}
		else {
			this.addToHS("091");
		}
				
		$(".hinweis").hide();
		
		//Wenn Anwendungsbereich 1+2 und Brandschutz F60 - F90 gewählt wurde kann nicht mehr Feuchtebereich A02 oder A2 oder C gewählt werden, Hinweis wird ausgespielt						
		if( (conf_instance.getOptInStep("000")=="00" || conf_instance.getOptInStep("000")=="01") && (conf_instance.getOptInStep("040")=="02" || conf_instance.getOptInStep("040")=="03")) {
			$("#opt_070_02").hide();
			$("#opt_070_03").hide();
			$("#opt_070_04").hide();
			
			$("#feuchttext").show();
		}
		//Wenn Anwendungsbereich 1+2 und Brandschutz F30 gewählöt wurde kann nicht mehr Feuchtebereich A2 und C gewählt werden, Hinwesi wird ausgespielt							
		else if( (conf_instance.getOptInStep("000")=="00" || conf_instance.getOptInStep("000")=="01") && (conf_instance.getOptInStep("040")=="01")) {
			$("#opt_070_03").hide();
			$("#opt_070_04").hide();
			
			$("#feuchttext").show();
		}
		//Wenn Anwendungsbereich 3+4 und Brandschutz F30 - F90 gewählt wurde kann nicht mehr Feuchtebereich A02 oder A2 oder C gewählt werden, Hinweis wird ausgespielt
		//Wenn Anwendungsbereich 3+4 und Brandschutz F30 - F90 gewählt wurde kann keine ElektroFBHZ gewählt werden, Hinweis wird ausgespielt
		else if( (conf_instance.getOptInStep("000")=="02" || conf_instance.getOptInStep("000")=="03") && (conf_instance.getOptInStep("040")!="00")) {
			$("#opt_070_02").hide();
			$("#opt_070_03").hide();
			$("#opt_070_04").hide();
			
			$("#opt_080_02").hide();
			
			$("#feuchttext").show();
			$("#heiztext").show();
		} else {
		    $("#opt_070_02").show();
			$("#opt_070_03").show();
			$("#opt_070_04").show();
			
			$("#opt_080_02").show();
			
			$("#feuchttext").hide();
			$("#heiztext").hide();
		}
		
		//Wenn Brandschutz gewählt wurde, können keine großen Fliesen mehr ausgewähtl werden, Hinweis wird ausgespielt
		// --> aktuell ist das eine Sackgasse, wir empfehlen daher immer TE (außer wenn Brandschutz gewählt wurde, dann weiterhin Sackgasse) vorschlagen
		if(conf_instance.page==3 && conf_instance.getOptInStep("040")!="00") {
			$("#opt_091_04").hide();
			$("#fliestext").show();
		} else {
		    $("#opt_091_04").show();
			$("#fliestext").hide();
		}
		
		//Wenn Wasserbodenheizung gewählt wurde, muss 2E22 ausgespielt werden
		if(conf_instance.page==3 && conf_instance.getOptInStep("080")=="01") {
			$("#opt_091_04").hide();
			$("#fliestext").show();
		}
		
		if(this.prodarray.dp!=null && this.prodarray.dp[0]=="xx") {
			this.addToHS("060");
		}
		else {
			this.removeFromHS("060");
		}
		
		if( !this.isPageTouched(this.page+1) ) {
			
			var next = $("#stepRow"+this.mystep).attr("next");
			
			if( $.inArray(next,this.hiddenSteps)!=-1 ) { 
				next = $("#stepRow"+next).attr("next");
			}
			
			if(this.getPageForStep(next)==this.page && $.inArray(next,this.hiddenSteps)==-1) {						
				$("#conConfig div.stepRow").removeClass("active");
									
				$("#stepRow"+next).addClass("active");
				
				$("#stepRow"+next).fadeIn();
				
				$("#pagination").hide();
			}
			else {
				$("#pagination").fadeIn();
			}
			
			this.updateHiddenSteps();
			
			this.mystep = next;
		}
		else {
			this.updateHiddenSteps();
			$("#pagination").fadeIn();
		}
	};
	
	this.updateAddProdRec = function() {
		if( this.getConfigPos("021")==-1 ) return;
		
		var opt = this.getOptInStep("021");
		
		switch(opt) {
			case "00":
				this.prodarray.zp = "ni"; break;
			case "01":
			case "02":
				this.prodarray.zp = "au"; break;
			case "03":
				if(this.prodarray.ep!=null) {
					if($.inArray("te",this.prodarray.ep)>-1 || $.inArray("se",this.prodarray.ep)>-1) //te oder se dabei?
						this.prodarray.zp = "ge";
					else
						this.prodarray.zp = "au";
				}
				break;
			case "04":
				if(this.getOptInStep("000")=="00")
						this.prodarray.zp = "au";
				else
					this.prodarray.zp = "ge";
				break;
			case "05":
				this.prodarray.zp = "ge";
				break;
		}
		
	};
	
	this.updateProductRec = function() {
	
		if( this.getConfigPos("040")==-1 ) return; //wenn step 4 noch nicht erreicht, abbrechen
		
		var anw = -1; var hoehe = -1; var brand = -1; var fliese = -1; var flieseTyp = -1;
			
		if(this.getConfigPos("000")!=-1) anw = this.getOptInStep("000");
		if(this.getConfigPos("030")!=-1) hoehe  = this.getOptInStep("030");
		if(this.getConfigPos("040")!=-1) brand = this.getOptInStep("040");
		if(this.getConfigPos("090")!=-1) fliese = this.getOptInStep("090");
		if(this.getConfigPos("091")!=-1) flieseTyp = this.getOptInStep("091");
		
		var anw_map = {"00":"01","01":"02","02":"03","03":"04"}; anw = anw_map[anw];
		var hoehe_map = {"00":"00","01":"10","02":"20","03":"60"}; hoehe = hoehe_map[hoehe];
		var brand_map = {"00":"xx","01":"F30","02":"F60","03":"F90"};
		
		//wenn brand egal, dann ignoriere config, sonst mappen
		if( getMembers(Configurator.configTree.anwend[anw].hoehe[hoehe].brand).length == 1 ) //=brand egal
			brand = "oo";
		else
			brand = brand_map[brand];
		
		//wenn fliesen egal, dann ignoriere config, sonst mappen
		if( getMembers(Configurator.configTree.anwend[anw].hoehe[hoehe].brand[brand].fliese).length == 1 ) //=fliesen egal
				fliese = "oo";
		else if(fliese=="07" && flieseTyp!="04") fliese="S"; //kleine fliesen
		else if(fliese=="07" && flieseTyp=="04") fliese = "XL"; //ergo: große fliesen gewählt
		else fliese = "xx"; //keine fliesen gewählt, default; 
		
		this.prodarray = dCopy(Configurator.configTree.anwend[anw].hoehe[hoehe].brand[brand].fliese[fliese]);
		 
		//ersetze oo (auswahl/beliebig) mit den entsprechenden produkten
		if($.inArray("oo",this.prodarray.dp)!=-1) {
			this.prodarray.dp = ["ho","xp","ep","so","mw"];
		}
		
		//Bei Holzbalkendecke und Schallschutz:	EPS, XPS nicht auswählbar
		if( this.getOptInStep("010")=="02" && this.getOptInStep("050")!="00" ) {
			this.prodarray.dp = this.prodarray.dp.diff(["xp","ep"]);
			$("#noepstext").show();
		}
		else
			$("#noepstext").hide();
			
		//wenn Warmwasserbodenheizung gewählt darf kein 2e11 gewählt werden
		if( this.getOptInStep("080")!=-1 && this.getOptInStep("080")!="00" ) {
			this.prodarray.ep = this.prodarray.ep.diff(["11"]);
			this.prodarray.aep = this.prodarray.aep.diff(["11"]);
			
			if(this.prodarray.ep.length==0) {
				this.prodarray.ep[0] = this.prodarray.aep[0];
			}
		}

		//wenn heizung gewählt darf kein 2e31, 2e33 gewählt werden
		if( this.getOptInStep("080")!=-1 && this.getOptInStep("080")!="00" && (this.getOptInStep("080")!="02" || this.getOptInStep("080")!="01")) {
			this.prodarray.ep = this.prodarray.ep.diff(["31","33"]);
			this.prodarray.aep = this.prodarray.aep.diff(["31","33"]);
			
			if(this.prodarray.ep.length==0) {
				this.prodarray.ep[0] = this.prodarray.aep[0];
			}
		}
		
		//Wenn Wasserbodenheizung gewählt wurde, muss 2E22 ausgespielt werden
		if(conf_instance.page==3 && conf_instance.getOptInStep("080")=="01") {
			this.prodarray.ep = ["22","te"];
			this.prodarray.aep = ["22","te"];
			//$("#fliestext").show();
		}
		// Wenn man einen Boden auf der Holzbalkendecke mit einem separaten Dämmstoff
		// (z.B. 20mm Holzweichfaser bzw. MF) anklickt, dann 2E22 ausspielen
		if( this.getOptInStep("010")!=-1 && this.getOptInStep("010")=="02" &&
				this.getOptInStep("030")!=-1 && this.getOptInStep("080")!="00" ) {
			this.prodarray.ep = this.prodarray.ep.diff(["11"]);
			this.prodarray.aep = this.prodarray.aep.diff(["11"]);
			
			if(this.prodarray.ep.length==0) {
				this.prodarray.ep[0] = this.prodarray.aep[0];
			}
		}
		/*
		 * Bitte beim Feuchtebereich ein neues Filter einbauen:
			- Für den Bereich 0 bzw. ohne Anforderung Gipsfaser (2E11, 2E22, 2E31) Empfehlen oder SE bei großen Fliesen
			- Bereich A02 nur Auswahl TE oder SE
			- Bereiche A2 und C weitere Wahl nur SE 
		 */
		if( this.getOptInStep("070")!=-1 ) {
			switch(this.getOptInStep("070")) {
				case "00":
				case "01":
					this.prodarray.ep = this.prodarray.ep.intersect(["11", "22", "23", "31", "33", "se"]);
					this.prodarray.aep = this.prodarray.aep.intersect(["11", "22", "23", "31", "33", "se"]);
					break;
					
				case "02":
					this.prodarray.ep = this.prodarray.ep.intersect(["te", "se"]);
					this.prodarray.aep = this.prodarray.aep.intersect(["te", "se"]);
					break;
					
				case "03":
				case "04":
					this.prodarray.ep = this.prodarray.ep.intersect(["se"]);
					this.prodarray.aep = this.prodarray.aep.intersect(["se"]);
					break;
			}
			
			if(this.prodarray.ep.length==0) {
				this.prodarray.ep[0] = this.prodarray.aep[0];
			}
		}
		
		//zum Punkt 5, Elektroheizung + Brandschutz EI30 bzw. EI60 nur mit TE möglich, wie Bedingung 1 und 2
		if( this.getOptInStep("080")!=-1 && this.getOptInStep("080")=="02" && this.getOptInStep("040")!=-1 && this.getOptInStep("040")!="00") {
			this.prodarray.ep = ["te"];//this.prodarray.ep.intersect(["te","se"]);
			this.prodarray.aep = ["te"];//this.prodarray.aep.intersect(["te", "se"]);
			if(this.prodarray.ep.length==0) {
				this.prodarray.ep[0] = this.prodarray.aep[0];
			}
		}
		
		//wenn dämmung manuell gewählt, verschiebe bisherige produkte in die alternative 
		if(this.getConfigPos("060")!=-1 && this.page>2) {
			this.prodarray.adp=this.prodarray.dp;
			var opt_map = {"00":"ep","01":"xp","02":"ho","03":"mw","04":"so"};			
			this.prodarray.dp=[opt_map[this.getOptInStep("060")]];
		}
		else
			this.prodarray.adp=[];
		
// - Generell ist mit grossformatigen Fliessen auf das SE zu verweisen, auch bei Warmwasser-, oder Elektroheizungen.
// Von: Damian.Kilchoer@xella.com [mailto:Damian.Kilchoer@xella.com] 
// Gesendet: Montag, 25. Mai 2009 08:41
// Umsetzung für DE? ---> Hr. Ruiz: "Bitte umsetzen."
		if(this.getConfigPos("091") != -1 && this.getOptInStep("091")=="04") {
			this.prodarray.ep = ['se'];
			this.prodarray.aep = ['se'];
		}


		if(this.page==2 && this.getConfigPos("060")==-1)
			this.prepareStep6();
	};
	
	this.prepareStep6 = function() {
		
		var dplist = this.prodarray.adp.union(this.prodarray.dp);
		
		$("#stepRow060 .selectList img").hide();
			
		var opt_map = {"ep":"00","xp":"01","ho":"02","mw":"03","so":"04"};
				
		if(dplist[0]!="xx" && dplist[0]!="ge" && dplist[0]!="au") {
			this.removeFromHS("060");
			for(var i=0;i<dplist.length;i++) {
				$("#opt_060_"+opt_map[dplist[i]]).fadeIn();
			}
		}
		else {
			this.addToHS("060");
		}
	};
	
	this.updateHiddenSteps = function() {
		for(var k=0;k<this.hiddenSteps.length;k++) {
			$("#stepRow"+this.hiddenSteps[k]).hide();
		}		
	};
	
	this.updatePresenter = function() {
	
		var ug = 0; var typ = 0;
		
		for( var i=0; i<this.config.length; i++) {
			
			var opt = this.config[i].split("_");
			var step = opt[1]; var opt = parseInt(opt[2]); var div=""; 
			
			switch(step) {
				case "000": // Anwendungsbereich
					$("#conPreview").empty();
					var pics = ["0wohnbereich.jpg","0bueroflaeche.jpg","0restaurant.jpg","0kongresssaele.jpg"];
					div = this.getPrevDiv( "000", "step", pics[opt] );
					$("#conPreview").append(div);
					break;
					
				case "010": // Untergrund
					$("#conPreview").empty();
					var pics = ["A110.gif","A110.gif","A210.gif","A310.gif","A410.gif","A510.gif"];
					div = this.getPrevDiv( "010", "step", pics[opt] );
					ug = pics[opt].substr(1,1);
					$("#conPreview").append(div);
					break;
	
				case "020": // Bodenbeschaffenheit
					$("#conPreview").empty();
					var pics = ["A"+ug+"10.gif", "A"+ug+"21.gif", "A"+ug+"31.gif"];
					div = this.getPrevDiv( "020","step", pics[opt] );
					typ = opt+1;
					$("#conPreview").append(div);
					break;
				
				case "021": // Niveauausgleich
					$("#conPreview").empty();
					var pics = ["A"+ug+typ+"1.gif", "A"+ug+typ+"1.gif","A"+ug+typ+"3.gif","A"+ug+typ+"3.gif", "A"+ug+typ+"5.gif", "A"+ug+typ+"5.gif"];
					img = "010_" + this.getOptInStep("010") + "_020_" + this.getOptInStep("020") + "_021_" + this.getOptInStep("021");
					div = this.getPrevDiv( "020","step step021", img + ".png"); //pics[opt] );
					
					px = (opt+1)*4;
					if (px > 20)
					{
						px=20;
					}
					if( this.getOptInStep("010") == "02" && this.getOptInStep("020")!=-1 && this.getOptInStep("020")!="00" &&  this.getOptInStep("021")!=-1 && (this.getOptInStep("021")=="02" || this.getOptInStep("021")=="03" || this.getOptInStep("021")=="04" || this.getOptInStep("021")=="05") && this.getOptInStep("050")=="01") {
							div = this.getPrevDiv( "020","step step021", img + "_00.png"); //pics[opt] );
					}
					$("#conPreview").append(div);
					//$("#prevStep020 img").css("margin-top", String(px)+"px");
					// Lastverteilende Platte
					if( opt!=0 &&  ((typeof this.prodarray.dp!=='undefined' && this.getOptInStep("060")=="03") || ((this.getOptInStep("080")=="01") && this.prodarray.dp == 'xx'))) {
						div = this.getPrevDiv( "031","step", "GFC10.gif" );
						$("#conPreview").prepend(div);
					}
					
					break;
					
				case "030": //höhe
					$("#prevStep030").remove();
					
					if(opt!=0 && (typeof this.prodarray.dp!=='undefined' && this.prodarray.dp[0]!="xx")) {
						//wurde dämmung schon gewählt?
						var pos = this.getConfigPos("060"); var dm = 1;
						if(pos>0) { dm = parseInt(this.config[pos].split("_")[2])+1; }
						var pics = ["C"+dm+"01.gif","C"+dm+"05.gif","C"+dm+"16.gif"];
						var size_map = ["10px","50px","160px"];
						//div = this.getPrevDiv( "030","step", "../null.gif" );
						
						div = this.getPrevDiv( "030","step", pics[opt-1]);
						$("#conPreview").prepend(div);
						//$("#prevStep030").css("background-image","url(images/planer/preview/step/"+pics[opt-1]+")");
						$("#prevStep030").css("height",size_map[opt-1]);
						$("#prevStep030").removeClass("step080");
					}
										
					break;
				case "040": //brandschutz
					if(this.prodarray.dp[0]=="au" || this.prodarray.dp[0]=="ge") {
						var thisopt = this.getOptInStep("030");
						var h = $("#prevStep030").css("height");
						$("#prevStep030").remove();
						var pics = ["C501.gif","C505.gif","C516.gif"]; //für die schüttungen das "sonstiges"-bild nehmen
						div = this.getPrevDiv( "030","step", "../null.gif" );
						$("#conPreview").prepend(div);
						$("#prevStep030").css("height",h);
						$("#prevStep030").css("background-image","url(images/planer/preview/step/"+pics[thisopt-1]+")");
						
						this.drawPrev060();
					}
					else if(this.prodarray.dp[0]=="xx"){
						this.drawPrev060();
					}
					break;
				case "060":
					if(this.prodarray.ep.length > 0 || this.prodarray.aep.length > 0) {
						if(this.getOptInStep("080")!=-1 && this.getOptInStep("080")=="01") {
							div = this.getPrevDiv( "080","step", "E1.gif" );
							$("#conPreview").prepend(div);
						}
						this.drawPrev060()
					}
					
					break;
				case "080": // Fußbodenheizung
					if(opt=="02") { // Elektro
						div = this.getPrevDiv( "080","step", "E2.gif" );
						$("#conPreview").prepend(div);
					}
					break;
				case "090":
					var picnum = parseInt(this.getOptInStep("090").substr(1,1))+1;
					if(picnum!=8)
						div = this.getPrevDiv( "090", "step", "F"+picnum+"0.gif");
					else
						div = this.getPrevDiv( "090", "step", "F81.gif");
					$("#conPreview").prepend(div);
					break;
				case "091":
					var picnum = parseInt(this.getOptInStep("091").substr(1,1))+1;
					div = this.getPrevDiv( "090", "step", "F8"+picnum+".gif");
					$("#prevStep090").replaceWith(div);
					break;
			}	
		};
	};
	
	this.drawPrev060 = function() {
	
	    // Fußbodenheizung
		if(this.getOptInStep("080")=="01") { // Warmwasser
			if(this.getOptInStep("060")==-1 && this.getOptInStep("080")!=-1 || this.prodarray.ep.length==0) {
				div = this.getPrevDiv( "080","step", "E1.gif" );
				$("#conPreview").prepend(div);
			}
		}
	    
		var img = Configurator.products.ep.products[this.prodarray.ep[0]].prevImg;
		div = this.getPrevDiv( "060","step", img );
		$("#conPreview").prepend(div);
		
		//dritte lage anzeigen
		if(this.prodarray.ep[0]=="23") {
			var img = Configurator.products.ep.products[this.prodarray.ep[0]].prevImg;
			div = this.getPrevDiv( "061","step", img );
			$("#conPreview").prepend(div);
			$("#prevStep061").css("height","18px");
		}
	}
	
	//liest aktuelle konfig und wählt entsprechend opts an
	//außerdem werden die entsprechenden steps gezeigt oder verborgen
	this.loadConfig = function() {
		var self = this;
		$(this.config).each( function() { 
			$("#"+this).addClass("active");
		 } );
		$(".stepRow").each( function() {
			if( self.getConfigPos(this["id"].substr(7,3)) != -1 )
				$(this).fadeIn();
			else
				$(this).hide();
		 });
		this.mystep = this.config[this.config.length-1].split("_")[1];
	};
	
	this.getPrevDiv = function( id, stepclass, imgname ) {
		return '<div id="prevStep'+id+'" class="'+stepclass+'"><img alt="" src="images/planer/preview/step/'+imgname+'" title="" /></div>';
	};	
	
	//liefert die position einer opt in der config zu einem best. step
	this.getConfigPos = function( step ) {
		var steps = $(this.config).map( function( i,n ) { return n.split("_")[1] });
		//^nur die bisher durchlaufenen steps aus aktueller konfig herausfiltern
					  
		return $.inArray(step, steps); //aktuelle position im konfig-array ermitteln
	};
	
	//welche opt wurde in step X gewählt
	this.getOptInStep = function( step ) {
		var pos = this.getConfigPos(step);
		if(pos!=-1)
			return this.config[pos].split("_")[2];
		else
			return -1;
	}
	
	this.getPageForStep = function( step ) {
		if( step < "030" ) return 1;
		else if( step < "070" ) return 2;
		else if( step < "100" ) return 3;
		else return 4;
	};
	
	this.removeFromHS = function(elem) {
		this.hiddenSteps=this.hiddenSteps.diff([elem]);
	};
	
	this.addToHS = function(elem) {
		this.hiddenSteps.push(elem);
		this.hiddenSteps=this.hiddenSteps.unique();
	};
	
	this.pushConfig = function(elem) {
		var thisstep = elem.split("_")[1];
		
		var steppos = this.getConfigPos(thisstep);
		if(steppos!=-1) {
			this.removeFromCfg(this.config[steppos]);
		}
		
		this.config.push(elem);
		this.config=this.config.unique();
	};
	
	this.removeFromCfg = function(elem) {
		this.config = this.config.diff(elem);
	};
}

