$.fn.cycle.defaults.timeout = 5000;
$(function() {
   
    $('#slider').before('<div id="nav" class="nav">').cycle({
        fx:     'fade',
        speed:  'slow',
        timeout: 5000,
        pager:  '#nav'
	});
	
});
 

$(document).ready(function(){ 
	my_lightbox("a[rel^='prettyPhoto'], a[rel^='lightbox']");


jQuery('.fotovetrina').kriesi_image_preloader({delay:400});	// activates preloader for non-slideshow images
jQuery('.fotovetrinatesto').kriesi_image_preloader({delay:400});	// activates preloader for non-slideshow images
jQuery('.product_image').kriesi_image_preloader({delay:400});	// activates preloader for non-slideshow images



$("a.switch_thumb").toggle(function(){
	$(this).removeClass("swap");
	$("ul.display").fadeOut("fast", function() {
	$(this).fadeIn("fast").addClass("thumb_view");
	});
	}, function () {
	$(this).addClass("swap");
	$("ul.display").fadeOut("fast", function() {
	$(this).fadeIn("fast").removeClass("thumb_view");
});
}); 
	
/*$('.product_image img').hover(function() {
  $(this).addClass('product_imagehover');
}, function() {
  $(this).removeClass('product_imagehover');
});*/



$(".product_image a img").hoverIntent({
				over: makeTall, 
				timeout: 100, 
				out: makeShort
			});

});
function makeTall(){  $(this).animate( {opacity: "0.5" }, 200 );}
function makeShort(){ $(this).animate( {opacity: "1" }, 200 );}

// -------------------------------------------------------------------------------------------
// The Image preloader
// -------------------------------------------------------------------------------------------


(function($)
{
	$.fn.kriesi_image_preloader = function(options) 
	{
		var defaults = 
		{
			repeatedCheck: 500,
			fadeInSpeed: 1000,
			delay:600,
			callback: ''
		};
		
		var options = $.extend(defaults, options);
		
		return this.each(function()
		{
			var imageContainer = jQuery(this),
				images = imageContainer.find('img').css({opacity:0, visibility:'hidden'}),
				imagesToLoad = images.length;				
				
				imageContainer.operations =
				{	
					preload: function()
					{	
						var stopPreloading = true;
						
						images.each(function(i, event)
						{	
							var image = $(this);
							
							
							if(event.complete == true)
							{	
								imageContainer.operations.showImage(image);
							}
							else
							{
								image.bind('error load',{currentImage: image}, imageContainer.operations.showImage);
							}
							
						});
						
						return this;
					},
					
					showImage: function(image)
					{	
						imagesToLoad --;
						if(image.data.currentImage != undefined) { image = image.data.currentImage;}
												
						if (options.delay <= 0) image.css('visibility','visible').animate({opacity:1}, options.fadeInSpeed);
												 
						if(imagesToLoad == 0)
						{
							if(options.delay > 0)
							{
								images.each(function(i, event)
								{	
									var image = $(this);
									setTimeout(function()
									{	
										image.css('visibility','visible').animate({opacity:1}, options.fadeInSpeed);
									},
									options.delay*(i+1));
								});
								
								if(options.callback != '')
								{
									setTimeout(options.callback, options.delay*images.length);
								}
							}
							else if(options.callback != '')
							{
								(options.callback)();
							}
							
						}
						
					}

				};
				
				imageContainer.operations.preload();
		});
		
	}
})(jQuery);


function my_lightbox($elements)
{	
	var usedCSS = 1;
	jQuery('link').each(function()
	{	
		styleURL = jQuery(this).attr('href'); 
		CSSnumber = styleURL.match(/style(\d).css/);
		if(CSSnumber && CSSnumber.length > 0)
		{
			usedCSS = CSSnumber[1];
		}
	});
		
	
	var theme_selected = 'light_rounded';
	if (usedCSS == 2 || usedCSS == 4)
	{
		theme_selected = 'dark_rounded';
	}
	
	jQuery($elements).prettyPhoto({
			"theme": theme_selected /* light_rounded / dark_rounded / light_square / dark_square */																	});
	
	jQuery($elements).each(function()
	{	
		var $image = jQuery(this).contents("img");

		$newclass = 'lightbox_video';
		
		if(jQuery(this).attr('href').match(/(jpg|gif|jpeg|png|tif)/)) $newclass = 'lightbox_image';
			
		if ($image.length > 0)
		{	
			if(jQuery.browser.msie &&  jQuery.browser.version < 7) jQuery(this).addClass('ie6_lightbox');
			
			var $bg = jQuery("<span class='"+$newclass+" ie6fix'></span>").appendTo(jQuery(this));
			
			jQuery(this).bind('mouseenter', function()
			{
				$height = $image.height();
				$width = $image.width();
				$pos =  $image.position();		
				$bg.css({height:$height, width:$width, top:$pos.top, left:$pos.left});
			});
		}
	});	
	
	jQuery($elements).contents("img").hover(function()
	{
		jQuery(this).stop().animate({opacity:0.5},400);
	},
	function()
	{
		jQuery(this).stop().animate({opacity:1},400);
	});
}
function showText(text) {
	return unescape(text);
}
