/* Filename: misura/javascripts/all.js */

/**
 * Extends jquery
**/
jQuery.extend
(
	{
		forge:function( tag )
		{
			return jQuery( document.createElement( tag ) );
		}
	}
);
jQuery.fn.setVisible = function( flag )
{
	return this.each
	(
		function()
		{
			jQuery( this ).css( 'visibility', flag ? 'visible' : 'hidden' );
		}
	);
};
jQuery.fn.tagName = function()
{
	return this.get(0).tagName.toLowerCase();
};

/**
 * Creates js objects
**/
var it = {
	misura:
	{
		vars: {}
		,
		lang: {}
	}
};

/**
 * Initializes the current script
**/
it.misura.initAll = function()
{
	var notify = jQuery( '.social-links .link-friend a' );

	jQuery.browser.idevices = !!( navigator.userAgent.toLowerCase().match(/(iphone|ipod|ipad)/i) );
	it.misura.vars.minWindowWidth = 990;
	it.misura.vars.minWindowHeight = 600;
	it.misura.vars.nullFunction = function(){};
	it.misura.vars.content = jQuery( '#content' );
	it.misura.vars.requiredFlashVersion = '9';
	it.misura.vars.hasFlashPlayerVersion = swfobject.hasFlashPlayerVersion( it.misura.vars.requiredFlashVersion );
	it.misura.createBackgroundContainer();
	it.misura.preloadImages( it.misura.vars.imagesToPreload );
	jQuery( window ).resize( it.misura.windowResize );
	jQuery( '.hint' ).hint();
	jQuery( 'a[rel*="external"]' ).click( it.misura.openExternalLink );
	jQuery( 'a.contact-us' ).click( it.misura.showMailPanel );
	jQuery( '#search-form' ).submit( it.misura.showSearchPanel );

	if( notify.length > 0 )
	{
		notify.click( it.misura.showNotifyPanel );
	}
};/* end function it.misura.initAll() */

/**
 * Set the scroll pane
**/
it.misura.setScrollPane = function( scroller, options )
{
	if( scroller.length > 0 )
	{
		if( jQuery.browser.idevices )
		{
			scroller.css( 'overflow-y', 'auto' );
		} else {
			scroller.jScrollPane( options );
		}
	}
};/* end function it.misura.setScrollPane() */

/**
 * Invoked when the send to friend link gets clicked
**/
it.misura.showNotifyPanel = function()
{
	var link = jQuery( this ).attr( 'href' );
	jQuery.ajax
	(
		{
			  type: 'POST'
			, url: it.misura.vars.siteUrl + 'includes/ajax.php'
			, data: 'm=getNotifyPanel&language=' + it.misura.vars.applicationLanguageId
			, success: function( result )
			{
				if( result === '' || it.misura.ajaxError( result ) )
				{
					jQuery.noop();
				} else {
					var div = jQuery.forge( 'div' )
						.attr( 'id', 'notify-panel' )
						.html( result )
						.appendTo( jQuery( '#container' ) )
					;
					div.modal( { close: false } );
					jQuery( '.hint', div ).hint();
					jQuery( '.required', div ).focus
					(
						function()
						{
							jQuery( this ).css( 'background', '#ffffff' );
						}
					);
					jQuery( '#notify-panel-send' ).click
					(
						function()
						{
							it.misura.sendNotifyForm( link, this );
							return false;
						}
					);
					jQuery( '.close-button', div ).click
					(
						function()
						{
							jQuery( '#notify-panel' ).hide();
							window.setTimeout( "it.misura.closeModal( '#notify-panel' );", 200 );
							return false;
						}
					);
				}
			}
		}
	);
	return false;
};/* end function it.misura.showNotifyPanel() */

/**
 * Shows the mail panel
**/
it.misura.showMailPanel = function()
{
	jQuery.ajax
	(
		{
			  type: 'POST'
			, url: it.misura.vars.siteUrl + 'includes/ajax.php'
			, data: 'm=getMailPanel&language=' + it.misura.vars.applicationLanguageId
			, success: function( result )
			{
				if( result === '' || it.misura.ajaxError( result ) )
				{
					jQuery.noop();
				} else {
					jQuery.forge( 'div' )
						.attr( 'id', 'mail-panel' )
						.html( result )
						.appendTo( jQuery( '#container' ) )
						.modal( { close: false } )
					;
					jQuery( '#mail-panel .required' ).focus
					(
						function()
						{
							jQuery( this ).css( 'background', '#ffffff' );
						}
					);
					jQuery( '#mail-panel-send' ).click( it.misura.sendMailForm );
					jQuery( '#mail-panel .close-button' ).click
					(
						function()
						{
							jQuery( '#mail-panel' ).hide();
							window.setTimeout( "it.misura.closeModal( '#mail-panel' );", 200 );
							return false;
						}
					);
				}
			}
		}
	);
	return false;
};/* end function it.misura.showMailPanel() */

