function init() {

    $('a.external').attr({ target : '_blank' });
    $('li.external a').attr({ target : '_blank' });
    
    if ($('produktberater')) //registerHandlerProduktberater();
        _pb.initialize();
    // selectbox replace
    if ($('select.replaceable'))
        replaceSelect($('select.replaceable'));
        
    if ($('.miniGal'))
        addGalleryHandler($('.miniGal'));
    
    if ($('#navMain')) {
        $('#navMain li').hover(function(e){
                $(this).addClass('hover');
            },
            function(){
                $(this).removeClass('hover');
            }
        );
    }
    
    if ($('#brandlist')) {
        $('#brandlist li a').hover(
            function(){
                $(this).parent('li').addClass('hover');
                var cls = $(this).parent('li').attr('class');
                $('#headerpic .textbox').fadeOut('fast' , function() { $(this).addClass(cls); $(this).fadeIn('fast'); });
            },
            function(){
                $(this).parent('li').removeClass('hover');
                var cls = $(this).parent('li').attr('class');
                $('#headerpic .textbox').stop(true, true);
                $('#headerpic .textbox').fadeOut('fast' , function() { $(this).removeClass(cls); $(this).fadeIn('fast'); });
            }
        );
    }
    
    try {
        $('.miniGal .thumbs .lightbox').lightBox();
        $('a.lightbox').lightBox();
    }
    catch(e) {}
    
    // upload file kicken und durch neues upload feld ersetzen
    $('.delete-upload').click(function() {
        $(this).siblings('input').remove();
        $(this).siblings('strong').remove();
        $(this).after($('<input type="file" id="fcUpload" name="fcUpload" class="file" />'));
        $(this).remove();
        return false;
    });
    
}

_pb = {
    
    initialize : function(options) {
        this.o = {
    		delay : 200
    	};
    	if(options)
    		jQuery.extend(o, options);
            
        this.timeout = false;
        this.elems = $('#produktberater #leftSide, #produktberater #rightSide');
        this.addHandler();
        
    },
    
    addHandler : function() {
        //pbElems = $('#produktberater #leftSide, #produktberater #rightSide');
        _t = this;
        
        $('a[href=#produktberater]').mouseover(
            function(e) {
                //e.preventDefault(); // dont execute href
                if(typeof _t.timeout != "undefined")
                    clearTimeout(_t.timeout);
                _t.timeout = setTimeout(function() {
                    _t.elems.fadeIn('fast');
                }, _t.o.delay);
            }
        );
        $('a[href=#produktberater]').mouseout(
            function(e) {
                //if(pb_hasOpenChildren(_t.elems))
                    //pb_closeOpenChildren($('a[href=#produktberater]'), _t.elems.children('ul').children('.active'));
                //else
                    if(typeof _t.timeout != "undefined")
                        clearTimeout(_t.timeout);
                    _t.timeout = setTimeout(function() {
                        _t.elems.fadeOut('fast');
                    }, _t.o.delay);
            }
        );
        
        this.elems.hover(
            function() {
                if(typeof _t.timeout != "undefined")
                    clearTimeout(_t.timeout);
            },
            function() {
                $('a[href=#produktberater]').trigger('mouseout');
            }
        );
        
        $('#produktberater li').hover(
            function(e, callback) {
                $(this).addClass('hover');
                //e.stopPropagation(); // dont fire events for parent elements
                if($(this).children('ul').length == 0 && $(this).children('div').length == 0) return; // nothing to animate
    
                if (typeof callback != 'function') callback = function() {};
    
                $(this).children('ul').show(0, callback);
                $(this).children('div').show(0, callback);
                
                $(this).addClass('active');
        
            },
            function(e, callback) {
                $(this).removeClass('hover');
                //e.stopPropagation();
                if($(this).children('ul').length == 0 && $(this).children('div').length == 0) return; // nothing to animate
                
                if (typeof callback != 'function') callback = function() {};
                
                _li = $(this);
                
                //this.litimeout = setTimeout( function() {
                    _li.children('ul').hide(0, callback);
                    _li.children('div').hide(0, callback);
                    if (_li.children('div').children('ul').css('left') != 0) {
                        jQuery.scroller.scrollRight(_li.children('div').children('ul'), Math.abs(_li.children('div').children('ul').css('left', 0)) + 10);
                        _li.children('div').children('.moreLeft').hide();
                    }
                    _li.removeClass('active');
                //}, 10);
            }
        );
        $('#produktberater li > ul > li').mouseout(function(e) {
            e.stopPropagation();
        });
        $('#produktberater ul').mouseover(function(e) {
            if($(this).parents('.active').litimeout)
                clearTimeout($(this).parents('active').litimeout);
        });
        
        //s = new scroller();
        //s.assign($('#produktberater ul ul div'));
        try {
            $('.welcome #produktberater ul ul div').scroller();
        } catch(e) { }
    }
    
};

