$(document).ready(function () {
	// Don't Spoil It!
	$('.spoiler-switcher')
	    .addClass('spoiler-pad')
	    .toggle(
		function () {
		    $(this).next('div.spoiler-div').addClass('spoiler-pad').fadeIn('slow');
		    $(this).empty().append('Per nascondere il testo fai click qui.');
		    
		},
		function () {
		    //$(this).siblings('div.spoiler-div').fadeOut('slow');
		    $(this).next('div.spoiler-div').fadeOut('slow');
		    $(this).empty().append('Testo nascosto, fai click qui per vederlo.');
		}
	    );
	
	// conflitto pubblicita' in flash con script grafici
	var nasvg = $(".embed-asvg").length;
	if (nasvg > 0) {
	    $('.banner_hd #FLASH_AD').empty();
	}

	// help LaTeX (PDF)
	$.nyroModalSettings(
	{
	    'bgColor':'#003159',
	    'closeButton': '<a href="#" class="nyroModalClose" id="closeBut" title="chiudi">Close</a>',
	    'endRemove': modalRemoved
	}

	);
	$("#txtarea-message").keydown(function(event) {
	    var code = (event.keyCode ? event.keyCode : event.which); 
	    var F1_KEYCODE = 112;
	    if(code == F1_KEYCODE) {
	        event.preventDefault();
		$.nyroModalManual({
		    url: 'http://www.matematicamente.it/share/scripts/latexrender/doc/latex-help-pdf.html#page=1&view=FitH&scrollbar=0&toolbar=0&statusbar=0%navpanes=0',
		    minHeight: 640,
		    minWidth: 940,
		    title: 'Simboli LaTeX'
	        });
	        return false;
	    } 
	});

	// richiamo regole forum nell'editor
	$("#txtarea-message").hide();
        var arr = [
		// regole forum
		'usa un linguaggio chiaro e scrivi in italiano corretto',
                'evita lo stile tvb dei telefonini',
                'non sono tollerati spam o flame',
                'racchiudi le tue formule matematiche tra due simboli di dollaro \\$, cos&igrave; \\$ x^2 - 1/2 = 0 \\$',
                'non &egrave; consentito postare lo stesso messaggio in pi&ugrave; aree del forum'
        ];

        var textToInsert = ['<ul>'];
        var i = 1;
        var length = arr.length;

        
	for (var a = 0; a <length; a += 1) {
              textToInsert[i++]  = '<li>';
              textToInsert[i++] = arr[a];
              textToInsert[i++] = '</li>' ;
        }

	$('#richiamo-regole-forum').append('<ul>' + textToInsert.join('') + '</ul>');
	$('#richiamo-regole-forum').append('<div id="hide-richiamo-regole-forum" align="center">Fai click in quest\'area per iniziare a scrivere.</div>');

	var docLoc = document.location;
	var regex = RegExp('posting\\.php(?:\\?mode=(newtopic|quote|editpost|reply)&(?:f|t|p)=[0-9]+)?$');
	var whatMode = [];
	whatMode = regex.exec(docLoc);
	if (!whatMode || (whatMode != null && (whatMode[1] == 'reply' || whatMode[1] == 'newtopic' || whatMode[1] == 'quote'))) {
	    $("#txtarea-message").hide();
	    $("#richiamo-regole-forum").show().click(function () {
		$(this).hide();
		$("#txtarea-message").show().focus();
	    });
	} else {
	   $("#txtarea-message").show();
	}
	
	// 2009-07-17T22:22:00+0100, autolink formule
	var img_dir = 'http://www.matematicamente.it/share/images/png/';
	var style_tooltip = 'background:#D1D496;padding:10px;font-size:8px;color:#AF6262';
	$(".autolink-formula").each(function () {
	    var title_formula = $(this).attr('title');
	    var img_formula = title_formula.replace(/ /g, '_').toLowerCase() + '.png';
	    var html_tooltip = '<div style="' + style_tooltip + '"><img src="' + img_dir + img_formula + '"/><hr /> <span>'+title_formula+'</span></div>';
	    $(this).tooltip({
		track: true,
		showURL: false,
		delay: 0,
		bodyHandler: function () {
		    return $(html_tooltip);
		}
	    });
	});
	
	$(".post-images img").each(function () {
	    var img_width = $(this).width();
	    var max_img_width = 169 /* vedi configurazione avatar nel forum */
	    if (img_width > max_img_width) {
		$(this).attr({width:max_img_width});
		$(this).css({'width':max_img_width});
	    } 
	});
	
	// pulsante conversione notazione PGN-SAN
	$('#lt-pgn-viewer-san').nyroModal();
	// pulsante imageshack.us
	$('#imageshack-button').click(function () {
	    $('#imageshack').toggle();
	})
	// pulsante tinypic
	if (showTinypicPlugin && hideTinypicPlugin) {
	    $('#tinypic-button').toggle(showTinypicPlugin, hideTinypicPlugin);
	}
	// editor formule
	$('#editor-formule-button').click(function () {
	    $('#editor-formule').toggle();
	});
});

// 2007-12-20T22:26:00+0100, corregge colore delle formule nell'intestazione blue
$(window).load(
    function () {
        $('.row-header mstyle').attr('mathcolor', 'white');
        $('.nav-header mstyle').attr('mathcolor', 'white');
    }
);
