/**
* @author ehandelsbureauet
* this script is copyrighted and is not allowed to be used in any way without permission
* MyTrendyPhone_nl
*/


image_url = new Array();


function init() { //GENERAL FUNCTION WHICH FIRES B4 BODY TAG END	
  xmaspresentStyler();
  fixPrMenuSpanTagMenu();
  sortListEngine();
  categoryPictureList();
  removeUnsafeElm();
  performImgScript();
  backBtnOrderStep();

  b2bBtnChange();

  changeClubTrendyButton();
}


function changeClubTrendyButton() {
  if ($('.Heading_B2Blogin h2').text() == "Club Trendy Inloggen") {
    $("#B2BLogin_TD input[name='submit']").css({
      'background-image': 'url("/images/design/design/nl/egnesider/login_knap.gif")',
      'width': '60px'
    })
  }
}
function fixPrMenuSpanTagMenu() {
  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 b2bBtnChange() {
  if ($('#ShopLayOut_Topmenu_TD a').text() == "B2BLOGOUT") {
    $('#logoutBtn').css({ 'visibility': 'visible' })
    $('#logoutName').load('/shop/customer.html #Field2_0 .TextInputField_Customer', function () {
      moveTextFromLogin();
    });
  }
}

function moveTextFromLogin() {
  var htmlTxt = $('#logoutName input').val() + ", U bent nu ingelogd.";
  $('#logoutName input').remove();
  $('#logoutName').html(htmlTxt);
}


/*FREIGHTLINK ON INFOCARDS*/

$(document).ready(function () {
  divBoxHeight = ($(document).height());
  divBoxWidth = ($(window).width());
});

$(window).scroll(function () {
  $("#liteBoxP").css("top", $(window).scrollTop() + (20) + "px");
  $("#returnBtn").css("top", $(window).scrollTop() + (430) + "px");
});



function fundMe(theURL) {
  document.getElementById('ShopLayOut_SpaceTop_TD').innerHTML = "<div id='popItBg'></div><div id='popIt'></div>";
  document.getElementById('popItBg').style.display = "block";
  document.getElementById('popIt').style.display = "block";
  document.getElementById('popIt').innerHTML = ("<div id='liteBoxP'><div id='liteBoxTopLogo'></div><iframe src=") + theURL + (" height='350px' width='850px' frameborder='0'></iframe></div><div id='returnBtn'><a href='#' onclick='return closeWin();'><img src='/images/design/design/nl/egnesider/ltbox_close.gif' /></a></div>");

  var divBoxBg = document.getElementById('popItBg');
  divBoxBg.style.height = ((divBoxHeight) + "px");
  divBoxBg.style.width = (divBoxWidth + "px");


  var divBox = document.getElementById('popIt')
  divBox.style.height = (divBoxHeight + "px");
  divBox.style.width = (divBoxWidth + "px");

  document.getElementById('liteBoxP').style.left = ((divBoxWidth / 2) - 425 + "px");
  document.getElementById('returnBtn').style.left = ((divBoxWidth / 2) - 425 + "px");
  $("#liteBoxP").css("top", $(window).scrollTop() + (20) + "px");
  $("#returnBtn").css("top", $(window).scrollTop() + (430) + "px");

  return false;
}
function closeWin() {
  document.getElementById('popIt').style.display = "none";
  document.getElementById('popItBg').style.display = "none";
  document.getElementById('ShopLayOut_SpaceTop_TD').innerHTML = "";
  return false;
}



/*XMAS PRMENU */
function xmaspresentStyler() {
  var findXmas = document.getElementById('ProductMenu_Table');
  var findXmasSpan = findXmas.getElementsByTagName('SPAN');
  for (var i = 0; i < findXmasSpan.length; i++) {
    if (findXmasSpan[i].className == "xmasColor") {
      findXmasSpan[i].parentNode.parentNode.className = "xmasParentColor";
    }
  }
}



function backBtnOrderStep() {
  if (document.getElementById('OrderStep3_TD')) {
    var findBtn = document.getElementById('OrderStep3_TD').getElementsByTagName('IMG');
    for (var i = 0; i < findBtn.length; i++) {
      if (findBtn[i].src == "http://www.mytrendyphone.nl/images/design/design/nl/buyprocess/accept.gif") {
        findBtn[i].align = "right";
        findBtn[i].style.position = "relative";
        findBtn[i].style.top = "20px";
      }
    }
  }
}


function removeUnsafeElm() {
  var seekProtocol = (("https:" == document.location.protocol) ? "secure" : "nonsecure");
  if (seekProtocol == "nonsecure") {
    document.write(unescape("%3C%21--%20BEGIN%20HumanTag%20Monitor.%20DO%20NOT%20MOVE%21%20MUST%20BE%20PLACED%20JUST%20BEFORE%20THE%20/BODY%20TAG%20--%3E%3Cscript%20language%3D%27javascript%27%20src%3D%27http%3A//server.iad.liveperson.net/hc/48940962/x.js%3Fcmd%3Dfile%26file%3DchatScript3%26site%3D48940962%26%26imageUrl%3Dhttp%3A//server.iad.liveperson.net/hcp/Gallery/CallButton-Gallery/English/General/1a%27%3E%20%3C/script%3E%3C%21--%20END%20HumanTag%20Monitor.%20DO%20NOT%20MOVE%21%20MUST%20BE%20PLACED%20JUST%20BEFORE%20THE%20/BODY%20TAG%20--%3E"));
    document.write(unescape("%3Cscript%20type%3D%22text/javascript%22%20src%3D%22http%3A//s3.amazonaws.com/new.cetrk.com/pages/scripts/0010/5301.js%22%3E%20%3C/script%3E%0A"));


    // FEEDBACK FORM
    document.write(unescape("%0D%0A%0D%0A%3C%21--%20Start%20Kampyle%20Css%20--%3E%0D%0A%3Clink%20rel%3D%22stylesheet%22%20type%3D%22text/css%22%20media%3D%22screen%22%20href%3D%22http%3A//cf.kampyle.com/k_button.css%22%20/%3E%0D%0A%3C%21--%20End%20Kampyle%20Css%20--%3E%0D%0A%0D%0A%3C%21--Start%20Kampyle%20Feedback%20Form%20Button--%3E%3Cdiv%20id%3D%22k_close_button%22%20class%3D%22k_float%20kc_bottom%20kc_right%22%3E%3C/div%3E%3Cdiv%3E%3Ca%20href%3D%22http%3A//www.kampyle.com/solutions/website-feedback-form/%22%20%20target%3D%22kampyleWindow%22%20id%3D%22kampylink%22%20class%3D%22k_float%20k_bottom_sl%20k_right%22%20onclick%3D%22javascript%3Ak_button.open_ff%28%27site_code%3D9322450%26amp%3Bform_id%3D19374%26lang%3Dnl%27%29%3Breturn%20false%3B%22%3E%3Cimg%20src%3D%22http%3A//www.mytrendyphone.com/images/nl-band-low-right.gif%22%20alt%3D%22Website%20Feedback%22%20border%3D%220%22/%3E%3C/a%3E%3C/div%3E%3Cscript%20src%3D%22http%3A//cf.kampyle.com/k_button.js%22%20type%3D%22text/javascript%22%3E%3C/script%3E%0D%0A%3Cscript%20type%3D%22text/javascript%22%20src%3D%22http%3A//cf.kampyle.com/k_push.js%22%3E%3C/script%3E%3C%21--End%20Kampyle%20Feedback%20Form%20Button--%3E"));
  }
  if (seekProtocol == "secure") {
    document.getElementById('breadCrumbContainer').style.display = "none";
  }
}
// WOGWCLASSLIB
function performImgScript() {
  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 preLoadImgPrMenu() { //PRELOAD MENU
  if (document.images) {
    preload_image_object = new Image();
    // set image url

    image_url.push = "/images/design/design/construction/prMenubackHover.jpg";
    image_url.push = "/images/design/design/construction/prMenuback.jpg";
    image_url.push = "/images/design/design/construction/prMenubackniv2Hover.jpg";
    image_url.push = "/images/design/design/construction/prMenubackniv2.jpg";
    image_url.push = "/images/design/design/construction/prMenubackActive.jpg";
    var i = 0;
    for (i = 0; i <= image_url.length; i++)
      preload_image_object.src = image_url[i];
  }
}



function shiftImgAddThis() { //REPLACE ADDTHIS IMAGE
  if (document.getElementById('shiftImgAddThis')) {
    var getImgID = document.getElementById('shiftImgAddThis');
    var getImg = getImgID.getElementsByTagName('IMG');
    getImg[0].setAttribute('src', '/images/design/design/com/prcard/share_com.gif');
  }
}


function unHideDescription() {
  document.getElementById("prCardLargeText").style.display = "block";

  document.getElementById('prCardDescriptionA').style.display = "none";
  document.getElementById('prCardDescriptionImgLess').style.display = "block";

  return false;
}

function HideDescription() {
  document.getElementById("prCardLargeText").style.display = "none";

  document.getElementById('prCardDescriptionImgLess').style.display = "none";
  document.getElementById('prCardDescriptionA').style.display = "block";
  return false;
}

function hideIfNoContent() {
  var DivText = (document.getElementById("prCardLargeText"));
  var DivTextContent = DivText.firstChild;
  if (DivTextContent.firstChild == null) {
    document.getElementById("prCardDescriptionImg").style.display = "none";
    return;
  }

}





//CATEGORY FPAGESCRIPT //
function categoryPictureList() {
  if (!document.getElementById('listSorterWrap')) {
    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      
    if (document.getElementById("ProductMenu_Table")) {
      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 += "";
            a[i].innerHTML += innerHTML;
            a[i].innerHTML += "";
          }
        }
      }
    }
    //MEMORY CLEAN
    var imgs = {};
    var img = "";

  }


}

