(function ($) {
var self = mm.index = mm.extend({
	
/* ===============================	 */
	
	firstTimeLag:       700,
	fadeInInterval:     10000,
	fadeInSpeed:        800,
	newsFadeSpeed:      6000,
	newsAnimationSpeed: 800,
	
	path:           '/uploads/img/index/',
	
/* ===============================	 */
	
	mainImages:     [],
	news:           [],
	shop:           [],
	lock:           false,
	
/* ===============================	 */
	
	init: function () {
		this.images();
		this.setMain();
		this.setList();
		this.action();
		this.pop();
		this.slide();
		this.rss();
		this.shop();
		this.initSetTimeout();
	},
	
/* ===============================	 */
	
	images: function () {
		this.each($('#main #ctrl ul li'), function (i, el) {
			var imgObj = new Image();
			imgObj.src = this.path + $(el).find('a').attr('rel');
			this.mainImages.push(imgObj);
		});
	},
	
	setMain: function () {
		this.each($('#main div.inners div.inner'), function (i, el) {
			$(el).css({
				zIndex:  (i+1) * 1000,
				opacity: 0,
				display: (i == 0 ? 'block' : 'none')
			});
		});
	},
	
	setList: function () {
		var a  = $('#main #ctrl ul li a');
		var ul = $('#bg ul');
		
		this.count = a.length;
		this.each(a, function (i, el) {
			var li = $('<li class="image_' + i + '"></li>');
			ul.append(li);
			li.css({
				backgroundImage: 'url(' + this.path + $(el).attr('rel') + ')',
				zIndex:          (i+1) * 10,
				opacity:         0,
				display:         'block'
			});
		});
	},
	
	initSetTimeout: function () {
		var bg    = $('#bg ul li:first');
		var inner = $('#main div.inners div.inner:first');
		this.now  = 0;
		
		this.timeout = setTimeout(this.bind(function () {
			this.lock = true;
			
			$('div.frm-loader').hide();
			
			inner.css({display: 'block'}).animate({opacity: 1}, this.fadeInSpeed, this.bind(function () {
				this.lock = false;
				this.initSetInterval();
			}));
			bg.animate({opacity: 1}, this.fadeInSpeed, this.bind(function () {
			}));
		}), this.firstTimeLag);
	},
	
	initSetInterval: function (prev) {
		this.interval = setInterval(this.bind(function () {
			if (this.lock)
				return;
			
			this.now++;
			this.change(this.now, this.now - 1);
		}), this.fadeInInterval);
	},
	
	cursol: function () {
		var a = $('#main #ctrl ul li a');
		a.find('span').removeClass('on');
		a.eq(this.now).find('span').addClass('on');
	},
	
	action: function () {
		$('#main #ctrl ul li a').click(this.bind(function (el) {
			if (this.lock)
				return false;
			
			var num = parseInt($(el).parent().attr('class').replace(/thumb_/, '')) - 1;
			if (num == this.now)
				return false;
			
			this.lock = true;
			
			clearTimeout(this.timeout);
			clearInterval(this.interval);
			this.timeout  = 0;
			this.interval = 0;
			
			this.change(num, this.now);
			
			return false;
		}));
	},
	
	change: function (next, prev) {
		var bg    = $('#bg ul li');
		var inner = $('#main div.inners div.inner');
		
		if (prev < 0)
			prev = this.count - 1;
		
		inner.eq(prev).css({zIndex: 999});
	     bg.eq(prev).css({zIndex: 9});
		
		inner.eq(next).css({display: 'block'}).animate({opacity: 1}, this.fadeInSpeed, this.bind(function () {
			this.now = next;
			this.cursol();
			
			if (prev < 0) {
				inner.eq(this.count - 1).css({display: 'none', opacity: 0, zIndex: this.count * 1000});
				   bg.eq(this.count - 1).css({opacity: 0, zIndex: this.count * 10});
			}
			else {
				inner.eq(prev).css({display: 'none', opacity: 0, zIndex: (prev + 1) * 1000});
				   bg.eq(prev).css({opacity: 0, zIndex: (prev + 1) * 10});
			}
			
			if (this.count-1 <= this.now)
				this.now = -1;
			
			this.lock = false;
			
			if(!this.interval)
				this.initSetInterval();
		}));
		
		bg.eq(next).animate({opacity: 1}, this.fadeInSpeed, this.bind(function () {
		}));
	},
	
	pop: function () {
		var popup = $('#popup');
		var img   = popup.find('img');
		var top   = -68;
		
		$('#scroll ul li a img').mouseover(this.bind(function (el) {
			var num = parseInt($(el).parent().parent().parent().attr('class').replace(/thumb_/, ''));
			num     = num - (parseInt($('#scroll ul').css('left')) * -1 / 50);
			img.attr('src', $(el).attr('src'));
			popup.addClass('position_' + (num % 5));
			popup.show().animate({top: top - 7}, 40, this.bind(function (_el) {
				$(_el).animate({top: top}, 40);
			}));
		})).mouseout(this.bind(function () {
			popup.hide();
			popup.removeClass();
		}));
	},
	
	slide: function () {
		var prev  = $('#main_prev a');
		var next  = $('#main_next a');
		var ul    = $('#scroll ul');
		var li    = $('#scroll ul li');
		var count = li.length;
		var lock  = false;
		var move  = 50;
		var view  = 5;
		
		if (count <= view)
			this.reClass(next, 'none', true);
		
		next.click(this.bind(function () {
			if (next.attr('class') == 'none' || lock)
				return false;
			lock = true;
			
			ul.animate({left: '-=' + move}, 300, this.bind(function () {
				if (((view - count) * move) >= parseInt(ul.css('left')))
					this.reClass(next, 'none', true);
				else
					this.reClass(next, 'none', false);
				this.reClass(prev, 'none', false);
				lock = false;
			}));
			return false;
		}));
		
		prev.click(this.bind(function () {
			if (prev.attr('class') == 'none' || lock)
				return false;
			lock = true;
			
			ul.animate({left: '+=' + move}, 300, this.bind(function () {
				if (0 <= parseInt(ul.css('left')))
					this.reClass(prev, 'none', true);
				else
					this.reClass(prev, 'none', false);
				this.reClass(next, 'none', false);
				lock = false;
			}));
			return false;
		}));
	},
	
	reClass: function (a, className, add) {
		a = $(a);
		if (add)
			a.addClass(className);
		else
			a.removeClass(className);
	},
	
/* ===============================	 */
	
	rss: function () {
		var target = $('div#frm-news div#frm-news-view ul');
		
		$.ajax({
			url      : '/news/feed/',
			type     : 'GET',
			dataType : 'xml',
			cache    : false,
			success  : this.bind(function (_ajax, r) {
				this.news = this.map($(r).find('channel item'), function (i, item) {
					item = $(item);
					
					return {
						title: item.find('title').text(),
						date:  this.date(item.find('pubDate').text()),
						link:  item.find('link').text(),
						cat:   this.category(item.find('category:first').text())
					}
				});
				
				this.each(this.news, function (i, news) {
					var li = [
						'<li>',
						'<img src="' + news.cat + '" />',
						'<span class="date">' + news.date + '</span>',
						'<p><a href="' + news.link + '">' + news.title + '</a><span class="guide">' + news.title + '</span></p>',
						'</li>'
					].join('');
					
					target.append(li);
				});
				
				var li    = target.find('li');
				var i     = 0;
				
				li.eq(i).fadeIn(this.newsAnimationSpeed);
				this.stringSlice(li.eq(i));
				
				setInterval(this.bind(function () {
					target.find('li:visible').fadeOut(this.newsAnimationSpeed, this.bind(function () {
						i++;
						if (i == li.length)
							i = 0;
						
					  li.eq(i).fadeIn(this.newsAnimationSpeed);
					  this.stringSlice(li.eq(i));
					}));
				}), this.newsFadeSpeed);
			})
		});
	},
	
	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 < 10) ? '0': '') + _date.month + '/' + ((_date.day < 10) ? '0': '') + _date.day;
	},
	
	category: function (string) {
		switch(string) {
			case 'トピックス':
				return '/shared/img/common/frm-btn-topics.gif';
			case 'プロダクト':
				return '/shared/img/common/frm-btn-product.gif';
			case 'イベント':
				return '/shared/img/common/frm-btn-event.gif';
			case 'キャンペーン':
				return '/shared/img/common/frm-btn-campaign.gif';
			case 'ストア':
				return '/shared/img/common/frm-btn-store.gif';
			case 'ブログ':
				return '/shared/img/common/frm-btn-blog.gif';
			case 'プレスリリース':
				return '/shared/img/common/frm-btn-press-release.gif';
			case 'crocs cares':
				return '/shared/img/common/frm-btn-crocs-cares.gif';
			default:
				return '/shared/img/common/frm-btn-other.gif';
		}
	},
	
	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;
	},
	
/* ===============================	 */

	shop: function () {
		$.ajax({
			url      : '/news/store/feed/',//'feed.xml',
			type     : 'GET',
			dataType : 'xml',
			cache    : false,
			success  : this.bind(function (_ajax, r) {
				r = $(r);
				
				this.shop = this.map($(r).find('channel item'), function (i, item) {
					item = $(item);
						
					if (i < 2)
						return {
							title: item.find('title').text(),
							link:  item.find('link').text(),
							date:  item.find('pubDate').text()
						}
				});
				
				this.each(this.shop, function (shop) {
					var div = [
						'<div class="hover">',
		  			'<a href="' + shop.link + '"></a><img src="/shared/img/store/index/list_img_logo.gif" width="59" height="39" alt="crocs" />',
		  		  '<h5>' + this.date(shop.date) + '</h5>',
			      '<span>' + shop.title.slice(0, 20) + '...</span>',
						'</div>'
					].join('');
					
					$('#store div.store_info').append(div);
				});
				
				mm.common.hover();
			})
		});
	}
});

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

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

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