/**
* @author ehandelsbureauet
* this script is copyrighted and is not allowed to be used in any way without permission.
*
**/

image_url = new Array();
preLoadImgPrMenu();
var i;

var prCard = {
  init: function () {
    prCard.stockScript();
  },
  stockScript: function () {

    var prCardExpDel = document.getElementById('prCardExpDel').getElementsByTagName('SPAN')[1].innerHTML;

    if (prCardExpDel == " Bestil nu for senere levering! " || prCardExpDel == "Bestil nu for senere levering! " || prCardExpDel == "Bestil nu for senere levering!") {
      document.getElementById('expDelHead').style.display = "none";
      document.write("<style>#prCardExpDel .Description_Productinfo{color:#ca0808 !important}</style>");
    }


  }

}




function preLoadImgPrMenu() { //PRELOAD MENUGRAPHICS
  if (document.images) {
    preload_image_object = new Image();
    // set image url        
    image_url.push = "/images/design/construction/prmenu_niv1.gif";
    image_url.push = "/images/design/construction/prmenu_niv1_h.gif";
    image_url.push = "/images/design/construction/prmenu_niv2.gif";
    image_url.push = "/images/design/construction/prmenu_niv2_h.gif";
    image_url.push = "/images/design/construction/prmenu_niv2_active.gif";
    var i = 0;
    for (i = 0; i <= image_url.length; i++)
      preload_image_object.src = image_url[i];
  }
}

/* DOCUMENTATION
* [fixPrMenuSpanTagMenu] fix problem with SPAN bold in prmenu
* [performImgScript] function for hover effect on images - eventclass
* [systemCategories] inserts headline on search, news and special offer in TD on prlist
* [insertFocusOnSearch] inserts onfocus on searchfield due lack of space in field
* [categoryPictureList] moves productpictures from prmenu to listitems
* [redesignCatPage] redesigns the listed catpage to easier items
* [moveLinkBox] moves prLinkBox to the product menu heading
* [repairBtn] fix problem with buttons in buy process
*/

var prMenuScript = {
  init: function () {


    var fPrA = document.getElementById('ProductMenu_Table').getElementsByTagName('A');
    for (i = 0; i < fPrA.length; i++) {
      if (fPrA[i].className == "spLink") {
        fPrA[i].style.display = "none";
        fPrA[i].parentNode.getElementsByTagName('A')[0].href = fPrA[i].href;
        fPrA[i].parentNode.getElementsByTagName('A')[0].innerHTML = fPrA[i].innerHTML;
      }
    }

    var fPrDIV = document.getElementById('ProductMenu_Table').getElementsByTagName('DIV');
    for (i = 0; i < fPrDIV.length; i++) {
      if (fPrDIV[i].className == "prMHead") {
        var _innerTemp = fPrDIV[i].parentNode.innerHTML;
        fPrDIV[i].parentNode.parentNode.innerHTML = _innerTemp;
      }
    }

  }
}




function init() {
  fixPrMenuSpanTagMenu();
  performImgScript();
  systemCategories();
  insertFocusOnSearch();
  categoryPictureList();
  moveLinkBox();
  repairBtn();
  prCardDataRemoveEmptyCells();
  prMenuScript.init();

  if (document.getElementById('prCardWrap')) {
    prCard.init();
    $('.Related_BuyButton_IMG').attr('src', '/images/design/source/buy_btn.gif');
  }
}

function prCardDataRemoveEmptyCells() {

  if (document.getElementById('prCardInfosheet')) {
    var allEmpty = true;
    var cells = document.getElementById('prCardInfosheet').getElementsByTagName('DIV');
    for (var i = 0; i < cells.length; i++) {
      if (cells[i].className == 'prInfoKey') {
        if (cells[i].firstChild.innerHTML == "" || cells[i].firstChild.innerHTML == " ") {
          cells[i].parentNode.style.display = "none";
        }
        else {
          allEmpty = false;
        }
      }
    }
    if (allEmpty) {
      document.getElementById('prCardInfosheet').style.display = "none";
    }
  }
}


function insertFocusOnSearch() {
  var findInput = document.getElementById("SFm").getElementsByTagName('INPUT')[1];
  findInput.value = "Søg efter produkt";
}


function fixPrMenuSpanTagMenu() { //FIXES PROBLEM WITH BOLD IN PRMENU
  var tempSpan = document.getElementsByTagName('SPAN');
  for (var i = 0; i < tempSpan.length; i++) {
    if (tempSpan[i].className == "ProductMenu_MenuItemBold") {
      tempSpan[i].parentNode.innerHTML += tempSpan[i].innerHTML;
      tempSpan[i].style.display = "none";
      tempSpan[i].innerHTML = "";
    }
  }
}

