var TEMPLATE_URL = 'http://aeg.nohype.de/templates/NohypeAEG';
function search_by_zip() {
	var url = TEMPLATE_URL + '/aeg_contact_search.php';
	var params = '';
	params+= 'zip=' + $F('zip');
	
	var check = new Ajax.Updater('zip_result', url,	{ method:'post', onComplete: function(){
		$('zip_result').show();
	} } );
	
	return false;
}


var halbzeug_image_counter = 1;
var fertigteile_image_counter = 1;
var kappenfraeser_image_counter = 1;
function rotate_diashow(section){
	var limit;
	// check if we have reached the limit and have to start at zero again
	if (section == 'kappenfraeser') {
		limit = 5;
	} else {
		limit = 4;
	}
	
	eval("var counter = " + section + "_image_counter;");
	
	// do the first transition
	new Effect.Fade(section + '_image_' + counter, {duration: 3});
	
	// check for the limits
	eval("if (" + section + "_image_counter == limit) { " + section + "_image_counter = 0 } ");
	eval("var counter = " + section + "_image_counter;");
	
	// do the second transition
	new Effect.Appear(section + '_image_' + (counter+1), {duration: 2});
	
	// increment the section counter
	eval(section + "_image_counter++");
	
	// install the next call
	setTimeout("rotate_diashow('"+section+"')", 18000);
}

function calculate_round_bar($Diameter, $Length) {
  if ($Diameter == 0) {
    return false;
  }
  if ($Length == 0) {
    return false;
  }
  
  $Diameter   = $Diameter.replace(',', '.');
  $Length     = $Length.replace(',', '.');

  var $weight = $Diameter * $Diameter;
  $weight     = $weight * 3.1415927;
  $weight     = $weight / 4;
  $weight     = $weight * 8.9;
  $weight     = $weight / 1000;
  $weight     = $weight / 1000 * $Length;
  
  $weight     = $weight.toFixed(2);
  $weight     = $weight.replace('.', ',');

  return $weight;
}

function calculate_flat_bar($Band, $Altitude, $Length) {
  if ($Band == 0) {
    return false;
  }
  if ($Altitude == 0) {
    return false;
  }
  if ($Length == 0) {
    return false;
  }
  
  $Band       = $Band.replace(',', '.');
  $Altitude   = $Altitude.replace(',', '.');
  $Length     = $Length.replace(',', '.');

  var $weight = $Band * $Altitude * $Length * 8.9 / 1000 / 1000;
  
  $weight     = $weight.toFixed(2);
  $weight     = $weight.replace('.', ',');

  return $weight;
}

function calculate_hex_bar($Diameter, $Length) {
  if ($Diameter == 0) {
    return false;
  }
  if ($Length == 0) {
    return false;
  }
  
  $Diameter   = $Diameter.replace(',', '.');
  $Length     = $Length.replace(',', '.');

  var $sqrt   = Math.sqrt(3);
  $sqrt.toFixed(2);
  var $d13    = $Diameter / ($sqrt);
  var $f13    = (6 * $d13 * $Diameter) / 4;
  var $weight = ($f13 * $Length * 0.89) / 100000;

  $weight     = $weight.toFixed(2);
  $weight     = $weight.replace('.', ',');


  return $weight;
}

