/**
 * @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
 * 
 */


function init(){
    fixPrMenuSpanTagMenu();
    performImgScript();
    systemCategories();
	insertFocusOnSearch();
	prCardDataRemoveEmptyCells();
	categoryPictureList();
	allReadyLoggedIn();
}

function allReadyLoggedIn(){
    if (document.getElementById('frontPgB2b')) {
        var findAParent = document.getElementById('frontPgB2b');
        if (findAParent.innerHTML == "B2BLogud") { 
		       findAParent.parentNode.style.display = "none";
            document.getElementById('loginTop').innerHTML = "<div class=\"SubmitButton_B2Blogout\">" + "<a href='/shop/b2blogin.html?Logout=1'><img src='/images/design/source/b2blogout.gif' /></a>" + "</div>";
			
        }
    }
}

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 == 'Description_Productinfo') {
				if (cells[i].innerHTML == "" || cells[i].innerHTML == " ") {
					cells[i].parentNode.parentNode.style.display = "none";
					cells[i].parentNode.style.display = "none";
				}
				else {
					allEmpty = false;
				}
			} 
		}
		
		if(allEmpty){
			var headlineDiv = document.getElementById('prCardDataHeadline');
			headlineDiv.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")
}

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 = "";
	
	//REDESIGN
	
	if (document.getElementById("category-picture-list")) {
		var findTags = document.getElementById("category-picture-list").getElementsByTagName("A");
		
		var findLink = [];
		var findImg = [];
		var findTxt = [];
		
		docCat = document.getElementById("category-picture-list")
		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);
		}
		docCat.innerHTML = "";
		for (var i = 0; i < findImg.length; i++) {
		
			docCat.innerHTML += "<div class='catListBox'>" + "<h2>" +
			"<a href='" +
			findLink[i] +
			"'>" +
			findTxt[i] +
			"</a></h2>" +
			"<div class='catListImgBox'>" +
			"<a href='" +
			findLink[i] +
			"'>" +
			"<img src='" +
			findImg[i] +
			"'>" +
			"</a></div>" +			
			"</div>";
		}
	}	
}


//WOGWCLASSLIB END

