/****************************************************************
*	FUNZIONE DI CONTROLLO PER IL MARKUP DEI COMMENTI
****************************************************************/
function bbCode(v) {
	try {
		var contentBox = document.getElementById(tbCommento);
		if (contentBox.selectionStart) // firefox
		{
			var pretxt = contentBox.value.substring(0, contentBox.selectionStart);
			var therest = contentBox.value.substr(contentBox.selectionEnd);
			var sel = contentBox.value.substring(contentBox.selectionStart, contentBox.selectionEnd);
			contentBox.value = pretxt + "[" + v + "]" + sel + "[/" + v + "]" + therest;
			contentBox.focus();
		}
		else if (document.selection && document.selection.createRange) // IE
		{
			var str = document.selection.createRange().text;
			contentBox.focus();
			var sel = document.selection.createRange();
			sel.text = "[" + v + "]" + str + "[/" + v + "]";
		}
	}catch (ex) { }

	return;
}

/****************************************************************
*	FUNZIONE DI CREAZIONE ANTEPRIMA COMMENTO
****************************************************************/
function previewComment() {
	var commento = $('#' + tbCommento);
	var testo = commento.val();
	var pattern = /(^|[^\/])(www\.[\S]+(\b|$))/gim;

	testo = testo.replace(/http:\/\//gi, "");
	testo = testo.replace(pattern, '$1<a href="http://$2" target="_blank" rel="nofollow">$2</a>');
	testo = testo.replace(/\r/gi, "<br />");
	testo = testo.replace(/\n/gi, "<br />");
	testo = testo.replace(/\[B\]/gi, "<b>");
	testo = testo.replace(/\[\/B\]/gi, "</b>");
	testo = testo.replace(/\[U\]/gi, "<u>");
	testo = testo.replace(/\[\/U\]/gi, "</u>");
	testo = testo.replace(/\[I\]/gi, "<i>");
	testo = testo.replace(/\[\/I\]/gi, "</i>");
	testo = testo.replace(/\[QUOTE\]/gi, "<quote>");
	testo = testo.replace(/\[\/QUOTE\]/gi, "</quote>");

	$('#blog-add-comment .preview-box-text').html(testo);
	$('#blog-add-comment .preview-box').toggle();
	$('#blog-add-comment .compile-box').toggle();

	return false;
}

/****************************************************************
*   Funzioni di visualizzazione modal popup
****************************************************************/
function showModal(pagina, larghezza, altezza, classe, stile, scrollabile, youtube) {
	var clientWidth = $(window).width();
	var clientHeight = $(window).height();
	var scrolla = scrollabile = '' ? 'auto' : scrollabile;
	var w = larghezza >= clientWidth ? clientWidth - 40 : larghezza;
	var h = altezza >= clientHeight ? clientHeight - 40 : altezza;
	var destinazione = pagina.indexOf("http://") == -1 ? baseURI + pagina : pagina;

	//sfondo
	var sfondo = $('<div id="popUpBackGround" />');
	sfondo.height((document.documentElement.scrollHeight ? document.documentElement.scrollHeight : document.body.scrollHeight) + 'px');
	sfondo.width((document.documentElement.scrollWidth ? document.documentElement.scrollWidth : document.body.scrollWidth) + 'px');
	sfondo.appendTo(document.body);

	//finestra
	var modal = $('<iframe id="popUp" frameborder="0" scrolling="' + scrolla + '" src="' + destinazione + '" style="' + stile + '" class="' + classe + '" width="' + w + '" height="' + h + '" />');
	if (youtube == 'si') {
		modal = $('<iframe id="popUp" title="YouTube video player" width="' + w + '" height="' + h + '" src="' + destinazione + '" frameborder="0" style="' + stile + '" class="' + classe + '" allowfullscreen="no"></iframe>');
	}
	modal.appendTo(document.body);
	modal.addClass("popUpBody");
	modal.css("left", ((clientWidth / 2) - (w / 2) + 'px'));
	modal.css("top", ((clientHeight / 2) - (h / 2) + 'px'));
	modal.show();

	//pulsante chiusura
	var close = $('<a id="closeModalButton" href="javascript:hideModal();"><img width="30" height="30" src="' + baseURI + 'images/close.png" /></a>');
	close.appendTo(document.body);
	close.addClass("close-modal-button");
	close.css("left", ((clientWidth / 2) - (w / 2) + w - 15) + 'px');
	close.css("top", ((clientHeight / 2) - (h / 2) - 15) + 'px');
	close.show();

	window.onresize = function () {
		sfondo.height((document.documentElement.scrollHeight ? document.documentElement.scrollHeight : document.body.scrollHeight) + 'px');
		sfondo.width((document.documentElement.scrollWidth ? document.documentElement.scrollWidth : document.body.scrollWidth) + 'px');
		var clientWidth = $(window).width();
		var clientHeight = $(window).height();
		modal.css("left", ((clientWidth / 2) - (w / 2) + 'px'));
		modal.css("top", ((clientHeight / 2) - (h / 2) + 'px'));
		close.css("left", ((clientWidth / 2) - (w / 2) + w - 15) + 'px');
		close.css("top", ((clientHeight / 2) - (h / 2) - 15) + 'px');
	}
}

function hideModal(control) {
	$('#popUpBackGround').remove();
	$('#popUp').remove();
	$('#closeModalButton').remove();
	window.onresize = null;
}

/****************************************************************
*   Funzioni di utility
****************************************************************/
// funzioni per l'apertura di popUp
function privacy(){
	w = 580;
	h = screen.height-50;
	LeftPosition = 0;//(screen.width) ? (screen.width-w)/2 : 0;
	TopPosition = 0;//(screen.height) ? (screen.height-h)/2 : 0;
	settings = 'toolbar=no,location=no,directories=no,status=no,menubar=no,scrollbars=yes,resizable=no,copyhistory=yes,width='+w+',height='+h+',left='+LeftPosition+', top='+TopPosition+',screenX='+LeftPosition+',screenY='+TopPosition;
	privacy = window.open(baseURI + 'modules/privacy.html', 'privacy', settings);
}
