(function ($) {
var self = mm.pickup = mm.extend({

/* ===============================	 */
	
	firstTimeLag:   500,
	firstFadeSpeed: 1000,
	fadeSpeed:      800,
	slideSpeed:     800,
	
/* ===============================	 */
	
	view:    4,
	page:    0,
	cat:     0,
	maxPage: 0,
	lock:    false,
	data:    [],
	badges:  [],
	ids:     [],
	news:    [],
	
/* ===============================	 */
	
	init: function () {
		$('#banners ul.center').hide();
		this.items(this.bind(function () {
			this.render(this.cat, this.page, 'center', this.bind(function () {
				setTimeout(this.bind(function () {
					$('div.frm-loader').hide();
					$('#banners ul.center').fadeIn(this.firstFadeSpeed);
					this.action();
					this.tab();
					this.loadRss();
				}), this.firstTimeLag);
			}));
		}));
	},
	
/* ===============================	 */
	
	items: function (fn) {
		var xml  = '/uploads/xml/pickup-items.xml';
		var data = [];
		
		$.ajax({
			url      : xml,
			type     : 'GET',
			dataType : 'xml',
			cache    : false,
			success  : this.bind(function (_ajax, r) {
				r = $(r);
				
				this.each(r.find('pickup item'), function (i, item) {
					item = $(item);
					data[i] = {
						id:     item.attr('id'),
						badge:  item.attr('badge'),
						url:    item.find('url').text(),
						target: item.find('target').text(),
						img:    '/uploads/img/banner/' + item.find('img').text(),
						alt:    item.find('alt').text()
					};
				});
				
				this.each(r.find('badges item'), function (i, item) {
					item = $(item);
					this.badges[i] = {
						id:  item.attr('id'),
						url: '/uploads/img/badge/' + item.find('url').text(),
						alt: item.find('alt').text()
					};
				});
				
				this.config(data, fn);
			})
		});
	},
	
	config: function (data, fn) {
		var xml  = '/uploads/xml/pickup.xml';
		
		$.ajax({
			url      : xml,
			type     : 'GET',
			dataType : 'xml',
			cache    : false,
			success  : this.bind(function (_ajax, r) {
				r = $(r);
				
				this.each(r.find('pickup category'), function (i, category) {
					this.ids[$(category).attr('id')] = this.map($(category).find('ids').text().split(','), function (id) {
						return parseInt(id);
					});
				});
				
				this.each(this.ids, function (i, ids) {
					this.data[i] = this.map(ids, function (id) {
						return data[id-1];
					});
				});
				
				if (fn)
					fn();
			})
		});
	},
	
/* ===============================	 */
	
	action: function () {
		$('div.ctrl div.right a').click(this.bind(function () {
			this.move('right');
			return false;
		}));
		
		$('div.ctrl div.left a').click(this.bind(function () {
			this.move('left');
			return false;
		}));
	},
	
	tab: function () {
		$('#tabs ul li a').click(this.bind(function (a) {
			$('#banners ul.center').fadeOut(this.fadeSpeed, this.bind(function (ul) {
				$(ul).empty();
				a = $(a);
				this.page = 0;
				this.cat  = parseInt(a.parent().attr('class').replace(/tab_/, ''));
				this.render(this.cat, this.page, 'center', this.bind(function () {
					$('#tabs ul li a').removeClass('on');
					a.addClass('on');
					$('#banners ul.center').fadeIn(this.fadeSpeed);
					$('div.frm-loader').hide();
				}));
			}));
			$('div.frm-loader').show();
			
			return false;
		}));
	},
	
	move: function (position) {
		if (position == 'right' && this.maxPage-1 <= this.page)
			return;
		if (position == 'left' && this.page <= 0)
			return;
		
		if (this.lock)
			return;
		this.lock = true;
		
		if (position == 'right')
			this.page++;
		else
			this.page--;
		
		$('div.frm-loader').show();
		
		this.render(this.cat, this.page, position, this.bind(function () {
			$('#banners div.content').animate({left: '+=' + ((position == 'right') ?  -940: 940)}, this.slideSpeed, this.bind(function (el) {
				$('div.frm-loader').hide();
				
				$('#banners ul.center').html('').append($('#banners ul.' + position + ' li'));
				$(el).css({left: 0});
				
				this.lock = false;
			}));
		}));
	},
	
/* ===============================	 */
	
	render: function (cat, page, position, fn) {
		this.maxPage = Math.ceil(this.data[cat].length / this.view);
		
		this.each(this.data[cat], function (i, data) {
			if ((page * this.view) <= i && i < this.view + (this.view * page)) {
				$('#banners ul.' + position).append([
					'<li>',
					'<a href="' + data.url + '" target="' + data.target + '">',
					(data.badge ? '<p><img class="png" src="' + this.badges[data.badge-1].url + '" alt="' + this.badges[data.badge-1].alt + '" /></p>' : ''),
					'<img src="' + data.img + '" alt="' + data.alt + '" />',
					'</a>',
					'</li>'
				].join(''));
			}
		});
		
		$('div.ctrl div.right a').removeClass('max');
		$('div.ctrl div.left a').removeClass('max');
		if (this.maxPage-1 <= this.page)
			$('div.ctrl div.right a').addClass('max');
		if (this.page <= 0)
			$('div.ctrl div.left a').addClass('max');
		
		if (fn)
			fn();
	},
	
/* ===============================	 */
	
	loadRss: function () {
		var target = $('div#frm-news div#frm-news-view ul');
		
		$.ajax({
			url      : '/uploads/xml/pickup-rss.xml',
			type     : 'GET',
			dataType : 'xml',
			cache    : false,
			success  : this.bind(function (_ajax, r) {
				r = $(r);
				
				this.news = this.map(r.find('channel item'), function (i, item) {
					if (i < 10) {
						item = $(item);
						return {
							title:  item.find('title').text(),
							date:   item.find('pubDate').text(),
							url:    item.find('link').text(),
							target: item.find('target').text()
						}
					}
				});
				
				this.each(this.news, function (i, news) {
					var li = [
						'<li>',
						'<img src="/shared/img/common/frm-btn-pickup.gif" />',
						'<span class="date">' + this.date(news.date) + '</span>',
						'<p><a href="' + news.url + '" target="' + (news.target || '') + '">' + news.title + '</a><span class="guide">' + news.title + '</span></p>',
						'</li>'
					].join('');
					
					target.append(li);
				});
				
				this.rss(target);
				
			})
		});
	},
	
	rss: function (ul) {
		var fadeSpeed      = 5000;
		var animationSpeed = 300;
		
		var li    = ul.find('li');
		var i     = 0;
		
		li.eq(i).fadeIn(animationSpeed);
	  this.stringSlice(li.eq(i));
		
		setInterval(this.bind(function () {
			ul.find('li:visible').fadeOut(animationSpeed, this.bind(function () {
				i++;
				if (i == ul.find('li').length)
					i = 0;
			  li.eq(i).fadeIn(animationSpeed);
			  this.stringSlice(li.eq(i));
			}));
		}), fadeSpeed);
	},
	
	date: function (date) {
/*
		date = new Date(date);
		
		var _date = {
			year:  parseInt(date.getFullYear()),
			month: parseInt(date.getMonth()) + 1,
			day:   parseInt(date.getDate())
		}
		
		return _date.year + '/' + _date.month + '/' + _date.day;
*/
		return date.slice(0,4) + '/' + date.slice(5,7) + '/' + date.slice(8,10);
	},
	
	stringSlice: function (li) {
	  if (li.find('span.guide').width() <= 450) return;
	  
	  for (var i = li.find('span.guide').text().length-1; i >= 1; --i) {
	    var s = li.find('span.guide').text().slice(0, i) + '...';
	    li.find('span.guide').text(s);
	    if (li.find('span.guide').width() <= 450) {
	    	li.find('p a').text(s);
	    	return;
	    }
	  }
	  return;
	}
	
});

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

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

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