if (typeof(zs) == 'undefined') var zs = new Object();
zs.main = new Object();

zs.main._activeOverlay = null;

jQuery.fn.rounded = function(){
    $(this).addClass("rounded");
    return $(this).each(function(){
        $(this).append('<span class="rtl"></span>');
        $(this).append('<span class="rtr"></span>');
        $(this).append('<span class="rbl"></span>');
        $(this).append('<span class="rbr"></span>');
    });
};
function checkWidth(){
    if ($(window).width() < 900){
        $('body').addClass('narrow');
    } else {
        if ($('body').hasClass('narrow')){
            $('body').removeClass('narrow');
        }
    }
}

function prepareMoreMenu(){
    $('#sidebar .block').hide();
    // The more menu
    $('#more, #more span, #more ul, #more li').css({'display':'block', 'float': 'none'});
    $('#more').css('position', 'absolute').bind('keydown', function(ev){
        if (ev.keyCode == 27){
            $('#more ul').slideUp();
        }
    });
    $('#more ul').hide();
    $('#more span a').bind('focus', function(ev){
        $('#more ul').slideDown();
    });
    $('#more').bind('mouseenter', function(ev){
        $('ul', $(this)).slideDown();
    }).bind('mouseleave', function(ev){
        $('ul', $(this)).slideUp();
    });
    if ($('#more li').length == 0){
        $('#more').hide();
    }
    $('#more ul a').bind('click',function(ev){
        ev.preventDefault();
        var elem = $($(this).attr('href'))
        zs.main._activeOverlay = elem.overlay({
           'api': true,
           'expose': {'opacity': 0.5, 'color': '#000', loadSpeed:200}
        }).bind('onLoad onClose', function(ev){
            // Register the original focus element and jump back to it
            // when the overlay is closed
            if (ev.type == 'onLoad'){
                this._previousFocus = document.activeElement;
                // The focus on the first focusable element within the
                // lightbox
                var focusable = $('input, a, button, textarea', elem);
                if (focusable.length > 0){
                    focusable[0].focus();
                }
            } else {
                this._previousFocus.focus();
                this._previousFocus = null;
            }
        });
        zs.main._activeOverlay.load();

    });
    $('#sidebar .block .content').css({'max-height':'400px', 'overflow': 'auto'});
    $('#sidebar .block').css('width', '550px');
}
function init(){
    prepareMoreMenu();
}
$(document).ready(function(){
    checkWidth();
    $(window).resize(checkWidth);
    var jlp = '/media/j/jquery-lightbox-0.5/';
    var jlconf = {
        imageLoading: jlp + 'images/lightbox-ico-loading.gif',
        imageBtnPrev: jlp + 'images/lightbox-btn-prev.gif',
        imageBtnNext: jlp + 'images/lightbox-btn-next.gif',
        imageBtnClose: jlp + 'images/lightbox-btn-close.gif',
        imageBlank: jlp + 'images/lightbox-blank.gif'
        };
    $('.figure>a').not('a[img.flickr]').lightBox(jlconf);
    $('.image-reference').not('a[img.flickr]').lightBox(jlconf);
    $('.postimg').not('.flickr').lightBox(jlconf);
    $('#main .primarylink').rounded();
    $('#main .meta.block').rounded();
    $('.spoiler').each(function(){
        var $spoiler = $(this);
        var $title = $('>.title', $spoiler);
        $title.append(' <span style="font-size:12px">(click to toggle)</span>');
        var $content = $('>.content', $spoiler);
        $content.hide(); $spoiler.addClass('minimized');
        $title.bind('click', function(){
            if ($spoiler.hasClass('minimized')){
                $content.fadeIn();
                $spoiler.removeClass('minimized');
            } else {
                $content.fadeOut();
                $spoiler.addClass('minimized');
            }
            });
        });
});

