function checkAll(btn)	{
	itemId = btn.form['itemId[]'];
	if (itemId.length)	{
		for (i=0; i<itemId.length; i++)	{
			itemId[i].checked = btn.checked;
		}
	} else {
		itemId.checked = btn.checked;
	}
}

function deleteSelected(btn, notConfirmAction)	{
	itemId = btn.form['itemId[]'];
	if (itemId.length == undefined && itemId.checked)	{
		itemChecked = 1;
	} else {
		itemChecked = 0;
		for (i=0; i<itemId.length; i++)	{
			if (itemId[i].checked)	{
				itemChecked++;
			}
		}
	}
	if (itemChecked == 0)	{
		alert('Devi selezionare almeno un elemento da eliminare');
		return;
	}
	if (notConfirmAction)	{
		btn.form.submit();
		return;
	}
	if (confirm("\n\nHai scelto di Eliminare [ "+itemChecked+" ] elemeto/i\n\nVuoi davvero completare l'operazione?\n\n"))	{
		btn.form.submit();
	}
}

function syncToRole(btn)	{
	itemId = btn.form['itemId[]'];
	if (itemId.length == undefined && itemId.checked)	{
		itemChecked = 1;
	} else {
		itemChecked = 0;
		for (i=0; i<itemId.length; i++)	{
			if (itemId[i].checked)	{
				itemChecked++;
			}
		}
	}
	if (itemChecked == 0)	{
		alert('Devi selezionare almeno un utente da sincronizzare');
		return;
	}
	if (btn.form.role.value == '')	{
		alert('Devi selezionare un ruolo');
		return;
	}
	if (confirm('Vuoi davvero sincronizzare i ['+itemChecked+'] utenti selezionati ?'))	{
		btn.form.action = btn.form.action.replace('Delete','SyncToRole');
		btn.form.submit();
	}
}

var SlideNews = Class.create();

SlideNews.prototype = {

	appearDuration: .8,
	fadeDuration: .6,
	timeOUT: 10,
	panels:Array(),
	perCicle: 1,
	currentPos: 0,
	initialize: function() {
		if (! $("newsroller")) return false;
		Element.hide("newsroller");
		this.panels = $$("table.blkcnt");
		this.panels.invoke('hide');
		Element.show("newsroller");
		this.start();
	},

	start: function() {

		start = this.perCicle * this.currentPos;
		end = start + this.perCicle;

		if (end >= this.panels.length) {
			end = this.panels.length
			this.currentPos = -1;
		}

		counter = 0;
		for (i=start; i < end; i++) {

			nws = this.panels[i];
			params = {
				delay: this.appearDuration * counter,
				duration: this.appearDuration,
				from: 0,
				to: 1
			};
			new Effect.Appear(nws, params);
			new Effect.Fade(nws, { delay: this.timeOUT + counter * this.fadeDuration, duration: this.fadeDuration });
			counter++;
		}

		window.setTimeout("mySlideNews.next()", this.timeOUT * 1000 + this.fadeDuration * this.perCicle * 1000);
	},
	next: function() {
		this.currentPos++;
		this.start();
	},
	prev: function() {
		this.currentPos--;
		this.start();
	}
}
function initSlideNews() { mySlideNews = new SlideNews(); }

Event.observe(window, 'load', initSlideNews, false);


/**************/
var HistoryBox = Class.create()

HistoryBox.prototype = {

	initialize: function() {

		if (!$("history")) return false;
		var objOverlay = document.createElement("div");
		objOverlay.setAttribute('id','historyOverlay');
		objOverlay.style.display = 'none';
		$("history").appendChild(objOverlay);
		this.goto("/events/news/view/history");
	},

	goto: function(url) {
		new Effect.Appear('historyOverlay', { duration: overlayDuration, from: 0.0, to: overlayOpacity });
		new Ajax.Request(url, {
			method: 'get',
			onComplete: function(transport) {
				$("history_box").update(transport.responseText);
			}
		});
		this.end();
	},

	end: function() {
		new Effect.Fade('historyOverlay', { duration: overlayDuration});
	}
}

function initHistoryBox() { myHistoryBox = new HistoryBox(); }

Event.observe(window, 'load', initHistoryBox, false);


/********************/
var FlashNews = Class.create();

FlashNews.prototype = {

	clock: 0,
	appearDuration: .6,
	fadeDuration: 0,
	timeOUT: 8,
	panels:Array(),
	perCicle: 1,
	cicles: 0,
	currentPos: 0,
	initialize: function() {
		if (! $("flashnews")) return false;
		Element.hide("flashnews");
		this.panels = $$("div.flashnews");
		this.panels.invoke('hide');
		Element.show("flashnews");
		$("flashnews").style.visibility = "visible";
		this.cicles = Math.ceil(this.panels.length / this.perCicle) - 1;
		this.start();
	},

	start: function() {

		if (this.currentPos < 0) { this.currentPos = this.cicles; }
		if (this.currentPos > this.cicles) { this.currentPos = 0; }

		start = this.perCicle * this.currentPos;
		end = start + this.perCicle;

// 		$("flashlabel").update("pos: "+this.currentPos+"; start: "+ start+"; end: "+end)

		if (end > this.panels.length) {
			end = this.panels.length
		}

		counter = 0;
		for (i=start; i < end; i++) {

			nws = this.panels[i];
			params = {
				delay: this.appearDuration * counter,
				duration: this.appearDuration,
				from: 0,
				to: 1
			};
			new Effect.BlindDown(nws, params);
			counter++;
		}
		this.clock = window.setTimeout("myFlashNews.next()", this.timeOUT * 1000 + this.fadeDuration * this.perCicle * 1000);
	},
	next: function() {

		this.panels.invoke('hide');
		window.clearTimeout(this.clock);
		this.currentPos++;
		this.start();
	},
	prev: function() {

		this.panels.invoke('hide');
		window.clearTimeout(this.clock);
		this.currentPos--;
		this.start();
	}
}
function initFlashNews() { myFlashNews = new FlashNews(); }
Event.observe(window, 'load', initFlashNews, false);
