var eh = {
	timer               : null,
	active_element      : null,
	next_active_element : null,
	
	start: function(){
		var p = this;
		this.timer = setInterval( function() {
			p.controller();
		} , 4000 );
	},
	end: function(){
		clearInterval(this.timer);
	},
	controller: function(){
		this.setActive();
		this.setNextActive();
		this.switchActive();
	},
	getActive: function(){
		var result = $('#mainVisual img.active');
		if ( result.length == 0 ) result = $('#mainVisual img:last');
		return result;
	},
	setActive: function(){
		this.active_element = $('#mainVisual img.active');
		if ( this.active_element.length == 0 ) this.active_element = $('#mainVisual img:last');
	},
	setNextActive: function(){
		this.next_active_element = this.active_element.next().length ? this.active_element.next() : $('#mainVisual img:first');
	},
	switchActive: function(){
		//現在のアクティブ要素をラストアクティブにする
		this.active_element.addClass('last-active');
		//次のアクティブ要素にCSSを設定する
		var obj = this;
		this.next_active_element.css({opacity: 0.0})
		//次のアクティブ要素をアクティブ要素にする
		.addClass('active')
		.animate({opacity: 1.0}, 1000, function() {
			//現在のアクティブ要素をアクティブ・ラストアクティブからはずす
			obj.active_element.removeClass('active last-active');
		});
	},
	link: function(href){
		window.open( href, null );
	}
};

$(function() {
	eh.start();
	//クリックされた場合
	$("#thumbnail a").click(function() {
		eh.end();
		var atcive = eh.getActive();
		atcive.removeClass('active');
		$("#" + $(this).attr("id").replace("thumb_","")).addClass('active');
		eh.start();
		return false;
	});
});