document.observe("dom:loaded", function() {
  
  //alert(calculate_round_bar(16, 1000));
  //alert(calculate_flat_bar(10, 70, 1520));
  //alert(calculate_hex_bar(24, 1000));
  
	if ($('search_button')) {
		Event.observe('search_button', 'click', 'search_by_zip');
	}
	
	/*
	  WEIGHT CALCULATION FOR ROUND BARS
	*/
	if ($('Diameter_Round')) {
	  $("Weight_Round").value = calculate_round_bar($("Diameter_Round").value, $("Length_Round").value);
	  Event.observe('Diameter_Round', 'change', function(){
	    $("Weight_Round").value = calculate_round_bar($("Diameter_Round").value, $("Length_Round").value);
	  });
	}
	if ($('Length_Round')) {
	  $("Weight_Round").value = calculate_round_bar($("Diameter_Round").value, $("Length_Round").value);
	  Event.observe('Length_Round', 'change', function(){
	    $("Weight_Round").value = calculate_round_bar($("Diameter_Round").value, $("Length_Round").value);
	  });
	}
	
	/*
	  WEIGHT CALCULATION FOR FLAT BARS
	*/
	if ($('Band_Flat')) {
	  $("Weight_Flat").value = calculate_flat_bar($("Band_Flat").value, $("Altitude_Flat").value, $("Length_Flat").value);
	  Event.observe('Band_Flat', 'change', function(){
	    $("Weight_Flat").value = calculate_flat_bar($("Band_Flat").value, $("Altitude_Flat").value, $("Length_Flat").value);
	  });
	}
	if ($('Altitude_Flat')) {
	  $("Weight_Flat").value = calculate_flat_bar($("Band_Flat").value, $("Altitude_Flat").value, $("Length_Flat").value);
	  Event.observe('Altitude_Flat', 'change', function(){
	    $("Weight_Flat").value = calculate_flat_bar($("Band_Flat").value, $("Altitude_Flat").value, $("Length_Flat").value);
	  });
	}
	if ($('Length_Flat')) {
	  $("Weight_Flat").value = calculate_flat_bar($("Band_Flat").value, $("Altitude_Flat").value, $("Length_Flat").value);
	  Event.observe('Length_Flat', 'change', function(){
	    $("Weight_Flat").value = calculate_flat_bar($("Band_Flat").value, $("Altitude_Flat").value, $("Length_Flat").value);
	  });
	}
	
	/*
	  WEIGHT CALCULATION FOR HEX BARS
	*/
	if ($('Diameter_Hex')) {
	  $("Weight_Hex").value = calculate_hex_bar($("Diameter_Hex").value, $("Length_Hex").value);
	  Event.observe('Diameter_Hex', 'change', function(){
	    $("Weight_Hex").value = calculate_hex_bar($("Diameter_Hex").value, $("Length_Hex").value);
	  });
	}
	if ($('Length_Hex')) {
	  $("Weight_Hex").value = calculate_hex_bar($("Diameter_Hex").value, $("Length_Hex").value);
	  Event.observe('Length_Hex', 'change', function(){
	    $("Weight_Hex").value = calculate_hex_bar($("Diameter_Hex").value, $("Length_Hex").value);
	  });
	}
	
	if($('selected_weight_calculation')){
		Event.observe('selected_weight_calculation', 'change', switch_weight_calculation);
	}
	
	// invoke the first pass for the diashow
	setTimeout("rotate_diashow('halbzeug')", 1000);
	setTimeout("rotate_diashow('fertigteile')", 7000);
	setTimeout("rotate_diashow('kappenfraeser')", 14000);
	
	// get all images from the content divs
	var images_in_content = $('aeg_website').select('img');
	
	for (i = 0; i < images_in_content.length; i++) {
	  // add the oncontextmenu event listener to all images in the content area
	  $(images_in_content[i]).oncontextmenu = function() { return false; };
	  // console.debug($(images_in_content[i]));
	}
	
	// get all anchor tags from the navigation divs
	var a_in_nav = $('leftcol').select('a');
	var debug = '';
	for (i = 0; i < a_in_nav.length; i++) {
	  // add the oncontextmenu event listener to all images in the content area
		if ($(a_in_nav[i]).href == 'http://aeg-svs-schweisstechnik.de/pages/aktuell-stellenangebote.php') {
			$(a_in_nav[i]).style.color = '#f00';
		}
		 debug+= $(a_in_nav[i]).href + ' || ';
	}
});

function switch_weight_calculation(){
	$('rundstangen').hide();
	$('flachstangen').hide();
	$('sechskantstangen').hide();
	if ($('selected_weight_calculation')){
		$($F('selected_weight_calculation')).show();
	}
}