
var jScrollPaneOptions = {
    wheelSpeed: 30,
    scrollbarWidth: 9,
    scrollbarMargin: 30
};

$(document).ready( function() {
    //Corner effect
    $w = $(window);
    $c = $('.content');
    
    var $s = $('.notification:visible .scroll');
    var sh = $s.height();
    if( sh >= 299) $s.css({height:'300px'});

    $c.css({
        'left' : parseInt( ($w.width() - $c.width()) /2 )+'px',
        'top' : parseInt( ($w.height() - $c.height()) / 2)-20 +'px'
    });

    $('.content:not(.nocorner)').corner();

    // Menu items
    $('ul#community-menu a.popup, ul#info-menu a.popup').each(function(){
        var url = this.href;
        var slug = url.split('/')
        slug = slug[slug.length-1]
        this.href = '/community#' + slug;

        if( document.body.id == 'Pages-community' ) {
            $(this).click( function(){ getPageContent(slug)});
        }
    });

    // Show content if requested
    if( document.body.id == 'Pages-community' ) {
        var slug = window.location.hash.replace(/^#/,'');
        if( slug ) getPageContent(slug);
    }
    
    // Resize
    $w.resize( resizeContent );
    $w.load(resizeContent);
});

function getPageContent(slug){
    $.getJSON( '/pages/json/'+slug, 
    function(data) {
        var t = data.Page.title;
        var c = data.Pagenode[0]['body'];
        var slug = data.Page.slug;
        var bg = data.Pagenode[0]['background_color'];
        showContent(c, t, slug, bg);
    });
}

function showContent(content,title,slug,bgcolor) {
    var $view = $('#page-view');
    if( $view[0] ) $view.remove();
    $view = createContentView();
    if(bgcolor) { 
        bgcolor = bgcolor.replace(/^#/,'');
        $view.css({'background' : 'url(/img/colorimage.php?color='+bgcolor+'&alpha=40)'});
    }
    $('#page').append($view[0]);
    $('.scroll', $view[0]).append(content);
    if( title ) {
        $('> div', $view[0]).prepend(
          '<img src="/img/image.php?text='+encodeURIComponent(title)+
          '" alt="'+title+'" class="transpng title" /><br/><br/>');
    }
    $view.corner().show();
    $('.transpng',$view).iepngfix();
    resizeContent();
    
    $('.close-content').click( function() {
        $view.remove();
        return false;
    });    
    $('.pdf-content').click( function() {
        window.location.href = '/pages/pdf/' + slug;
        return false;
    });
}

function createContentView(){
    return $(
      '<div id="page-view" class="content">'+
      '<a href="#" class="pdf-content"><img src="/img/pdf.png" alt="close" /></a>'+
      '<a href="#" class="close-content"><img src="/img/close.gif" alt="close" /></a>'+
      '<div><div class="scroll"></div></div></div>'
    ).css({'background':'url(/img/colorimage.php?color=995599&alpha=40)'});
}

// Resizing of Content scroller
var isResizing;
function resizeContent() {
    // IE triggers the onResize event internally when you do the stuff in this function
    // so make sure we don't enter an infinite loop and crash the browser
    if (!isResizing) { 
        isResizing = true;
        $w = $(window);
        $c = $('.content:visible');
        $s = $('.scroll', $c);
        if( $c.length < 1 ) { isResizing = false; return; }

        $c.css({
            'left' : parseInt( ($w.width() - $c.width()) /2 )+'px',
            'top' : parseInt( ($w.height() - $c.height()) / 2)-20 +'px'
        });

        if( $s.length > 0 && $('.jScrollPaneContainer', $c ).length < 1)  {
            /*$('.jScrollPaneContainer', $c[0]).css({
                'height': parseInt($w.height() * 0.52 ) + 'px', 
                'width': ( $c.width() - 40 ) + 'px'
            });*/
            if( $s.height() >= 299) $s.jScrollPane(jScrollPaneOptions);
        }
        isResizing = false;    
    }
}
