/**
 * @author ehandelsbureauet
 * this script is copyrighted and is not allowed to be used in any way without permission.
 *
 **/

image_url = new Array();
preLoadImgPrMenu();



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
 */


function init(){
    fixPrMenuSpanTagMenu();
    performImgScript();
    systemCategories();
    insertFocusOnSearch();
    categoryPictureList();
    switchPicRelated();
    repairBtn();
    removePrMenuBr();
    fixTopMenu();
}

function removePrMenuBr() {
    $("#ProductmenuContainer_DIV > br").hide();
}

function fixTopMenu() {
    $("#ShopLayOut_Topmenu_TD a:last").addClass("specialColor");
    $("#ShopLayOut_Topmenu_TD a").wrap('<li></li>').append('<li>|</li>');
    $("#ShopLayOut_Topmenu_TD li").wrapAll('<ul></ul>');
    $("#ShopLayOut_Topmenu_TD li:last").remove();
}

function switchPicRelated() {
    if (document.getElementById('relWrap')) {
        var relWrapImg = document.getElementById('relWrap').getElementsByTagName('IMG');
        for (var i = 0; i < relWrapImg.length; i++) {
            if (relWrapImg[i].className == "Related_BuyButton_IMG") {
                relWrapImg[i].src = "/images/design/source/buy_btn.gif";
            }
        }
    }
}


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 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");
}
