var sl = jQuery("#slide_show");
var sl_act_imgs = 0;
var sl_imgs = new Array();
jQuery.preloadImages = function () {
	if (typeof arguments[arguments.length - 1] == 'function') {
		var callback = arguments[arguments.length - 1];
	} else {
		var callback = false;
	}
	if (typeof arguments[0] == 'object') {
		var images = arguments[0];
		var n = images.length;
	} else {
		var images = arguments;
		var n = images.length - 1;
	}
	var not_loaded = n;
	for (var i = 0; i < n; i++) {
		jQuery(new Image()).attr({src: images[i],id:"sl_"+i,id:"sl_"+i}).css({display: "none", position: "absolute"}).load(function() {
			sl_imgs.push(this);
			if (--not_loaded < 1 && typeof callback == 'function') {
				callback();
			}
		});
	}
}
function galleryStart(imgs, speed, width) {
	if( imgs.length > 0 ) {
		jQuery(document).ready(function() {
				jQuery.preloadImages(imgs, function() {
					n = sl_imgs.length;
					for (var i = 0; i < n; i++) { sl.append(sl_imgs[i]); }
					sl_imgs[0] = jQuery("#sl_0");
					setInterval(function() { slShow(speed, width) }, speed+1500); 
					sl_imgs[0].fadeIn("slow").animate( {marginLeft : -width}, speed);
					for (var i = 1; i < n; i++) sl_imgs[i] = jQuery("#sl_"+i); 
				});
		});
	}
}

function slShow(speed, width) {	
	sl_imgs[sl_act_imgs].fadeOut("slow").fadeOut("fast");
	sl_act_imgs++; if( sl_act_imgs >= sl_imgs.length ) sl_act_imgs = 0;
	sl_imgs[sl_act_imgs].css({marginLeft : 0}).fadeIn("slow").animate( {marginLeft : -width}, speed);
}