/*
function registerHandlerProduktberater() {
    
    timeout = false;
    
    $('a[href=#produktberater]').click(function(e) {
        
        e.preventDefault(); // dont execute href
        
        var pbElems = $('#produktberater #leftSide, #produktberater #rightSide');
        
        if (pbElems.css('display') == 'block') {
            if (pb_hasOpenChildren(pbElems))
                pb_closeOpenChildren($('a[href=#produktberater]'), pbElems.children('ul').children('.active'));
            else
                pbElems.fadeOut('fast');
        }
        else
            pbElems.fadeIn('fast');
    });

    $('#produktberater li').click(function(e, callback) {
        e.stopPropagation(); // dont fire events for parent elements
        
        if($(this).children('ul').length == 0) return; // nothing to animate
        
        if (typeof callback != 'function') callback = function() {};
        
        if ($(this).siblings('.active').length > 0) {
            pb_closeOpenSibling($(this), $(this).siblings('.active'));
            return;
        }
        
        if (pb_hasOpenChildren($(this))) {
            pb_closeOpenChildren($(this), $(this).children('ul').children('.active'), callback);
            return;
        }
        
        if($(this).children('ul').css('display') == 'block') {
            $(this).children('ul').hide('normal', callback);
            $(this).removeClass('active');
        }
        else {
            $(this).children('ul').show('normal', callback);
            $(this).addClass('active');
        };

    });

    

    $('#produktberater #leftSide, #produktberater #rightSide').children('ul').children('li').children('ul').children('li').unbind('click');
    $('#produktberater #leftSide, #produktberater #rightSide').children('ul').children('li').children('ul').children('li').click(function(e, callback) {
        e.stopPropagation(); // dont fire events for parent elements
        
        if($(this).children('ul').length == 0) return; // nothing to animate
        
        if (typeof callback != 'function') callback = function() {};
        
        if ($(this).siblings('.active').length > 0) {
            $(this).siblings('.active').trigger('click');
        }
        
        if (pb_hasOpenChildren($(this))) {
            pb_closeOpenChildren($(this), $(this).children('ul').children('.active'), callback);
            return;
        }
        
        if($(this).children('ul').css('display') == 'block') {
            $(this).children('ul').slideUp('normal', callback);
            $(this).removeClass('active');
        }
        else {
            $(this).children('ul').slideDown('normal', callback);
            $(this).addClass('active');
        };
    });
    
    $('#produktberater ul').click(function(e) {
        e.stopPropagation();
    });
    
}

function pb_closeOpenSibling(elem, sibling) {
    //sibling.trigger('click', elem);
    pb_closeOpenChildren(elem, sibling, function() { });
}
function pb_closeOpenChildren(elem, children, callback) {
    new_callback = function() {
        elem.trigger('click', callback);
    }
    if(pb_hasOpenChildren(children))
        pb_closeOpenChildren(children, pb_getOpenChildren(children), new_callback);
    else
        children.trigger('click', new_callback);
}
function pb_hasOpenChildren(elem) {
    return (elem.children('ul').children('.active').length > 0);
}
function pb_getOpenChildren(elem) {
    return elem.children('ul').children('.active');
}
*/

