dojo.query("body").style("opacity", "0.01");

if(location.href.match(/ObjectPath=.Shops.Jencam.Categories.Firma$/)) {
  location.href = location.href + "/Vorstellung";
}

if(location.href.match(/ObjectPath=.Shops.Jencam.Categories.Aktuelles$/)) {
  location.href = location.href + "/neue_Produkte";
}

dojo.addOnLoad(function() {
  // Kopfbild anhand der aktiven Kategorie aktualisieren
  var cat = location.href.match(/ObjectPath=.Shops.Jencam.Categories.([a-zA-Z]+)/);
  if(cat) {
    dojo.query(".NavBarTop").forEach("item.style.background = 'url(/WebRoot/Store/Shops/Jencam/MediaGallery/Kopfbilder/header_"+cat[1]+".jpg)'");
  }
});

dojo.addOnLoad(function() {
  // Veröffentlichungsinfo von Blogs kürzen
  dojo.query("span.PublishInfo").forEach(function(i) {
    i.innerHTML = i.innerHTML.replace(/ \d\d:\d\d .*/, "");
  });
});

dojo.addOnLoad(function() {
  // Kontaktformular erweitern
  if(epages.vars.ViewAction == 'ViewContactForm') {
    dojo.query("table.Dialog").orphan();
    var content = dojo.doc.createElement("span");
    content.innerHTML = '<div class="Separator"></div>'
       + '<table class="Imprint">'
       + '   <tr>'
       + '      <td width="24%"></td>'
       + '      <td width="25%"><h2>Adresse</h2></td>'
       + '      <td colspan="2"><h2>Ansprechpartner</h2></td>'
       + '   </tr>'
       + '   <tr>'
       + '      <td><img src="/WebRoot/Store/Shops/Jencam/MediaGallery/jencam_logo.jpg"></td>'
       + '      <td>Felsbachstra&szlig;e 9<br>D-07745 Jena</td>'
       + '      <td class="Label" width="27%">Horst Gottschall</td>'
       + '      <td class="Label" >Uwe Jatzkowski</td>'
       + '   </tr>'
       + '   <tr>'
       + '      <td class="Label">Telefon</td>'
       + '      <td>03641-2253-0</td>'
       + '      <td>03641-2253-11</td>'
       + '      <td>03641-2253-12</td>'
       + '   </tr>'
       + '   <tr>'
       + '      <td class="Label">Fax</td>'
       + '      <td>03641-2253-19</td>'
       + '      <td>03641-2253-19</td>'
       + '      <td>03641-2253-19</td>'
       + '   </tr>'
       + '   <tr>'
       + '      <td class="Label">E-Mail</td>'
       + '      <td><a href="mailto:info@jencam.de">info@jencam.de</a></td>'
       + '      <td><a href="mailto:horst.gottschall@jencam.de">horst.gottschall@jencam.de</a></td>'
       + '      <td><a href="mailto:uwe.jatzkowski@jencam.de">uwe.jatzkowski@jencam.de</a></td>'
       + '   </tr>'
       + '</table>'
       + '<div class="Separator"></div>'
       + '<table class="Dialog"><tbody>'
       + '  <tr>'
       + '     <td class="InputLabelling">Firma<sup>*</sup></td>'
       + '     <td class="InputField"><input class="LongText" required="1" name="Sender" type="text"></td>'
       + '  </tr>'
       + '        <tr>'
       + '           <td class="InputLabelling">Abteilung</td>'
       + '           <td class="InputField"><input class="LongText" collect="Sender" name="Abteilung" type="text"></td>'
       + '        </tr>'
       + '        <tr> '
       + '           <td class="InputLabelling">Ansprechpartner<sup>*</sup></td>'
       + '           <td class="InputField"><input class="LongText" required="1" collect="Sender" name="Ansprechpartner" type="text"></td>'
       + '        </tr>'
       + '        <tr> '
       + '           <td class="InputLabelling">Strasse</td>'
       + '           <td class="InputField"><input class="LongText" collect="Sender" name="Strasse" type="text"></td>'
       + '        </tr>'
       + '        <tr> '
       + '              <td class="InputLabelling">PLZ</td>'
       + '              <td class="InputField"><input class="LongText" collect="Sender" name="PLZ" type="text"></td>'
       + '        </tr>'
       + '        <tr> '
       + '           <td class="InputLabelling">Ort</td>'
       + '           <td class="InputField"><input class="LongText" collect="Sender" name="Ort" type="text"></td>'
       + '        </tr>'
       + '        <tr> '
       + '           <td class="InputLabelling">Telefon<sup>*</sup></td>'
       + '           <td class="InputField"><input class="LongText" required="1" name="Phone" type="text"></td>'
       + '        </tr>'
       + '        <tr> '
       + '           <td class="InputLabelling">Fax</td>'
       + '           <td class="InputField"><input class="LongText" collect="Phone" name="Fax" type="text"></td>'
       + '        </tr>'
       + '        <tr> '
       + '           <td class="InputLabelling">eMail<sup>*</sup></td>'
       + '           <td class="InputField"><input class="LongText" required="1" name="Email" type="text"></td>'
       + '        </tr>'
       + '        <tr>'
       + '           <td>&nbsp;<input type="hidden" name="Subject" value="Kontaktformular"></td>'
       + '        </tr>'
       + '        <tr> '
       + '           <td class="InputLabelling">Produkt&uuml;bersicht</td>'
       + '           <td class="InputField"><input type="checkbox" collect="Subject" name="Produktuebersicht"></td>'
       + '        </tr>'
       + '        <tr> '
       + '           <td class="InputLabelling">Neuheiten</td>'
       + '           <td class="InputField"><input type="checkbox" collect="Subject" name="Neuheiten"></td>'
       + '        </tr>'
       + '        <tr> '
       + '           <td class="InputLabelling">Beratung</td>'
       + '           <td class="InputField"><input type="checkbox" collect="Subject" name="Beratung"></td>'
       + '        </tr>'
       + '        <tr> '
       + '           <td class="InputLabelling">Kontakt</td>'
       + '           <td class="InputField"><input type="checkbox" collect="Subject" name="Kontakt" checked="checked"></td>'
       + '        </tr>'
       + '           <tr> '
       + '           <td class="InputLabelling">Angebot</td>'
       + '           <td class="InputField"><input id="Kontakt_Preisanfrage" type="checkbox" collect="Subject" name="Angebot"></td>'
       + '       </tr>'
       + '        <tr>'
       + '           <td>&nbsp;</td>'
       + '        </tr>'
       + '        <tr> '
       + '           <td class="InputLabelling">Nachricht<sup>*</sup></td>'
       + '           <td class="InputField"><textarea class="VeryLongText" required="1" name="Message" cols="30" rows="10"></textarea></td>'
       + '        </tr>'
       + '</tbody></table>'
    ;
    dojo.query("form#epages_widget_Form_1 div.TableHead").empty().removeClass("TableHead");
    dojo.query("form#epages_widget_Form_1 > div").addContent(content, "first");

    var submit = function(e) {
      var errors = 0;
      dojo.query("form#epages_widget_Form_1 *[required]").forEach(function(f) {
        if(f.value.match(/./)) {
          f.parentNode.parentNode.className = "";
        } else {
          f.parentNode.parentNode.className = "DialogError";
          errors++;
        }
      });
      dojo.query("form#epages_widget_Form_1 input[name=Email]").forEach(function(f) {
        if(f.value.match(/[a-z].*@.*[a-z]/)) {
          f.parentNode.parentNode.className = "";
        } else {
          f.parentNode.parentNode.className = "DialogError";
          errors++;
        }
      });
      dojo.stopEvent(e);
      if(errors == 0) {
        dojo.query("input[collect]").forEach(function(f) {
          var val = f.value;
          if(f.getAttribute("type") == "checkbox") {
            if(f.checked) {
              val = f.getAttribute("name");
            } else {
              val = '';
            }
          }
          if(val.match(/./)) {
            dojo.query("input[name='"+f.getAttribute("collect")+"']").forEach(function(m) {
              m.value = m.value + " / " + val;
            });
            dojo.query("textarea[name='"+f.getAttribute("collect")+"']").forEach(function(m) {
              m.value = f.getAttribute("name") + ": " + val + "\n" + m.value;
            });
          }
        });
        dojo.query("form#epages_widget_Form_1").forEach(function (f) { f.submit(); });
      } else {
        dojo.query("form#epages_widget_Form_1 input[type=submit]").connect("onsubmit", submit).forEach(function(i) { i.removeAttribute("disabled"); });
      }
    };
    dojo.query("form#epages_widget_Form_1").connect("onsubmit", submit);

    // Kontaktformular zur Preisanfrage umbauen
    if(location.href.match(/Request=Price/)) {
      var title = 'Produktanfrage';
      if(epages.vars.Locale.language == 'en') {
        title = 'Pricing request';
      }
      dojo.query("h1").empty().addContent(title);
      dojo.query("input[name=Kontakt]").forEach(function (i) { i.checked = false; });
      dojo.query("input[name=Angebot]").forEach(function (i) { i.checked = true ; });
      dojo.query(".NavBarElement2 .MiniBasketContainer a").forEach(function (a) {
        var e = dojo.doc.createElement("div");
        e.innerHTML = '<input type="text" collect="Message" value="1" name="'+ a.getAttribute('title') +'" style="width: 3em"> * '+ a.getAttribute('title');
        dojo.place(e, "Kontakt_Preisanfrage", "after");
      });
      if(dojo.query(".NavBarElement2 .MiniBasketContainer a").length > 0) {
        dojo.query("form#epages_widget_Form_1").forEach(function (i) {
          i.innerHTML = i.innerHTML.replace(/Um uns eine Nachricht zu schicken, f.*llen Sie einfach/, "Mit diesem Formular haben Sie die M&ouml;glichkeit, eine Anfrage zu vorher ausgew&auml;hlten Produkten an JenCam zu stellen. Tragen Sie einfach die gew&uuml;nschte St&uuml;ckzahl ein, f&uuml;llen Sie");
        });
        dojo.query("#Kontakt_Preisanfrage").style("display", "none");
      }
    }
  }
});


