document.observe("dom:loaded", function() {

  autohide_notice();
  provide_popup_links();
  provide_info_button_popups();
  
  if (Prototype.Browser.IE == 6) {
    provide_ie6_hover_fix_for_error_descriptions();
    provide_ie6_anchor_fix();
  }

});

function provide_ie6_anchor_fix() {
  $$('#content a[name]').each(function(a) {
    a.addClassName('name');
  });
}

function provide_ie6_hover_fix_for_error_descriptions() {
  $$('#content form span.error-desc').each(function(e) {
    e.observe("mouseover", function(event) {
      var element = event.element();
      element.addClassName("hover");
    });
    e.observe("mouseout", function(event) {
      var element = event.element();
      element.removeClassName("hover");
    });
  });
}

function provide_ie6_hover_fix_for_recommendation_info() {
  $$('#recommendation_info').each(function(e) {
    e.observe("mouseover", function(event) {
      var element = event.element();
      element.addClassName("hover");
    });
    e.observe("mouseout", function(event) {
      var element = event.element();
      element.removeClassName("hover");
    });
  });
}

function provide_ie6_hover_fix_for_registration_with_freikarte() {
  $$('#freikarte span.info').each(function(e) {
    e.observe("mouseover", function(event) {
      var element = event.element();
      element.addClassName("hover");
    });
    e.observe("mouseout", function(event) {
      var element = event.element();
      element.removeClassName("hover");
    });
  });
}

function provide_ie6_hover_fix_for_contact_file_details() {
  provide_ie6_hover_fix_for('sellers-show');
}

function provide_ie6_hover_fix_for_commission_base_details() {
  provide_ie6_hover_fix_for('commission_bases-index');
}

function provide_ie6_hover_fix_for(page_id) {
  if ($('page-' + page_id)) {
    $$('#content table div.info-detail').each(function(e) {
      e.observe("mouseover", function(event) {
        var element = event.element();
        element.addClassName("hover");
      });
      e.observe("mouseout", function(event) {
        var element = event.element();
        element.removeClassName("hover");
      });
    });
  }
}

function autohide_notice() {
  var notice = $$('p.flash-notice');
  if (notice.length > 0) {
    new Effect.Fade(notice.first(), { delay: 4 });
  }
}

function provide_popup_links(container_id) {
  var c = (container_id == null ? "" : "#" + container_id + " ");
  $$(c + 'a[href]').each(function(a) { 
    if (a.readAttribute("href").match(/http(s?):\/\//) || a.readAttribute("rel") == "external") { 
      if (a.readAttribute("rel") == "popup") {
        a.observe("click", function(event) {
          var element = event.element();
          var win = window.open(element.readAttribute("href"), "o2popup", "width=820,height=600,location=yes,menubar=no,resizeable=yes,scrollbars=yes,toolbar=no,status=yes");
          win.focus();
          Event.stop(event);
        });
      } else {
        a.setAttribute("target", "_blank"); 
      }
    }
  });
}

function provide_info_button_popups() {
  //TODO create iframes via JS only for IE6
  var infoDivs = $$('#content div.info');
  if (infoDivs.length > 0) {
    infoDivs.each(function(div) {
        var showBtn = div.down("a.show-info");
        if (!showBtn) return;
        showBtn.setAttribute("href", "javascript:void(null)");
        showBtn.observe('click', function(event) {
          var content = event.element().next("div.content");
          content.setStyle({ display: "block" });  
          content.down('.inner').scrollTop = 0;
          var iframe =  event.element().next("iframe");
          if (iframe) iframe.setStyle({ visibility: "visible" });
          Event.stop(event);
        });
        div.down("a.close").observe('click', function(event) {
          event.element().up().setStyle({ display: "none" });
          var iframe = event.element().up().siblings().last();
          if (iframe.tagName.toLowerCase() == "iframe") {
            iframe.setStyle({ visibility: "hidden" });
          }
          Event.stop(event);
        });
    });
  }
}

function change_selection() {
  var params = {
    selected_tariff_id: $F($('selected_tariff_id')),
    selected_audience_id: $F($('selected_audience_id')),
    selected_product_group_id: $F($('selected_product_group_id')),
    authenticity_token: $F($$('input[name=authenticity_token]').first()),
    recommend: $F($('recommend'))
  };
  
  $('selected_audience_id').setAttribute('disabled', true);
  $('selected_tariff_id').setAttribute('disabled', true);
  $('selected_product_group_id').setAttribute('disabled', true);
  
  var selected_hardware = $('selected_hardware_id');
  if (selected_hardware) {
    params.selected_hardware_id = $F(selected_hardware);
    selected_hardware.setAttribute('disabled', true);
  }
  
  var selected_benefit = $('selected_benefit_id');
  if (selected_benefit) {
    params.selected_benefit_id = $F(selected_benefit);
    selected_benefit.setAttribute('disabled', true);
  }
  
  var selected_dsl_speed_pack = $('selected_dsl_speed_pack_id');
  if (selected_dsl_speed_pack) {
    params.selected_dsl_speed_pack_id = $F(selected_dsl_speed_pack)
    selected_dsl_speed_pack.setAttribute('disabled', true);
  }
  
  var selected_internet_pack = $('selected_internet_pack_id');
  if (selected_internet_pack) {
    params.selected_internet_pack_id = $F(selected_internet_pack)
    selected_internet_pack.setAttribute('disabled', true);
  }
  
  var container_id = 'new_without_layout';
  var ajaxUpdater = new Ajax.Updater(
    container_id,
    '/orders/new',
    {
      asynchronous: true, 
      parameters: params,
      onComplete: function() { 
        provide_info_button_popups(); 
        provide_popup_links(container_id);
        provide_ie6_hover_fix_for_recommendation_info();
      }
    });
   
  return false;
}

function change_selection_groups() {
  var params = {
    authenticity_token: $F($$('input[name=authenticity_token]').first())
  };
  
  var selected_group_type = $('selected_group_label');
  if (selected_group_type) {
    params.selected_group_label = $F(selected_group_type);
    selected_group_type.setAttribute('disabled', true);
  }
  
  var selected_role = $('selected_role_id');
  if (selected_role) {
    params.selected_role_id = $F(selected_role);
    selected_role.setAttribute('disabled', true);
  }
  
  var selected_group = $('selected_group_id');
  if (selected_group) {
    params.selected_group_id = $F(selected_group);
    selected_group.setAttribute('disabled', true);
  }
  
  var container_id = 'group_admin_without_layout';
  var ajaxUpdater = new Ajax.Updater(
    container_id,
    '/groups/new',
    {
      asynchronous: true,
      parameters: params
    }
  );
}
