﻿


var redirectIfNotLoggedin = {
  init: function () {

    if (document.getElementById('OrderStep1_TD') || document.getElementById('OrderStep2_TD') || document.getElementById('OrderStep3_TD')) {
      this.findA = document.getElementById('ShopLayOut_Topmenu_TD').getElementsByTagName('A');
      for (var i = 0; i < this.findA.length; i++) {
        if (this.findA[i].innerHTML == "B2BLogin") {
          alert("For at bestille varer skal man være logget ind. De henvises nu til login-side.");
          window.location = "/shop/b2blogin.html";
        }
      }
    }
  }
}



init();
function init() {
  fixPrMenuSpanTagMenu();
  fixPrCard();
  switchPicRelated();
  categoryPictureList();
  allReadyLoggedIn();
  B2BFixes();
  AddB2BFix();
  redirectIfNotLoggedin.init();
}


$(document).ready(function () {
  insertValSearchTxt();


  $('.ProductMenu_MenuItemBold').remove(); //REMOVE SPAN IN PRMENU 


});






function insertValSearchTxt() {
  var defaultText = "Indtast produkt";
  $('#headerSearch .SearchField_SearchPage').val(defaultText);
  $('#headerSearch .SearchField_SearchPage').focus(function () {
    if (this.value == defaultText) {
      this.value = "";
    }
  });
  $('#headerSearch .SearchField_SearchPage').blur(function () {
    if (this.value == "") {
      this.value = defaultText;
    }
  });
}


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 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 img = imgs[a[i].href];

      if (img) {
        var innerHTML = a[i].innerHTML;
        a[i].innerHTML = "";
        a[i].appendChild(img);
        // a[i].innerHTML += innerHTML;

        a[i].innerHTML = '<div class="catListBox">' + '<h2>' + innerHTML + '</h2>' + '<span class="catListImgBox ">' + a[i].innerHTML + '</span>' + '</div>';
        a[i].onclick = function () {
          document.location = this.href;
          return false;
        } // Onclick due to IE bug on block A
      }

    }
  }

}


function allReadyLoggedIn() {

  if (document.getElementById('ShopLayOut_Topmenu_TD')) {
    var findAParent = document.getElementById('ShopLayOut_Topmenu_TD');
    var findA = findAParent.getElementsByTagName('A');
    for (var i = 0; i < findA.length; i++) {
      if (findA[i].innerHTML == "B2BLogud") {
        document.getElementById('headerB2BLogin').style.display = "none";
        document.getElementById('headerB2BLogout').style.display = "block";
        $('#rightMenu #basketLink1').css('display', 'block');
        $('#rightMenu #basketLink2').css('display', 'block');
        $('#rightMenu #basketLink3').css('display', 'none');
        $('#rightMenu #basketLink4').css('display', 'block');
      }
    }
  }

}

function B2BFixes() {
  // MOVE ERROR MSG
  var error = $('.ErrorText_B2Blogin');
  $('.ErrorText_B2Blogin').remove();
  $('.b2bInputHeader').append(error);

  // REMOVE VALUE ON LOGIN FOR GRAPHIC BUTTON
  $('#B2BLogin_TD .SubmitButton_B2Blogin').val(" ");

  //INSERT MARGIN ON TEXT
  $('#B2BLogin_TD').find('td').each(function (index) {
    if ($(this).text() == "Brugernavn") {
      $(this).css('padding-left', '10px');
    }


    if ($(this).text() == "Password") {
      $(this).css('padding-left', '10px');
    }
  });

}

function AddB2BFix() {
  if (/addb2b.html/i.test(window.location)) {
    $('#B2BLogin_TD .SubmitButton_B2Blogin').removeClass('SubmitButton_B2Blogin').addClass('SubmitButton_AddB2B');
  }
}




