var nowImg = 0;
var preImgID = 'indexImg_';
var showActive = true;
var timeoutEventObj = null;

$(document).ready(function(){
	var offsetObj = $('#alignTarget').offset();
	$('#imgCarouselContainer').css('left', offsetObj.left);
	$('#imgCarouselContainer').show();
	
	setTimeout(function(){
		$("#indexImgMask").animate({
		    opacity: 0
		  }, 1000, function() {
			  $("#indexImgMask").hide();
			  if (document.getElementById('indexImgBtn_0'))
				{
					document.getElementById('indexImgBtn_0').src = 'images/btn_25a.jpg';
				}
				
				if (document.getElementById(preImgID+(nowImg + 1)))
				{
					setTimeout(activeCarousel,6000);
				}
		  });
	},2000);
	
	$(window).resize(function() {
		var offsetObj = $('#alignTarget').offset();
		$('#imgCarouselContainer').css('left', offsetObj.left);
	});
});

function activeCarousel()
{
	if (showActive)
	{
		var nextImg = parseInt(nowImg) + 1;
		if (!document.getElementById(preImgID+nextImg))
		{
			nextImg = 0;
		}
		
		hideImg(nowImg);
		showImg(nextImg);
		nowImg = nextImg;
		if (showActive){timeoutEventObj = setTimeout(activeCarousel,6000);}
	}
}

function hideImg(imgIndex)
{
	$("#"+preImgID+imgIndex).animate({
	    opacity: 0
	  }, 1000, function() {
		  $("#"+preImgID+imgIndex).hide();
	  });
	
	document.getElementById('indexImgBtn_'+imgIndex).src = 'images/btn_25.jpg';
}

function showImg(imgIndex)
{
	$("#"+preImgID+imgIndex).show();
	$("#"+preImgID+imgIndex).animate({
	    opacity: 1
	  }, 1000, function() {
		  
	  });
	
	document.getElementById('indexImgBtn_'+imgIndex).src = 'images/btn_25a.jpg';
}

function reShowImg(imgIndex)
{
	if (imgIndex != nowImg)
	{
		if (timeoutEventObj)
		{
			clearTimeout(timeoutEventObj);
		}
		
		showActive = false;
		hideImg(nowImg);
		showImg(imgIndex);
		nowImg = imgIndex;
		
		timeoutEventObj = setTimeout(function(){
			showActive = true;
			activeCarousel();
		},6000);
	}
}
