/**
 * Code by Giancarlo "GM" Moschitta (info@myphp.it) and Simone "negatyve" Auteritano (negatyve@negatyve.com)
 * Powered by jQuery (http://jquery.com))
**/
/**
 * Avvia tutti gli script necessari alla homepage di Manager
**/
function initManagerArticles()
{
	var isIE6 = $.browser.msie && Number( $.browser.version.split('.')[0] ) <= 6;
	if( !isIE6 )
	{
		setResourcePicturesRollover();
		setResourcePicturesNavigation();
	}
	$( '.comment-form-send' ).click( commentsFormSendComment );
	$( '.resource-see-also li' )
		.css( 'cursor', 'pointer' )
		.hover( addHoverClass, delHoverClass )
		.click( gotoListInnerLink )
	;
	getCommentCookie();
}

/**
 * Imposta il rollover sulle immagini delle gallerie delle risorse
**/
function setResourcePicturesRollover()
{
	$( '.resource-pictures li a' ).each
	(
		function()
		{
			var li = $( this ).parent();
			var div = $( '<div />' );
			div
				.appendTo( li )
				.css( 'width', '115px' )
				.css( 'height', '65px' )
				.css( 'opacity', '0.5' )
				.css( 'position', 'absolute' )
				.css( 'top', '0' )
				.css( 'left', '0' )
				.css( 'z-index', '2' )
			;
			li
				.hover
				(
					function(){ div.css( 'background', SHADE_COL ); },
					function(){ div.css( 'background', 'none' ); }
				).click
				(
					function()
					{
						gotoSelectedPage( $( 'a', li ).attr( 'href' ) );
						return false;
					}
				)
				.css( 'cursor', 'pointer' )
			;
		}
	);
}

/**
 * Imposta la navigazione delle immagini della galleria
**/
function setResourcePicturesNavigation()
{
	$( '.resource-pictures a.prev-pic' ).click
	(
		function()
		{
			if( PIC_NAV_CURRENT_POS > 0 )
			{
				$( '.resource-pictures ul' ).animate
				(
					{ left: '-' + (120 * --PIC_NAV_CURRENT_POS ) + 'px' }, 200, 'swing'
				);
			}
			return false;
		}
	);
	$( '.resource-pictures a.next-pic' ).click
	(
		function()
		{
			if( PIC_NAV_CURRENT_POS < $( '.resource-pictures li' ).length - 5 )
			{
				$( '.resource-pictures ul' ).animate
				(
					{ left: '-' + (120 * ++PIC_NAV_CURRENT_POS ) + 'px' }, 200, 'swing'
				);
			}
			return false;
		}
	);
}


/*****************
* COMMENTS BLOCK *
*****************/


/**
 * Gestisce il click sul pulsante di invio commento del form dei commenti
 **/
function commentsFormSendComment()
{
	var errors = checkCommentFields
	(
		[
			{type:'name', field:$('#comment-form-name')},
			{type:'mail', field:$('#comment-form-mail')},
			{type:'area', field:$('#comment-form-area')}
		]
	);
	if( errors == 0 )
	{
		insertComment
		(
			$('#comment-form-name').val(),
			$('#comment-form-mail').val(),
			$('#comment-form-site').val(),
			$('#comment-form-area').val(),
			$('#comment-form-check:checked').length,
			$('#comment-form-data:checked').length,
			'article',
			ARTICLE_ID
		);
	}
	return false;
}

/**
 * Inserisce il commento di un utente
 *
 * @param name, string, required, Il nome del commentatore
 * @param mail, string, required, L'indirizzo email
 * @param site, string, required, Il sito
 * @param area, string, required, Il corpo del commento
 * @param send, int, required, Se inviare o no le notifiche sulle risposte
 * @param type, string, required, Il tipo di dato commentato
 * @param id, string, required, L'id dell'elemento commentato
**/
function insertComment( name, mail, site, area, send, data, type, id )
{
	if( data == 0 )
	{
		resetCommentCookie();
	} else {
		setCommentCookie();
	}
	var querystring = '';
	querystring += '&id=' + id;
	querystring += "&name=" + fixAmpersand( $.trim( name ) );
	querystring += "&mail=" + $.trim( mail );
	querystring += "&site=" + fixAmpersand( $.trim( site ) );
	querystring += "&area=" + fixAmpersand( $.trim( area ) );
	querystring += "&send=" + send;
	querystring += "&data=" + data;
	querystring += "&type=" + type;

	$( '.comments-box form').fadeTo( 'normal', '0.4' );
	showAjaxLoader( $( '.comments-box' ) );
	$.ajax
	(
		{
			type: 'POST',
			url: SITE_PATH + 'a/ajax.php',
			data: 'c=dati&m=insertComment' + querystring,
			success: function( result )
			{
				hideAjaxLoader( $( '.comments-box' ) );
				$( '.comments-box form' ).fadeTo( 'slow', '1.0' );
				if( ajaxError( result ) )
				{
					return false;
				}
				switch( result )
				{
					case "1":
						showAlert( "Il tuo commento \xE8 in attesa di essere approvato" );
					    break;
					case "2":
						showAlert( "Il tuo commento \xE8 stato correttamente inserito" );
						window.location.reload();
					    break;
					default:
						showAlert( result );
					    break;
				}
				$( '#comment-form-area' ).val( '' );
			}
		}
	);
}

/**
 * Esegue il controllo sui campi di un form di invio commento
 *
 * @param fields, array, required, L'array di campi da controllare
**/
function checkCommentFields( fields )
{
	var type, field, value, errors = 0;
	for( var i = 0; i < fields.length; i++ )
	{
		field = fields[i].field;
		value = field.val();
		wrong = false;
		switch( fields[i].type )
		{
			case 'name': case 'area':

				wrong = $.trim( value ) == '';
				break;

			case 'mail':

				wrong = $.trim( value ) == '' || value.match(/^([a-z0-9_\.-])+@(([a-z0-9_-])+\.)+[a-z]{2,6}$/i) == null;
				break;

			case 'site':

				break;

		}
		if( wrong )
		{
			errors++;
			setFieldErrorColor( field );
		}
	}
	return errors;
}

/**
 * Imposta il colore di errore sul campo di un form
 *
 * @param field, jQuery, required, Il campo da evidenziare
**/
function setFieldErrorColor( field )
{
	var container = field.parent();
	container.addClass( 'text-input-container-error' );
	field.focus
	(
		function()
		{
			$( this ).unbind('focus');
			$( this ).parent().removeClass( 'text-input-container-error' );
		}
	);
}

/**
 * Resetta il cookie dei dati dei form
**/
function resetCommentCookie()
{
	$.cookie( 'mol_comment_data', null, { path: '/' } );
}

/**
 * Setta il cookie dei dati dei form
**/
function setCommentCookie()
{
	$.cookie
	(
		'mol_comment_data',
		$( '#comment-form-name' ).val() + '<sep>' + $( '#comment-form-mail' ).val() + '<sep>' + $( '#comment-form-site' ).val()
		,
		{ path: '/', expires: 365 }
	);
}

/**
 * Recupera il cookie dei dati dei form
**/
function getCommentCookie()
{
	var content = $.cookie( 'mol_comment_data' );
	if( content == null || content.length == 0 )
	{
		return false;
	}
	var data = content.split( '<sep>' );
	$( '#comment-form-name' ).val( data[0] );
	$( '#comment-form-mail' ).val( data[1] );
	$( '#comment-form-site' ).val( data[2] );
}

/**
 * Associa la funzione initManagerArticles all'evento onload della pagina
**/
$( document ).ready( initManagerArticles );