/**
 * @author Christoph Gentsch / Ironshark Entertainment
 */
 
 calc_instance = 0;
 
 function Calculator() {
 	
 	this.std_keys = [8,9,35,36,37,38,39,40,45,46]; //bsp,tab,end,home,left,up,down,right,ins,del
 	this.num_keys = [48,49,50,51,52,53,54,55,56,57,96,97,98,99,100,101,102,103,104,105]; //0-9 std & numpad
 	this.add_keys = [110,188,190]; //, .
 	this.prodarray = [];
 	this.dims = {};
 	
 	this.init = function(prodarray) {
 		calc_instance = this;
 		
 		this.prodarray = prodarray;
		
		$("#calc input").keydown( function(e) {
				var keys = calc_instance.std_keys.concat(calc_instance.num_keys,calc_instance.add_keys);
				if($.inArray(e.which,keys)!=-1) {
					return this;
				}
				else
					return false;
		});
		
		$("#calc input").focus( function(e) {
			if($(this).attr("value")=="0") {
				$(this).attr("value","");
			}
		});
		
		$("#calc input").keyup( function(e) {
			calc_instance.calc();
		});
		
		if(this.prodarray.ep[0]=="23")
 			$("#e23").show();
 		else
 			$("#e23").hide();
		
		//IE fix
		if(typeof(this.prodarray.ep) == 'object' && this.prodarray.ep[0]) {
 			$("#ep")[0].innerHTML = $("#ep")[0].innerHTML + "&nbsp;" + Configurator.products.ep.products[this.prodarray.ep[0]].name;
 			$("#ep2")[0].innerHTML = $("#ep2")[0].innerHTML + "&nbsp;" + Configurator.products.ep.products[this.prodarray.ep[0]].name;
 		}
		//old
		//$("#ep")[0].innerHTML = $("#ep")[0].innerHTML + "&nbsp;" + Configurator.products.ep.products[this.prodarray.ep[0]].name;

		if (this.prodarray.ep[0]=='te')
		{
			$("#calcResults tr span").each( function(){
				if($(this).attr("dataV")!=null)
				{
					$(this).attr("dataV",'1.6');
					$(this).text($(this).attr("dataV"));
					$('#elems').text('( 1.6 Elemente je m² )')
				}
				else
				{
					if($(this).attr("data")!=null)
					{
						//$(this).text(Math.ceil($(this).attr("data")*flaecheVoll));
					}
				}
			});
		}
		else
		if (this.prodarray.ep[0]=='se')
		{
			$("#calcResults tr span").each( function(){
				if($(this).attr("dataV")!=null)
				{
					$(this).attr("dataV",'9');
					$(this).text($(this).attr("dataV"));
					$('#elems').text('( 9 Elemente je m² )')
				}
				else
				{
					if($(this).attr("data")!=null)
					{
						//$(this).text(Math.ceil($(this).attr("data")*flaecheVoll));
					}
				}
			});
			$('#bracket').hide();
			$('#bolt').hide();
			$('#glue').hide();
			$('#klammern').hide();
			$('#schrauben').hide();
			$('.se').hide();
		}
		else
		{
					$('#klammern').hide();
					$('#schrauben')[0].innerHTML = 'Benutzen Sie 3,9 x 19 mm FERMACELL Schnellbauschrauben, um FERMACELL Estrich-Elemente 20 mm miteinander zu verschrauben.';
		}
 		//nur bei holzbalkendecke und mit trittschalldämmung zeigen
		if(conf_instance.getOptInStep("050")=="01" && conf_instance.getOptInStep("010")=="02") {
 			$(".waben").show();
 		}
 		else
 			$(".waben").hide();
 		
 		if(this.prodarray.zp==undefined)
 			$(".hidden").hide();
 		else {
 			$(".hidden").show();
 		
 			if( this.prodarray.zp=="ge" ) {
 				$("#schutttext").text("FERMACELL Gebundene Schüttung");
 			}
 			
 		}
 			
 		//links setzen
 		
 		$("#back").click( function() {
 			conf_instance.showPage(4);
 		});
 		
 		$("#new").click( function() {
 			Boxy.ask("Bisherige Auswahl verwerfen?", ["OK", "Abbrechen"], function(val) { calc_instance.boxyHandler(val);}, {title: "Aktion bestätigen"});
 		});
 		
 		$("#print").click( function() {
 			window.print()
 		});
 		
 		$("#contact").click( function() {
 			conf_instance.showPage(6);
 		});
 	}
 	
 	this.boxyHandler = function(val) {
 		if(val=="OK") {
 			conf_instance.init();
 		}
 	};
 	
 	this.calc = function() {
 		var a = parseFloat($("#iArea").attr("value").replace(",","."));
 		var c = parseFloat($("#iHeight").attr("value").replace(",","."));
 		this.dims = { area: a, height: c }; 
 		var flaecheVoll = a; var flaecheVerschnitt = flaecheVoll + (flaecheVoll*0.1);
 		
 		parseFloat($("#calcResults tr span").text())*2
 		$("#calcResults tr span").each( function(){
 			if($(this).attr("dataV")!=null)
			{
 				$(this).text(Math.ceil($(this).attr("dataV")*flaecheVerschnitt));
			}
 			else
			{
 				if($(this).attr("data")!=null)
				{
					$(this).text(Math.ceil($(this).attr("data")*flaecheVoll));
				}
			}
 		});
 		
 		var schutt = Math.ceil($("#schutt").text()*c)
 		
 		if(this.prodarray.zp!=undefined) {
 			if( this.prodarray.zp=="au" ) {
 				schutt = Math.ceil(schutt/50);
 			}
 			else {
 				schutt = Math.ceil(schutt/70);
 			}
 		}
 		$("#schutt").text( schutt );
 		
 		$("#calcResults .screws").each( function() {
 			$(this).text( Math.ceil($(this).text()/250)*250 );
 		});
 	};
 }
 

