//var newsContents;
//var maxNewsCount;

var displayTime   = 10000;
var scrollingTime = 1800;

var displayNewsIndex = 0;

$(document).ready(function(){
    if(displayNewsIndex < maxNewsCount){
        $('div#scrollNewsBox').appendNews(newsContents[displayNewsIndex]);
    }
	//$.getJSON('/?m=pc&a=do_news_json', null, onNewsLoadSuccess);
});

var onNewsLoadSuccess = function(json){
	newsContents = json;
	maxNewsCount = json.length;
	
    if(displayNewsIndex < maxNewsCount){
        $('div#scrollNewsBox').appendNews(newsContents[displayNewsIndex]);
    }
};

(function() {
    jQuery.fn.appendNews = function(config){
        // default params
        config = jQuery.extend({
            title : null,
            link : null,
			category : null,
            date : null
        },config);
        
        // target object
        var target = this;
        
        var init = function(nextNewsIndex){
            // ニュースを生成する
            var news = document.createElement('div');
			if(config.category) $(news).addClass(config.category);
			
            // タイトルを生成する
            var title = document.createElement('a');
            $(title).attr('href', config.link);
            $(title).attr('target', '_blank');
            $(title).text(config.title);
            
            // 日付を生成する
            var date_str = document.createElement('span');
            $(date_str).text('');
            
            $(news).css({
                'display':'none',
                'top':'-30px'
            }).append(title).append(date_str);
            
            $(news).animate({opacity: "show", top: 0}, scrollingTime, 'easeOutQuad', function(){
                $.timer(displayTime, function(timer) {
                    // タイマーを止める
                    timer.stop();
                    
                    // 次に表示するニュースがあればニュースを消し、新たに追加する
                    if(maxNewsCount > 1){
                        $(news).animate({opacity: "hide", top: 30}, scrollingTime, 'easeOutQuad', function(){
                            $(news).remove();
                        });
                        $(target).appendNews(newsContents[nextNewsIndex]);
                    }
                });
            });
            // ニュースを追加する
            $(target).append(news);
        }
        
        // 次に表示する予定のニュースのindexを指定
        if(displayNewsIndex == maxNewsCount - 1){
            displayNewsIndex = 0;
        }else{
            displayNewsIndex++;
        }
        
        // タイトルが存在すれば表示する
        if(config.title) init(displayNewsIndex);
    }
})(jQuery);