// JavaScript Document

var currgaltag = 'ev';
var is_anim = false;

$(document).ready(
	function()
	{
	    if (jQuery.browser.msie && parseInt(jQuery.browser.version) < 8) {
	        $('div.alert').show();
		}
	
		$('div.listcont').jScrollPane({scrollbarWidth:14,showArrows:true});
		jQuery.event.add(window, "load", resizeFrame);
		jQuery.event.add(window, "resize", resizeFrame);
		
		$('#header').localScroll({
			axis:'y',
			queue:true //one axis at a time
		});
		$('#contacts').localScroll({
			axis:'y',
			queue:true //one axis at a time
		});
		
		var gals = $('ul.gal');
		for (i=0; i<gals.length; i++) {
		    var neww = $(gals[i]).children('li').width() * $(gals[i]).children('li').length;
			$(gals[i]).css('width',neww + 'px');
		}
		resetGalleries();
		
		$('div.gal-small-box div.box-navi a').click(function() {
		    slideGallery('#gal-small-'+currgaltag,$(this).attr('href'));
		    return false;
		});
		$('div.gal-small-box ul li a').click(function() {
		    slideGalleryAt('#gal-big-'+currgaltag,$(this).attr('href'));
		    return false;
		});
		$('div.gal-big-box a').click(function() {
		    slideGallery('#gal-big-'+currgaltag,$(this).attr('href'));
		    return false;
		});
		
		$('ul.gal-navi a').click(function() {
		    openGallery(this);
		    return false;
		});
		
		openGallery($('ul.gal-navi a')[0]);
		
		form_init();
		
		/*$('#school ul.news').toggle();
		$('#school div.listcont').jScrollPane({scrollbarWidth:14,showArrows:true});*/
		$('#school a.month').click(function() {
		    var act = $(this).attr('href');
		    if (act == '#backmonths') {
		        $('#school a.mhead').show();
		        $('div.monthnews').hide();
		    } else {
		    	$('#school a.mhead').hide();
		    	$(act).show();
		    }
		    //$('#school div.listcont').jScrollPane({scrollbarWidth:14,showArrows:true});
		    
		    return false;
		});
		
		$('div.monthnews div.scrollnews').jScrollPane({scrollbarWidth:14,showArrows:true});
		$('div.monthnews').hide();
		$('#school div.listcont').jScrollPane({scrollbarWidth:14,showArrows:true});
    }
);

function form_init() {
	$('#sendbox a.send').click(function() {
	    var inputs = $('#sendbox .mand');
        var notfilled = check_empty(inputs);

		if (notfilled.length == 0) {
	    	var msg = '';
	        if (!exp_email.test($("#sendbox input[name='uemail']").val())) {
	            msg += "\n" + '- il campo email non è in una forma corretta';
	        }
	        
	        if (msg == '') {
	            var qstring = ''
				for (i=0; i<inputs.length; i++) {
				    qstring += '&' + inputs[i].name + '=' + inputs[i].value;
				}
		        var phpstring = 'action=sendemail' + qstring;
		        sendEmail(phpstring);
			} else {
			    alert('I seguenti campi non sono compilati correttamente: ' + msg);
			}
	    } else {
	        var errstr = '';
	        for (i=0; i<notfilled.length; i++) {
	            errstr += '\n- ' + notfilled[i];
			}
	        alert('I seguenti campi non sono compilati correttamente:' + errstr);
		}

	    return false;
	});
}

function sendEmail(phpstring) {
	$.ajax({
        type: "POST",
        url: "aj_dialer.php",
        dataType: 'json',
        data: phpstring,
        //Evento di riuscita
        success: function(ret) {
			if (ret.msg_type == 'ok') {
				$('#sendbox')[0].reset();
				alert('Messaggio inviato con successo!');
			}
        }
    });
}

function openGallery(aobj) {
    resetGalleries();
    currgaltag = $(aobj).attr('href').substr(1);
    $('#gal-small-'+currgaltag).show();
    $('#gal-big-'+currgaltag).show();
    
    $('ul.gal-navi a').removeClass('active');
    $(aobj).addClass('active');
    
    $('div.gal-small-box h4.title').text($(aobj).text());
}

function resetGalleries() {
    $('ul.gal').hide();
    $('ul.gal').css('left',0);
}

function slideGallery(gallery,dir) {
	var galx = $(gallery)[0].offsetLeft;
	var elnum = $(gallery).children('li').length;
	//alert(elnum);
	if (elnum > 0 && !is_anim) {
	    is_anim = true;
	    var elw = $(gallery).children('li').width();
		if (dir == '#right' && galx > - elw * (elnum -1)) {
			$(gallery).animate(
			    {left: galx -elw},
			    1000 * 0.5,
			    null,
			    function() { is_anim = false; }
			);
		} else if (dir == '#left' && galx < 0) {
		    $(gallery).animate(
			    {left: galx +elw},
			    1000 * 0.5,
			    null,
			    function() { is_anim = false; }
			);
		} else {
		    is_anim = false;
		}
	}
}

function slideGalleryAt(gallery,elid) {
	if (!is_anim) {
	    is_anim = true;
	    $(gallery).animate(
		    {left: -$(elid)[0].offsetLeft},
		    1000 * 0.5,
		    null,
		    function() { is_anim = false; }
		);
	}
}

function resizeFrame() {
	var h = $(window).height();
	var w = $(window).width();
	var blh = $("#homep").height(); //tti i blocchi sono alti uguale
	/*var z = $("#homep").height();
	var x = $("#school").height();
	var y = $("#about").height();*/

	/*if (h < 700) {
		$("#bg_1").css("height", 800);
		$("#bg_2").css("height", 864);
		$("#bg_3").css("height", 900);
		$("#bg_4").css("height", 800);

		$("#a").css("margin-top", 800 - blh);
		$("#b").css("margin-top", (((1600) - blh) - blh));
	}
	else {*/
		/*$("#bg_1").css("height", h);
		$("#bg_2").css("height", h);
		$("#bg_3").css("height", h);
		$("#bg_4").css("height", h);*/
		$("#images div.bg_block").css("height", h);

		/*$("#a").css("margin-top", h - z);
		$("#b").css("margin-top", (((h *2) - x) - y));*/
		$("#a").css("margin-top", h - blh);
		$("#b").css("margin-top", (h *2 - blh *2));
		$("#c").css("margin-top", (h *3 - blh) - (h + blh));
		$("#d").css("margin-top", (h *7 - blh) - (h*5 + blh) );
		$("#e").css("margin-top", (h *9 - blh) - (h*7 + blh));
		//alert( (h - blh) + ' ' + (h *2 - blh *2) + ' ' + ((h *3 - blh) - (h + blh)) + ' ' + ((h *7 - blh) - (h*5 + blh)) + ' ' + ((h *9 - blh) - (h*7 + blh)) ); //
	//}

	if (w < 1096) {
		$(".bg_block").css("width", 1096);
	}
	else {
		$(".bg_block").css("width", w);
	}

	$(".bg_block").css("background-position");
}


// uses jquery fade
function killIt(item){
	$('#'+item).fadeOut(1000);
}



