function toggle_liefer() {
  
  var checkbox = document.getElementById("user_liefer_is_billing");
  
  if(checkbox.checked == true){
    var wie = 'disabled';
    var col = 'url(/images/hg_input.gif)';
  }else{
    var wie = '';
    var col = '';
  } 
  
  document.getElementById("user_liefer_firma").disabled=wie;
  document.getElementById("user_liefer_firma").style.backgroundImage = col;

  document.getElementById("user_liefer_vorname").disabled=wie;
  document.getElementById("user_liefer_vorname").style.backgroundImage = col;

  document.getElementById("user_liefer_nachname").disabled=wie;
  document.getElementById("user_liefer_nachname").style.backgroundImage = col;

  document.getElementById("user_liefer_strasse").disabled=wie;
  document.getElementById("user_liefer_strasse").style.backgroundImage = col;

  document.getElementById("user_liefer_plz").disabled=wie;
  document.getElementById("user_liefer_plz").style.backgroundImage = col;

  document.getElementById("user_liefer_ort").disabled=wie;
  document.getElementById("user_liefer_ort").style.backgroundImage = col;

  document.getElementById("user_liefer_land").disabled=wie;
  document.getElementById("user_liefer_land").style.backgroundImage = col;
  
}

function setUidLand() {
  land = $('user_land').getValue();
  
  switch (land) {
    case "Belgien":
      uid_land = "BE";
      break;
    case "Niederlanden":
      uid_land = "NL";
      break;
    case "Luxemburg":
      uid_land = "LU";
      break;
    case "Bulgarien":
      uid_land = "BG";
      break;
    case "Deutschland":
      uid_land = "DE";
      break;
    case "Dänemark":
      uid_land = "DK";
      break;
    case "Estland":
      uid_land = "EE";
      break;
    case "Finnland":
      uid_land = "FI";
      break;
    case "Frankreich":
      uid_land = "FR";
      break;
    case "Griechenland":
      uid_land = "EL";
      break;
    case "Großbritannien":
      uid_land = "GB";
      break;
    case "Irland":
      uid_land = "IE";
      break;
    case "Italien":
      uid_land = "IT";
      break;
    case "Lettland":
      uid_land = "LV";
      break;
    case "Litauen":
      uid_land = "LT";
      break;
    case "Malta":
      uid_land = "MT";
      break;
    case "Polen":
      uid_land = "PL";
      break;
    case "Portugal":
      uid_land = "PT";
      break;
    case "Rumänien":
      uid_land = "RO";
      break;
    case "Schweden":
      uid_land = "SE";
      break;
    case "Slowakei":
      uid_land = "SK";
      break;
    case "Slowenien":
      uid_land = "SI";
      break;
    case "Spanien":
      uid_land = "ES";
      break;
    case "Tschechien":
      uid_land = "CZ";
      break;
    case "Ungarn":
      uid_land = "HU";
      break;
    case "Zypern":
      uid_land = "CY";
      break;
      case "Österreich":
      uid_land = "AT";
      break;
    default:
      uid_land = "";
      break;
  }
  $('user_uid_land').value = uid_land;
}

function toggle_pictures(pic) {
  old_gr_image = $('gr_image').src
  old_gr_alt = $('gr_image').alt
  $('gr_image').src = $(pic).src.split('mini').join('original');
  $('gr_image').alt = $(pic).alt;
  $(pic).src = old_gr_image.split('original').join('mini');
  $(pic).alt = old_gr_alt;
  $("image_subtitle").update($("gr_image").readAttribute("alt"));

}

f4_browser_name = navigator.appName;
f4_browser_version = parseFloat(navigator.appVersion); 

if (f4_browser_name == "Netscape" && f4_browser_version < 2.0)
   {f4_roll = 'false';}
else if (f4_browser_name == "Netscape" && f4_browser_version == 2.0)
   {f4_roll = 'false';}
else if (f4_browser_name == "Netscape" && f4_browser_version >= 3.0)
   {f4_roll = 'true';}
else if (f4_browser_name == "Microsoft Internet Explorer" && f4_browser_version <= 2.0)
   {f4_roll = 'false';}
