function init() {
  initNavMainDD();
  initSearchField();
  //if(document.getElementById("documents") && document.getElementById('docItems') && document.getElementById("docViewSpan")) {
  //  document.getElementById('documents').style.display = 'none';
  //  document.getElementById("docViewSpan").onclick = toggleDocuments;
  //}
}

function toggleDocuments() {
  if(document.getElementById('documents').style.display == 'none') { 
    document.getElementById('documents').style.display = 'block'; 
  } else {
    document.getElementById('documents').style.display = 'none';
  }
}

// Initialisierung der Dropdown Navigation für oberes Menü
function initNavMainDD() {
  var navPoints = new Array("navTopPointCompany","navTopPointSupport","navTopPointDownloads","navTopPointNews","navTopPointContact");
  var i = 0;
  while(navPoints[i]) {
    var idTemp = navPoints[i];
    var divTemp = idTemp.replace(/Point/,"Sub");
    if(chkElm(idTemp) && chkElm(divTemp)) {
      getElm(idTemp).onmouseover = new Function("showNavMainDD('"+idTemp+"','"+divTemp+"');");
      getElm(idTemp).onmouseout = new Function("hideNavMainDD('"+idTemp+"','"+divTemp+"');");
      getElm(divTemp).onmouseover = new Function("showNavMainDD('"+idTemp+"','"+divTemp+"');");
      getElm(divTemp).onmouseout = new Function("hideNavMainDD('"+idTemp+"','"+divTemp+"');");
    }
    i++;
  }
}

function showPress(ix) {
    if (ix > 3) ix = 1;
    else if (ix < 1) ix = 3;

    getElm("linkPress_" + ix + "_1").className = '';
    getElm("linkPress_" + ix + "_2").className = '';
    getElm("linkPress_" + ix + "_3").className = '';

    getElm("homePress_1").style.display = 'none';
    getElm("homePress_2").style.display = 'none';
    getElm("homePress_3").style.display = 'none';

    getElm("homePress_" + ix).style.display = 'block';
    getElm("linkPress_" + ix + "_" + ix).className = 'act';
}


function showHighlight(ix) {
    if (ix > 5) ix = 1;
    else if (ix < 1) ix = 5;

    getElm("linkHighlight_" + ix + "_1").className = '';
    getElm("linkHighlight_" + ix + "_2").className = '';
    getElm("linkHighlight_" + ix + "_3").className = '';
    getElm("linkHighlight_" + ix + "_4").className = '';
    getElm("linkHighlight_" + ix + "_5").className = '';

    getElm("homeHighlight_1").style.display = 'none';
    getElm("homeHighlight_2").style.display = 'none';
    getElm("homeHighlight_3").style.display = 'none';
    getElm("homeHighlight_4").style.display = 'none';
    getElm("homeHighlight_5").style.display = 'none';

    getElm("homeHighlight_" + ix).style.display = 'block';
    getElm("linkHighlight_" + ix + "_" + ix).className = 'act';
}

function autoChangeHighlight(ix) {
    if (ix > 5) ix = 1;
    setTimeout("showHighlight(" + ix + ")", 10000);
    setTimeout("autoChangeHighlight(" + (ix + 1) + ")", 10000);
}

// Initialisierung des Suchfelds
function initSearchField() {
  if(chkElm("inputSearch")) {
    getElm("inputSearch").onfocus = new Function("clickSearchField();");
    getElm("inputSearch").onblur = new Function("blurSearchField();");
  }
}

function initGlossarySearch() {
  if(chkElm("searchphrase")) {
    getElm("searchphrase").onfocus = new Function("clickSearchPhraseField();");
    getElm("searchphrase").onblur = new Function("blurSearchPhraseField();");
  }
}

