var SwitchTime = 4000;
var SwitchTimer = null;
var now_img_index = -1;
var click_flg = 0;
var timer_flg = 0;
var menu_count = 0;
function swap_image(i,img) {
	var id_name = "#swap_img"+i;
	var arr_swap = $(id_name);
	$(id_name).attr('src', img);
}
function next_photo() {
	if(now_img_index==-1) {
		change_img(1);
	} else {
		change_img(now_img_index);
	}
}
function before_photo() {
	if(now_img_index==-1 || (now_img_index-2)<0) {
		change_img(menu_count-1);
	} else {
		change_img(now_img_index-2);
	}
}

function change_img(img_index) {
	if(click_flg) {
		return;
	}
	
	if(img_index>=menu_count) {
		img_index = 0;
	}
	now_img_index = img_index;
	var thumbs = $('.timessever_view .navi a');
	var id_count = 0;
	thumbs.each(function(i){
		var src = $(this).attr('href');
		if(src.match(/\.(html)/)) {
			if(img_index == id_count) {
				timer_flg = 1;
				$(this).hover();
			}
			id_count++;
		}
	});
}
$(function() {
	SwitchTimer = window.setTimeout("change_img(1)", SwitchTime);

	var view = $('.timessever_view .view');
	var img = $('img', view);
	var thumbs = $('.timessever_view .navi a');
	$('.timessever_thumbs').css("z-index", 100);
	
	$(view).css('position','relative');
	
	thumbs.each(function(i){
		var src = $(this).attr('href');
		if(src.match(/\.(jpg|gif|png)/)) {
			var content = img.clone().insertAfter(img).attr('src', src).css('display','none')
			             .css('position','absolute').css("top","0px").css("left","0px");
			content.attr('usemap',"#Map"+(menu_count+1));
			content.attr('id',"swap_img"+(menu_count+1));
			if ($('img', this).hasClass('btn_img') && src.match(/\.(jpg|gif|png)/)) {
				var btn = $('img', this);
				var btn_src = btn.attr('src');
				var btn_src_over = btn_src.replace(/^(.+)(\.[a-z]+)$/, '$1_on$2');
				var btn_over = btn.clone().insertAfter(btn).attr('src', btn_src_over).hide();
			}
			
			$(this).attr('href', $("area", "#Map"+(menu_count+1)).attr('href'));
			
			if(menu_count == 0) {
				$(content).css("z-index", 10);
				if (btn) btn.hide();
				if (btn) btn_over.show();
			}
			$(this).hover(
				function(){
					if ($(content).css("z-index") == 10) return false;
					if(SwitchTimer) {
					    window.clearTimeout(SwitchTimer);
					    SwitchTimer = 0;
					}
					if(timer_flg) {
						now_img_index += 1;
						var func = "change_img("+now_img_index+")";
						SwitchTimer = window.setTimeout(func, SwitchTime);
						timer_flg  = 0;
					} else {
						click_flg = 1;
					 	now_img_index = i;
					}
					
					$('img',view).each(function(){
						if($(this).css("z-index") == 10){
							$(this).css("z-index", 5);
							$(this).stop().fadeTo(250, 1);
						}
						else {
							$(this).css("z-index", 1);
						}
					});
					
					$(content).css("z-index", 10);
					
					if ($(content).is(':animated')) {
						$(content).stop().fadeTo(500, 1);
					} else {
						$(content).fadeOut(0);
						$(content).fadeIn(500);
					}
					
					$('img', thumbs).show();
					$('.timessever_view img[src*="_on."]').hide();
					if (btn) btn.hide();
					if (btn) btn_over.show();
				}
			).mouseout(
	    		function(){
	    			if(!click_flg && !SwitchTimer) {
	    				var func = "change_img("+now_img_index+")";
	    				SwitchTimer = window.setTimeout(func, SwitchTime);
	    			}
	    			click_flg = 0;
	    		}
			);
			menu_count++;
		}
	});
});