dojo.addOnLoad(function() {
  // Die generierten Aktionen pro Produkt eingrenzen
  dojo.query("div.ProductDetails div.Links > form#epages_widget_Form_2").orphan();
  dojo.query("div.ProductDetails div.Links > a").orphan();
  dojo.query("div.ProductDetails div.Links > br").orphan();
  dojo.query("div.ProductDetails div.Links > input").addContent("<br />", "before");
});

dojo.addOnLoad(function() {
  // Warenkorb ist für anonyme Nutzer nur eine Preisanfrage
  dojo.query(".NavBarLeft .NavBarElement3 a[href*=ViewRegistrationRedirect]").empty().addContent("Anmelden / Registrieren");
  dojo.query("a[href*=ViewRegistrationRedirect]").at(0).forEach(function() {
    var frage = 'Produktanfrage';
    var verb = 'anfragen';
    if(epages.vars.Locale.language == 'en') {
      frage = 'Pricing request';
      verb = 'on request';
    }     

    dojo.query("tr.InputBlock > td.InputField > input.Price").forEach(function (i) {
      i.parentNode.parentNode.style.display = 'none';
    });

    dojo.query("input.LinkBasket").attr("value", frage);
    dojo.query("div.MiniCheckoutDiv").orphan();
    dojo.query(".Price").orphan();
    dojo.query(".OrderByListPrice").orphan();
    dojo.query(".Money").forEach(function(i) {
      i.innerHTML = i.innerHTML.replace(/\d[\d\s.,]+[^<]*/, verb);
    });
    dojo.query(".NavBarLeft .NavBarElement2 a").attr("preserve", "no");
    dojo.query(".NavBarLeft .NavBarElement2 .MiniBasketContainer a").attr("preserve", "yes");
    dojo.query(".NavBarLeft .NavBarElement2 a[preserve=no]").forEach(function(i) {
      i.setAttribute("href", "?ViewAction=ViewContactForm&ObjectPath=/Shops/Jencam&Request=Price");
      i.innerHTML = i.innerHTML.replace(/((>|^)\s*)\w[^<]+\w/, '$1'+verb);
    });
    dojo.query(".NavBarLeft .NavBarElement2 span").forEach(function(i) {
      i.innerHTML = i.innerHTML.replace(/Warenkorb|Shopping basket/, frage);
    });
    dojo.query(".NavBarLeft .NavBarElement2 a.LinkBasket").forEach(function(i) {
      i.innerHTML = i.innerHTML.replace(/((>|^)\s*)\w[^<]+\w/, '$1'+frage);
    });
    dojo.query(".NavBarLeft .NavBarElement2 a.ContextBoxHead").forEach(function(i) {
      i.innerHTML = i.innerHTML.replace(/((>|^)\s*)\w[^<]+\w/, '$1'+frage);
    });

    dojo.query("ul.OrderProcess").forEach(function(i) {
      location.href="?ViewAction=ViewContactForm&ObjectPath=/Shops/Jencam&Request=Price";
    });

    // Keine Downloads für anonyme Nutzer
    dojo.query(".CrossellingInfoArea a").forEach(function(a) {
      if(a.innerHTML.match(/Software/)) {
        a.setAttribute("mode", "protected");
      }
    });
    if(dojo.query("a.BreadcrumbItem[href*=Downloads]").length > 0) {
      dojo.query(".AttributeContent").forEach(function(i) {
        if(i.childNodes.length == 1 && i.firstChild.nodeType == 3) {
          i.innerHTML = i.firstChild.data.replace(/\S+:\/\/\S+/ig, '<a href="?ObjectPath=/Shops/Jencam&ViewAction=ViewRegistrationRedirect">Downloads nur f&uuml;r angemeldete Nutzer!</a>');
        }
      });
    }
  });
});

