var _H = _H || { };
_H.MAPPER = _H.MAPPER || { };

(function(_H) {

    var _html   = document.getElementsByTagName('html')[0];
    var _body   = document.getElementsByTagName('body')[0];

    _H.$        = function(id) {
        if (typeof id !== 'string') return id;
        return (document.getElementById)
            ? document.getElementById(id)
            : document.all[id];
    };

    _H.toggleClass    = function(element, c1, c2, forcedrepl) {
        var e = _H.$(element);
        if (0 > e.className.indexOf(c1) && 0 > e.className.indexOf(c2)) {
            var forcedrepl = forcedrepl || '!';
            if (forcedrepl === "?") e.className = [e.className, c1].join(' ');
        }
        else {
            e.className = (-1 < e.className.indexOf(c1))
                ? e.className.replace(c1, c2)
                : e.className.replace(c2, c1);
        }
    };

    _H.isIE  = ('v' == '\v'),
    _H.isIE7 = ('v' == '\v') && window.XMLHttpRequest,
    _H.isIE6 = ('v' == '\v') && !window.XMLHttpRequest,
    _H.isIE8 = ('v' == '\v') && Object.prototype.toString.call(self.JSON) === "[object JSON]" && document.documentMode > 7,
    _H.isltIE8 = ('v' == '\v') && !document.documentMode

    /*                                                 */

    _H.Overlayer = {

            open            : function(layerid) {
                   var layer = _H.$(layerid);
                   var layergroup = layer.parentNode.parentNode;
                   if (_H.isltIE8) layergroup = layergroup.parentNode;

                   if (layergroup.parentNode.tagName.toLowerCase() !== 'body') {
                        if (!_H.$('innermost_layerwrapper')) {
                            /* no layer group still exists inside body element, we
                             * append the whole layer group into the body and set
                             * the id to the innermost div as reference for next layers.
                             */
                            document.body.appendChild(layergroup);
                            var layersgroup_wrapper         = _H._bytag("div",layergroup)[0];
                            if (_H.isltIE8) layersgroup_wrapper = _H._bytag("div", layersgroup_wrapper)[0];
                            layersgroup_wrapper.id  = 'innermost_layerwrapper';
                         }
                         else {
                            /* a layer group already exists inside body element, we just
                             * append the single layer into the innermost wrapper and delete
                             * the empty inner wrappers.
                             */
                            _H.$('innermost_layerwrapper').appendChild(layer);
                            layergroup.parentNode.removeChild(layergroup);
                        }
                  };

                _html.className = 'overlayer';
                _H.toggleClass(layerid, 'openedlayer', 'closedlayer', "?");
                layergroup.style.zoom= "1";
                return false;
            },

            close           : function(layerid) {
                _html.className = '';
                _H.toggleClass(layerid, 'closedlayer', 'openedlayer', "?");
                return false;
            }

    };



    _H.Accordion = function() {

        var _list = null;
        var _currentopen = 0;

        var _addLinkHandlers = function() {

            jQuery('dd', _list).css({ "display" : "none" });


            var dt = _list.getElementsByTagName('dt');
            var ldt  = dt.length;
            while (ldt--) {
                var link = dt[ldt].getElementsByTagName('a')[0];
                link.onclick = (function() {
                    return function() {
                        var id_description = this.rel;
                        if (!_H.$(id_description)) return false;
                        if (this.id === 'currentfaq') {
                            if (_H.$(id_description)) jQuery(_H.$(id_description)).hide(300);
                            this.removeAttribute('id');
                        }
                        else {
                            if (_H.$('currentfaq')) {
                                desctoclose = _H.$('currentfaq').rel;
                                if (_H.$(desctoclose)) jQuery(_H.$(desctoclose)).hide(300);
                                _H.$('currentfaq').removeAttribute('id');
                            }

                            if (_H.$(id_description)) {
                                jQuery(_H.$(id_description)).show(300);
                            }
                            this.id = 'currentfaq';
                        }
                        return false;
                    }
                })();
            }
        };

        return {
            init    : function(id, opened) {
                if (!_H.$(id)) return false;
                _list = _H.$(id);
                _currentopen = opened || 0;
                _addLinkHandlers();
            }
        }

    };

    _H.Scrollertexts = function() {

        var _scrollerlist;
        var _scrollerlength;
        var _current = 1;
        var _timeout = 0;
        var _mutex = false;

        var _startSlide = function(inc) {
            _current += inc;
            jQuery("#prevsc").toggleClass("disabled", _current === 1);
            jQuery("#nextsc").toggleClass("disabled", _current === _scrollerlength);

            var pos = -800 * (_current - 1);
            jQuery(_scrollerlist).animate({"left" : pos}, _timeout, function() {
                _mutex = false;

            });
        };


        var _addLinkHandlers = function() {

            var allowmoving     = function(inc) {
                if (_mutex === false) {
                    _mutex = true;
                    _startSlide(inc);
                }
            }

            _H.$('prevsc').onclick = function() {
                if (_current > 1) allowmoving(-1);
                return false;
            };

            _H.$('nextsc').onclick = function() {
                if (_current < _scrollerlength) allowmoving(1);
                return false;
            };

        };

        return {

            init    : function(id, timeout) {
                _timeout = timeout || 2000;
                if (!_H.$(id)) throw new Error("id not found");
                _H.$('prevsc').style.visibility = 'visible';
                _H.$('nextsc').style.visibility = 'visible';
                _scrollerlist = _H.$(id).getElementsByTagName('ul')[0];
                _scrollerlength = _scrollerlist.getElementsByTagName('li').length;
                if ('\v' == 'v') _scrollerlist.style.width = [(800 * _scrollerlength),'px'].join('')

                jQuery("#nextsc").toggleClass("disabled", _scrollerlength === 1);
                _addLinkHandlers();
            }
        }
    };

})(_H);