/**
 * @author SECI
 */

var link_da_seguire = '';

function salva_email(e) {
	Event.stop(e);
	options = {
		method : 'post',
		postBody : 'email=' + $F('box_newsletter_email') + '&action=iscriviti',
		onSuccess : conferma_salva_email
	};

	new Ajax.Request($F('box_newsletter_lingua') + '/modulo/newsletter.htm', options);
}

function conferma_salva_email(t) {
	alert(t.responseText);

	if(t.responseText == 'Iscrizione effettuata')
		$('box_newsletter_email').value == '';
}

function heff(e) {
	var id = Event.element(e).id;
	$(id).addClassName('hover');
}

function nheff(e) {
	var id = Event.element(e).id;
	$(id).removeClassName('hover');
}

function indietro(e) {
	Event.stop(e);
	history.back();
}

function stampa(e) {
	Event.stop(e);
	window.print();
}

function banner_click(e) {
	Event.stop(e);
	var id = Event.element(e).id;
	var idBanner = id.split('_')[1];
	var link_da_seguire = $('banlink_' + idBanner).href;
	options = {
		method : 'post',
		postBody : 'idBanner=' + idBanner + '&action=contatore',
		onSuccess : function(t) {
			location.href = link_da_seguire;
		}
	};

	new Ajax.Request('modulo/banners.htm', options);
}

function externalLinks() {
	if(!document.getElementsByTagName)
		return;
	var anchors = document.getElementsByTagName("a");
	for(var i = 0; i < anchors.length; i++) {
		var anchor = anchors[i];
		if(anchor.getAttribute("href") && anchor.getAttribute("rel") == "external")
			anchor.target = "_blank";
	}
}

function nifty_start() {

}

function abilita_post_rating() {
	parts = $F('pag_voto').split('/');

	if(parts.length == 2)
		var updateU = 'index.php?pag=sezione&sezione=' + parts[1];
	else
		var updateU = 'index.php?pag=' + $F('pag_voto');
	updateU += '&idRisorsa=' + $F('idRisorsa_voto') + '&c_ID=' + $F('c_ID_voto') + '&action=Vota';

	var post_rating = new Control.Rating('post_rating', {
		value : 0,
		rated : false,
		min : 1,
		max : 5,
		multiple : false,
		updateParameterName : 'post_rating_voto',
		updateUrl : updateU
	});
}

function salva_commento(e) {
	Event.stop(e);
	options = {
		method : 'post',
		postBody : $('form_commenti').serialize() + '&action=Commenta',
		onSuccess : conferma_salva_commento
	};
	parts = $F('pag_commento').split('/');

	if(parts.length == 2)
		new Ajax.Request('index.php?pag=sezione&sezione=' + parts[1], options);
	else
		new Ajax.Request('index.php?pag=' + $F('pag_commento'), options);
}

function conferma_salva_commento(t) {
	msg = t.responseText;
	msg_esito = msg.split('/');

	if(msg_esito[0] == 'Commento salvato') {
		$('show_errors').hide();
		
		if(msg_esito[1] == 1) {
			imp_commento = '<div class="post_comment">' + '<span class="post_comment_author png_bg">' + $F('firma') + '</span> (<span class="post_comment_date">Postato ora</span>)' + '<p>' + $F('commento') + '</p>' + '</div>';

			new Insertion.Bottom('post_comments_list', imp_commento);
			new Effect.toggle('post_comments_form');
		} 
		else {
			$('post_comments_form').update('<p class="commento_lasciato png_bg">Il commento sarà pubblicato al più presto, grazie.</p>');
		}
	} else {
		if($('show_errors')) {
		
			var stato = $('show_errors').style.display;
			
			elts = msg_esito[0].split('\n');
			html_ul = '<ul>';

			for( x = 0; x < elts.length; x++)
			html_ul += '<li>' + elts[x] + '</li>';
			html_ul += '</ul>';

			$('show_errors').update(html_ul);

			if(stato == 'none')
				new Effect.Appear('show_errors');
			else
				new Effect.Highlight('show_errors');
		} else
			alert(msg_esito[0]);
	}
}

function on_load_event(e) {

	/***  Specifiche del sito   ***/
	if($('featured')) jQuery("#featured > ul").tabs({fx:{opacity: "toggle"}}).tabs("rotate", 5000, true);
	/******************************/

	/* Box con angoli arrotondati */
	nifty_start();

	/* link con target esterni */
	externalLinks();

	/* Banners */
	var banners = $$('.lay_banner');
	banners.each(function(s) {
		$(s).observe('click', banner_click);
	});
	/* Lettori RSS */
	var rssreader = $$('.newsreader');
	rssreader.each(function(s) {
		new rsspausescroller("news", s.id, 3000, "", "date");
	});
	/* Form per iscrizione newsletter */
	if($('box_newsletter'))
		$('box_newsletter').observe('submit', salva_email);

	/* Pulsante per tornare indietro nelle pagine */
	if($('back'))
		$('back').observe('click', indietro);

	/* Stampa pagina */
	if($('print_button'))
		$('print_button').observe('click', stampa);

	/* Voto articolo */
	if($('post_rating'))
		abilita_post_rating();

	/* Gestione commenti */
	if($('form_commenti'))
		$('form_commenti').observe('submit', salva_commento);

	/* Popup con effetto overlay */
	if($('lay_alcagest_overlay_popup')) {

		$("chiudi").observe('click', function(e) {
			$('lay_alcagest_overlay_popup').hide();
			$('alcagest_overlay_popup').hide();
		});

		$("lay_alcagest_overlay_popup").observe('click', function(e) {
			$('lay_alcagest_overlay_popup').hide();
			$('alcagest_overlay_popup').hide();
		});

		$("alcagest_overlay_popup").observe('click', function(e) {
			$('lay_alcagest_overlay_popup').hide();
			$('alcagest_overlay_popup').hide();
		});

		$('lay_alcagest_overlay_popup').show();
		new Effect.SlideDown('alcagest_overlay_popup');
	}
}

Event.observe(window, 'load', on_load_event);


