(function ($) {
var self = mm.bnr = mm.extend({
	
/* ===============================	 */
	
	slideInterval: 5000,
	slideSpeed:    300,
	target: false,
	autoscroll: true,
	file: '/Contents/uploads/xml/bnr.xml',
	data: [],
	
/* ===============================	 */
	
	init: function () {
		this.target = $('#main_bnr');

		if (!this.target.length)
			return;
		
		this.items();
	},
	
/* ===============================	 */
	
	items: function () {
		
		$.ajax({
			url      : this.file,
			type     : 'GET',
			dataType : 'xml',
			cache    : false,
			success  : this.bind(function (_ajax, r) {
				r = $(r);
				this.each(r.find('items item'), function (i, item) {
					item = $(item);
					this.data.push({
						url:    item.find('url').text(),
						target: item.find('target').text(),
						img:    item.find('img').text()
					});
				});
				this.render();
			})
			
		});
	},
	
	render: function () {
		var el = $([
			'<div class="ctrl">',
			'<ul>',
			'<li class="prev"><a href="#">prev</a></li>',
			'<li class="next"><a href="#">next</a></li>',
			'</ul>',
			'<ol></ol>',
			'</div>'
		].join(''));
		
		var ul = $('<ul class="scroll"></ul>');
		var ol = el.find('ol');
		
		this.target.append(ul);
		this.target.append(el);
		
		this.each(this.data, function (i, _data) {
			ol.append('<li' + ((i == 0) ? ' class="on"' : '') + '><span></span></li>');
			
			if (i == 0)
				ul.append([
					'<li>',
					'<a href="' + _data.url + '" target="' + (_data.target || '') + '">',
					'<img src="' + _data.img + '" alt="" width="540" height="211" />',
					'</a>',
					'</li>'
				].join(''));
		});
		
		this.scroll(ul, ol);
		
		if (this.autoscroll)
			this.initSetInterval();
	},
	
	scroll: function (ul, ol) {
		var count = this.data.length;
		var max   = (count-1) * this.move * -1;
		var min   = 0;
		var move  = 540;
		var now   = 0;
		var left  = 0;
		var lock  = false;
		var prev  = this.target.find('li.prev a');
		var next  = this.target.find('li.next a');
		
		next.click(this.bind(function () {
			if (lock)
				return false;
			lock = true;
			
			if (this.autoscroll) {
				clearInterval(this.intervalTimer);
			}
			
			now++;
			
			if (now >= this.data.length)
				now = 0;
			
			var _data = this.data[now];
			ul.prepend([
				'<li>',
				'<a href="' + _data.url + '" target="' + (_data.target || '') + '">',
				'<img src="' + _data.img + '" alt="" width="540" height="211" />',
				'</a>',
				'</li>'
			].join(''));
			
			ul.find('li:last').fadeOut('normal', this.bind(function () {
				ol.find('li').removeClass('on').eq(now).addClass('on');
				ul.find('li:last').remove();
				
				lock = false;
				
				if (this.autoscroll)
					this.initSetInterval();
			}));
			
			return false;
		}));
		
		prev.click(this.bind(function () {
			if (lock)
				return false;
			lock = true;
			
			if (this.autoscroll) {
				clearInterval(this.intervalTimer);
			}
			
			now--;
			
			if (now < 0)
				now = this.data.length -1;
			
			var _data = this.data[now];
			ul.prepend([
				'<li>',
				'<a href="' + _data.url + '" target="' + (_data.target || '') + '">',
				'<img src="' + _data.img + '" alt="" width="540" height="211" />',
				'</a>',
				'</li>'
			].join(''));
			
			ul.find('li:last').fadeOut('normal', this.bind(function () {
				ol.find('li').removeClass('on').eq(now).addClass('on');
				ul.find('li:last').remove();
				
				lock = false;
				
				if (this.autoscroll)
					this.initSetInterval();
			}));
			
			return false;
		}));
	},
	
	
	initSetInterval: function () {
		this.intervalTimer = setInterval(this.bind(function () {
			this.target.find('li.next a').click();
		}), this.slideInterval);
	}
});

/* ===============================	 */

$(function () {
	mm.bnr.init();
});
})(jQuery);

/* ===============================	 */
