
/* pouze pokud je jQuery */

function mixer() {
    /* zamicha reklamni zony s class="mixer" a podle mixgroup="name" */
    var groupname = 'mixgroup'
    var elems = []
    $(".mixer").each( function(index) {
        if ( $(this).attr(groupname) ) {
            $(this).attr('mix-index',index)
            elems.push( $(this) );
            $(this).after('<span id="mixer-pos-'+index+'"></span>')
        }
    } )
    elems.sort(function(a,b) {
        if (a.attr(groupname) == b.attr(groupname)) {
            return (Math.round(Math.random())-0.5);
        }
        else {
            return Number(a.attr(groupname) > b.attr(groupname) )-0.5; // vraci 0.5 nebo -0.5
        }
    } )
    for (i=0;i<elems.length;i++) {
        var index_new = elems[i].attr('mix-index')
        if (i != index_new) {
            $("#mixer-pos-"+i).after(elems[i])
            $("#mixer-pos-"+i).remove()
        }
    }
}
var STICKY_ACTIVE=0
if (typeof STICKY_TOP == 'undefined') {
    STICKY_TOP = 40
}
function sticky() {

    if (STICKY_ACTIVE) { return }
    STICKY_ACTIVE=1

    var stickyElems = $(".sticky").each( function(index) {
        var initTop = $(this).offset().top-STICKY_TOP;
        var stop = 0
        if ($(this).attr('stickystop') && $("#sticky-stop-"+$(this).attr('stickystop'))) {
            stop = $("#sticky-stop-"+$(this).attr('stickystop')).offset().top
        }
        else if ($(this).attr('id') && $("#sticky-stop-"+$(this).attr('id').replace(/\./g,'-')).length) {
            //alert('1x'+index)
            stop = $("#sticky-stop-"+$(this).attr('id').replace(/\./g,'-')).offset().top
        }
        else if ($("#sticky-stop").length) {
            //alert('2x'+index)
            stop = $("#sticky-stop").offset().top
        }
        else {
            //alert('3x'+index)
            stop = $(document).height()
        }
        //alert(stop)
        var maxOffset = stop - initTop - $(this).outerHeight(true) - STICKY_TOP
        if (maxOffset<0) { maxOffset=0 }
        //alert(initTop+' '+maxOffset+' '+$(this).outerHeight(true))
        $(this).attr('initTop', initTop)
        $(this).attr('maxOffset', maxOffset)
    } )
    $(window).scroll(function() {
        var windowTop = $(window).scrollTop();
        for (i=0; i<stickyElems.length;i++) {
            var elem = $(stickyElems[i]);

            var offset = windowTop-elem.attr('initTop')
            if (offset > elem.attr('maxOffset')) { offset = elem.attr('maxOffset') }
            if (offset < 0) { offset = 0 }
            if (offset==0 || offset==elem.attr('maxOffset')) {
                elem.css({marginTop: offset + 'px','position':'static','top':0});
            }
            else{
                elem.css({marginTop: '0px','position':'fixed','top':STICKY_TOP});
            }
        }
    })


}
function hideEmpty() {
    $(".bbtitle").each(function() {
        if ($(this).height()<15) {
            $(this).hide()
        }
    })
}
if (typeof jQuery != 'undefined') {
    $(window).load(function(){
        setTimeout("sticky()",100)
        setTimeout("hideEmpty()",100)
    } )
    mixer()
}

