/**
 * Endesia
 *
 * JS enhancements for piemme-it.com
 * Dev by domenico maresca - domenicomaresca@endesia.it
 */


$(function(){
	$('a.new').click(function(){
		window.open($(this).attr('href'));
		return false;
	});

	$('a.fancybox').fancybox({
		zoomSpeedIn: 500,
		zoomSpeedOut: 500,
		frameWidth: 640,
		frameHeight: 480,
		transitionIn: 'elastic',
		transitionOut: 'elastic'
	});
	
	if (swfobject.hasFlashPlayerVersion("9.0.18")) {
		swfobject.embedSWF("/images/layout/top_piemme.swf", "welcome", "667", "218", "9.0.18", "/images/expressInstall.swf", null, null, null);
	}
	
	$("#gallery #thumbs ul").jcarousel({
        vertical: true,
        scroll: 2,
        initCallback: mycarousel_initCallback,
        buttonNextHTML: null,
        buttonPrevHTML: null
    });
	
	function mycarousel_initCallback(carousel){
		showImage($('#gallery #thumbs li:first a'));
		$('#gallery .next').click(function(){
		    carousel.next();
		    return false;
		});							
		$('#gallery .prev').click(function(){
		    carousel.prev();
		    return false;
		});
		$('#gallery #thumbs a').click(function(){
			showImage(this);
			return false;
		});
	};
	
	function showImage(el){
		$('#gallery #thumbs img').removeClass('active');
		$(el).find('img').addClass('active');
		var imageUrl = $(el).attr('href');
		$('#gallery #big').addClass('loading');
		$('<img />').attr('src', imageUrl)
					.load(function(){
						$(this).hide();
						$('#gallery #big').find('img').fadeOut('slow',function(){$(this).remove();});
						$('#gallery #big').append($(this))
										  .removeClass('loading');
						$(this).fadeIn('slow');
					});
	}
	
});
