/**
 * Code by Giancarlo "GM" Moschitta (info@myphp.it) and Negatyve (http://www.negatyve.com)
 * Powered by jQuery (http://jquery.com))
**/
/**
 * Avvia tutti gli script necessari ad una pagina di comparazione di Digital
**/
function initDigitalCompare()
{
	/* impostazione del sistema di visualizzazione delle specifiche */
	switch( SECTION_VIEW_SYSTEM )
	{
		case 'single':
			$( '.section-divider a' ).each
			(
				function()
				{
					$( this ).click
					(
						function(){ sectionDividerClicked( $( this ) );return false; }
					);
					sectionDividerClicked( $( this ) );
				}
			);
			break;

		case 'all':
			$( '.show-all a' ).each
			(
				function()
				{
					$( this ).click
					(
						function(){ allSectionsDividerClicked( $( this ) );return false; }
					);
					allSectionsDividerClicked( $( this ) );
				}
			);
			break;
	}
	/* impostazione dell'handler per la rimozione di un prodotto dal comparatore */
	$( '.remove-product a' ).click
	(
		function(){ removeProduct( $( this )); return false; }
	);
}

/**
 * Gestice il click su un pulsante per la rimozione di un prodotto dal comparatore
 *
 * @param link, object, required, Il link cliccato
**/
function removeProduct( link )
{
	var choice = showAlert( 'Sei sicuro di voler rimuovere questo prodotto dalla comparazione?', 'confirm', removeCompareProduct, link );
	if( choice )
	{
		removeCompareProduct( link );
	}
}

/**
 * Gestice la rimozione di un elemento della comparazione
 *
 * @param link, object, required, Il link cliccato
**/
function removeCompareProduct( link )
{
	var product = link.attr( 'href' ).substr( 1 );
	deleteCompareListItem( product );
	window.location.reload();
}

/**
 * Gestice il click sul section divider globale
 *
 * @param link, object, required, Il link cliccato
**/
function allSectionsDividerClicked( link )
{
	var open = link.hasClass( 'opened' );
	if( open )
	{
		link.removeClass().addClass( 'closed' );
		link.attr( 'title', 'Espandi' );
		link.html( 'Visualizza tutti i parametri' );
		$( '.datasheet-table .section-0, .datasheet-table .section-divider' ).hide();
	} else {
		link.removeClass().addClass( 'opened' );
		link.attr( 'title', 'Chiudi' );
		link.html( 'Nascondi tutti i parametri' );
		$( '.datasheet-table .section-0, .datasheet-table .section-divider' ).show();
	}
}

/**
 * Gestice il click su un section divider
 *
 * @param link, object, required, Il link cliccato
**/
function sectionDividerClicked( link )
{
	var href = link.attr( 'href' );
	var section = href.substr( href.lastIndexOf( '-' ) + 1 );
	var open = link.hasClass( 'opened' );
	if( open )
	{
		link.removeClass().addClass( 'closed' );
		link.attr( 'title', 'Espandi' );
		$( '.datasheet-table .section-' + section ).hide();
	} else {
		link.removeClass().addClass( 'opened' );
		link.attr( 'title', 'Chiudi' );
		$( '.datasheet-table .section-' + section ).show();
	}
}

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