var imageFadingTimer = null;
function viewNextImage()
{
    var totalImgs = $('.image_fade_view img').length;
    if(totalImgs < 1)
        return;
    var curObj = null;
    $('.image_fade_view img').each(function(){
        if($(this).css("display") != "none")
            curObj = this;
    });
    var curIdx = 0;
    
    if(curObj != null)
        curIdx = $('.image_fade_view img').index(curObj);
    
    if(totalImgs <= 1)
        return;
    var nextIdx = curIdx + 1;
    if(nextIdx >= totalImgs)
        nextIdx = 0;
    $('.image_fade_view img').eq(curIdx).css("z-index", 100).fadeOut(1200);
    $('.image_fade_view img').eq(nextIdx).css("z-index", 50).fadeIn(1200);
    $('.image_fade_thumb_view .selected').removeClass("selected");
    $('.image_fade_thumb_view a').eq(nextIdx).addClass("selected");
}
$(function() {
$('#pb-holder-nav').css("z-index", 200);
if($('.image_fade_view').length > 0)
{
    $('.image_fade_view img').click(function(){
		if(imageFadingTimer != null)
			clearInterval(imageFadingTimer);
        var curIdx = $('.image_fade_view img').index(this);
        var totalImgs = $('.image_fade_view img').length;
        if(totalImgs <= 1)
            return;
        var nextIdx = curIdx + 1;
        if(nextIdx >= totalImgs)
            nextIdx = 0;
        $('.image_fade_view img').eq(curIdx).css("z-index", 100).fadeOut(800);
        $('.image_fade_view img').eq(nextIdx).css("z-index", 50).fadeIn(800);
        $('.image_fade_thumb_view .selected').removeClass("selected");
        $('.image_fade_thumb_view a').eq(nextIdx).addClass("selected");
    });
    $('.image_fade_thumb_view a').click(function(){
		if(imageFadingTimer != null)
			clearInterval(imageFadingTimer);
        var curIdx = $('.image_fade_thumb_view a').index($('.image_fade_thumb_view .selected'));
        var nextIdx = $('.image_fade_thumb_view a').index(this);
        if(curIdx == nextIdx)
            return;
        $('.image_fade_view img').eq(curIdx).css("z-index", 100).fadeOut(800);
        $('.image_fade_view img').eq(nextIdx).css("z-index", 50).fadeIn(800);
        $('.image_fade_thumb_view .selected').removeClass("selected");
        $('.image_fade_thumb_view a').eq(nextIdx).addClass("selected");
    });
    var pbxViewTimer = null;
    $('#play-pbx-view').click(function(){
        if(pbxViewTimer == null)
        {
            viewNextImage();
            pbxViewTimer = setInterval("viewNextImage()", 3000);
        }else{
            clearInterval(pbxViewTimer);
            pbxViewTimer = null;
        }
    });
}
	
	imageFadingTimer = setInterval("autoFadeIn()", 3000);
});
function autoFadeIn()
{
	var isProceed = false;
	$('.image_fade_view img').each(function(){
		if($(this).css("display") != "none" && !isProceed)
		{
			var curIdx = $('.image_fade_view img').index(this);
			var totalImgs = $('.image_fade_view img').length;
			if(totalImgs <= 1)
				return;
			var nextIdx = curIdx + 1;
			if(nextIdx >= totalImgs)
				nextIdx = 0;
			$('.image_fade_view img').eq(curIdx).css("z-index", 100).fadeOut(800);
			$('.image_fade_view img').eq(nextIdx).css("z-index", 50).fadeIn(800);
			$('.image_fade_thumb_view .selected').removeClass("selected");
			$('.image_fade_thumb_view a').eq(nextIdx).addClass("selected");
			isProceed = true;
		}

	});
}

