regionNewsActiveTab = 'region';
regionNewsActiveRegion = 0;
regionNewsRubric = 0;
regionNewsSubrubric = 0;

function switchRegionNewsTab( activeTab ) {

    if ( activeTab == regionNewsActiveTab ) {
        return;
    }

    $('#region-news-didgest').html('');

    switch ( activeTab ) {
        case 'country':
            $('#region-news-select').hide();
            $('#region-news-country-tab').attr('className', 'selected');
            $('#region-news-region-tab').attr('className', 'not-selected');
            redrawRegionNews( regionNewsCache['country'] );
            break;

        case 'region':
            $('#region-news-country-tab').attr('className', 'not-selected');
            $('#region-news-region-tab').attr('className', 'selected');
            $('#region-news-select').show();
            
            redrawRegionNews( regionNewsCache['region'] );
            break;
    }

    regionNewsActiveTab = activeTab;

} // end of function switchRegionNewsTab()

function changeRegion( regionId ) {

    // Get news for selected region
    fetchRegionNews(regionId);

    if ( regionId > 1 ) {
        // Not empty and not all regions
        regionNewsActiveRegion = regionId;
    }

    regionNewsActiveTab = 'region';

} // end of function changeRegion()

function fetchRegionNews( regionId ) {
    $.getJSON('/ajax/home-news.json', {region_id: regionId, rubric_id: regionNewsRubric, subrubric_id: regionNewsSubrubric}, function(json) {
        regionNewsCache['region'] = json;
        redrawRegionNews(json);
    });
}

function redrawRegionNews( json ) {
    var html = '';
    for ( i=0; i < json.length; i++) {
        html += '<h4>'+json[i]['date']+' | '+json[i]['day']+'</h4>';
        html += '<dl>';
        for ( j=0; j < json[i]['events'].length; j++ ) {
            html += '<dt>'+json[i]['events'][j]['time']+'</dt>';
            if ( json[i]['events'][j]['important'] == '1' ) {
                html += '<dd><a href="'+json[i]['events'][j]['url']+'" class="important">'+json[i]['events'][j]['title']+'</a></dd>';
            }
            else {
                html += '<dd><a href="'+json[i]['events'][j]['url']+'">'+json[i]['events'][j]['title']+'</a></dd>';
            }
        }
        html += '</dl>';
    }
    html += '<p class="all"><a href="/news/">Все новости</a></p>';
    $('#region-news-didgest').html(html);
}

function switchBlogTab( activeTab ) {

    switch ( activeTab ) {
        case 'month':
            $('#region-news-didgest').hide();
            $('#country-news-didgest').show();
            $('#region-news-country-tab').attr('className', 'selected');
            $('#region-news-region-tab').attr('className', 'not-selected');
            break;

        case 'week':
            $('#country-news-didgest').hide();
            $('#region-news-didgest').show();
            $('#region-news-country-tab').attr('className', 'not-selected');
            $('#region-news-region-tab').attr('className', 'selected');
            break;
    }

} // end of function switchBlogTab()
