(function ($) {
var self = mm.index = mm.extend({
	
/* ===============================	 */
	
	firstTimeLag:       500,
	fadeInInterval:     4000,
	fadeInSpeed:        1000,
	
/* ===============================	 */
	
	lock:           false,
	
/* ===============================	 */
	
	init: function () {
		this.setMain();
		this.initSetTimeout();
	},
	
/* ===============================	 */
	
	
	setMain: function () {
		this.each($('#main_visual div.main_visual_item'), function (i, el) {
			$(el).css({
				zIndex:  (i+1) * 1000,
				opacity: 0,
				display: (i == 0 ? 'block' : 'none')
			});
		});
		this.count = $('#main_visual div.main_visual_item').length;
		$('#main_visual div.main_visual_item:first').css({display: 'block', opacity: '1'});
	},
	
	initSetTimeout: function () {
		var inner = $('#main_visual div.main_visual_item:first');
		this.now  = 0;
		
		this.timeout = setTimeout(this.bind(function () {
			this.lock = true;

//			$('div.frm_loader').hide();
			this.lock = false;
			this.initSetInterval();
		}), 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);
	},
	
	change: function (next, prev) {
		var inner = $('#main_visual div.main_visual_item');
		
		if (prev < 0)
			prev = this.count - 1;
		
		inner.eq(prev).css({zIndex: 999});
		inner.eq(next).css({display: 'block'}).animate({opacity: 1}, this.fadeInSpeed, this.bind(function () {
			this.now = next;
			if (prev < 0) {
				inner.eq(this.count - 1).css({display: 'none', opacity: 0, zIndex: this.count * 1000});
			}
			else {
				inner.eq(prev).css({display: 'none', opacity: 0, zIndex: (prev + 1) * 1000});
			}
			
			if (this.count-1 <= this.now)
				this.now = -1;
			
			this.lock = false;
			
			if(!this.interval)
				this.initSetInterval();
		}));
	},
	
	sleepTime: function ( T ) {
		var d1 = new Date().getTime(); 
		var d2 = new Date().getTime(); 
		while( d2 < d1+T ){
			d2 = new Date().getTime(); 
		} 
		return; 
	}
});

/* ===============================	 */

$(function () {
	mm.index.init();
});
})(jQuery);

/* ===============================	 */
