/*!
 * Gubernator74 news informer v1.0
 * http://gubernator74.ru/
 *
 * Copyright 2011, Seenta
 *
 * Requires jQuery javascript library >= 1.2.6
 *
 * Date: Mon Mar 28 15:20:53 2011 +0500
 */

(function($) {
    var gcount = window.chelnewsGubernatorNewsCount || 2, 
        pcount = window.chelnewsPravminNewsCount || 2,
        gurl = 'http://gubernator74.ru/',
        purl = 'http://pravmin74.ru/',
        gblock = null, pblock = null;    
      
    document.write('<div id="gubernator-news" class="chelnews-block"><div class="loader">Загрузка...</div></div>');
    document.write('<div id="pravmin-news" class="chelnews-block"></div>');
    
    function loadGubernatorNews() {
        $.ajax({
            url: gurl + 'news_json',
            dataType: 'jsonp',
            data: {
                count: gcount
            },
            success: function(data) {                
                var content = $('<div />');
                
                $('<div class="chelnews-link" />').append('<a href="' + gurl + '">www.gubernator74.ru</a>').appendTo(content);
              
                $.each(data, function(i, item) {
                    var link = '<div class="chelnews-title"><a href="' + item.link + '">' + item.title + '</a></div>';
                    var date = '<div class="chelnews-date"><p>' + item.date + '</p></div>';
                    $('<div class="chelnews-item" />').append(date).append(link).appendTo(content);
                });
                
                loadPravminNews(content);
            }
        });
    }
    
    function loadPravminNews(content) {
        $.ajax({
            url: purl + 'news_json',
            dataType: 'jsonp',
            data: {
                count: pcount
            },
            success: function(data) {
                gblock.html(content.html());
                pblock.html('');
                
                $('<div class="chelnews-link" />').append('<a href="' + purl + '">www.pravmin74.ru</a>').appendTo(pblock);
              
                $.each(data, function(i, item) {
                    var link = '<div class="chelnews-title"><a href="' + item.link + '">' + item.title + '</a></div>';
                    var date = '<div class="chelnews-date"><p>' + item.date + '</p></div>';
                    $('<div class="chelnews-item" />').append(date).append(link).appendTo(pblock);
                });
            }
        });
    }
    
    function insertCss() {
        $.ajax({
            url: gurl + 'informer/news.css.php',
            dataType: 'jsonp',
            success: function(data) {
                gblock.parent().append('<style type="text/css" style="display: none;">' + data + '</style>');
                loadGubernatorNews();
            }
        });
    }
    
    $(document).ready(function() {
        gblock = $('#gubernator-news');
        pblock = $('#pravmin-news');
        insertCss();   
    });
})(jQuery);
