
  var  max_size = 700;
var n_slides = 0;
function resize_slides() {
$("#apl-gallery").css("background-image","none");
$("#apl-slides").show();
$("#apl-slides .slide a img").each(function(i) {


    var w = max_size;
    var h = Math.ceil($(this).height() / $(this).width() * max_size);
if(h>160){
var h = 160;
var w = Math.ceil($(this).width() / $(this).height() * 160);

}


  $(this).css({ height: h, width: w });


});


$("#apl-slides .slide a").each(function(i) {


var h2 = Math.ceil(($("#apl-slides").height() - $(this).height())/2) ;
$(this).css("top",h2);
});




}


$(window).load(function(){
	/* This code is executed after the DOM has been completely loaded */
	resize_slides()
	var totWidth=0;
	var positions = new Array();
	
	$('#apl-slides .slide').each(function(i){
		
		/* Traverse through all the slides and store their accumulative widths in totWidth */
		n_slides++;
		positions[i]= totWidth;
		totWidth += $(this).width();
		
		/* The positions array contains each slide's commulutative offset from the left part of the container */
		
		if(!$(this).width())
		{
			//alert("Please, fill in width & height for all your images!");
			return false;
		}
		
	});
	
	$('#apl-slides').width(totWidth);

	
	
	/*****
	 *
	 *	Enabling auto-advance.
	 *
	 ****/
	 

	function resetAllBalls() {
	for (i  = 0; i < n_slides ; i++) {
	$('#ball_' + i).removeClass('ball_grey');
	$('#ball_' + i).removeClass('ball_blue');
	
			}
	$('.ball').addClass('ball_grey');
	}
	
	function setBlue(b) {
	$('#ball_' + b).removeClass('ball_grey');
	$('#ball_' + b).addClass('ball_blue');
	}
	
	function autoAdvance()
	{
		if(current==-1) return false;
		resetAllBalls();		
		if(positions[current] == undefined) {
        current = 0;
        }

		setBlue(current);
		$('#apl-slides').animate({marginLeft:-positions[current]+'px'},600);
		current++;
	}

	// The number of seconds that the slider will auto-advance in:
	
	var changeEvery = 8;
	resetAllBalls();
	setBlue(0);
	var current=1;
	
	$(".ball").click(function() {
			current = -1;
			var c = this.id.substring(5);
			resetAllBalls() 
			setBlue(c);
			$('#apl-slides').animate({marginLeft:-positions[c]+'px'},600);
	
			});
	
	
	var itvl = setInterval(function(){autoAdvance()},changeEvery*1000);

	/* End of customizations */
});
