(function ($) {
	var playlist = mm.playlist = mm.extend({
		//初回時スクロール開始までのライムラグ
		initScrollTimeout: 500,
		//初回時スクロールの移動スピード(ミリ秒)
		initScrollSpeed: 800,
		//スクロールする移動スピード(ミリ秒)
		scrollSpead: 250,
		//スクロールする時間間隔(ミリ秒)
		interval: 5000,
		//liの高さ(px)
		itemHeight: 100,
		//移動個数(個) *showItems以下
		itemSet: 1,
		//表示個数(個)
		showItems: 3,
		//自動スクロール復帰までのウェイトタイム(ミリ秒)
		initScrollTimerTimeout: 3000,
		
//=========================================================================================//
		
		lock:  false,
		intervalTimer: null,
		timeoutTimer: null,
		
		init: function () {
			var ul = $('div.frm-playlist-content ul');
			ul.css({top: this.itemHeight * ul.find('li').length * -1});
			setTimeout(this.bind(function () {
				playlist.ctrl();
				ul.animate({top: 0}, this.initScrollSpeed, this.bind(function () {
					this.timer();
				}));
			}), this.initScrollTimeout);
		},
		
		ctrl: function () {
			var ul    = $('div.frm-playlist-content ul');
			var up    = $('li.frm-playlist-ctrl-up a');
			var down  = $('li.frm-playlist-ctrl-down a');
			var max   = (ul.find('li').length - this.showItems) * this.itemHeight * -1;
			var now   = 0;
			var min   = 0;
			var move  = this.itemHeight * this.itemSet;
			this.lock = false;
			
			up.click(this.bind(function () {
				if (this.lock)
					return false;
				this.lock = true;
				
				clearInterval(this.intervalTimer);
				
				if (now == 0) {
					now -= move;
					for (var i = 0; i < this.itemSet; i++)
						ul.prepend(ul.find('li:last'));
					ul.css({top: now});
				}
				
				ul.animate({top: '+=' + move}, this.scrollSpeed, this.bind(function () {
					now = parseInt(ul.css('top'));
					
					this.timer();
					this.lock = false;
				}));
				
				return false;
			}));
			
			down.click(this.bind(function () {
				if (this.lock)
					return false;
				this.lock = true;
				
				clearInterval(this.intervalTimer);
				
				if (now == (-1 * this.itemHeight * (ul.find('li').length % this.itemSet)))
					for (var i = 0; i < this.itemSet; i++)
						ul.append(ul.find('li:eq(' + i + ')').clone());
				
				ul.animate({top: '-=' + move}, this.scrollSpeed, this.bind(function () {
					now = parseInt(ul.css('top'));
					
					if (now <= (-1 * this.itemHeight * this.itemSet)) {
						now += move;
						for (var i = 0; i < this.itemSet; i++)
							ul.find('li:first').remove();
						ul.css({top: now});
					}
					
					this.timer();
					this.lock = false;
				}));
				
				return false;
			}));
		},
		
		timer: function () {
			this.intervalTimer = setInterval(this.bind(function () {
				$('li.frm-playlist-ctrl-down a').click();
			}), this.interval);
		}
	});
	
	$(playlist.bind(playlist.init));
})(jQuery);
