(function ($) {
var self = mm.index = mm.extend({
	
/* ===============================	 */
	
	firstTimeLag:       700,
	fadeInInterval:     10000,
	fadeInSpeed:        800,
	newsFadeSpeed:      6000,
	newsAnimationSpeed: 800,
	
	path:           'images/top-mainvisual/',
	
/* ===============================	 */
	
	mainImages:     [],
	news:           [],
	shop:           [],
	lock:           false,
	
/* ===============================	 */
	
	init: function () {
		this.images();
		this.setMain();
		this.setList();
		this.action();
		this.nextbtn();
		this.prevbtn();
		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;
		}));
	},

	nextbtn: function () {
		$('#main #ctrl #mainvisualNext img').click(this.bind(function (el) {
			if (this.lock)
				return false;
			
			var num = parseInt(this.now) + 1;
			if (num > this.count)
				num = 0;
			
			this.lock = true;
			
			clearTimeout(this.timeout);
			clearInterval(this.interval);
			this.timeout  = 0;
			this.interval = 0;
			
			this.change(num, this.now);
			
			return false;
		}));
	},

	prevbtn: function () {
		$('#main #ctrl #mainvisualPrev img').click(this.bind(function (el) {
			if (this.lock)
				return false;
			
//edit 
			if(this.now == -1)
				this.now = this.count - 1;
//edit 
			var num = parseInt(this.now) - 1;
//			var this.now = parseInt(num) - 1;
//			if (num < 0) {
//				num = this.count - 1;
//				this.now = 0;
//			}
			if (num < 0) {
				num = this.count - 1;
				this.now = 0;
			}

			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);
	}
});

/* ===============================	 */

$(function () {
	mm.index.init();
});
})(jQuery);

/* ===============================	 */
