/**
 * @author ehandelsbureauet
 * this script is copyrighted and is not allowed to be used in any way without permission
 */
image_url = new Array();
//performImgScript();


function init(){ //GENERAL FUNCTION WHICH FIRES B4 BODY TAG END	
     xmaspresentStyler();
	fixPrMenuSpanTagMenu();
    sortListEngine();
    categoryPictureList();
    removeUnsafeElm();
    performImgScript();
    backBtnOrderStep();
   
    customerProfileChange();
    b2bBtnChange();
    
}


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 (document.getElementById('B2BLogin_TD')) {
        var findH2 = document.getElementById('B2BLogin_TD').getElementsByTagName('H2');
        var findInputBtn = document.getElementById('B2BLogin_TD').getElementsByTagName('INPUT');
        for (var i = 0; i < findH2.length; i++) {
            if (findH2[i].innerHTML == "Club Trendy login") {
                for (var i = 0; i < findInputBtn.length; i++) {
                    if (findInputBtn[i].className == "SubmitButton_B2Blogin") {
                        findInputBtn[i].className = "SubmitButton_login"
                    }
                }
            }
        }
    }
}


function customerProfileChange(){
    if (document.getElementById('country')) {
    
        document.getElementById('lzipcode').parentNode.previousSibling.innerHTML = "Zip code *<br />(not for Ireland)<br /><br/>";
        document.getElementById('lzipcode').style.width = "165px";
        document.getElementById('zipcode').parentNode.previousSibling.innerHTML = "Zip code *<br />(not required for Ireland)<br /><br/>";
        if (document.getElementById('country').options[document.getElementById('country').selectedIndex].text == "Ireland") {
            document.getElementById('zipcode').value = "****";
        }
        if (document.getElementById('lcountry').options[document.getElementById('lcountry').selectedIndex].text == "Ireland") {
            document.getElementById('lzipcode').value = "****";
        }
        if (document.getElementById('country').options[document.getElementById('country').selectedIndex].text != "Ireland") {
            if (document.getElementById('zipcode').value == "****") {
                document.getElementById('zipcode').value = ""
            };
                    }
        if (document.getElementById('lcountry').options[document.getElementById('lcountry').selectedIndex].text != "Ireland") {
            if (document.getElementById('lzipcode').value == "****") {
                document.getElementById('lzipcode').value = ""
            };
                    }
    }
    runTest = setTimeout("customerProfileChange();", 1000);
    
}






/*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('jqueryHolder').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/com/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('jqueryHolder').innerHTML = "";
    return false;
}


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.com/images/design/design/com/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("%3Cscript src='http:\/\/server.iad.liveperson.net\/hc\/48940962\/x.js?cmd=file&file=chatScript3&site=48940962&&imageUrl=http:\/\/server.iad.liveperson.net\/hcp\/Gallery\/CallButton-Gallery\/English\/General\/1a' type='text\/javascript'%3E%3C\/script%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"));*/
		
		
document.write(unescape("%3C%21--Start%20Kampyle%20Exit-Popup%20Code--%3E%20%0D%0A%3Cscript%20type%3D%22text/javascript%22%3E%0D%0Avar%20k_push_vars%20%3D%20%7B%0D%0A%20%20%20%20%20%22display_after%22%3A%2030%2C%0D%0A%09%22view_percentage%22%3A%2030%2C%0D%0A%09%22popup_font_color%22%3A%20%22%23000000%22%2C%0D%0A%09%22popup_background%22%3A%20%22%23ffffff%22%2C%0D%0A%09%22popup_separator%22%3A%20%22%23D4E2F0%22%2C%0D%0A%09%22header%22%3A%20%22Your%20feedback%20is%20important%20to%20us%21%22%2C%0D%0A%09%22question%22%3A%20%22Would%20you%20be%20willing%20to%20give%20us%20a%20short%20%281%20minute%29%20feedback%3F%22%2C%0D%0A%09%22footer%22%3A%20%22Thank%20you%20for%20helping%20us%20improve%20our%20website%22%2C%0D%0A%09%22remind%22%3A%20%22Remind%20me%20later%22%2C%0D%0A%09%22remind_font_color%22%3A%20%22%233882C3%22%2C%09%0D%0A%09%22yes%22%3A%20%22Yes%22%2C%0D%0A%09%22no%22%3A%20%22No%22%2C%0D%0A%09%22text_direction%22%3A%20%22ltr%22%2C%0D%0A%09%22images_dir%22%3A%20%22http%3A//cf.kampyle.com/%22%2C%0D%0A%09%22yes_background%22%3A%20%22%2376AC78%22%2C%0D%0A%09%22no_background%22%3A%20%22%238D9B86%22%2C%0D%0A%09%22site_code%22%3A%209322450%0D%0A%7D%0D%0A%3C/script%3E%0D%0A%3C%21--End%20Kampyle%20Exit-Popup%20Code--%3E%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%0D%0A%0D%0A%0D%0A%0D%0A%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%3D19227%26lang%3Den%27%29%3Breturn%20false%3B%22%3E%3Cimg%20src%3D%22http%3A//www.mytrendyphone.com/images/en-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"));


    }
}




// 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.com/shop/categories-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 += "";
                    }
                }
            }
            a = "";
            
        }
        //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'>" + ("Browse ") + (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);
    }
}



