/**
 * FOG BAR UTILITY
 */
(function() {
	return fogbarUtility = {
		newsBox: {
			init: function() {
				this.filePath = 'news.html'; // ファイルパス
				this.rows = 3; // 1度に表示する数
				this.speed = 450; // 移動スピード
				this.delay = 100; // 遅延スピード
				this.load();
				this.next();
			},
			load: function() {
				var self = this;
				$.ajax({
					url: this.filePath,
					cache: false,
					success: function(html) {
						var tmpData = [];
						var elements = $(html).find('li');
						elements.each(function(i, element) {
							tmpData.push(element);
						});
						self.display(tmpData);
					}
				});
			},
			next: function() {
				this.a = true;
				var self = this;
				$('#next_btn').click(function(e) {
					e.preventDefault();
					if (self.a) {
						self.execute();
					};
					self.a = false;
				});
			},
			display: function(tmpData) {
				this.data = $.shuffle(tmpData);
				var add = this.data.length;
				while(/\./.test(this.detectShort(add))) {
					++add;
					this.detectShort(add);
				}
				var short = add - this.data.length;
				for (var i = 0; i < short; ++i) {
					this.data.push('<li class="newsbox_empty"></li>');
				}
				for (var i = 0, n = 0, count = Math.ceil(this.data.length / this.rows); i < count; ++i) {
					$('#newsbox_wrap_inner').append('<ul class="newsbox"></ul>');
					for (var j = n, l = n + this.rows; j < l; ++j) {
						$($('.newsbox')[i]).append(this.data[j]);
						++n;
					}
				}
				this.columnWidth = parseInt($('.newsbox').width()) + parseInt($('.newsbox').css('margin-right'));
				this.columnLength = Math.ceil($('.newsbox').find('li').length / this.rows);
				$('#newsbox_wrap_inner').css('width', this.columnWidth * this.columnLength + 'px');
				this.noLink();
			},
			execute: function() {
				this.parentDiv = $('.newsbox');
				if (this.parentDiv.length > 1) {
					for (var i = 0, l = this.parentDiv.length; i < l; ++i) {
						var objs = $(this.parentDiv[i]).find('li');
						this.setTimer(objs, objs.length, 0, i);
					}
				}
			},
			setTimer: function(objs, l, n, i) {
				var self = this;
				$(objs[n]).stop().animate({
					'left': '-=' + this.columnWidth + 'px'
				}, this.speed, 'easeOutBack', function() {
					if (n === l) {
						$(objs).css('left', '0px');
						if (i === 0) {
							$(self.parentDiv[0]).insertAfter(self.parentDiv[self.parentDiv.length - 1]);
						};
						self.a = true;
						clearTimeout(this.timerID);
					}
				});
				n = ++n;
				if (n !== l) {
					this.timerID = setTimeout(function() {self.setTimer(objs, l, n, i)}, this.delay);
				}
			},
			detectShort: function(i) {
				return i / this.rows;
			},
			noLink: function() {
				$('.newsbox a').each(function(i, element) {
					if (/#$/.test(element.href)) {
						$(element).bind('click', function(e) {
							e.preventDefault();
						}).bind('focus', function() {
							$(this).blur();
						}).bind('mouseover', function() {
							$(this).css('color', $(this).css('color'));
						}).css({
							'cursor': 'text',
							'outline': 'none'
						});
					};
				});
			}
		},
		recommendVoice: function() {
			var liHeight = $('#recommend_voice li').height();
			$('#recommend_voice li').css('height', liHeight + 'px');
			$('#recommend_voice').css('margin-top', parseInt((25 - liHeight + 1) / 2) + 'px')
			.jCarouselLite({
				auto: 3000,
				speed: 500,
				visible: 1,
				vertical: true
			});
		}
	}
})();

$(function() {
	fogbarUtility.newsBox.init();
})