$(document).ready(function () {
    $("a[rel='galB']").colorbox({ maxWidth: 950 });
    $("a[rel='gal']").colorbox({ maxWidth: 950, maxHeight: 600 });
    $("a[rel='gal2']").colorbox({ maxWidth: 950, maxHeight: 600 });
    $("a[rel='galschulung']").colorbox({ iframe: true, innerWidth: 500, innerHeight: 500 });
    $("a[rel='galpdf']").colorbox({ iframe: true, innerWidth: 950, innerHeight: 600 });
    $("a[rel='galds']").colorbox({ iframe: true, innerWidth: 950, innerHeight: 600 });
    $("a[rel='galflv']").colorbox({ iframe: true, innerWidth: 500, innerHeight: 400 });
    $("a[rel='galform']").colorbox({ iframe: true, innerWidth: 650, innerHeight: 590 });
    $("a[rel='galformheader']").colorbox({ iframe: true, innerWidth: 500, innerHeight: 300 });
    $("a[rel='galformheader2']").colorbox({ iframe: true, innerWidth: 500, innerHeight: 300 });

    $("#cat_shop").click(
		function () {
		    $("#shoplogin").slideDown(1000, function () { });		    
		    $(".presshighlight").addClass("presshighlight2");
		}
	);

    $("#ship_different").click(
		function () {
		    if (document.getElementById("tabShipAddress").style.display == "none") $("#tabShipAddress").slideDown(1000, function () { });
		    else $("#tabShipAddress").slideUp(1000, function () { });
		}
	);

    $("#basket_hidelogin").click(
		function () {
		    $("#loginform").slideUp(1000, function () { });
		}
	);


    $("#inputSearch").autocomplete("/suche.aspx?sf=1",
		{
		    delay: 5,
		    minChars: 1,
		    matchSubset: 1,
		    autoFill: false,
		    maxItemsToShow: 10,
		    width: 300,
		    onItemSelect: startSearch
		}
	);
});

function startSearch(li) {
    method = "post"; // Set method to post by default, if not specified.
    // The rest of this code assumes you are not using a library.
    // It can be made less wordy if you use one.
    var form = document.createElement("form");
    form.setAttribute("method", method);
    form.setAttribute("action", "/detail.aspx?p=" + li.selectValue.substr(li.selectValue.indexOf(" ArtNo: ") + 8, 10));

    var hiddenField = document.createElement("input");
    hiddenField.setAttribute("type", "hidden"); hiddenField.setAttribute("name", "searchphrase"); hiddenField.setAttribute("value", li.selectValue); form.appendChild(hiddenField);
    
    document.body.appendChild(form);    // Not entirely sure if this is necessary
    form.submit();
}

function addItemToBasket(strNo, strDim1, strDim2) {
    $.post("/detail.aspx", { addB: "1", no: strNo, dim1: strDim1, dim2: strDim2 },
   function (data) {
      $('#basket_login').effect("highlight", {}, 2000);
      document.getElementById("basket_prev_num").firstChild.nodeValue = data;
      $('#basket_prev_num').animate({ fontSize: 18 }, "fast");
      $('#basket_prev_num').animate({ fontSize: 12 }, "fast");
      document.location.href = "/basket.aspx";
   });
}

function delItemFromBasket(strNo) {
    $.post("/basket.aspx", { delB: "1", no: strNo },
   function (data) {
       document.location.href = document.location.href;
   });

}

function showNews(ix) {
    if (ix > 4) ix = 1;
    else if (ix < 1) ix = 4;

    getElm("newsentry_1").style.display = 'none';
    getElm("newsentry_2").style.display = 'none';
    getElm("newsentry_3").style.display = 'none';
    getElm("newsentry_4").style.display = 'none';
    getElm("newsentry_" + ix).style.display = 'block';
}

function autoChangeNews(ix) {
    if (ix > 4) ix = 1;
    setTimeout("showNews(" + ix + ")", 7000);
    setTimeout("autoChangeNews(" + (ix + 1) + ")", 7000);
}


function login(strMail, strPW, strRedir) {
    $.post("/confirmorder.aspx", { login: "1", mail: strMail, pw: strPW },
   function (data) {
       if (data.length > 0) {
           
               document.location.href = "http://www.bressner.de/basket.aspx";

       }
       else alert("Dieser Loginname existiert nicht oder das Passwort ist falsch.");
   });

}

function logout() {
    $.post("/confirmorder.aspx", { logout: "1" },
   function (data) {
       //if (data.length > 0) document.location.href = document.location.href.replace("https://", "http://");
       if (data.length > 0) document.location.href = "/?logout=1";
       else alert("Beim Logout ist ein Fehler aufgetreten. Bitte versuchen Sie es erneut.");
   });

}


function showNavMainDD(point, id) { getElm(id).style.display='block'; }
function hideNavMainDD(point, id) { getElm(id).style.display='none'; }
function clickSearchField() { if(getElm("inputSearch").value == "Stichwort/Artikel-Nr." || getElm("inputSearch").value == "Keyword/Article no.") getElm("inputSearch").value = ""; }
function blurSearchField() { if(getElm("inputSearch").value == "") getElm("inputSearch").value = "Stichwort/Artikel-Nr."; }
function clickSearchPhraseField() { if(getElm("searchphrase").value == "Fachwort") getElm("searchphrase").value = ""; }
function blurSearchPhraseField() { if(getElm("searchphrase").value == "") getElm("searchphrase").value = "Fachwort"; }
function chkElm(id) { if(getElm(id)) return true; else return false; }
function getElm(id) { return document.getElementById(id); }