dojo.addOnLoad(function() {
  dojo.query("body").style("opacity", "1.0");
});

dojo.addOnLoad(function() {
  // Schnellsucht auf Produkte beschränken
  dojo.query("#ProductSearchBoxString").addContent("<input type='hidden' name='Category' value='8353'>", "before");
});

dojo.addOnLoad(function() {
  if(dojo.query("#Prices").length > 0) {
    // Produktzusätze als Tab-Navigation anbieten: Separatoren entferenen und neuen Navi Block einführen.
    dojo.query("#Prices ~ div.Separator").orphan();
    dojo.query("#Prices").addContent("<span id='content_tab_navi' style='display:block' />", "after");

    // Nackte Tabellen in einen Div verschieben
    dojo.query("#Prices ~ table").forEach(function(t) {
      t.parentNode
       .insertBefore(dojo.doc.createElement('div'), t)
       .appendChild(t);
    });

    // Überschriften in den Navi-Block schieben und nebeneinander darstellen
    var hs =  dojo.query("#Prices ~ h2");
    hs.place("span#content_tab_navi", "last");
    hs.style({
      "display": "inline",
      "padding": ".2em .5em",
      "borderTop" : "1px solid gray",
      "borderLeft" : "1px solid gray",
      "borderRight" : "1px solid gray",
      "marginRight" : ".2em",
      "cursor" : "pointer"
    });

    var minimal = 0;
    if(dojo.query("a.BreadcrumbItem[href*=Service]").length > 0) {
      minimal = 1;
    }

    // Inhalte in den Navi-Block schieben, markieren, ausblenden und mit der Navi verknüpfen
    var ds = dojo.query("#Prices ~ div");
    ds.place("span#content_tab_navi", "last");
    var i = 0;
    ds.forEach(function(d) {
      d.setAttribute("id", "content_navi_" + i);
      d.style.borderTop = "1px dotted gray";
      d.style.marginTop = ".2em";
      if(i > 0) { d.style.display = "none"; }
      i++;
    });
    i = 0;
    hs.forEach(function(h) {
      var ctx = dojo.byId("content_navi_" + i);
      dojo.connect(h, "onclick", h, function(e) {
        dojo.query("span#content_tab_navi > h2").style("background", "");
        this.style.backgroundColor = "lightGrey";
      });
      dojo.connect(h, "onclick", ctx, function(e) {
        dojo.query("span#content_tab_navi > div").style("display", "none");
        this.style.display = "block";
      });
      if(i > 0 && minimal) { h.style.display = "none"; }
      i++;
    });
    hs.at(0).style("backgroundColor", "lightGrey");

    // Navigationsbreich und weitere Inhalte abtrennen
    dojo.query("span#content_tab_navi").addContent("<div class='Separator' />", "after");

    // Baue Links in Attributwerte, wenn nur Text angegeben wurde
    dojo.query(".AttributeContent").forEach(function(i) {
      if(i.childNodes.length == 1 && i.firstChild.nodeType == 3) {
        i.innerHTML = i.firstChild.data.replace(/(\S+:\/\/\S+\/(\S+))/ig, '<a href="$1">$2</a>');
      }
    });

    // Ersetze Herunterladen durch Filenamen
    dojo.query(".AttributeContent a[target=Download]").forEach(function(i) {
      if(i.childNodes.length == 1 && i.firstChild.nodeType == 3) {
        i.innerHTML = i.getAttribute("href").replace(/.*\//, '');
      }
    });

    // Downloads aus Crossellings aussortieren und direkt ansprechen
    var cs = dojo.query("span#content_tab_navi table.CrossellingCount");
    var cd = cs.query("a").orphan();
    var count = 0;
    cd.forEach(function (i) {
      var baseurl = location.href.replace(/\?.*/, "?");
      dojo.xhrGet({
        url: i.href.replace(/[^?]*\?/, baseurl),
        load: function(res, args) {
           var id = "data_" + count;
           var html = "<div id='"+id+"'></div>";
           cs.addContent("<h2>"+i.innerHTML+"</h2>"+html, "after");
           dojo.byId(id).style.display = 'none';
           dojo.byId(id).innerHTML = res;
           dojo.query("#"+id+" .AttributeContent").forEach(function(d) {
             if(d.childNodes.length == 1 && d.firstChild.nodeType == 3) {
               d.innerHTML = d.firstChild.data.replace(/(\S+:\/\/\S+\/(\S+))/ig, '<a href="$1">$2</a>');
             }
           });
           dojo.query("#"+id+" .AttributeContent a[target=Download]").forEach(function(d) {
             if(d.childNodes.length == 1 && d.firstChild.nodeType == 3) {
               d.innerHTML = d.getAttribute("href").replace(/.*\//, '');
             }
           });
           if(i.getAttribute("mode") == "protected") {
             dojo.query("#"+id+" .AttributeContent a").forEach(function(d) {
               if(epages.vars.Locale.language == 'en') {
                 d.innerHTML = "Downloads nur f&uuml;r angemeldete Nutzer!";
               } else {
                 d.innerHTML = "Please register before download.";
               }
               d.href = "?ObjectPath=/Shops/Jencam&ViewAction=ViewRegistrationRedirect";
             });
           }
           dojo.query("#"+id+" table.UserAttributes").place("#"+id, "before");
           dojo.byId(id).empty();
           return res;
        },
        handleAs: "text"
      });
    });
    cs.style("display", "none").empty();
  }
});

dojo.addOnLoad(function() {
  // Registrierung anbieten
  if(dojo.query("div.NewCustomer input[value=Anmelden]").length > 0 &&
     dojo.query("input[value='Jetzt registrieren']").length == 0) {
     dojo.query("div.NewCustomer").addContent(''
       + '<form dojoType="epages.widget.Form" action="http://epages.jencam.de/epages/Jencam.sf/de_DE/?ObjectPath=/Shops/Jencam&amp;ViewAction=ViewRegistration" method="post">'
       + ' <div class="TableHead"><div>Sind Sie neu bei uns?</div></div>'
       + ' <div class="Dialog">'
       + '  <div class="SizeContainer">'
       + '   <div class="TextBlock">'
       + '    Registrieren Sie sich jetzt und haben Sie folgende Vorteile:'
       + '    <ul class="TopSmallMargin BottomSmallMargin">'
       + '     <li>Verwalten Ihrer persönlichen Daten</li>'
       + '     <li>Hinterlegen abweichender Lieferadressen </li>'
       + '     <li>Abfragen des Bestellstatus</li>'
       + '     <li>Verwalten eigener Merklisten, uvm.</li>'
       + '    </ul>'
       + '   </div>'
       + '  </div>'
       + '  <div class="ClearBoth"></div>'
       + ' </div>'
       + ' <span class="CustomButton">'
       + '  <input type="submit" value="Jetzt registrieren" />'
       + ' </span>'
       + ' <br />'
       + '</form>'
       + '<div class="Separator"></div>', "last");
   }
});