/* 
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

$(function() {
    refreshnavi();
    add_edit_functions();
});

function add_edit_functions() {
    $('.radios input[type=radio]').change(
        function() {
            var id = $(this).attr('name');
            var value = $(this).val();
            if($(this).attr('checked')) {
                $('#ajaxinfo').load('index.php?controller=news&action=settype&mode=ajax',
                                    {id: id, type: value},
                                    function() {
                                        location = 'index.php?controller=news&action=show&page=' + $('#page').val();
                                    }
                );
            }
        }
    );

    $('.editable').editable( 'index.php?mode=ajax&controller=news&action=edit',
                             { loadurl: 'index.php?mode=ajax&controller=news&action=gettitle',
                               indicator: '<img src="img/spinner.gif" style="display:block; margin:auto;">' }
    );

    $('.textarea').editable( 'index.php?mode=ajax&controller=news&action=edit',
                             { loadurl: 'index.php?mode=ajax&controller=news&action=getcontent',
                               type: 'textarea',
                               submit: 'OK',
                               rows: 7,
                               indicator: '<img src="img/spinner.gif" style="display:block; margin:auto;">' }
    );
}

var loading = false;

function left() {
    var page = new Number($('#page').val());
    loadnews(page - 1);
}

function right() {
    var page = new Number($('#page').val());
    loadnews(page + 1);
}

function loadnews( page ) {
    var effect = 'fold';
    if( loading ) return;
    loading = true;
    $('#news_content').hide( effect, 1000 );
    $('#news_content').load( 'index.php?mode=ajax&controller=news&action=show', {page: page}, function() {
        $('#news_content').show( effect, 1000 );
        loading = false;
        main_content('#news_content');
        add_edit_functions();
        refreshnavi();
    });
}

function refreshnavi() {
    if( $('#leftvalid').val() == 'true' ) {
        $('#leftbutton').show();
        $('#leftdeactivated').hide();
    } else {
        $('#leftbutton').hide();
        $('#leftdeactivated').show();
    }
    if( $('#rightvalid').val() == 'true' ) {
        $('#rightbutton').show();
        $('#rightdeactivated').hide();
    } else {
        $('#rightbutton').hide();
        $('#rightdeactivated').show();
    }
    $('#pagedisplay').text( $('#page').val() );
}