﻿/**
 * Copyright(c) 2010- Japan Electronic Industrial Arts.co.ltd. All Rights Reserved.
 * 
 * http://www.jeia.co.jp/
 * 
 * @notes	RSSを読み込み、フロート表示する
 * @author	Japan Electronic Industrial Arts Co. Ltd.
 * @version	$Revision: 1.00 2011/01/07 $
 * @use		Using jquery.js.
 */

function newsline( _selector, _url ) {
	this.selector = _selector;
	this.url = _url;
	
	this.$area = null;
	this.$scroll = null;
	this.que = [];
	this.key = 0;
	this.left = 0;
	this.speed = 8;
	this.fontsize = 11;
	this.frameSpan = 150;
	this.lineStyle = {
		'display': 'block',
		'margin': '0px',
		'padding': '0px',
		'position': 'absolute',
		'whiteSpace': 'nowrap',
		'textAlign': 'left',
		'fontSize': this.fontsize + 'px',
		'lineHeight': (this.fontsize + 2) + 'px'
	};
	this.page = [ location.protocol, '//', location.hostname, location.pathname ].join('');
	
	var o = this;
	$(document).ready( function() {
		o.init();
	});
		
	return this;
}

/**
 * @method	init
 * @notes	初期化
 */
newsline.prototype.init = function() {
	
	// set style
	this.$area = $( this.selector );
	this.$area.css({
		'overflow': 'hidden'
	});
	
	if ( this.$area.css( 'position' ) != 'absolute' ) {
		this.$area.css({
			'position': 'relative'
		});
	}
	
	// loading
	var html = '<p class="loading">Loading...</p>';
	
	this.$area.html( html ).
	find( '.loading' ).css( this.lineStyle );
	
	// request
	var o = this;
	
	$.ajax({
		'type': 'GET',
		'url': this.url,
		'dataType': 'xml',
		'cache': false,
		'data': {},
		'error': function( _d1, _d2, _d3 ) {
			o.onError( _d1, _d2, _d3 );
		},
		'success': function( _d ) {
			o.onSuccess( _d );
		}
	});
};


/**
 * @method	onError
 * @notes	エラー発生時
 */
newsline.prototype.onError = function( _d1, _d2, _d3 ) {
	var html = '<p class="error">ニュースが読み込めませんでした。</p>';
	this.$area.html( html ).
	find( '.error' ).css( this.lineStyle );
};


/**
 * @method	onSuccess
 * @notes	読み込み成功時
 */
newsline.prototype.onSuccess = function( _d ) {
	
	var o = this;
	
	$( _d ).find( 'entry' ).each( function() {
		var $item = $(this);
		var data = {
			'title': $item.find( 'title' ).text(),
			'link':  $item.find( 'link' ).text().replace(/^\s+|\s+$/g, ""),
			'date':  $item.find( 'issued' ).text()
		};
		o.que.push( data );
	});
	
	if ( this.que.length > 0 ) {
		this.showNext();
	}
	else {
		var html = '<p class="error">ニュースがまだありません。</p>';
		this.$area.html( html ).
		find( '.error' ).css( this.lineStyle );
	}
};


/**
 * @method	showNext
 * @notes	読み込み成功時
 */
newsline.prototype.showNext = function() {
	
	// show line
	var html = this.getLine( this.que[ this.key ] );
	this.$area.html( html );
	
	this.left = this.$area.width();
	
	this.$scroll = this.$area.find( '.line' ).
	css( this.lineStyle ).
	css({
		'left': this.left + 'px',
		'top': '0px'
	}).
	find( 'a' ).css({
		'textDecoration': 'none'
	}).end();
	
	this.floatLine();
	
	// update
	this.key ++;
	
	if ( this.key >= this.que.length ) {
		this.key = 0;
	}
};

/**
 * @method	getLine
 * @notes	読み込み成功時
 */
newsline.prototype.getLine = function( _d ) {
	var reg = new RegExp();
	reg.compile( '^([0-9]{4})-([0-9]+)-([0-9]+)T', 'i' );
	
	var title = _d[ 'title' ];
	var link = _d[ 'link' ];
	var date = '';
	
	if ( _d[ 'date' ].match( reg ) ) {
		date = [
			RegExp.$1, '.',
			RegExp.$2, '.',
			RegExp.$3
		].join('');
	}
	
	if ( link == this.page ) {
		link = title;
	}
	else {
		link = [
			'<a href="', link, '" target="_blank">', title, '</a>'
		].join('');
	}
	
	var line = [
		'<p class="line">', date, ' - ', link, '</p>'
	].join('');
	
	return line;
};

/**
 * @method	floatLine
 * @notes	行を左移動
 */
newsline.prototype.floatLine = function() {
	var o = this;
	
	if ( this.left + this.$scroll.width() < 0 ) {
		this.showNext();
	}
	else {
		this.left -= this.speed;
		
		this.$scroll.css({
			'left': this.left + 'px'
		});
		
		setTimeout( function(){ o.floatLine(); }, this.frameSpan );
	}
};