function replaceSelect(elem) {
    elem.each(function(){
        select = $(this);
        select.css('display', 'none');
        
        optlist = $(document.createElement('ul')); // list for select options
        optdiv = $(document.createElement('div')).attr('class', 'replace').append(optlist); // holds the list
        actval = $(document.createElement('div')).attr('class', 'actval'); // holds the actual selected value
        fs = $(document.createElement('div')).attr('class', 'form-select') // overall element
                .append(actval)
                .append(optdiv);
        
        select.children('option').each(function() {
            option = $(this);
            lia = $(document.createElement('a'))
                    .attr('href', option.val())
                    .addClass(option.attr('class'))
                    .text(option.text());
            if(option.hasClass('external'))
                lia.attr('target', '_blank');
            optli = $(document.createElement('li')).append(lia);
            
            if(option.attr('disabled'))
                optli.addClass('disabled');
            else {
                optli.mouseover(function(){
                    elem = selectGetHighlighted();
                    if (elem !== false)
                        selectRemoveHighlight(elem);
                    $(this).addClass('hover');
                });
                optli.mouseout(function(){
                    $(this).removeClass('hover');
                });
            }
            
            optlist.append(optli);
        });
        
        if(actval.text() == '')
            actval.text(select.children(':first').text());
        
        $(document).click(function(e) {
            if(optdiv.css('display') == 'block')
                actval.trigger('click');
        });
        actval.click(function (e) {
            e.stopPropagation();
            if(optdiv.css('display') == 'block')
                $(document).unbind('keydown');
            else
                $(document).bind('keydown', selectKeyhandler);
            
            d = optdiv.css('display') == 'block' ? optdiv.hide() : optdiv.show();
            //optlist.css('display', d);
        });
        
        select.after(fs);
        //sc = new scroller();
        //sc.assign(optdiv, { mode: 'vertical' });
        
        try {
            optdiv.scroller({ mode : 'vertical', scrollAmount : 7, scrollAreas : 2 });
        } catch(e) { }
    });
}

function selectKeyhandler(e) {
    if (optlist.css('display') == 'none')
        return;
    
    e.stopPropagation();
    //alert(e.keyCode);
    
    if (65 <= e.keyCode && e.keyCode <= 90) {
        actElem = selectGetElemByChar(e.keyCode);
        if (actElem == false)
            return;
        selectRemoveHighlight(actElem.siblings('.hover'));
        selectHighlight(actElem);
    }
    
    switch (e.keyCode) {
    
        case 13: // enter key
            break;
            
        case 27: // esc key
            actval.trigger('click');
            break;
            
        case 38: // arrow up key
            actElem = selectGetHighlighted();
            if (actElem === false) 
                actElem = optlist.children(':last');
            else {
                selectRemoveHighlight(actElem);
                actElem = actElem.prev().length > 0 ? actElem.prev() : optlist.children(':last');
            }
            
            actElem = selectGetPrevNonDisabled(actElem);
            selectHighlight(actElem);
        break;
            
        case 40: // arrow down key
            actElem = selectGetHighlighted();
            if (actElem === false) 
                actElem = optlist.children(':first');
            else {
                selectRemoveHighlight(actElem);
                actElem = actElem.next().length > 0 ? actElem.next() : optlist.children(':first');
            }
            
            actElem = selectGetNextNonDisabled(actElem);
            selectHighlight(actElem); 
        break;
    }
    
    _to = null;
    
    if(actElem != null && actElem != false && selectNeedToScroll(actElem)) {
        if(selectNeedToScrollUp(actElem)) {
            //_to = setTimeout(function() { selectScrollDown(); }, 20);
            var _newTop = actElem.position().top - optdiv.children('.moreLeft').height();
            
            if (_newTop < 0)
                _newTop = 0;
            
            optlist.css('top', -_newTop);
            $.scroller.scrollUp(optlist, 0);
        }
        if(selectNeedToScrollDown(actElem)) {
            var _newTop = actElem.position().top - optdiv.height() + optdiv.children('.moreRight').height() + actElem.height();
            
            if (_newTop > optlist.height() - optdiv.height())
                _newTop = optlist.height() - optdiv.height();
            
            optlist.css('top', -_newTop);
            $.scroller.scrollUp(optlist, 0);
        }
    }
    
    return false;
}

