var ImgStage = new Class({
	
	initialize : function(){
		this.link = $('stage-lnk');
		this.stage = $('stage-img');
		this.max = 8;
		this.min = 1;
		this.ticktark = 0;
		this.image_index = 1;
		this.bind_events();
		this.stage_fx = false;
		this.src_pool = {};
	},
	
	start : function() {
		var self = this;
		this.pause();
		this.ticktark = setTimeout(function() {
			self.change();
		}, 3000);
	},
	
	pause : function() {
		clearTimeout(this.ticktark);
	},
	
	change : function(index, mouse_over) {
		mouse_over = !$defined(mouse_over) ? false : true;
		if (!mouse_over)
			index = this.image_index + 1;
		this.setImageIndex(index);
		var last = $('girl-' + this.last_image_index), target = $('girl-' + this.image_index), self = this, fx, target_src;
		if (!this.stage_fx) fx = this.stage_fx = new Fx.Morph(this.stage, { duration: 210, transition: Fx.Transitions.Quad.easeOut });
		else fx = this.stage_fx;
		if (!this.src_pool[index]) target_src = this.src_pool[index] = target.getElement('img').getProperty('src').replace('.jpg', '_b.jpg');
		else target_src = this.src_pool[index];
		last.removeClass('focus');
		target.addClass('focus');
		fx.start({
			opacity : [1, 0]
		}).chain(function() {
			self.stage.src = target_src;
			self.link.href = target.getProperty('href');
			if (self.image_index % 4 == 1 && !mouse_over) {
				self.__turn_left(function() {
					fx.start({
						opacity : [0, 1]
					});
				});
			}
			else {
				fx.start({
					opacity : [0, 1]
				});
			}
		});
		if (!mouse_over)
			this.start();
	},
	
	turn : function(scroll_to) {
		if ($('girls').getProperty('idx') === null) $('girls').setProperty('idx', 1);
		var idx = $('girls').getProperty('idx').toInt(), max = 2, obj = $('girl-group-' + idx);
		if (scroll_to == 'next') idx++;
		else idx--;
		if (idx < 1) idx = max;
		if (idx > max) idx = 1;
		$('girls').setProperty('idx', idx);
		var target = $('girl-group-' + idx);
		var fx1 = new Fx.Morph(obj, { duration: 400, transition: Fx.Transitions.Sine.easeOut });
		var fx2 = new Fx.Morph(target, { duration: 400, transition: Fx.Transitions.Sine.easeOut });
		if (scroll_to == 'next') {
			var ef1 = [0, -300], ef2 = [300, 0];
		}
		else {
			var ef1 = [0, 300], ef2 = [-300, 0];
		}
		fx1.start({
			//'left': ef1,
			'opacity'  : [1, 0]
		});
		setTimeout(function() {
			obj.setStyles({ 'display': 'none' })
			target.setStyles({ 'opacity': 0, 'display': '' });
			fx2.start({
				//'left': ef2,
				'opacity' : [0, 1]
			});
		}, 100);
	},
	
	__turn_left : function(fn) {
		this.turn('next');
		fn.call();
	},
	
	setImageIndex : function(index) {
		if (index > this.max) index = this.min;
		if (index < this.min) index = this.max;
		this.last_image_index = this.image_index;
		this.image_index = index;
	},
	
	bind_events : function() {
		var girls = $$('div#girl-list a'), self = this;
		girls.addEvents({
			'mouseenter' : function() {
				self.pause();
				var idx = this.id.replace('girl-', '').toInt();
				self.change(idx, true);
			}
		});
		$('girl-list').addEvents({
			'mouseleave' : function() {
				self.pause();
				self.start();
			}
		});
		$$('div#girls input').addEvents({
			'click' : function() {
				self.turn(this.getProperty('id'));
			}
		});
	}
	
});

window.addEvent('domready', function() {
    $$('a').setProperty('target', '_blank');
    if ($('index')) {
	    var stage = new ImgStage();
		stage.start();
    }
});




