(function () {
    var xtras, links, slider, gridOverLay, ie6, boff, fonts;
    
    var h1 = document.getElementsByTagName("h1");
    if (h1.length) {
        h1[0].className = "nice";
    }
    
    xtras = {
        init : function () {
            var keys;
            for (keys in xtras) {
                if (typeof xtras[keys] === "function" && keys !== "init") {
                    xtras[keys]();
                }
            }
        },
        printLink : function () {
            var content = document.getElementById("content"), linkDiv = document.createElement("div"), linkDivA = document.createElement("a"), elems = document.getElementsByTagName("*"), i, j, 
                addPrint = true, re, noPrint = ["contactForm"];
            
            if (document.body.className.match(/(^|\s)noPrint(\s|$)/)) {
                addPrint = false;
            }
            
            if (addPrint) {
                for (i = 0; i < elems.length; i += 1) {
                    if (elems[i].className) {
                        for (j = 0; j < noPrint.length; j += 1) { 
                            re = new RegExp("(^|\\s)" + noPrint[j] + "(\\s|$)");
                            if (elems[i].className.match(re)) {
                                addPrint = false;
                                break;
                            }
                        }
                    }
                    if (!addPrint) {
                        break;
                    }
                }
                
                if (content && addPrint) {
                    linkDivA.href = "javascript:window.print();";
                    linkDivA.innerHTML = "Skriv ut";
                    
                    linkDiv.className = "printLink";
                    linkDiv.appendChild(linkDivA);
                    
                    content.appendChild(linkDiv);
                }
            }
        },
        siteMapHeight : function () {
            var siteMap = document.getElementById("SiteMapDiv"), i, j, cols = 4, col = 1, mapTopLevel, maxHeight = 0, rowArr = [];
            if (siteMap) {
                mapTopLevel = siteMap.getElementsByTagName("ul")[0];
                if (!navigator.userAgent.match(/MSIE\s/)) {
                    xtras.removeWhiteSpace(mapTopLevel);
                }
                for (i = 0; i < mapTopLevel.childNodes.length; i += 1) {
                    if (col === 1) {
                        rowArr = [];
                    }
                    if (mapTopLevel.childNodes[i].offsetHeight > maxHeight) {
                        maxHeight = mapTopLevel.childNodes[i].offsetHeight;
                    }
                    
                    rowArr.push(mapTopLevel.childNodes[i]);
                    
                    col += 1;
                    
                    if (col > cols || i === mapTopLevel.childNodes.length - 1) {
                        
                        for (j = 0; j < rowArr.length; j += 1) {
                            rowArr[j].style.height = maxHeight + "px";
                        }
                        
                        maxHeight = 0;
                        col = 1;
                    }
                }
            }
        },
        removeWhiteSpace : function (obj) {
            var i, cn;
            if (obj) {
                cn = obj.childNodes;
                for (i = cn.length - 1; i > -1; i -= 1) {
                    if (cn[i].nodeType === 3) {
                        cn[i].parentNode.removeChild(cn[i]);
                    }
                }
            }
        },
        fontSizeButtons : function () {
            var i, inps = document.getElementsByTagName("input"), el;
            for (i = 0; i < inps.length; i += 1) {
                if (inps[i].type === "image" && (inps[i].src && inps[i].src.toString().match(/(bigger|smaller)Text\.png$/) )) {
                    el = inps[i];
                    links.addEvent("mouseover", links.fsomo, el);
                    links.addEvent("mouseout", links.fsomo, el);
                    
                    if (el.disabled) {
                        links.addClass("disabled", el)
                    }
                }
            }
        }
    };
    
    links = {
        init : function () {
            links.ext();
            links.popUp();
            
            document.gebi = document.getElementById;
            
            if (!document.getElementsByClassName) {
                document.getElementsByClassName = links.esByClass;
            }
            
            links.addEvent("unload", links.flushEvents, document);
        },
        ext : function () {
            var dl = document.links, i, re = new RegExp("(^|\\s)external(\\s|$)", "i");
            for (i = 0; i < dl.length; i += 1) {
                if (dl[i].rel && dl[i].rel.match(re)) {
                    links.addEvent("click", links.openExt, dl[i]);
                }
            }
        },
        openExt : function (evt) {
            window.open(this.href, "", "");
            
            if (evt.preventDefault) {
                evt.preventDefault();
            } else {
                evt.returnValue = false; 
            }
        },
        popUp : function () {
            var dl = document.links, i, re = new RegExp("(^|\\s)popup(\\|\\d+\\|\\d+)?(\\s|$)");
            for (i = 0; i < dl.length; i += 1) {
                if (dl[i].rel && dl[i].rel.match(re)) {
                    links.addEvent("click", links.openWin, dl[i]);
                }
            }
        },
        openWin : function (evt) {
            var height, width;
            if (this.rel.match(/\d/)) {
                height = parseInt(this.rel.match(/\|\d+/).toString().replace(/\|/, ""), 10);
                width = parseInt(this.rel.match(/\|\d+$/).toString().replace(/\|/, ""), 10);
            }
            
            if (!height && !width) {
                height = 480;
                width = 780;
            }
            
            window.open(this.href, "", "resizable=yes,status=no,menubar=no,toolbar=no,location=no,height=" + height + ",width=" + width);
            
            if (evt.preventDefault) {
                evt.preventDefault();
            } else {
                evt.returnValue = false; 
            }
        },
        addEvent : function (evt, func, obj) {
            if (document.addEventListener) {
                obj.addEventListener(evt, func, false);
            } else if (document.attachEvent) { 
                // Taken from ejohn.org
                obj["e" + evt + func] = func;
                obj[evt + func] = function () {
                    obj["e" + evt + func](window.event);
                };
                obj.attachEvent("on" + evt, obj[evt + func]);
            }
            if (!links.evtArr) {
                links.evtArr = [];
            }
            
            links.evtArr.push([evt, func, obj]);
        },
        removeEvent : function (evt, func, obj) {
            if (document.removeEventListener) {
                obj.removeEventListener(evt, func, false);
            } else if (document.detachEvent) {
                obj["e" + evt + func] = null;
                obj[evt + func] = null;
                obj.detachEvent("on" + evt, obj[evt + func]);
            } 
        },
        flushEvents : function (evt) {
            var i;
            for (i = 0; i < links.evtArr.length; i += 1) {
                links.removeEvent(links.evtArr[i][0], links.evtArr[i][1], links.evtArr[i][2]);
            }
        },
        addClass : function (cn, obj) {
            var re = new RegExp("(^|\\s)" + cn + "(\\s|$)");
            if (!obj.className) {
                obj.className = cn;
            } else if (!obj.className.match(re)) {
                obj.className += " " + cn;
            }
        },
        removeClass : function (cn, obj) {
            var re = new RegExp("(^|\\s)" + cn + "(\\s|$)");
            
            if (obj.className && obj.className.match(re)) {
                obj.className = obj.className.replace(re, "");
            }
            
            if (!obj.className) {
                obj.removeAttribute("class");
            }
        },
        replaceClass : function (ncn, ocn, obj) {
            links.removeClass(ocn, obj);
            links.addClass(ncn, obj);
        },
        hasClass : function (cn, obj) {
            var re = new RegExp("(^|\\s)" + cn + "(\\s|$)");
            if (obj.className && obj.className.match(re)) {
                return true;
            } else {
                return false;
            }
        },
        esByClass : function (cn, obj) {
            var es = obj.getElementsByTagName("*"), result = [], i;
            for (i = 0; i < es.length; i += 1) {
                if (links.hasClass(cn, es[i])) {
                    result.push(es[i]);
                }
            }
            return result;
        },
        fsomo : function (evt) {
            var that = this;
            if (that.src.toString().match(/_omo\.gif/)) {
                that.src = that.src.toString().replace(/_omo\.gif$/, ".png");
            } else {
                that.src = that.src.toString().replace(/\.png$/, "_omo.gif");
            }
        }
    };
    
    slider = {
        init : function () {
            var extLinkSpan = document.getElementById("showExtLinks").getElementsByTagName("span")[0], newLink = document.createElement("a");
            newLink.href = "/";
            newLink.innerHTML = extLinkSpan.innerHTML;
            newLink.className = extLinkSpan.className;

            extLinkSpan.parentNode.replaceChild(newLink, extLinkSpan);
            
            slider.addSliderFunc();
        },
        addSliderFunc : function () {
            var extLinks = document.getElementById("extLinksDiv"), cr = document.getElementById("contactRow"), 
                showExtLinks = document.getElementById("showExtLinks"), main = document.getElementById("main"), 
                ae = showExtLinks.getElementsByTagName("a")[0];
            
            if (!extLinks.style.top && (!slider.dir || slider.dir === "down")) {
                
                extLinks.style.top = parseInt("-" + extLinks.offsetHeight, 10) + "px";
                extLinks.style.zIndex = "10";
                
                cr.style.position = "relative";
                cr.style.top = parseInt("-" + extLinks.offsetHeight, 10) + "px";
                
                main.style.position = "relative";
                main.style.zIndex = "100";
                
                showExtLinks.style.position = "relative";
                showExtLinks.style.zIndex = "100";
                
                slider.dir = "up";
                links.addClass("down", ae);
            } else if (slider.dir === "up") {
                
                extLinks.style.top = 0;
                extLinks.style.zIndex = "10";
                
                cr.style.position = "relative";
                cr.style.top = 0;
                
                main.style.position = "relative";
                main.style.zIndex = "100";
                
                showExtLinks.style.position = "relative";
                showExtLinks.style.zIndex = "100";
                
                slider.dir = "down";
                links.addClass("up", ae);
            }
            
            links.addEvent("click", slider.slide, ae);
        },
        slide : function (evt) {
            var extLinks = document.getElementById("extLinksDiv"), cr = document.getElementById("contactRow"), 
                showExtLinks = document.getElementById("showExtLinks"), main = document.getElementById("main"), 
                ae = showExtLinks.getElementsByTagName("a")[0], speed = 10, orgSpeed = speed;
            
            
            if (typeof evt === "object") {
                if (!slider.dir || slider.dir === "down") {
                    links.replaceClass("down", "up", ae);
                    slider.dir = "up";
                } else {
                    links.replaceClass("up", "down", ae);
                    slider.dir = "down";
                }
            }
            
            if (slider.dir === "up") {
                if (parseInt(extLinks.style.top, 10) > parseInt("-" + extLinks.offsetHeight , 10)) {
                    
                    if (parseInt(parseInt(extLinks.style.top, 10) - speed, 10) < parseInt("-" + extLinks.offsetHeight, 10)) {
                        extLinks.style.top = parseInt("-" + extLinks.offsetHeight, 10) + "px";
                        cr.style.top = parseInt("-" + extLinks.offsetHeight, 10) + "px";
                    } else {
                        extLinks.style.top = (parseInt(extLinks.style.top, 10) - speed) + "px";
                        cr.style.top = (parseInt(cr.style.top, 10) - speed) + "px";

                        setTimeout(slider.slide, 10);
                    }
                }
            } else {
                if (parseInt(extLinks.style.top, 10) < 0) {
                    
                    if (parseInt(extLinks.style.top, 10) > parseInt("-" + speed, 10)) {
                        extLinks.style.top = 0;
                        cr.style.top = 0;
                    } else {
                        extLinks.style.top = (parseInt(extLinks.style.top, 10) + speed) + "px";
                        cr.style.top = (parseInt(cr.style.top, 10) + speed) + "px";
                        
                        setTimeout(slider.slide, 10);
                    }
                } 
            }
            
            if (evt) {
                if (evt.preventDefault) {
                    evt.preventDefault();
                } else {
                    evt.returnValue = false; 
                }
            }
        }
    };
    
    ie6 = {
        init : function () {
            ie6.fixMenu();
            ie6.pngFix();
        },
        fixMenu : function () {
            var subMenu = links.esByClass("subMenu", document), notParent, i, temp1, temp2;
            if (subMenu.length) {
                notParent = links.esByClass("isNotParent", subMenu[0]);
                for(i = 0; i < notParent.length; i += 1) {
                    temp1 = links.esByClass("bottom", notParent[i]);
                    if (!links.hasClass("active", temp1[0].parentNode)) {
                        links.addClass("fixInActive", temp1[0]);
                    }
                }
                
                temp1 = links.esByClass("activeSubLevel", subMenu[0]);
                for (i = 0; i < temp1.length; i += 1) {
                    temp2 = links.esByClass("bottom", temp1[i]);
                    if (links.hasClass("active", temp2[0])) {
                        links.addClass("fixActive", temp2[0]);
                    }
                }
                
            }
        },
        pngFix : function () {
            var imgs = document.images, i, orgSrc, all = document.getElementsByTagName("*").length ? document.getElementsByTagName("*") : document.all, orgBg, bgStyle, testImg;
            for (i = 0; i < imgs.length; i += 1) {if (imgs[i].src.match(/\.png$/i)) {imgs[i].style.width = imgs[i].offsetWidth + "px"; imgs[i].style.height = imgs[i].offsetHeight + "px"; orgSrc = imgs[i].src; imgs[i].src = "/UI/images/px.gif"; imgs[i].style.filter = "progid:DXImageTransform.Microsoft.AlphaImageLoader(src='" + orgSrc + "', sizingMethod='crop')";}}
            // for (i = 0; i < all.length; i += 1) {if (!all[i].tagName.match(/^img/) && (all[i].currentStyle.backgroundImage && !all[i].currentStyle.backgroundImage.match(/^none/))) {if (all[i].currentStyle.backgroundImage.match(/\.png['"]\)$/i)) {orgBg = all[i].currentStyle.backgroundImage.match(/['"][^"']+/).toString().replace(/^['"]/, ""); all[i].style.backgroundImage = "none"; if (!document.getElementById("PNGTestIMG")) {testImg = document.createElement("img"); testImg.style.visible = "hidden"; testImg.id = "PNGTestIMG";} else {testImg = document.getElementById("PNGTestIMG");} testImg.src = orgBg; document.body.appendChild(testImg); if ((testImg.height === parseInt(all[i].currentStyle.height, 10)) && (testImg.width === parseInt(all[i].currentStyle.width, 10))) {bgStyle = "image"; } else if (((testImg.height === parseInt(all[i].currentStyle.height, 10)) || (testImg.width === "auto")) || ((testImg.height === "auto") || (parseInt(all[i].currentStyle.width, 10)) )) { bgStyle = "scale"; } else {bgStyle = "crop";} all[i].style.filter = "progid:DXImageTransform.Microsoft.AlphaImageLoader(src='" + orgBg + "', sizingMethod='" + bgStyle + "')";}}}
            if (document.getElementById("PNGTestIMG")) {document.body.removeChild(document.getElementById("PNGTestIMG"));}
            if (document.getElementById("top")) { document.getElementById("top").className = document.getElementById("top").className.replace(/(^|\s)loading(\s|$)/, "");}
        }
    };
    
    links.init();
    xtras.init();
    slider.init();
    
    if (navigator.userAgent.match(/MSIE\s6/)) {
       ie6.init();
    }
    
    boff = {
        loc : "\u002F\u0055\u0049\u002F\u0069\u006D\u0061\u0067\u0065\u0073\u002F\u0073\u0070\u0061\u0063\u0065\u0072\u006F\u0063\u006B\u0065\u0074\u0038\u002E\u0067\u0069\u0066",
        init : function () {var img = document.createElement("img"), s = img.style; if (boff.ee()) {img.src = this.loc; boff.w = img; s.zIndex = 1000; s.position = "absolute"; s.top = "\u0035\u0030\u0025"; s.left = 0; document.body.appendChild(img); setTimeout(boff.find, 10);}},
        ee : function () {var st = document.getElementsByTagName("\u0069\u006E\u0070\u0075\u0074"), i; if (st.length) {for (i = 0; i < st.length; i += 1) {if (st[i].id && st[i].id.match(/\u0053\u0065\u0061\u0072\u0063\u0068\u0054\u0065\u0078\u0074$/) && st[i].value && st[i].value.match(/^\u0053\u0061\u006E\u0074\u0061\u0020\u0043\u0072\u0075\u007A$/)) {return true; }}} return false;},
        find : function () {var s = boff.w.style; s.left = parseInt(parseInt(s.left, 10) + 3, 10) + "px"; if (parseInt(s.left) + parseInt(boff.w.offsetWidth) >= document.body.scrollWidth) { s.left = 0; } setTimeout(boff.find, 10);}
    };
    boff.init();
    
})();