/*
	キービジュアル部
*/
var KV={};

KV.config = [
	{// 1
		photo : {
			left:0,
			top:-60
		},
		text : {
			left  :0,
			top   :190,
			width :720,
			height:76
		},
		link : {
			left:625,
			top:40,
			width:85,
			height:19
		}
	},
	{// 2
		photo : {
			left:0,
			top:0
		},
		text : {
			left  :0,
			top   :190,
			width :720,
			height:76
		},
		link : {
			left:625,
			top:40,
			width:85,
			height:19
		}
	},
	{// 3
		photo : {
			left:0,
			top:-60
		},
		text : {
			left  :0,
			top   :30,
			width :720,
			height:76
		},
		link : {
			left:625,
			top:40,
			width:85,
			height:19
		}
	},
	{// 4
		photo : {
			left:0,
			top:-60
		},
		text : {
			left  :0,
			top   :30,
			width :720,
			height:76
		},
		link : {
			left:625,
			top:40,
			width:85,
			height:19
		}
	},
	{// 5
		photo : {
			left:0,
			top:-70
		},
		text : {
			left  :0,
			top   :30,
			width :720,
			height:76
		},
		link : {
			left:625,
			top:40,
			width:85,
			height:19
		}
	},
	{// 6
		photo : {
			left:0,
			top:-70
		},
		text : {
			left  :0,
			top   :30,
			width :720,
			height:76
		},
		link : {
			left:625,
			top:40,
			width:85,
			height:19
		}
	},
	{// 7
		photo : {
			left:0,
			top:0
		},
		text : {
			left  :0,
			top   :30,
			width :720,
			height:76
		},
		link : {
			left:625,
			top:40,
			width:85,
			height:19
		}
	}
];

KV.init = function(){
	for(var i=0; i<KV.config.length; i++){
		var photo = $('.kv_main_photo').eq(i);
		var text =  $('.kv_main_text').eq(i);
		var link =  $('.kv_main_link').eq(i);
		var ini = KV.config[i];

		photo.css({
			left : ini.photo.left + 'px',
			top  : ini.photo.top + 'px'
		});
		text.css({
			left : ini.text.left + 'px',
			top  : ini.text.top + 'px',
			width : ini.text.width + 'px',
			height : ini.text.height + 'px'
		});
		link.css({
			left : ini.link.left + 'px',
			top  : ini.link.top + 'px',
			width : ini.link.width + 'px',
			height : ini.link.height + 'px'
		});
	}
	KV.Menu.init();
}


KV.Animation = {
	is_started : false,
//current_frame_id : 0,

	start : function(isResume){
		if(KV.Animation.is_started) return;
		$('.kv_main_photo').hide(0).stop();
		$('.kv_main_text').hide(0).stop();
		$('.kv_main_link').hide(0).stop();
		KV.Animation.is_started = true;
//this._makeFunc(KV.Animation.current_frame_id, isResume).apply();

		this._makeFunc(0).apply();
	},
	stop : function(i){
		if(!KV.Animation.is_started) return;
		
		$('.kv_main_photo').hide(0).stop();
		$('.kv_main_text').hide(0).stop();
		$('.kv_main_link').hide(0).stop();
		KV.Animation.is_started = false;
//KV.Animation.current_frame_id = i;
	},
	show : function(id){
		$('.kv_main_photo').hide(0);
		$('.kv_main_text').hide(0);
		$('.kv_main_link').hide(0);

		$('.kv_main_photo').eq(id).fadeIn(500);
		$('.kv_main_text').eq(id).fadeIn(500);
		$('.kv_main_link').eq(id).fadeIn(500);
	},

	_makeFunc : function(i){
		return function(){
			if(!KV.Animation.is_started) return;
	
			var j = (i+1)%(KV.config.length);
			var photo = $('.kv_main_photo').eq(i);
			var text =  $('.kv_main_text').eq(i);
			var link =  $('.kv_main_link').eq(i);
			var next = KV.Animation._makeFunc(j);



//var k = (i+ KV.config.length -1)%(KV.config.length);
//KV.Menu.deselect(k);
KV.Menu.deselectAll();
//KV.Menu.select(i, true);

link.show(200, function(){KV.Menu.select(i, true);});

if(i==0){
			// 最初だけ少し早く
			photo.fadeIn(500);// IN
			text.fadeIn(500);
}else{
			photo.fadeIn(2000);// IN
			text.fadeIn(2000);
}
//			link.fadeIn(2000);

			photo.fadeTo(5000,1,next);// 待機
			text.fadeTo(5000,1);
//			link.fadeTo(5000,1);

			photo.fadeOut(2000);// OUT
			text.fadeOut(1000);
//			link.fadeOut(2000);
//link.hide(200, function(){KV.Menu.deselect(i);});
		}
	}
}

KV.Menu = {
	buttons : null,

	init : function(){
		this.buttons = $('.kv_menu_button');
		this._init_clicked_status();
		this._add_events();
	},
	select : function(id, dont_deselect_on_mouseout/*=false*/){
		var target = this.buttons.eq(id);
		var src = target.attr('src');
		target.attr('src', src.replace('_out.gif', '_over.gif'));
		if(dont_deselect_on_mouseout) KV.Menu.buttons[id].dont_deselect_on_mouseout = true;
	},
	deselect : function(id){
		var target = this.buttons.eq(id);
		var src = target.attr('src');
		target.attr('src', src.replace('_over.gif', '_out.gif'));
		KV.Menu.buttons[id].dont_deselect_on_mouseout = false;
	},
	deselectAll : function(){
		this.buttons.each(function(i){
			KV.Menu.deselect(i);
		});
	},
	_add_events : function(){
		this.buttons.each(function(i){
			$(this).hover(
				// over
				function(){
					KV.Menu.select(i);
				},
				// out
				function(){
					if(!KV.Menu.buttons[i].dont_deselect_on_mouseout)
						KV.Menu.deselect(i);
				}
			);

			// クリックでアニメーションを停止、指定フレームを表示
			$(this).click(function(){
				KV.Menu._init_clicked_status();
				KV.Menu.select(i, true);
				KV.Animation.stop(i);
				KV.Animation.show(i);
			});
		});

/*
		// メニュークリックによる停止状態から復帰
		$('#kv_main').mouseout(function(event){
			var target = $(event.relatedTarget);
			if(!target.is('div')) return;

			setTimeout(function(){
				if(!KV.Animation.is_started){
					KV.Animation.start(true);
					KV.Menu._init_clicked_status();
				}
			}, 2000);
		});
*/
	},
	_init_clicked_status : function(){
		KV.Menu.deselectAll();
		this.buttons.each(function(i){
			KV.Menu.buttons[i].dont_deselect_on_mouseout = false;
		});
	}
}