/**
 * Sends the notify form
**/
it.misura.sendNotifyForm = function( link, button )
{
	var
		  form = jQuery( '#notify-panel' )
		, errors = 0
		, content = []
		, textarea = jQuery( 'textarea', form )
		, mailto = jQuery( '#notify_mail_to', form )
		, mailfrom = jQuery( '#notify_mail_from', form )
		, send = ''
		, prop
	;
	content.notify_message = textarea.val();
	content.notify_mail_to = mailto.val();
	content.notify_mail_from = mailfrom.val();
	content.resource = link;
	if( content.notify_message.length > 255 || content.notify_message === textarea.attr( 'title' ) )
	{
		textarea.css( 'background', '#ff0000' );
		errors++;
	}
	if( content.notify_mail_to.match(/^([a-z0-9_\.\-])+@(([a-z0-9_\-])+\.)+[a-z]{2,6}$/i) === null )
	{
		mailto.css( 'background', '#ff0000' );
		errors++;
	}
	if( content.notify_mail_from.match(/^([a-z0-9_\.\-])+@(([a-z0-9_\-])+\.)+[a-z]{2,6}$/i) === null )
	{
		mailfrom.css( 'background', '#ff0000' );
		errors++;
	}
	if( errors === 0 )
	{
		jQuery( button ).attr( 'disabled', 'disabled' );
		for( prop in content )
		{
			send += "&" + prop + "=" + content[prop];
		}
		jQuery.ajax
		(
			{
				  type: 'POST'
				, url: it.misura.vars.siteUrl + 'includes/ajax.php'
				, data: 'm=sendNotifyForm&language=' + it.misura.vars.applicationLanguageId + send
				, success: function( result )
				{
					if( result === '' || it.misura.ajaxError( result ) )
					{
						it.misura.closeModal( '#notify-panel' );
					} else {
						jQuery( 'form', form ).remove();
						jQuery( 'h2', form ).css
						(
							'background-image',
							"url('" + it.misura.vars.imagesUrl + "lang/" + it.misura.vars.applicationLanguage + "/misc/title-thank-you.png')"
						);
						jQuery.forge( 'strong' )
							.html( result )
							.addClass( 'message' )
							.appendTo( form )
						;
						window.setTimeout( "it.misura.closeModal( '#notify-panel' );", 4000 );
					}
				}
			}
		);
	}
	return false;
};/* end function it.misura.sendNotifyForm() */

/**
 * Sends the email form
**/
it.misura.sendMailForm = function()
{
	var
		  form = jQuery( '#mail-panel' )
		, errors = 0
		, content = []
		, send = ''
		, prop
	;

	jQuery( '.required', form ).each
	(
		function()
		{
			var t = jQuery( this ), v = jQuery.trim( t.val() );
			content[ t.attr( 'name' ) ] = v;
			if( v === '' )
			{
				t.css( 'background', '#ff0000' );
				errors++;
			}
		}
	);

	content[ jQuery( 'textarea', form ).attr( 'name' ) ] = jQuery( 'textarea', form ).val();
	if( jQuery( 'textarea', form ).val().length > 255 )
	{
		jQuery( 'textarea', form ).css( 'background', '#ff0000' );
		errors++;
	}
	content.birthdate = jQuery( '#day' ).val() + '/' +  jQuery( '#month' ).val() + '/' + jQuery( '#year' ).val();
	content.gender = jQuery( 'input[name=gender]' ).val();
	content.gender = content.gender === undefined ? '' : content.gender;
	content.recipient = jQuery( '#recipient' ).val();
	content.city = jQuery( '#city' ).val();
	if( errors === 0 )
	{
		jQuery( this ).attr( 'disabled', 'disabled' );
		for( prop in content )
		{
			send += "&" + prop + "=" + content[prop];
		}
		jQuery.ajax
		(
			{
				  type: 'POST'
				, url: it.misura.vars.siteUrl + 'includes/ajax.php'
				, data: 'm=sendMailForm&language=' + it.misura.vars.applicationLanguageId + send
				, success: function( result )
				{
					if( result === '' || it.misura.ajaxError( result ) )
					{
						it.misura.closeModal( '#mail-panel' );
					} else {
						jQuery( 'form', form ).remove();
						jQuery( 'h2', form ).css
						(
							'background-image',
							"url('" + it.misura.vars.imagesUrl + "lang/" + it.misura.vars.applicationLanguage + "/misc/title-thank-you.png')"
						);
						jQuery.forge( 'strong' )
							.html( result )
							.addClass( 'message' )
							.appendTo( form )
						;
						window.setTimeout( "it.misura.closeModal( '#mail-panel' );", 4000 );
					}
				}
			}
		);
	}
	return false;
};/* end function it.misura.sendMailForm() */