else if (f4_browser_name == "Microsoft Internet Explorer" && f4_browser_version >= 3.0)
   {f4_roll = 'true';}
else if (f4_browser_version >= 4.0) 
   {f4_roll = 'true';}
else
   {f4_roll = 'false';}
function msover(f4_img,f4_ref)
   {if (f4_roll == 'true')
       {document.images[f4_img].src = f4_ref;}
   }
function msout(f4_img,f4_ref)
   {if (f4_roll == 'true')
       {document.images[f4_img].src = f4_ref;}
  }

function setspanne(e){
  
  function getSetValues(e){
    var id_string = e.id;
    var id = id_string.replace(/product_versions_attributes_/g, "");
    var id = id.replace(/_anlieferung_euro/g, "");
    var id = id.replace(/_ek_str/g, "");
    var id = id.replace(/_vk_str/g, "");
    //alert(id);
    $('product_versions_attributes_'+id+'_spanne_calc').value = calcspanne(id);
  }
  
  if(e){
    getSetValues(e);
  }else{
    $$('.anlieferung').each(function(anlieferung) {
      getSetValues(anlieferung);
    });
  }
}
  
function calcspanne(id){

  var ust = $('product_mwst').value;
  //alert(id);
  var anlieferung = $('product_versions_attributes_' + id + '_anlieferung_euro').value;
  var ek = $('product_versions_attributes_' + id + '_ek_str').value;
  var vk = $('product_versions_attributes_' + id + '_vk_str').value;
    
  var anliefer_kosten = Math.round(parseFloat(anlieferung.split(',').join('.')) * 100.0);
  
  var ek = Math.round(parseFloat(ek.split(',').join('.')) * 100.0);
  var vk = Math.round(parseFloat(vk.split(',').join('.')) * 100.0);
  
  var spanne = ((100.00 / (ek + anliefer_kosten)) * (vk / (100.0 + parseFloat(ust)) * 100.0)) - 100;
  
  return ((Math.round(spanne * 100)) / 100)

}

// Popup mit data-popup="true" öffnen
document.on("click", "a[data-popup]", function(event, element) {
  if (event.stopped) return;
  var options = element.readAttribute('data-popup').evalJSON(true);
  window.open($(element).href, options.windowname, options.appearance);
  event.stop();
});

// Observe select field mit Ajax request
// # OPTIMIZE Parameter könnte auch mal nicht id sein...
document.on("change", "*[data-observe-remote]", function(event, element) {
  if (event.stopped) return;
  var csrf_param = $$('meta[name=csrf-param]')[0].content;
  var csrf_token = $$('meta[name=csrf-token]')[0].content;
  var url = element.readAttribute('data-observe-remote');
  new Ajax.Request(url, {asynchronous:true, evalScripts:true, parameters:'id='+ element.value + '&' + csrf_param + '=' + encodeURIComponent(csrf_token)});
  event.stop();
});

// Promo Animation
document.observe("dom:loaded", function() {
  $$('#promo h2').invoke('hide');
  $$('#promo a.no-border').invoke('hide');
  if($('move_img_dtm') != undefined){
    $('move_img_dtm').setStyle({top: '-93px', left: '220px'});
  }
  if($('move_img_langenscheidt') != undefined){
    $('move_img_langenscheidt').setStyle({top: '-145px', left: '160px'});
  }
  if($('move_simons_cat') != undefined){
    $$('#move_simons_cat img').first().setStyle({bottom: '-86px'});
  }
});
Event.observe(window, 'load', function() { 
  $$('#promo h2').invoke('appear');
  $$('#promo a.no-border').invoke('appear');
  if($('move_img_dtm') != undefined){
    new Effect.Move('move_img_dtm', { x: 0, y: 140, mode: 'relative' });
  }
  if($('move_img_langenscheidt') != undefined){
    new Effect.Move('move_img_langenscheidt', { x: 0, y: 140, mode: 'relative' });
  }
  if($('move_simons_cat') != undefined){
    el = $$('#move_simons_cat img').first();
    new Effect.Move(el, { x: 0, y: -86, mode: 'relative' });
  }
});

