// JavaScript Document

var imgtop_c = 1;
var imgbttm_c = 1;

var gallery_dir = 'gallery_home/';
var gallery_jpg = null;
var jpg_c = 0;
var gallery_gif = null;
var gif_c = 0;

var animtime = 1.5*1000;
var cicletime = 3*1000;

var is_img_loading = false;
var is_img_loading2 = false;

$(document).ready(
	function()
	{
	
	    $('#imgbttm1').css('opacity','0');
	    $('#imgbttm1').show();
	    $('#imgbttm2').css('opacity','0');
	    $('#imgbttm2').show();
	    
	    $('#imgtop1').css('opacity','0');
	    $('#imgtop1').show();
	    $('#imgtop2').css('opacity','0');
	    $('#imgtop2').show();
	
		/*$('#imgbttm1').load(function () {
		    is_img_loading = false;
		    switchImgAnim();
		});
		$('#imgbttm2').load(function () {
		    is_img_loading = false;
		    switchImgAnim();
		});*/
	
		$.ajax({
			type: "POST",
			url: "gallery_dialer.php",
			data: '',

			//Evento di riuscita
			success: function(msg) {
				eval(msg);
				gallery_jpg = ret_arr_jpg;
				gallery_gif = ret_arr_gif;
				//alert(ret_arr_jpg[0]+' '+ret_arr_gif[0]);
				
				$('#imgbttm1').load(function () {
				    is_img_loading = false;
				    switchImgAnim();
				});
				$('#imgbttm2').load(function () {
				    is_img_loading = false;
				    switchImgAnim();
				});
				$('#imgtop1').load(function () {
				    is_img_loading2 = false;
				    switchImgAnim();
				});
				$('#imgtop2').load(function () {
				    is_img_loading2 = false;
				    switchImgAnim();
				});
				
				switchImgs();
				timerid = setInterval("switchImgs()",cicletime);
			}
		});
	}
);

function fadein(objid,easing_type) {
	var easing = 'easeOutCubic';
	if (easing_type == 1) {
        easing = 'easeOutCubic';
	}

    $('#'+objid).stop();
    if (easing_type == 1) {
        $('#'+objid).fadeTo(1200, 0).fadeTo(
            1300,
            1,
            null,
            jQuery.easing.def = easing
        );
    } else {
        $('#'+objid).fadeTo(
            animtime,
            1,
            null,
            jQuery.easing.def = easing
        )
    }
}

function fadeout(objid,easing_type) {
	var easing = 'easeOutCubic';
	if (easing_type == 1) {
        easing = 'easeOutCubic';
	}

    $('#'+objid).stop();
    if (easing_type == 1) {
        $('#'+objid).fadeTo(
            300,
            0,
            null,
            jQuery.easing.def = easing
        );
    } else {
        $('#'+objid).fadeTo(
            animtime,
            0,
            null,
            jQuery.easing.def = easing
        )
    }
}

function switchImgs() {
	if (!is_img_loading && !is_img_loading2) {
	    is_img_loading = true;
	    is_img_loading2 = true;

		var newt = imgbttm_c +1;
		if (newt > 2) {
		    newt = 1;
		}
		
		var newt2 = imgtop_c +1;
		if (newt > 2) {
		    newt = 1;
		}

		$('#imgbttm'+newt).attr('src',gallery_dir+gallery_jpg[jpg_c]);
		$('#imgtop'+newt).attr('src',gallery_dir+gallery_gif[gif_c]);
		
		jpg_c++;
		if (jpg_c == gallery_jpg.length) {
		    jpg_c = 0;
		}
		
		gif_c++;
		if (gif_c == gallery_gif.length) {
		    gif_c = 0;
		}
	}
}

function switchImgAnim() {
	if (!is_img_loading && !is_img_loading2) {
    	var newt = imgbttm_c +1;
		if (newt > 2) {
		    newt = 1;
		}
		
		var newt2 = imgtop_c +1;
		if (newt2 > 2) {
		    newt2 = 1;
		}

		//alert(newt);
		/*$('#imgbttm'+newt).css('opacity','0');
		$('#imgbttm'+newt).css('z-index','2');
		$('#imgbttm'+imgbttm_c).css('z-index','1');
		
		$('#imgtop'+newt2).css('opacity','0');
		$('#imgtop'+newt2).css('z-index','4');
		$('#imgtop'+imgtop_c).css('z-index','3');*/

        fadein('imgbttm'+newt,0);
		fadein('imgtop'+newt2,1);
		fadeout('imgbttm'+imgbttm_c,0);
		fadeout('imgtop'+imgtop_c,1);

		imgbttm_c = newt;
		imgtop_c = newt2;
    }
}
