var scrollerPannello = function(lh, tc_h) {
	var aperto = null;
	var num_linee = 4;

	this.open = function(p) {
		aperto = p;
		aperto.css({"marginTop":0}).fadeIn(1000);
	}
	this.close = function() {
		aperto.fadeOut(1000);
		aperto = null;
	}
	this.isopen = function() {
		return aperto !== null;
	}
	this.top = function() {
		aperto.css({marginTop: 0});
	}
	this.scrolla = function(s) {
		if (aperto === null)
			return;
		var m = aperto.css("marginTop");
		if (m === undefined)
			m = 0;
		else
			m = parseInt(m);
		if (s>0) {
			var h = aperto.height();
			if (h<tc_h)
				return;
			m -= num_linee*lh;
			if (m<tc_h-h)
				m = tc_h-h;
		} else {
			m += num_linee*lh;
			if (m>0)
				m = 0;
		}
		aperto.stop(true).animate({"marginTop": m}, 500);
	}
}

var slideshowObject = function(modo, tag, isLooping) {
	var ss = $(tag);
	var dd = [null,null];
	var me = this;

	var corrente = null;

	var slides = null;
	var didas = null;
	var idx = null;
	var autorun = null;

	this.crossfade = function(url,dida) {
		if (corrente !== null) {
			ss.eq(corrente).fadeOut(1000);
			if (dd[corrente] !== null)
				dd[corrente].hide();
		} else
			corrente = 1;

		ss.eq(corrente = (corrente+1)%2).attr({src:url}).fadeIn(1000);
		if (dida !== undefined) {
			dd[corrente] = $(dida);
			dd[corrente].show();
		}
	};

	switch (modo) {
	case 0: // crossfade su img
		ss.clone().insertAfter(ss).hide();
		ss = $(tag);
		this.scambia = this.crossfade;
		break;
	default:
		return undefined;
	}

	this.set_slides = function(s, d) {
		slides = s;
		didas = d;
		idx = null;
	};

	this.set = function(n) {
		if (n < 0)
			n += slides.length;
		if (idx == n)
			return;
		idx = n;
		this.scambia( slides[idx], $.isArray(didas)?didas[n]:undefined );
	};

	this.prev = function() { 
		if (idx < 1) {
			if (!isLooping)
				return null;
			idx = slides.length;
		}

		this.set(idx-1);
		return idx < 1;
	};

	this.next = function() { 
		if (idx >= slides.length-1) {
			if (!isLooping)
				return null;
			idx = -1;
		}

		this.set(idx+1);
		return idx < (slides.length-1);
	};

	this.start = function(t) {
		var zulu = (new Date()).getTime();

		if (autorun !== null)
			clearInterval(autorun);

		autorun = setInterval(function() {
			var n = Math.floor(((new Date()).getTime()-zulu) / t)%slides.length;
			if (n != idx)
				me.set(n);
		}, 250);
	};

	this.stop = function() {
		if (autorun !== null)
			clearInterval(autorun);
	};
}

