/**
 * switchviewer.js
 *
 * @version    1.0.0
 * @modified   2010-04-11
 * @required   jQuery v1.4.2
 *
 */ 

/* -----------------------------------------------------------------------*/
// switchviewer
/* -----------------------------------------------------------------------*/

var SwitchTime = 5000;
var SwitchTimer = null;
var now_img_index = -1;
var click_flg = 0;
var timer_flg = 0;
function swap_image(i,img) {
	var id_name = "#swap_img"+i;
	var arr_swap = $(id_name);
	$(id_name).attr('src', img);
}

function change_img(img_index) {
	if(click_flg) {
		return;
	}
	if(img_index>7) {
		img_index = 0;
	}
	now_img_index = img_index;
	var thumbs = $('.switchviewer .timessever_thumbs a');
	thumbs.each(function(i){
		if(img_index == i) {
			timer_flg = 1;
			$(this).hover();
		}
	});
}
$(function() {
	SwitchTimer = window.setTimeout("change_img(1)", SwitchTime);

	var view = $('.timessever_view .view');
	var img = $('img', view);
	var thumbs = $('.timessever_view .timessever_thumbs a');
	$('.timessever_thumbs').css("z-index", 100);
	
	$(view).css('position','relative');
	
	thumbs.each(function(i){
		var src = $(this).attr('href');
		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"+(i+1));
		content.attr('id',"swap_img"+(i+1));
		if ($('img', this).hasClass('btn') && 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_over$2');
			var btn_over = btn.clone().insertAfter(btn).attr('src', btn_src_over).hide();
		}
		
		$(this).attr('href', $("area", "#Map"+(i+1)).attr('href'));
		
		if(i == 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+1;
				}
				
				$('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(1000, 1);
				} else {
					$(content).fadeOut(0);
					$(content).fadeIn(1000);
				}
				
				$('img', thumbs).show();
				$('.timessever_view img[src*="_over."]').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;
    		}
		);
	});
});