// Sounds bei M&M rechts oben
soundManager.url = '/flash/soundmanager/';
soundManager.useFlashBlock = false; // optionally, enable when you're ready to dive in
soundManager.onready(function() {
  soundManager.createSound({
    id: 'hund_sound',
    url: '/hund.mp3',
    autoLoad: true,
    autoPlay: false
  });
  soundManager.createSound({
    id: 'katze_sound',
    url: '/katze.mp3',
    autoLoad: true,
    autoPlay: false
  });
});
function manageSound(mode, tier) {
  if(mode == 'play'){
    soundManager.play(tier + '_sound');
  }else{
    soundManager.stop(tier + '_sound');
  }
}

// Links in neuem Fenster öffnen
document.on("click", "a[rel=new-window]", function(event, element) {
  window.open(element.href);
  return false;
});

// Admin: Infotext bei Minusbestellungen ein/ausblenden.
function checkMinusDeliveryInfoNeedness(){
  var minuslieferung = false;
  $$('div.preistabelle_table input[id$=_minusbestand]').each(function(minusbestand) {
    if(minusbestand.checked == true){
      minuslieferung = true;
    }
  });
  return minuslieferung;
}
document.observe("dom:loaded", function() {
  if(checkMinusDeliveryInfoNeedness() == false){
    $('minus_delivery_info').hide();
  }
});
document.on("change", "div.preistabelle_table input[id$=_minusbestand]", function(event, element) {
  if (event.stopped) return;
  if(checkMinusDeliveryInfoNeedness() == true){
    if($('minus_delivery_info').visible() == false) $('minus_delivery_info').slideDown({duration:0.4});
  }else{
    if($('minus_delivery_info').visible() == true) $('minus_delivery_info').slideUp({duration:0.4});
  }
  event.stop();
});

/**
* Event.simulate(@element, eventName[, options]) -> Element
*
* - @element: element to fire event on
* - eventName: name of event to fire (only MouseEvents and HTMLEvents interfaces are supported)
* - options: optional object to fine-tune event properties - pointerX, pointerY, ctrlKey, etc.
*
* $('foo').simulate('click'); // => fires "click" event on an element with id=foo
*
**/
(function(){
  
  var eventMatchers = {
    'HTMLEvents': /^(?:load|unload|abort|error|select|change|submit|reset|focus|blur|resize|scroll)$/,
    'MouseEvents': /^(?:click|mouse(?:down|up|over|move|out))$/
  }
  var defaultOptions = {
    pointerX: 0,
    pointerY: 0,
    button: 0,
    ctrlKey: false,
    altKey: false,
    shiftKey: false,
    metaKey: false,
    bubbles: true,
    cancelable: true
  }
  
  Event.simulate = function(element, eventName) {
    var options = Object.extend(defaultOptions, arguments[2] || { });
    var oEvent, eventType = null;
    
    element = $(element);
    
    for (var name in eventMatchers) {
      if (eventMatchers[name].test(eventName)) { eventType = name; break; }
    }
 
    if (!eventType)
      throw new SyntaxError('Only HTMLEvents and MouseEvents interfaces are supported');
 
    if (document.createEvent) {
      oEvent = document.createEvent(eventType);
      if (eventType == 'HTMLEvents') {
        oEvent.initEvent(eventName, options.bubbles, options.cancelable);
      }
      else {
        oEvent.initMouseEvent(eventName, options.bubbles, options.cancelable, document.defaultView,
          options.button, options.pointerX, options.pointerY, options.pointerX, options.pointerY,
          options.ctrlKey, options.altKey, options.shiftKey, options.metaKey, options.button, element);
      }
      element.dispatchEvent(oEvent);
    }
    else {
      options.clientX = options.pointerX;
      options.clientY = options.pointerY;
      oEvent = Object.extend(document.createEventObject(), options);
      element.fireEvent('on' + eventName, oEvent);
    }
    return element;
  }
  
  Element.addMethods({ simulate: Event.simulate });
})()