//CATEGORY FPAGESCRIPT //
function sortListEngine() {
  if (document.getElementById('listSorterWrap')) {

    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      
    if (document.getElementById("ProductMenu_Table")) {
      var productMenu = document.getElementById("ProductMenu_Table");
      var img = productMenu.getElementsByTagName("IMG");
      for (var i = 0; i < img.length; i++) {

        //if (window.location != "http://www.mytrendyphone.nl/shop/categorie%ebn-2418s.html") {
        img[i].src = img[i].src.replace(/-h.jpg/, "-s.jpg") //optimized script
        // }


        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].title = innerHTML;
            a[i].innerHTML = "";
            a[i].appendChild(img);
            a[i].innerHTML += "";
            a[i].innerHTML += innerHTML;
            a[i].innerHTML += "";
          }
        }
      }
    }
    //PRMENULIST CLEAN UP AND STYLE DIVISIONS
    var divisions = document.getElementById("ProductMenu_Table");
    var divisionsImgs = divisions.getElementsByTagName("IMG");
    for (var i = 0; i < divisionsImgs.length; i++) {

      if (divisionsImgs[i].src.match(/sorter_img.gif$/i)) {
        divisionsImgs[i].parentNode.parentNode.style.display = "none";
      }
    }



    //INNER FUNCTION BEGIN SORT
    var titles = new Array();
    var links = new Array();
    var imagearray = new Array();





    var productMenu = document.getElementById("listSorterWrap");
    var img = productMenu.getElementsByTagName("IMG");

    for (var i = 0; i < img.length; i++) {
      imagearray.push(img[i].src)
      links.push(img[i].parentNode.href);
      titles.push(img[i].parentNode.title);
    }

    productMenu.innerHTML = "";

    //INNER FUNCTION SWAP A END			

    var ProductList_TD = document.getElementById('listSorterWrapIns');

    //BEGIN INSERTION WITH DIV WITH OPTIONBOX AND LINKS

    // floatboxes
    var prSortOptWrap = document.createElement("div");
    prSortOptWrap.id = "prSortOptWrap";
    var prSortOpt1 = document.createElement("div");
    prSortOpt1.id = "prSortOpt1";
    var prSortOpt2 = document.createElement("div");
    prSortOpt2.id = "prSortOpt2";


    document.write("<div id='formFinderWrap'><form name='indexForm' id='indexForm'><select id='indexFormBox' name='indexFormBox' onchange='location.href=indexForm.indexFormBox.options[selectedIndex].value'><option value='1'>" + ("Bladeren ") + (document.getElementById('catNameH2Sorter').innerHTML) + "</option></select></form></div>");

    var selectFinder = document.getElementById('indexFormBox');


    //insert link header and elements
    for (var i = 0; i < imagearray.length; i++) {
      if (imagearray[i].match(/sorter_img.gif$/i)) {
        var tempOptHead = document.createElement("OPTGROUP");
        tempOptHead.label = (titles[i]);
        selectFinder.appendChild(tempOptHead);
        //create div2 as well
        var tempLink2 = document.createElement("A");
        tempLink2.href = ("#" + (titles[i]));
        tempLink2.innerHTML = (titles[i])
        prSortOpt2.appendChild(tempLink2);
      }
      if (!imagearray[i].match(/sorter_img.gif$/i)) {
        var tempOptLink = document.createElement("OPTION");
        tempOptLink.value = (links[i]);
        tempOptLink.innerHTML = " - " + titles[i];
        selectFinder.appendChild(tempOptLink);
      }
    }


    //total box insert
    prSortOptWrap.appendChild(prSortOpt1);
    prSortOptWrap.appendChild(prSortOpt2);
    ProductList_TD.appendChild(prSortOptWrap);

    var formFinder = document.getElementById('formFinderWrap');
    prSortOpt1.innerHTML = formFinder.innerHTML;
    formFinder.parentNode.removeChild(formFinder);

    // PROCEED SORTING

    for (var i = 0; i < imagearray.length; i++) {
      //SORTERIMG INSERT
      if (imagearray[i].match(/sorter_img.gif$/i)) {
        var prSortBreakerWrap = document.createElement("div");
        prSortBreakerWrap.className = "prSortBreakerWrap";
        var prSortBreaker = document.createElement("div");
        prSortBreaker.className = "prSortBreaker";
        var prSortParagraph = document.createElement("P");
        var nameLinkT = document.createTextNode(titles[i]);
        prSortParagraph.appendChild(nameLinkT);
        prSortBreaker.appendChild(prSortParagraph);
        prSortBreakerWrap.appendChild(prSortBreaker);


        //insert anchor

        var txt = (titles[i]);
        document.write("<DIV ID='" + (titles[i]) + "'>" + txt.anchor(titles[i]) + "'</DIV>");
        var divTempp = document.getElementById(titles[i]);
        ProductList_TD.appendChild(divTempp);
        divTempp.className = "prSortAnchorWrap";
        ProductList_TD.appendChild(prSortBreakerWrap);
      }

      if (!imagearray[i].match(/sorter_img.gif$/i)) {


        //LINK INSERT PIX AND LINK
        var tempDIV = document.createElement("div");
        tempDIV.className = "prSorterFloatImg";
        var tempIMG = document.createElement("img");
        tempIMG.src = imagearray[i];
        var tempA = document.createElement("A");
        tempA.href = links[i];
        tempA.title = titles[i];
        var tempA2 = document.createElement("A");

        tempA2.className = "prSorterFloatA";
        tempA2.href = links[i];
        tempA2.title = titles[i];

        //INSERTION
        tempDIV.appendChild(tempA);
        tempA.appendChild(tempIMG);
        tempDIV.appendChild(tempA2)
        tempA2.innerHTML = titles[i];
        ProductList_TD.appendChild(tempDIV);
      }
    }
    var tempDIV = document.createElement("div");
    tempDIV.className = "clearBoth";
    ProductList_TD.appendChild(tempDIV);
  }
}



