{
//	var BASE = "http://www.demo.ksm-tempo.de/fileadmin/templates/tools/";
	var BASE = "http://www.kloeckner-stahl-und-metallhandel.de/fileadmin/templates/tools/";
//	var BASE = "http://localhost/kloeckner-tools/tools/";
}
{
	function Calculator(params)
	{
		params = params || {};
	
		var material = jQuery(params.selectId || "#material").val();
		this.toolId = params.toolId || "calculator";
		
		this.baseUrl = BASE + this.toolId + ".php?p=";
		this.changeMaterial(material);

		this.prefix = params.prefix || "calculator";

		this.defaultIdx = null;
		this.idx = 0;
		this.weight = null;
		this.valueArray = new Object();
	}
	
	Calculator.prototype.changeMaterial = function(material)
	{
		jQuery("#" + this.toolId + " .content").load(this.baseUrl + material.toLowerCase()); 
		this.weight = null;
	}

	Calculator.prototype.setDefaultIdx = function(index)
	{
		this.defaultIdx = index;
		this.idx = index;
	}
	
	Calculator.prototype.addData = function(symbol, values, isWeight)
	{
		this.valueArray[symbol] = values;
		if (isWeight == 1)
		{
			this.weight = symbol;
		}
		else if (isWeight == 2)
		{
			if (this.weight == null)
			{
				this.setWeight(symbol);
			}
		
			jQuery("#" + symbol).parent().bind("click", function(e) 
			{
				var id = jQuery(".value", this).attr("id");
				if (id.indexOf("steel") != -1)
				{
					calculator.setWeight(id);
				}
				else
				{
					alu.setWeight(id);
				}
			});
			jQuery("#" + symbol).parent().addClass("clickable");		
		}		
	}

	Calculator.prototype.setWeight = function(symbol)
	{
		if (this.weight != null)
		{
			jQuery("#" + this.weight).removeClass("weightValue");
		}
		this.weight = symbol;
		jQuery("#" + this.weight).addClass("weightValue");
		this.compute();
	}	

	Calculator.prototype.padLeft = function()
	{
		if (this.idx > 0)
		{
			this.idx--;
			this.setValues();
			this.compute();
		}
	}
	
	Calculator.prototype.padRight = function()
	{
		if (this.idx < (this.valueArray[this.weight].length - 1))
		{
			this.idx++;
			this.setValues();
			this.compute();
		}
	}
	
	Calculator.prototype.setValues = function()
	{
		for (value in this.valueArray)
		{
			if (this.valueArray[value][this.idx])
			{
				jQuery("#" + value).html(this.valueArray[value][this.idx]);
			}
			else
			{
				jQuery("#" + value).html("-");
			}
		}	
	}

	Calculator.prototype.compute = function()
	{
		if (this.weight != null)
		{
			var number = jQuery("#" + this.prefix + "number").val();
			var length = jQuery("#" + this.prefix + "length").val();
			var result = number * length * this.valueArray[this.weight][this.idx];
	
			if (isNaN(result))
			{
				jQuery("#" + this.prefix + "result").val("-");
			}
			else
			{
				jQuery("#" + this.prefix + "result").val(result);
			}
		}
	}
	Calculator.prototype.computeDiameter = function()
	{
		var PI = 3.14159265;
		var Y = 7.85; // kg/dm3

		var d = jQuery("#" + this.prefix + "diameter").val();
		var number = jQuery("#" + this.prefix + "number").val();
		var length = jQuery("#" + this.prefix + "length").val();

		d = d / 100; // mm > dm
		var A = (d * d * PI)/4; // dm2
		var V = A * (length * 10); //dm3
		var single = V * Y; //kg  

		var result = number * single;

		if (isNaN(result))
		{
			jQuery("#" + this.prefix + "result").val("-");
		}
		else
		{
			jQuery("#" + this.prefix + "result").val(result);
		}
	}			
	Calculator.prototype.computeTube = function()
	{
		var PI = 3.14159265;
		var Y = 7.85; // kg/dm3

		var d = jQuery("#" + this.prefix + "diameter").val();
		var s = jQuery("#" + this.prefix + "width").val();
		var number = jQuery("#" + this.prefix + "number").val();
		var length = jQuery("#" + this.prefix + "length").val();
		s = parseFloat(s.replace(/,/,"."));

		d = d / 100; // mm > dm
		var A = (d * d * PI)/4; // dm2
		var V = A * (length * 10); //dm3
		var single = V * Y; //kg  

		d2 = d - ((s / 100) * 2); 
		var A2 = (d2 * d2 * PI)/4; // dm2
		var V2 = A2 * (length * 10); //dm3
		var single2 = V2 * Y; //kg  

		var result = number * (single - single2);

		if (isNaN(result))
		{
			jQuery("#" + this.prefix + "result").val("-");
		}
		else
		{
			jQuery("#" + this.prefix + "result").val(result);
		}
	}		
}
{
	function Hardness()
	{
		jQuery("#hard .content").load(BASE + "hardness.php"); 
		this.defaultIdx = null;
		this.idx = 0;
		this.length = null;
		this.valueArray = new Object();
	}
	
	Hardness.prototype.setDefaultIdx = function(index)
	{
		this.defaultIdx = index;
		this.idx = index;
	}
	
	Hardness.prototype.addData = function(symbol, values)
	{
		this.valueArray[symbol] = values;
		this.length = values.length;
	}
	
	Hardness.prototype.padLeft = function()
	{
		if (this.idx > 0)
		{
			this.idx--;
			this.setValues();
		}
	}
	
	Hardness.prototype.padRight = function()
	{
		if (this.idx < this.length - 1)
		{
			this.idx++;
			this.setValues();
		}
	}
	
	Hardness.prototype.setValues = function()
	{
		for (value in this.valueArray)
		{
			if (this.valueArray[value][this.idx])
			{
				jQuery("#" + value).html(this.valueArray[value][this.idx]);
			}
			else
			{
				jQuery("#" + value).html("-");
			}
		}	
	}
}
{
	function Coil()
	{
		var type = jQuery("#type").val();
		this.baseUrl = BASE + "coil.php?type=";
		this.changeType(type);
	}
	
	Coil.prototype.changeType = function(type)
	{
		jQuery("#coil .content").load(this.baseUrl + type.toLowerCase()); 
	}
	Coil.prototype.computeWeight = function()
	{
		var PI = 3.1415927;
		var ad = parseInt(jQuery("#coil-ad").val());
		var id = parseInt(jQuery("#coil-id").val());
		var b = parseInt(jQuery("#coil-b").val());

		if (ad <= id)
		{
			jQuery("#coil-error").html("Der Au" + unescape("%DF") + "endurchmesser muss gru" + unescape("%F6%DF") + "er als der Innendurchmesser sein!");
			jQuery("#coil-ad").val(id + 1);
		}
		else
		{
			jQuery("#coil-error").html("");
			var base = PI / 4 * (ad * ad - id * id) * 7.85;
			var weight = Math.round(base / 1000000 * b);
			
			jQuery("#coil-result").val(weight)
		}
	}
	Coil.prototype.computeDiameter = function()
	{
		var PI = 3.1415927;
		var weight = parseInt(jQuery("#coil-weight").val());
		var id = parseInt(jQuery("#coil-id").val());
		var b = parseInt(jQuery("#coil-b").val());

		var aussen1 = 4000 * weight/b/0.00785/PI + id * id;
		var aussend_ = Math.sqrt(aussen1);
		var wertaussend = parseInt(aussend_);

		jQuery("#coil-result").val(wertaussend);
	}
}
{
	var TITLEBAR_WIDTH = 192;
	var TITLEBAR_DOUBLE_WIDTH = 384;
	var TITLEBAR_TRIPLE_WIDTH = 768;
	var raisedTool = null;
	var raisedToolCallback = null;
  	
  	function switchOn()
  	{
  		var glassPane = jQuery("#glassPane");
  		if (glassPane.css("display") == "none")
  		{
	  		glassPane.height(jQuery("#footer").offset().top + 30);
	  		glassPane.css("opacity", "0");
	  		glassPane.css("display", "block");
	  		
	  		// special handling for IE6 
	  		if (isIe(6))
	  		{
				var width = jQuery(document).width();
				width = width - 22;
		  		glassPane.css("width", width + "px");
			}			  		
			
			glassPane.fadeTo("fast", 0.70);
	  	}
  	}

  	function switchOff()
  	{
  		jQuery("#glassPane").fadeOut("fast");
  	}
  	
 	
  	function onToolClick(elem)
  	{
  		var parent = jQuery(elem).parent().parent();
  		var tool = parent.attr("id");
  		
  		if (jQuery(".inner", parent).css("display") == "none")
  		{
			if (tool == "calculator" || tool == "alu" || tool == "coil")
			{
				jQuery(elem).css("width", TITLEBAR_DOUBLE_WIDTH + "px");
			}
			else if (tool == "stock")
			{
				jQuery(elem).css("width", TITLEBAR_TRIPLE_WIDTH + "px");
			}
			else if (tool == "hard")
			{
				jQuery(elem).css("width", TITLEBAR_DOUBLE_WIDTH + "px");
				jQuery(parent).css("width", TITLEBAR_DOUBLE_WIDTH + "px");
				jQuery(parent).css("left", "596px");
				jQuery("#alu").css("z-index", "1000");
			}

			jQuery(".titlebar .rightEdge", parent).addClass("open");
			
			if (raisedTool != null)
			{
				raisedToolCallback = raisedTool;
				jQuery(".inner", raisedTool.parent).slideToggle("fast", function() 
				{
					jQuery(raisedToolCallback.titleBar).css("width", TITLEBAR_WIDTH + "px");
					jQuery(".titlebar .rightEdge", raisedToolCallback.parent).removeClass("open");
					if (raisedToolCallback.tool == "hard")
					{
						jQuery(raisedToolCallback.parent).css("width", TITLEBAR_WIDTH + "px");
						jQuery(raisedToolCallback.parent).css("left", "788px");
						jQuery("#alu").css("z-index", "0");
					}
				});
			}
			raisedTool = {titleBar:elem, tool:tool, parent:parent};
			
			switchOn();
  		}
  		else
  		{
			switchOff();
  		}

		jQuery(".inner", parent).slideToggle("fast", function() 
		{
			if (jQuery(".inner", parent).css("display") == "none")
			{
				jQuery(elem).css("width", TITLEBAR_WIDTH + "px");
				jQuery(".titlebar .rightEdge", raisedTool.parent).removeClass("open");
				raisedTool = null;
				
				if (tool == "hard")
				{
					jQuery(parent).css("width", TITLEBAR_WIDTH + "px");
					jQuery(parent).css("left", "788px");
					jQuery("#alu").css("z-index", "0");
				}				
			}
		});
  	}
  	
  	jQuery(window).scroll(function()
  	{
  		if (isIe(6))
  		{
			var viewPortHeight = jQuery(document).height();
			var documentOffset = jQuery(document).scrollTop();
			var footerTop = jQuery(window).height() - 30 + documentOffset;
			jQuery("#footer").css("top", footerTop + "px");
 			jQuery("#tools").css("top", footerTop + "px");
		}		
  		
  		var footerTop = jQuery("#footer").offset().top;
  		if (!jQuery.browser.msie)
  		{
 			jQuery("#tools").css("top", footerTop + "px");
  		}
  		var glassPane = jQuery("#glassPane");
  		if (glassPane.css("display") != "none")
  		{
	  		glassPane.height(footerTop + 30);
  		}
	});
	
  	jQuery(window).resize(function()
  	{
  		if (isIe(6))
  		{
			var viewPortHeight = jQuery(document).height();
			var documentOffset = jQuery(document).scrollTop();
			var footerTop = jQuery(window).height() - 30 + documentOffset;
			jQuery("#footer").css("top", footerTop + "px");
 			jQuery("#tools").css("top", footerTop + "px");
		}
		
  		var footerTop = jQuery("#footer").offset().top;
  		var glassPane = jQuery("#glassPane");
  		if (glassPane.css("display") != "none")
  		{
	  		glassPane.height(footerTop + 30);
  		}				
	});

  	jQuery(document).ready(function()
  	{
  		jQuery("#glassPane").bind("click", function() 
  		{
			if (raisedTool != null)
			{
				raisedToolCallback = raisedTool;
				jQuery(".inner", raisedTool.parent).slideToggle("fast", function() 
				{
					jQuery(raisedToolCallback.titleBar).css("width", TITLEBAR_WIDTH + "px");
					jQuery(".titlebar .rightEdge", raisedToolCallback.parent).removeClass("open");
					if (raisedToolCallback.tool == "hard")
					{
						jQuery(raisedToolCallback.parent).css("width", TITLEBAR_WIDTH + "px");
						jQuery(raisedToolCallback.parent).css("left", "788px");
						jQuery("#alu").css("z-index", "0");
					}
				});
			}  		
			raisedTool = null;
  			switchOff();
  		});
	});
	
	function isIe(version)
	{
		var ua = window.navigator.userAgent;
		var msie = ua.indexOf("MSIE ");
		
		return parseInt(ua.substring(msie+5, ua.indexOf(".", msie))) == version;
	}

  	jQuery(document).ready(function()
  	{
		if (jQuery(document).getUrlParam("area") != null)
		{
			var area = decodeURI(jQuery(document).getUrlParam("area"));
			area = area.replace(/\+/g, " ");	
			jQuery("#mailformStandort").val(area);	
		}
	});	
}