function systemCategories() { // MAKES HEADLINES IN SEARCH RESULT
  if (window.location.pathname.match(new RegExp('^/shop/news'))) {
    document.getElementById('prListHeader').innerHTML = "<h1>Nyheder</h1>";
  }
  if (window.location.pathname.match(new RegExp('^/shop/specialoffer'))) {
    document.getElementById('prListHeader').innerHTML = "<h1>Tilbud</h1>";
  }
  if (window.location.pathname.match(new RegExp('^/shop/search'))) {
    document.getElementById('prListHeader').innerHTML = "<h1>Søgeresultat</h1>";
  }
}


// WOGWCLASSLIB
function performImgScript() { // READY FUNCTION FOR HOVER EFFECTS ON GIF
  eventImgArray = document.getElementsByTagName('A');
  for (var i = 0; i < eventImgArray.length; i++) {
    if (eventImgArray[i].className == 'eventClass') {
      addEvent(eventImgArray[i], "mouseover", processOver, false);
      addEvent(eventImgArray[i], "mouseout", processOut, false);
    }
  }
  //CLEANMEM
  eventImgArray = {};
}

function addEvent(elem, evtType, func, capture) {
  capture = (capture) ? capture : false;
  if (elem.addEventListener) {
    elem.addEventListener(evtType, func, capture);
  }
  else
    if (elem.attachEvent) {
      elem.attachEvent("on" + evtType, func);
    }
    else {
      elem["on" + evtType] = func;
    }
}

function processOver(evt) {
  evt = (evt) ? evt : window.event;
  var elem = (evt.target) ? evt.target : evt.srcElement;
  elem.src = elem.src.replace(/.gif/, "_h.gif")
}

function processOut(evt) {
  evt = (evt) ? evt : window.event;
  var elem = (evt.target) ? evt.target : evt.srcElement;
  elem.src = elem.src.replace(/_h.gif/, ".gif")
}

//WOGWCLASSLIB END




function categoryPictureList() {

  var imgs = {}; // Map img tag from url to img object
  // Find all images in menu, swap their a hrefs title into the place of the image
  var productMenu = document.getElementById("ProductMenu_Table");

  var img = productMenu.getElementsByTagName("IMG");

  for (var i = 0; i < img.length; i++) {
    var parent = img[i].parentNode; // The a tag
    var title = (parent.tagName == "B" ? parent.parentNode.title : parent.title); // title of a tag, which becomes link text
    var href = (parent.tagName == "B" ? parent.parentNode.href : parent.href)

    if (title) {
      imgs[href] = img[i];
      parent.innerHTML += title;
    }

  }

  // Find product list and move the image into that position
  var a = document.getElementsByTagName("A");

  for (var i = 0; i < a.length; i++) {

    if ((a[i].className == "SubCats_Prodlink")) {

      var a = document.getElementsByTagName("A");
      for (var i = 0; i < a.length; i++) {
        if ((a[i].className == "SubCats_Prodlink")) {

          var img = imgs[a[i].href];
          if (img) {
            var innerHTML = a[i].innerHTML;
            a[i].appendChild(img);
          }
        }
      }
    }
  }

  a = "";
  imgs = "";

  redesignCatPage();

}

function redesignCatPage() {
  htmlIns = "";

  //REDESIGN

  if (document.getElementById("category-picture-list")) {
    var findTags = document.getElementById("category-picture-list").getElementsByTagName("A");

    var findLink = [];
    var findImg = [];
    var findTxt = [];


    for (var i = 0; i < findTags.length; i++) {
      findImg.push(findTags[i].lastChild.src);
      findLink.push(findTags[i].href);
      findTxt.push(findTags[i].firstChild.nodeValue);
    }

    document.getElementById("category-picture-list").innerHTML = "";

    for (var i = 0; i < findImg.length; i++) {
      document.getElementById("category-picture-list").innerHTML += "<div class='catListBox'><h2><a href='" +
            findLink[i] +
            "'>" +
            findTxt[i] +
            "</h2><div class='catListImgBox'><a href='" +
            findLink[i] +
            "'><img src='" +
            findImg[i] +
            "'>" +
            "</a></div></div>";

    }

  }

}

function moveLinkBox() {
  var linkBox = document.getElementById("prLinkBox");

  var menuContainerSpans = document.getElementById("ProductmenuContainer_DIV").getElementsByTagName("SPAN");

  for (var i = 0; i < menuContainerSpans.length; i++) {
    if (menuContainerSpans[i].className == "Heading_Productmenu") {
      menuContainerSpans[i].appendChild(linkBox);
    }
  }
}


function repairBtn() {
  $("#OrderStep1_TD input[name='Search']").before('<div class="decopayProcess"></div>').css("float", "right");
  $("#OrderStep2_TD input[name='Search']").before('<div class="decopayProcess"></div>').css("float", "right");
}