/**
 * Shows the search panel
**/
it.misura.showSearchPanel = function()
{
	var
		  search = jQuery( '#search-form input' )
		, value = search.val().replace( /'/g, '' ).replace( /"/g, '' )
		, title = search.attr( 'title' )
	;
	if( value === title || value === '' )
	{
		search.val( '' ).focus();
		return false;
	}
	jQuery.ajax
	(
		{
			  type: 'POST'
			, url: it.misura.vars.siteUrl + 'includes/ajax.php'
			, data: 'm=getSearchPanel&language=' + it.misura.vars.applicationLanguageId + '&search=' + value
			, success: function( result )
			{
				if( result === '' || it.misura.ajaxError( result ) )
				{
					jQuery.noop();
				} else {
					jQuery.forge( 'div' )
						.attr( 'id', 'search-panel' )
						.html( result )
						.appendTo( jQuery( '#container' ) )
						.modal( { close: false } )
					;
					jQuery( '#search-panel form' ).submit
					(
						function()
						{
							it.misura.searchPanelNewSearch( 'none' );
							return false;
						}
					);
					jQuery( '#search-panel .close-button' ).click
					(
						function()
						{
							jQuery( '#search-panel' ).hide();
							window.setTimeout( "it.misura.closeModal( '#search-panel' );", 200 );
							return false;
						}
					);
					it.misura.performSearch( value, 'none' );
				}
			}
		}
	);
	return false;
};/* end function it.misura.showSearchPanel() */

/**
 * Performs a site search
 *
 * @param search, string, required, The text to search for
 * @param limit, string, required, Limit the research to ...
**/
it.misura.performSearch = function( search, limit )
{
	var params = [
		  'm=performSearch'
		, 'language=' + it.misura.vars.applicationLanguageId
		, 'search=' + Base64.encode( search )
		, 'limit=' + limit
	];
	jQuery.ajax
	(
		{
			  type: 'POST'
			, url: it.misura.vars.siteUrl + 'includes/ajax.php'
			, data: params.join( '&' )
			, success: function( result )
			{
				if( it.misura.ajaxError( result ) )
				{
					jQuery.noop();
				} else {
					var panel = jQuery( '#search-panel' );
					jQuery( '.pager', panel ).remove();
					jQuery( '.no-results', panel ).remove();

					var
						  temp = result.split( '---' )
						, code = parseInt( temp[0], 10 )
					;
					if( code === 0 )
					{
						jQuery( '.results-list', panel ).remove();
						jQuery.forge( 'p' )
							.addClass( 'no-results' )
							.appendTo( panel )
							.html( Base64.decode( temp[1] ) )
						;
					} else {
						var results_list = jQuery( '.results-list', panel );
						if( results_list.length > 0 )
						{
							results_list.empty();
						} else {
							results_list = jQuery.forge( 'div' )
								.addClass( 'results-list' )
								.appendTo( panel )
							;
						}
						results_list.html( Base64.decode( temp[1] ) );
						panel.append( Base64.decode( temp[2] ) );
						var pager = jQuery( '.pager', panel );
						jQuery( '.prev', pager ).click( it.misura.searchPanelPagePrev );
						jQuery( '.next', pager ).click( it.misura.searchPanelPageNext );
						jQuery( 'ul li', results_list ).click( it.misura.searchPanelRedirectToPage );
					}
					jQuery( '.searched-words strong' ).html( search );
				}
			}
		}
	);
};/* end function it.misura.performSearch() */

/**
 * Redirects to a search panel result page
**/
it.misura.searchPanelRedirectToPage = function()
{
	window.location.href = jQuery( this ).find( 'a' ).eq( 0 ).attr( 'href' );
	return false;
};/* end function it.misura.searchPanelRedirectToPage() */

/**
 * Performs a new search from the search panel
 *
 * @param limit, string, required, Limit the research to ...
**/
it.misura.searchPanelNewSearch = function( limit )
{
	var
		  panel = jQuery( '#search-panel' )
		, field = jQuery( '#search-field' )
		, value = jQuery.trim( field.val().replace( /'/g, '' ).replace( /"/g, '' ) )
	;
	if( value === '' )
	{
		field.val( '' ).focus();
	} else {
		field.val( value );
		it.misura.performSearch( value, limit );
	}
	return false;
};/*end function it.misura.searchPanelNewSearch() */

/**
 * Manages the search panel prev page pager click event
**/
it.misura.searchPanelPagePrev = function()
{
	var
		  panel = jQuery( '#search-panel' )
		, pager = jQuery( '.pager', panel )
		, pages = parseInt( jQuery( 'span', pager ).html(), 10 )
		, page = parseInt( jQuery( 'strong', pager ).html(), 10 )
		, prev = Math.max( 1, page - 1 )
		, list = jQuery( '.results-list', panel )
	;
	if( page !== prev )
	{
		jQuery( 'strong', pager ).html( prev );
		jQuery( 'ul', list ).animate( { top: '-' + ( ( prev - 1 ) * list.height() ) + 'px' }, 500 );
	}
	return false;
};/* end function it.misura.searchPanelPagePrev() */

/**
 * Manages the search panel next page pager click event
**/
it.misura.searchPanelPageNext = function()
{
	var
		  panel = jQuery( '#search-panel' )
		, pager = jQuery( '.pager', panel )
		, pages = parseInt( jQuery( 'span', pager ).html(), 10 )
		, page = parseInt( jQuery( 'strong', pager ).html(), 10 )
		, next = Math.min( pages, page + 1 )
		, list = jQuery( '.results-list', panel )
	;
	if( page !== next )
	{
		jQuery( 'strong', pager ).html( next );
		jQuery( 'ul', list ).animate( { top: '-' + ( ( next - 1 ) * list.height() ) + 'px' }, 500 );
	}
	return false;
};/* end function it.misura.searchPanelPageNext() */

/**
 * Closes the modal window
 *
 * @param panel, string, required, The panel name
**/
it.misura.closeModal = function( panel )
{
	jQuery.modal.close();
	jQuery( panel ).remove();
};/* end function it.misura.closeModal() */

/**
 * Starts the header and footer slide in animation
**/
it.misura.slideHeaderAndFooter = function()
{
	var head = jQuery( '#header-content' );
	var foot = jQuery( '#footer-content' );

	head.css( 'top', '-' + head.height() )
		.animate( { 'top': 0 }, 2000 )
	;
	foot.css( 'bottom', '-' + foot.height() )
		.animate( { 'bottom': 0 }, 2000 )
	;
};/* end function it.misura.slideHeaderAndFooter() */

/**
 * Manages full window resize event
**/
it.misura.windowResize = function()
{
	var
		  header_image
		, hbg_folder = it.misura.vars.imagesUrl + it.misura.vars.headerBackgroundsFolder + '/'
		, fbg_folder = it.misura.vars.imagesUrl + it.misura.vars.footerBackgroundsFolder + '/'
		, w = jQuery( window ).width()
		, h = jQuery( window ).height()
		, l = jQuery( '#main-menu li' )
	;

	jQuery( 'img', it.misura.vars.backgroundContainer ).each
	(
		function()
		{
			it.misura.resizeBackgroundImage( jQuery( this ), w, h );
		}
	);
	if( w <= it.misura.vars.minWindowWidth )
	{
		header_image = 'header-background-990.png';
		l.css( 'padding', '0 5px' );
	} else if( w > 1280 )
	{
		header_image = 'header-background-3000.png';
		l.css( 'padding', '0 20px' );
	} else {
		header_image = 'header-background-1280.png';
		l.css( 'padding', '0 5px' );
	}
	jQuery( '#header-content' ).css( 'background-image', "url('" + hbg_folder + header_image + "')" );
	jQuery( '#footer-content' ).css( 'background-image', "url('" + fbg_folder + "footer-background.png')" );
};/* end function it.misura.windowResize() */


/**
 * Resizes a background image
 *
 * @param image, jQuery, required, The image to resize
 * @param w, int, required, The window width
 * @param h, int, required, The window height
**/
it.misura.resizeBackgroundImage = function( image, w, h )
{
	var ratio;
	if( image.hasClass( 'loaded' ) )
	{
		ratio = image.width() / image.height();
		w = Math.max( w, it.misura.vars.minWindowWidth );
		image.width( w ).height( w / ratio );
		if( image.height() < h )
		{
			image.height( h ).width( h * ratio );
		}
	}
};/* end function it.misura.resizeBackgroundImage() */

/**
 * Creates the background container
**/
it.misura.createBackgroundContainer = function()
{
	it.misura.vars.backgroundContainer = jQuery.forge( 'div' )
		.attr( 'id', 'background-container' )
		.appendTo( jQuery( '#content' ) )
	;
};/* end function it.misura.createBackgroundContainer() */

/**
 * Loads a background image
**/
it.misura.loadBackgroundImage = function( name, image )
{
	return jQuery.forge( 'img' )
		.css
		(
			{ opacity: 0, zIndex: it.misura.vars.backgroundContainer.children().length + 1 }
		)
		.load( it.misura.backgroundImageLoaded )
		.attr( { alt: '', src: image } )
		.addClass( name )
		.appendTo( it.misura.vars.backgroundContainer )
	;
};/* end function it.misura.loadBackgroundImage() */

/**
 * Manages a background image load complete event
**/
it.misura.backgroundImageLoaded = function()
{
	jQuery( this ).addClass( 'loaded' );
	it.misura.windowResize();
};/* end function it.misura.backgroundImageLoaded() */

/**
 * Shows the page loading screen
**/
it.misura.showPageLoading = function()
{
	jQuery( window ).load( it.misura.hidePageLoading );
	jQuery( '#container' ).css('left', '-20000px' );
	jQuery.forge( 'div' )
		.attr( 'id', 'page-loading' )
		.append
		(
			jQuery.forge( 'img' ).attr( { alt: '', src: it.misura.vars.imagesUrl + 'common/loader.gif'  } )
		)
		.append
		(
			jQuery.forge( 'p' ).html( it.misura.lang.waitWhilePageLoads )
		)
		.prependTo( jQuery( 'body' ) )
	;
};/* end function it.misura.showPageLoading() */

/**
 * Hides the page loading screen
**/
it.misura.hidePageLoading = function()
{
	jQuery( '#page-loading' ).remove();
	jQuery( '#container' ).css('left', '0' );
};/* end function it.misura.hidePageLoading() */

/**
 * Print the area video player
**/
it.misura.printAreaVideoPlayer = function( name, link, color )
{
	it.misura.closePopupLastNews();
	var
		  path = it.misura.vars.videosUrl + name + '/' + it.misura.vars.applicationLanguage
		, id = it.misura.vars.areaPlayer.attr( 'id' )
		, flashvars = {
			  percorso: path + '/main.mp4'
			, percorso_sottotitoli: path + '/main.srt'
			, skip_target: link
			, skip_color: color
			, isLive: 'false'
			, autoLoop: 'false'
			, autoPlay: 'true'
			, resizeToFit: 'true'
			, show_controls: 'true'
			, volume: '50'
		}
		, params = {
			  allowscriptaccess: 'sameDomain'
			, allowfullscreen: 'true'
			, menu: 'false'
			, wmode: 'transparent'
		}
		, attributes = {
			  xiRedirectUrl: self.location.href
			, id: id
		}
	;
	swfobject.embedSWF
	(
		  it.misura.vars.siteUrl + 'players/player.swf'
		, id
		, '100%'
		, '100%'
		, it.misura.vars.requiredFlashVersion
		, it.misura.vars.siteUrl + 'players/express-install.swf'
		, flashvars
		, params
		, attributes
	);
};/* end function it.misura.printAreaVideoPlayer() */

/**
 * Manage the close button click on the last news popup
**/
it.misura.closePopupLastNews = function()
{
	var popup = jQuery( '.popup-last-news' );
	if( popup.length > 0 )
	{
		jQuery( '.popup-last-news' ).remove();
	}
	return false;
};/* end function it.misura.closePopupLastNews() */

/**
 * Preload images
**/
it.misura.preloadImages = function( images )
{
	var i;
	for( i = 0; i < images.length; i++ )
	{
		jQuery.forge( 'img' ).attr( 'src', images[i] );
	}
};/* end function it.misura.preloadImages() */

/**
 * Manages ajax error
 *
 * @param result, string, required, Text to verify
 * @return boolean
**/
it.misura.ajaxError = function( result )
{
	var isError = false, errorString = '', match;
	if( result.match(/^Error:/i) )
	{
		isError = true;
		errorString = result.replace('Error:','');
	}
	else if( result.match(/<b>(Notice|Warning)<\/b>/i ) )
	{
		isError = true;
		errorString = '';
	}
	if( isError && errorString !== '' )
	{
		it.misura.log( errorString );
	}
	return isError;
};/* end function it.misura.ajaxError() */

/**
 * Common dialogs wrapper
 *
 * @param type, string, required, The dialog type
**/
it.misura.dialog = function( type )
{
	switch( type )
	{
		case 'alert':
			window.alert( arguments[1] );
			break;

		case 'prompt':
			if( arguments.length > 2 )
			{
				return window.prompt( arguments[ 1 ], arguments[ 2 ] );
			} else {
				return window.prompt( arguments[ 1 ] );
			}
			break;

		case 'confirm':
			return window.confirm( arguments[1] );

		default:
			window.alert( arguments[1] );
	}
};/* end function it.misura.dialog() */

/**
 * Returns the name of the file from its full path
 *
 * @param file, string, required, The file path
**/
it.misura.getFileName = function( path )
{
	var value = path.split('\\').join('/');
	value = value.split('/');
	return value[ value.length - 1];
};/* end function it.misura.getFileName() */

/**
 * Logs a message to the firefox console log
 *
 * @param message, string, required, The message to log
**/
it.misura.log = function( message )
{
	if ( window.console )
	{
		window.console.log( message );
	}
};/* end function it.misura.log() */

/**
 * Opens the link in an external page
**/
it.misura.openExternalLink = function()
{
	window.open( jQuery( this ).attr( 'href' ) );
	return false;
};/* end function it.misura.openExternalLink() */

/**
 * Set the items list scroller
**/
it.misura.setItemsListScroller = function()
{
	var
		  div = jQuery( '#items-list' )
		, pag = jQuery( '.items-list-page', div )
		, li = jQuery( 'li', pag ).eq( 0 )
		, cols = Number( div.attr( 'class' ).match( /matrix-[0-9]{1}x([0-9]){1}/ )[1] )
	;
	it.misura.vars.itemsListPagesContainer = jQuery( '.items-list-pages-container' );
	it.misura.vars.itemsListPageWidth = li.outerWidth( true ) * cols;
	it.misura.vars.itemsListFullWidth = it.misura.vars.itemsListPageWidth * pag.length;
	it.misura.vars.itemsListPagesContainer.width( it.misura.vars.itemsListFullWidth );
	it.misura.vars.itemsListScrollEnabled = true;
	pag.width( it.misura.vars.itemsListPageWidth );
	div.width( it.misura.vars.itemsListPageWidth );

	if( pag.length > 1 )
	{
		div.css( 'overflow', 'hidden' );

		jQuery.forge( 'a' )
			.addClass( 'scroll-prev' )
			.attr
			(
				{ href: '#', title: it.misura.lang.scrollPrev }
			)
			.append
			(
				jQuery.forge( 'span' ).addClass( 'hidden' ).html( it.misura.lang.scrollPrev )
			)
			.css( 'opacity', 0 )
			.click( it.misura.scrollItemsListPrev )
			.appendTo( div )
		;

		jQuery.forge( 'a' )
			.addClass( 'scroll-next' )
			.attr
			(
				{ href: '#', title: it.misura.lang.scrollNext }
			)
			.append
			(
				jQuery.forge( 'span' ).addClass( 'hidden' ).html( it.misura.lang.scrollNext )
			)
			.click( it.misura.scrollItemsListNext )
			.appendTo( div )
		;

	}
};/* end function it.misura.setItemsListScroller() */

/**
 * Scrolls the items list backward
**/
it.misura.scrollItemsListPrev = function()
{
	var
		  link = jQuery( this )
		, left = parseInt( it.misura.vars.itemsListPagesContainer.css( 'left' ), 10 )
		, goto = left + it.misura.vars.itemsListPageWidth
		, next = jQuery( '#items-list .scroll-next' )
	;
	if( left < 0 && it.misura.vars.itemsListScrollEnabled )
	{
		it.misura.vars.itemsListScrollEnabled = false;
		link.attr( 'disabled', 'disabled' );
		next.attr( 'disabled', 'disabled' );
		it.misura.vars.itemsListPagesContainer.animate
		(
			{ 'left': goto + 'px' }, 1000,
			function()
			{
				it.misura.vars.itemsListScrollEnabled = true;
				next.removeAttr( 'disabled' ).show().animate( { 'opacity': 1 }, 600 );
				if( goto < 0 )
				{
					link.removeAttr( 'disabled' );
				} else {
					link.animate( { 'opacity': 0 }, 600, function(){ link.hide(); } );
				}
			}
		);
	}
	return false;
};/* end function it.misura.scrollItemsListPrev() */

/**
 * Scrolls the items list forward
**/
it.misura.scrollItemsListNext = function()
{
	var
		  link = jQuery( this )
		, left = parseInt( it.misura.vars.itemsListPagesContainer.css( 'left' ), 10 )
		, goto = left - it.misura.vars.itemsListPageWidth
		, prev = jQuery( '#items-list .scroll-prev' )
	;
	if( left > it.misura.vars.itemsListPageWidth - it.misura.vars.itemsListFullWidth && it.misura.vars.itemsListScrollEnabled )
	{
		it.misura.vars.itemsListScrollEnabled = false;
		link.attr( 'disabled', 'disabled' );
		prev.attr( 'disabled', 'disabled' );
		it.misura.vars.itemsListPagesContainer.animate
		(
			{ 'left': goto + 'px' }, 1000,
			function()
			{
				it.misura.vars.itemsListScrollEnabled = true;
				prev.removeAttr( 'disabled' ).show().animate( { 'opacity': 1 }, 600 );
				if( goto > it.misura.vars.itemsListPageWidth - it.misura.vars.itemsListFullWidth )
				{
					link.removeAttr( 'disabled' );
				} else {
					link.animate( { 'opacity': 0 }, 600, function(){ link.hide(); } );
				}
			}
		);
	}
	return false;
};/* end function it.misura.scrollItemsListNext() */

/**
 * Sets the products list items effects (hover and filter)
 *
 * @param type, string, required, The list type
**/
it.misura.setProductsListEffects = function( type )
{
	var t = jQuery( this );
	if( t.parent().hasClass( 'item-is-fresh' ) )
	{
		t.append
		(
			jQuery.forge( 'img' )
				.addClass( 'fresh' )
				.attr( { alt: '', src: it.misura.vars.imagesUrl + 'lang/' + it.misura.vars.applicationLanguage + '/misc/fresh-product-' + type + '.png' } )
		);
	}
	t
		.append
		(
			jQuery.forge( 'img' )
				.addClass( 'filter' )
				.attr( { alt: '', src: it.misura.vars.imagesUrl + 'common/products-' + type + '-filter.png' } )
		)
		.append
		(
			jQuery.forge( 'img' )
				.addClass( 'hover' )
				.attr( { alt: '', src: it.misura.vars.imagesUrl + t.attr( 'class' ) + '/products-' + type + '-hover.png' } )
		)
	;
};/* end function it.misura.setProductsListEffects() */

/**
 * Shows the product video
**/
it.misura.showProductVideo = function( product, product_class )
{
	var pc = jQuery.forge( 'div' ).attr( 'id', 'product-player' ).appendTo( jQuery( '#product-file' ) );
	if( pc.length )
	{
		var flashvars = {
			  percorso: it.misura.vars.videosUrl + product_class + '/prodotti/' + product + '.mp4'
			, isLive: 'false'
			, autoLoop: 'false'
			, autoPlay: 'true'
			, resizeToFit: 'true'
			, volume: '50'
		};
		var params = {
			  allowscriptaccess: 'sameDomain'
			, allowfullscreen: 'true'
			, menu: 'false'
			, wmode: 'transparent'
		};
		var attributes = {
			  xiRedirectUrl: self.location.href
			, id: 'product-player'
		};

		swfobject.embedSWF
		(
			  it.misura.vars.siteUrl + 'players/product-player.swf'
			, 'product-player'
			, '480'
			, '165'
			, it.misura.vars.requiredFlashVersion
			, it.misura.vars.siteUrl + 'players/express-install.swf'
			, flashvars
			, params
			, attributes
		);
	}
};/* end function it.misura.showProductVideo() */

/**
 * Closes the product file window
**/
it.misura.closeProductFile = function()
{
	jQuery( this ).unbind( 'click' );
	if( jQuery.browser.msie )
	{
		it.misura.productFile.hide();
		it.misura.productFileHidden();
	} else {
		it.misura.productFile.animate( { 'opacity': 0 }, 600, it.misura.productFileHidden );
	}
	return false;
};/* end function it.misura.closeProductFile() */

/**
 * Invoked once the product file is hidden
**/
it.misura.productFileHidden = function()
{
	it.misura.productFile.empty().remove();
	if( jQuery.browser.msie )
	{
		jQuery( '.products-list ul li a .filter' ).hide();
	} else {
		jQuery( '.products-list ul li a	.filter' )
			.animate
			(
				{ opacity: 0 }, 1000,
				function()
				{
					jQuery( this ).hide().css( 'opacity', '1' );
				}
			)
		;
	}
};/* end function it.misura.productFileHidden() */

/**
 * Shows the hover effect over the products list links
**/
it.misura.showProductsListLinkHover = function()
{
	jQuery( this ).children( '.hover' ).show();
};/* end function it.misura.showProductsListLinkHover() */


/**
 * Hides the hover effect over the products list links
**/
it.misura.hideProductsListLinkHover = function()
{
	jQuery( this ).children( '.hover' ).hide();
};/* end function it.misura.hideProductsListLinkHover() */

/**
 * Shows a product info file
**/
it.misura.showProductFile = function( product_class )
{
	var
		  item = jQuery( this )
		, href = item.attr( 'href' )
		, name = href.replace(/\/+$/, '').split( '/' ).pop()
		, link = jQuery( '.products-list ul li a' )
	;
	link.removeClass( 'selected' );
	item.addClass( 'selected' );
	jQuery( '.products-list ul li a' ).not( '.selected' ).children( '.filter' ).show();
	jQuery( '.filter', item ).hide();
	if( typeof it.misura.productFile !== 'undefined' )
	{
		it.misura.productFile.remove();
	}
	it.misura.productFile = jQuery.forge( 'div' )
		.attr( 'id', 'product-file' )
		.addClass( product_class )
	;
	if( jQuery.browser.msie )
	{
		it.misura.productFile.hide();
	} else {
		it.misura.productFile.css( 'opacity', 0 );
	}
	it.misura.productFile.appendTo( jQuery( '#content' ) );
	var close = jQuery.forge( 'a' )
		.addClass( 'close-button' )
		.attr
		(
			{ href:'#', title: it.misura.lang.closeButton }
		)
		.append
		(
			jQuery.forge( 'span' ).addClass( 'hidden' ).html( it.misura.lang.closeButton )
		)
		.click( it.misura.closeProductFile )
		.appendTo( it.misura.productFile )
	;
	var container = jQuery.forge( 'div' )
		.addClass( 'product-container' )
		.attr( 'id', name )
		.appendTo( it.misura.productFile )
	;
	jQuery.ajax
	(
		{
			  type: 'POST'
			, url: it.misura.vars.siteUrl + 'includes/ajax.php'
			, data: 'm=getProductFile&product=' + name + '&language=' + it.misura.vars.applicationLanguageId
			, success: function( result )
			{
				if( result === '' || it.misura.ajaxError( result ) )
				{
					container.append
					(
						jQuery.forge( 'p' ).addClass( 'error' ).html( 'errore' )
					);
				} else {
					if( jQuery.browser.msie )
					{
						container.html( result );
						it.misura.productFile.show();
						it.misura.productFileShown();
					} else {
						it.misura.productFile.animate
						(
							{ 'opacity': 1 }, 1000,
							function()
							{
								container.html( result );
								it.misura.productFileShown();
							}
						);
					}
				}
			}
		}
	);
	return false;
};/* end function it.misura.showProductFile() */

/**
 * Invoked once product file has been shown
**/
it.misura.productFileShown = function()
{
	var
		  product = jQuery( '#product-file .product-container' ).attr( 'id' )
		, area = jQuery.trim( jQuery( '#product-file').attr( 'class' ).replace( 'single-product', '' ) )
	;
	it.misura.setScrollPane( jQuery( '.product-content' ) );
	jQuery( '.product-menu a' ).click( it.misura.productMenuClicked );
	if( it.misura.vars.hasFlashPlayerVersion )
	{
		it.misura.showProductVideo( product, area );
	} else {

		jQuery.forge( 'img' )
			.attr
			(
				{
					  id: 'product-player'
					, alt: ''
					, src: it.misura.vars.imagesUrl + area + '/prodotti/video/' + product + '.png'
				}
			).load
			(
				function()
				{
					jQuery( this ).appendTo( jQuery( '#product-file' ) );
				}
			)
		;
	}
};/* end function it.misura.productFileShown() */

/**
 * Manages the click event on the product menu link (inside product file panel)
**/
it.misura.productMenuClicked = function()
{
	var
		  t = jQuery( this )
		, c = t.attr( 'class' )
		, img = jQuery( '#product-image' ).hide()
	;
	jQuery( '.product-menu li' ).removeClass( 'selected' );
	t.parent().addClass( 'selected' );

	switch( c )
	{
		case 'nutritionals':

			jQuery( '.product-menu' ).css( 'top', '330px' );
			jQuery( '.product-content' ).height( 290 );
			jQuery( '#product-player' ).hide();
			break;

		case 'description':

			img.show();
			jQuery( '.product-menu' ).css( 'top', '165px' );
			jQuery( '.product-content' ).height( 130 );
			jQuery( '#product-player' ).show();
			break;

		default:

			jQuery( '.product-menu' ).css( 'top', '165px' );
			jQuery( '.product-content' ).height( 130 );
			jQuery( '#product-player' ).show();
	}

	jQuery( '.product-content li' ).hide();
	jQuery( '.product-content li.' + c ).show();
	it.misura.setScrollPane( jQuery( '.product-content' ) );
	return false;
};/* end function it.misura.productMenuClicked() */

/**
 * Add the current object initialization to the onload event
**/
jQuery( document ).ready( it.misura.initAll );
jQuery.noConflict();