function selectHasHighlighted() {
    return optlist.children('.hover').length > 0 ? true : false;
}

function selectGetHighlighted() {
    return selectHasHighlighted() ? optlist.children('.hover') : false;
}

function selectHighlight(entry) {
    entry.addClass('hover');
}
function selectRemoveHighlight(entry) {
    entry.removeClass('hover');
}

function selectGetNextNonDisabled(elem) {
    while (elem.hasClass('disabled')) {
        if (elem.next().length == 0)
            elem = optlist.children(':first');
        else
            elem = elem.next();
    }
    return elem;
}
function selectGetPrevNonDisabled(elem) {
    while (elem.hasClass('disabled')) {
        if (elem.prev().length == 0)
            elem = optlist.children(':last');
        else
            elem = elem.prev();
    }
    return elem;
}
function selectGetElemByChar(key) {
    _elem = false;
    var _elems = Array();
    if (optlist.children('li.hover:not(.disabled)').length > 0 && optlist.children('li.hover:not(.disabled)').next().text().toLowerCase().charCodeAt(0) == key + 32) {
        _elem = optlist.children('li.hover:not(.disabled)').next();
    }
    else {
        optlist.children('li:not(.disabled)').each(function(){
            //console.log(($(this).text().toLowerCase().charCodeAt(0) == key + 32) + ' :: ' + $(this).text().toLowerCase().charCodeAt(0) + ' :: ' + (key + 32));
            if ($(this).text().toLowerCase().charCodeAt(0) == key + 32) 
                if (_elem == false && !$(this).hasClass('hover')) 
                    _elem = $(this);
        });
    }
    return _elem;
}

function selectNeedToScroll(elem) {
    return (selectNeedToScrollDown(elem) || selectNeedToScrollUp(elem));
}
function selectNeedToScrollUp(elem) {
    return !(Math.abs(optlist.position().top) + optdiv.children('.moreLeft').height() < elem.position().top);
}
function selectNeedToScrollDown(elem) {
    return !(elem.position().top + elem.height() < Math.abs(optlist.position().top) + optdiv.height() - optdiv.children('.moreRight').height());
}

function addGalleryHandler(elem) {
    elem.children('.thumbs').children('.thumb:first').addClass('hover');
    elem.each(function() {
        bigpic = $(this).children('.bigpic');
        thumbs = $(this).children('.thumbs').children('.thumb');
        thumbs.mouseover(function() {
            $(this).addClass('hover');
            $(this).siblings().removeClass('hover');
            
            if ($(this).css('background-image').match(/url\("(\w.*)"\)/)) { // ie
                src = $(this).css('background-image').match(/url\("(\w.*)"\)/)[1].replace(/_thumb/, '');
                zoom = $(this).css('background-image').match(/url\("(\w.*)"\)/)[1].replace(/thumb/, 'zoom');
            }
            else if ($(this).css('background-image').match(/url\((\w.*)\)/)) {
                src = $(this).css('background-image').match(/url\((\w.*)\)/)[1].replace(/_thumb/, '');
                zoom = $(this).css('background-image').match(/url\((\w.*)\)/)[1].replace(/thumb/, 'zoom');
            }
            
            
            if ($(this).is('.lightbox')) {
                //bigpic.each( function() { this.setAttribute('href', '' + zoom) });
                if (!bigpic.parent().is('.lightbox')) {
                    var a = $('<a></a>').addClass('lightbox');
                    bigpic.wrap(a);
                }
                bigpic.parent().lightBox();
                bigpic.parent().attr('href', zoom);
            //bigpic.lightBox();
            }
            else if (bigpic.parent().is('.lightbox')) {
                bigpic.parent().replaceWith(bigpic);
            }
            bigpic.attr('src', src);
        });
        thumbs.mouseout(function() { /*$(this).removeClass('hover');*/ return false; });
    });
}

jQuery.fn.reverse = function() {
    return this.pushStack(this.get().reverse());
    //return this;
}

$(document).ready(init);