var pic_array = new Array();
var url_array = new Array();
var nowPos = 0;
var thumbs;
var nowThumb;
var timerID;

$(function(){
	//キービジ
	var myD = new Date();
	var jsonPath = "pic.js?"+myD.getSeconds();
	 $.getJSON(jsonPath, function(data){
		for(i in data){
			pic_array.push("img/index/key/"+data[i].path);
			url_array.push(data[i].linkURL);
			$("#cashImg").append("<img src=\"img/index/key/"+data[i].path+"\" />");
			$("#ctrlList").append("<li><a href=\"javascript:void(0);\" onclick=\"changePic("+(Number(i)+1)+");\"><img src=\"img/index/renew1012/btn_o.gi\" /></a></li>");
		}
		$("#cashImg").remove();
		$("#slidePic img").fadeTo(0,0);	
		nowPos = 1;
		changePic(nowPos);
		timerStart();
		
		$("#slidePic").click(function(){
			location.href = url_array[nowPos-1];
		});
		
		$(thumbs).hover(function(){
			$(this).find("img").attr("src", "img/index/renew1012/btn_o.gif");
		},
		 function () {
			 if($(this).index() != nowPos-1){
				$(this).find("img").attr("src", "img/index/renew1012/btn.gif");
			 }
		});
		$(thumbs).click(function(){
			clearInterval(timerID);
			timerStart();
		});
		
	});
	 
	//エリアリンク
	var linkFlg = false;
	
	$(".colLink").click(function(){
			if(linkFlg){
				var linkURL = $(this).find(".linkURL>a").attr("href");
				location.href = linkURL;
			}
	});
	
	$(".colLink").hover(function(){
		linkFlg = true;
		$(this).css("cursor","pointer");	
	}, function(){
		linkFlg = false;
		$(this).css("cursor","normal");	
	});
	
	$(".blankTarg").hover(function(){
		linkFlg = false;
	},function(){
		linkFlg = true;
	});
	
	$(".noLink").hover(function(){
		$(this).css("cursor","default");	
	});
});

function timerStart(){
	timerID = setInterval(function(){		
		if(nowPos < pic_array.length){
			nowPos++;
		}
		else{
			nowPos = 1;
		}
		changePic(nowPos);
	},5000);
}

function changePic(num){
	nowPos = num;
	thumbs = $("#ctrlList li");
	nowThumb = thumbs[nowPos-1];
	$(thumbs).find("img").attr("src", "img/index/renew1012/btn.gif");	
	$(nowThumb).find("img").attr("src", "img/index/renew1012/btn_o.gif");
	$("#slidePic img").before("<img src='"+pic_array[num-1]+"' alt=''>");
	$("#slidePic img:last").fadeOut("slow",function(){
		$(this).remove();											
	});
	return false;
}


