﻿var SiteLib = function() {
    var _playerId = 0;
    var _siteBase = window.location.protocol + "//" + window.location.host + (SITEROOT)?SITEROOT:"";
    var _documentBase = window.location.href.substring(0, window.location.href.lastIndexOf("/") + 1);

    function _insertTOC() {
        var _container = null;
        var _content = null;

        if ((_container = document.getElementById("table_of_content")) && (_content = document.getElementById("div_content_main"))) {
            var _toc = _createTOC(_content);
            var _nodes = _getToc(_toc);
            _container.appendChild(_nodes);
        }
    }
    function _createTOC(el) {
        /// <summary>
        /// Creates an array containing all headings
        /// </summary>
        var _toc = [];

        $(el).find('h1,h2,h3').addClass('gen_toc_element');
        var selectedElements = $(el).find('.gen_toc_element');
        var _lvl = 0;
        var _text = null;
        selectedElements.each(function(i, _h) {
            _h.id = "anc_" + i;
            _text = _h.innerHTML;
            _text = _text.replace(/<.*?>/g, "");
            _h.innerHTML = _text;
            _toc.push(
                [Number(_h.tagName.substring(1)), "anc_" + i, _text]
            );
            $(_h).before("<a name=\"anc_" + i + "\"></a>");
        });
        selectedElements.removeClass("gen_toc_element");
        return _toc;
    }
    function _getToc(_toc) {
        //passe på at vi står på topp
        var _lvl = _toc[0][0];
        var _root = _ul = document.createElement("ul");
        var _li = null;
        _ul.appendChild(_li = document.createElement("li"));
        _li.innerHTML = "<a href=\"#anc_top\">Til Toppen</a>";
        var i = null;
        for (i = 1; i < _lvl; i++) {
            _li = document.createElement("li");
            _ul.appendChild(_li);
            _ul = document.createElement("ul");
            _li.appendChild(_ul);
        }

        var _item = null;
        for (i = 0; i < _toc.length; i++) {
            _item = _toc[i];
            if (_lvl < _item[0]) {
                //gå ned
                _ul.appendChild(_li = document.createElement("li"));
                _li.appendChild(_ul = document.createElement("ul"));
            }
            if (_lvl > _item[0]) {
                //gå opp
                _ul = _ul.parentNode.parentNode;
            }

            _lvl = _item[0];
            _li = document.createElement("li");
            _li.innerHTML = "<a href=\"#" + _item[1] + "\">" + _item[2] + "</a>";
            _ul.appendChild(_li);
        }
        return _root;
    }
    function _extendImages() {
        $("img.ext_imagetext").each(function() {
            var _div = null;
            this.parentNode.insertBefore(_div = document.createElement("div"), this);
            _div.className = "Article_ImageAndText";

            if ($.browser.msie) {
                _div.style.styleFloat = this.style.styleFloat;
            } else {
                _div.style.cssFloat = this.style.cssFloat;
            }

            _div.appendChild(this);
            _div.style.width = $(this).width() + "px";
            var _imageText = document.createElement("div");
            _imageText.innerHTML = this.alt;
            _div.appendChild(_imageText);

        });
        $("img.ext_imagepopup").bind("click", function(e) {
            _getImageData(this, this.getAttribute("entityid"));
        });

        function _getImageData(obj, _id) {
            if (!_id) {
                _id = obj.id.substring(5);
            }
            Brik.Cms.Site.WebServices.Core.getImage(_id, function(response) {
                _displayImagePopup(obj, response);
            });
        }

        function _displayImagePopup(img, response) {
            var _pos = $(img).offset();
            var winwidth = $(window).width() / 2;
            winwidth = winwidth - 350;

            var largeDiv = document.createElement("div");
            largeDiv.style.position = "absolute";
            largeDiv.style.width = "700px";
            largeDiv.style.top = _pos.top + "px";
            largeDiv.style.left = winwidth + "px";
            largeDiv.style.border = "inset 1px black";
            largeDiv.style.backgroundColor = "white";
            var heading = document.createElement("div");
            //heading.style.borderBottom="1px dotted grey";
            var headingleft = document.createElement("div")
            headingleft.appendChild(document.createTextNode(response.Name));
            headingleft.style.width = "594px"
            headingleft.style.fontSize = "12px";
            headingleft.style.padding = "3px";
            var headingright = document.createElement("div")
            headingright.style.width = "94px";
            headingright.style.padding = "3px";
            headingright.style.cursor = "pointer";
            headingright.appendChild(document.createTextNode("[Steng]"));
            //    headingright.onclick="document.body.removeChild(largeDiv)";

            headingright.style.zIndex = "100";
            headingright.style.textAlign = "right";
            if (navigator.appName == "Microsoft Internet Explorer") {
                headingleft.style.styleFloat = "left";
                headingright.style.styleFloat = "left";
                headingright.onclick = function() {
                    document.body.removeChild(this.parentNode.parentNode)
                };
            }
            else {
                headingright.setAttribute("onclick", "document.body.removeChild(this.parentNode.parentNode)");
                headingleft.style.cssFloat = "left";
                headingright.style.cssFloat = "left";
            }

            heading.appendChild(headingleft)
            heading.appendChild(headingright)
            largeDiv.appendChild(heading);
            var limg = document.createElement("img")
            limg.setAttribute("src", "/image" + response.Id + "@700x0.jpg");

            largeDiv.appendChild(limg);
            var t = document.createElement("div");
            t.appendChild(document.createTextNode(response.Comment));
            largeDiv.appendChild(t);
            document.body.appendChild(largeDiv);
        }
    }
    function _searchGoogle(query) {
        var win = window.open("http://www.google.com/search?hl=en&lr=&domains=" + window.location.host + "&q=" + query + "&btnG=Search&sitesearch=" + window.location.host);
    }

    return {
        videoFormats: {
            FLV: "flv",
            SWF: "swf"
        },
        insertVideo: function(/*Integer*/_videoId, /*String*/_container, /*videoFormats*/_format, /*Integer*/_width, /*Integer*/_height) {
            _playerId++;
            var _playerId = "player_" + _playerId;
            var _element = (typeof _container==="string") ? $get(_container) : _container;
            if (_element.onclick) {0
                _element.onclick = null;
            }
            flashembed(_container, {
                src: _siteBase + '/WebResource.axd?d=opPeVOW0FsF0WbDtT758Ich96eeQG2O_yEocEOGn8zVroNA8OepRXUeTr1xJo3LHhYF8jEsULYxIU3qmPgKpDQ2&t=634069497583069901',
                width: _width,
                height: _height,
                expressInstall: _siteBase + '/WebResource.axd?d=opPeVOW0FsF0WbDtT758Ich96eeQG2O_yEocEOGn8zUuJFih2joJyDpLLYNBifEzG3q0DJQBpgi3NGWMFkapxg2&t=634069497583069901',
                quality: "best",
                wmode: "transparent",
                allowfullscreen: "true",
                allowscriptaccess: "always"
            }, { config: {
                videoFile: _siteBase + "/Content/Videos/video" + _videoId + ".flv",
                autoPlay: true,
                initialScale: "fit",
                showMenu: false,
                usePlayOverlay: false,
                hideControls: false,
                controlsOverVideo: 'ease'
            }
            });
        },
        flashEmbed: function(params, config) {
            flashembed(params.container, {
                src: _siteBase + '/WebResource.axd?d=opPeVOW0FsF0WbDtT758Ich96eeQG2O_yEocEOGn8zVroNA8OepRXUeTr1xJo3LHhYF8jEsULYxIU3qmPgKpDQ2&t=634069497583069901',
                width: params.width,
                height: params.height,
                expressInstall: _siteBase + '/WebResource.axd?d=opPeVOW0FsF0WbDtT758Ich96eeQG2O_yEocEOGn8zUuJFih2joJyDpLLYNBifEzG3q0DJQBpgi3NGWMFkapxg2&t=634069497583069901',
                quality: "best",
                wmode: "transparent",
                allowfullscreen: "true",
                allowscriptaccess: "always"
            }, { config: config
            });
        },
        getBaseURL: function() {
            return _siteBase;
        },
        navigate: function(_url, _new, _settings) {
            if (typeof _url !== "string") {
                if (_url.nodeName == "A") {
                    _url = _url.href;
                } else {
                    _url = $(_url).find('a:first').attr('href')
                }
            }

            if (_url.substring(0, 4) !== "http") {
                if (_url.substring(0, 1) == "/") {
                    _url = _siteBase + _url;
                } else {
                    _url = _documentBase + _url;
                }
            }
            if (_new) {
                if (typeof _settings === "object") {
                    var _str = "";
                    for (var x in _settings) {
                        _str += x + "=" + _settings[x] + ",";
                    }
                    window.open(_url, "external", _str.substring(0, _str.length - 1));
                } else {
                    window.open(_url);
                }
            } else {
                window.location = _url;
            }
            return false;
        },
        search: {
            search: function(query) {
                _searchGoogle(query);
            },
            searchGoogle: function(query) {
                _searchGoogle(query);
            }
        },
        setRoot: function setRoot(root) {
            _siteBase = _siteBase + root;
        },
        init: function() {
            _extendImages();
            _insertTOC();
        }
    };
}
();

function pageLoad(sender, args) {
    $(window).bind("load", function() {
        SiteLib.init();
    });
}
if(typeof(Sys)!=='undefined')Sys.Application.notifyScriptLoaded();