$(window).load(function()
{
	$('#searchlanguage').click(function() {
		$('#langMenu .menuLang').slideToggle();					 
	});
	
    //$(".scrollable").scrollable({ size: 4, clickable: false }).circular();
    $(".scrollable").each(function()
    {
        var $this = $(this);
        if ($this.find("li").length > 4)
        {
            $this.scrollable({ size: 4, clickable: false, keyboard: false }).circular();
            return;
        }
        $this.parent().find(".prev").hide();
        $this.parent().find(".next").hide();
    })

    //$(".scrollablebig").scrollable({ size: 5, clickable: false }).circular();
    $(".scrollablebig").each(function()
    {
        var $this = $(this);
        if ($this.find("li").length > 5)
        {
            $this.scrollable({ size: 5, clickable: false, keyboard: false }).circular();
            return;
        }
        $this.parent().find(".prev").hide();
        $this.parent().find(".next").hide();
    })

    $('#menu a').mouseover(function()
    {
        var tipo = $(this).attr('id');
        $('#' + tipo + 'submenu').show();
        $(this).addClass('over');
    });
    $('#menu a').mouseout(function()
    {
        var tipo = $(this).attr('id');
        $('#' + tipo + 'submenu').hide();
        $(this).removeClass('over');
    });

    $('.submenu').mouseover(function()
    {
        $(this).show();
        var menuname = $(this).attr('id').replace('submenu', '');
        $('#' + menuname).addClass('over');
    });

    $('.submenu').mouseout(function()
    {
        $(this).hide();
        var menuname = $(this).attr('id').replace('submenu', '');
        $('#' + menuname).removeClass('over');
    });
/*     DA RIATTIVARE PER LA HOME PAGE
    $('.claim').click(function()
    {
        $('.claim').children('p').fadeOut('fast');

        if ($(this).css('height') == '20px')
        {
            if ($(this).attr('id') == 'claim01')
            {
                $('#claim01').animate({ height: '130', top: '148' });
                $('#claim02').animate({ height: '20', top: '288' });
                $('#claim03').animate({ height: '20', top: '318' });
                $(this).children('p').fadeIn();

            }
            if ($(this).attr('id') == 'claim02')
            {
                $('#claim01').animate({ height: '20', top: '148' });
                $('#claim02').animate({ height: '130', top: '178' });
                $('#claim03').animate({ height: '20', top: '318' });
                $(this).children('p').fadeIn();
            }

            if ($(this).attr('id') == 'claim03')
            {
                $('#claim01').animate({ height: '20', top: '148' });
                $('#claim02').animate({ height: '20', top: '178' });
                $('#claim03').animate({ height: '130', top: '208' });
                $(this).children('p').fadeIn();
            }

            if ($(this).attr('id') == 'claim04')
            {
                $('#claim04').animate({ height: '130', top: '78' });
                $('#claim05').animate({ height: '20', top: '218' });
                $('#claim06').animate({ height: '20', top: '248' });
                $(this).children('p').fadeIn();
            }

            if ($(this).attr('id') == 'claim05')
            {
                $('#claim04').animate({ height: '20', top: '78' });
                $('#claim05').animate({ height: '130', top: '108' });
                $('#claim06').animate({ height: '20', top: '248' });
                $(this).children('p').fadeIn();
            }

            if ($(this).attr('id') == 'claim06')
            {
                $('#claim04').animate({ height: '20', top: '78' });
                $('#claim05').animate({ height: '20', top: '108' });
                $('#claim06').animate({ height: '130', top: '138' });
                $(this).children('p').fadeIn();
            }
        }

        else
        {
            $('#claim01').animate({ height: '20', top: '258' });
            $('#claim02').animate({ height: '20', top: '288' });
            $('#claim03').animate({ height: '20', top: '318' });
            $('#claim04').animate({ height: '20', top: '188' });
            $('#claim05').animate({ height: '20', top: '218' });
            $('#claim06').animate({ height: '20', top: '248' });
        }
    });
*/
    $('#hplistaimg img').click(function()
    {
				var imgsrc = $(this).attr('alt');
        $('#hpheaderimg').attr('src', imgsrc);
        var metadati = $(this).attr('rel');
        var index = metadati.indexOf('|');
        var first = metadati.slice(0, index);
        metadati = metadati.replace(first + '|', '');
        index = metadati.indexOf('|');
        var second = metadati.slice(0, index);
        metadati = metadati.replace(second + '|', '');
        index = metadati.indexOf('|');
        var third = metadati.slice(0, index);
        metadati = metadati.replace(third + '|', '');
        index = metadati.indexOf('|');
        var fourth = metadati.slice(0, index);
        metadati = metadati.replace(fourth + '|', '');        

        index = first.indexOf('#');
        var first_titolo = first.slice(0, index);
        first = first.replace(first_titolo + '#', '');
        index = first.indexOf('#');
        var first_desc = first.slice(0, index);

        index = second.indexOf('#');
        var second_titolo = second.slice(0, index);
        second = second.replace(second_titolo + '#', '');
        index = second.indexOf('#');
        var second_desc = second.slice(0, index);

        index = third.indexOf('#');
        var third_titolo = third.slice(0, index);
        third = third.replace(third_titolo + '#', '');
        index = third.indexOf('#');
        var third_desc = third.slice(0, index);

        index = fourth.indexOf('#');
        var fourth_url = fourth.slice(0, index);

        $('#claim01 span').html('<a href="'+fourth_url+'">'+first_titolo+'</a>');
        $('#claim01 p').html(first_desc);
        $('#claim02 span').html(second_titolo);
        $('#claim02 p').html(second_desc);
        $('#claim03 span').html(third_titolo);
        $('#claim03 p').html(third_desc);

        $('#claim04 span').html(first_titolo);
        $('#claim04 p').html(first_desc);
        $('#claim05 span').html(second_titolo);
        $('#claim05 p').html(second_desc);
        $('#claim06 span').html(third_titolo);
        $('#claim06 p').html(third_desc);

    });

    $('.submenu ul.firstlevel li').mouseover(function()
    {
        $('.submenu ul.firstlevel li a').removeClass('active');
        $('.submenu ul.secondlevel li a').removeClass('active');
        $(this).children('a').addClass('active');
        $('.submenu ul.thirdlevel, .submenu ul.fourlevel').css('display', 'none');
        var idanchor = 'anchor' + $(this).attr('id');
        $('.submenu ul.secondlevel').each(function()
        {
            if ($(this).attr('rel') == idanchor)
            {
                $(this).css('display', 'block');
            }
            else $(this).css('display', 'none');
        });
    });

    $('.submenu ul.secondlevel li').mouseover(function()
    {
        $('.submenu ul.secondlevel li a').removeClass('active');
        $(this).children('a').addClass('active');
        var idanchor = 'anchor' + $(this).attr('id');
        $('.submenu ul.thirdlevel, .submenu ul.fourlevel').each(function()
        {
            if ($(this).attr('rel') == idanchor)
            {
                $(this).css('display', 'block');
            }
            else $(this).css('display', 'none');
        });
    });

    $('#toppanel .collimgbox').mouseenter(function()
    {
        var altz = 190 + $(this).children('div').children('ul').height();
        $(this).children('div').stop().animate({
            height: altz
        }, 300);
    });

    $('#toppanel .collimgbox').mouseleave(function()
    {
        var altz = 30;
        $(this).children('div').stop().animate({
            height: altz
        }, 300);
    });

    $('.etichetta a').mouseenter(function()
    {
        var num = $(this).attr('class') * 150;
        //alert(num);
        $(this).parent().parent().next('div').animate({
            left: '-' + num
        }, 50);
    });

    $('#scrollarrowleft').click(function()
    {
        var lung = $('.rullobig img:first').next().next().next().next().width();
        $('.rullobig').animate({
            left: '-=' + lung
        }, 500);
        $('.rullobig').append($('.rullobig img:first').next().next().next().clone());
        var offset = $('.rullobig img:first').width();
        $('.rullobig').animate({
            left: '+=' + offset
        }, 0, function()
        {
            $('.rullobig img:first').remove();
        });
        var text = $('.rullobig img:first').next().next().next().next().attr("alt");
        $('.infoPanel p').text(text);
    });

    $('#scrollarrowright').click(function()
    {
        var lung = $('.rullobig img:first').next().next().next().width();
        $('.rullobig').prepend($('.rullobig img:last').prev().prev().prev().clone());
        var offset = $('.rullobig img:first').width();
        $('.rullobig').animate({
            left: '-=' + offset
        }, 0, function()
        {
            $('.rullobig img:last').remove();
        });
        $('.rullobig').animate({
            left: '+=' + lung
        }, 500);
        var text = $('.rullobig img:first').next().next().next().attr("alt");
        $('.infoPanel p').text(text);
    });

    $('.arrowdown').click(function()
    {
        $(this).parent().next().slideToggle();
        $(this).toggleClass("arrowup");
    });

    $('.titledown').click(function()
    {
        $(this).parent().parent().next().slideToggle();
        $(this).parent().parent().find("a.arrowdown").toggleClass("arrowup");
    });

    initbigrullo();

    $('#showsearch').toggle(function()
    {
        var $this = $(this);
        $('#toppanel').animate({
            height: '570'
        }, 300, function() { $this.html("X chiudi"); $("#searchTitle").show(); });

        $('.slice.search.boxhide').animate({
            bottom: '70'
        }, 300);

    }, function()
    {
        var $this = $(this);
        $('#toppanel').animate({
            height: '470'
        }, 300, function() { $this.html("Reimposta ricerca"); $("#searchTitle").hide(); });

        $('.slice.search.boxhide').animate({
            bottom: '-30'
        }, 300);
    });

    $("#waterwheelCarousel").waterwheelCarousel("horizontal", {
        startingItem: 7,
        itemDecreaseFactor: .5,
        flankingItems: 2,
        opacityDecreaseFactor: .9,
        startingItemSeparation: 130,
        centerOffset: 20,
        clickedCenter: changevideo,
        movedToCenter: setmetadati
    });

    $('#waterwheelCarousel img').click(reservideo);

	$('.videobox img').click(function(){
		$("#waterwheelCarousel").rotateCarousel(false,1);
	});	
	
    $("#ambientazionisubmenu a").mouseover(function()
    {
        var imgsrc = $(this).attr("rel");
        if (imgsrc != "")
            $("#ambientazionisubmenu img").attr("src", imgsrc);
    });

    var productLineBoxTimer = null;
    var productLineBoxTimerDelay = 5000; //ms
    $("#productLineBox a").click(function()
    {
        if (productLineBoxTimer != null) { window.clearTimeout(productLineBoxTimer); productLineBoxTimer = null };
        var $this = $(this);
        var bigImageUrl = $this.attr("rel");
        $("#imageViewerBox img")
            .hide()
            .load(function()
            {
                $("#rullobigBox").slideUp(500, function()
                {
                    $("#imageViewerBox img").fadeIn(500);
                });
            })
            .attr("src", bigImageUrl);

        if (productLineBoxTimer != null)
        {
            window.clearTimeout(productLineBoxTimer);
            productLineBoxTimer = null
        };
        if (productLineBoxTimer != null) { window.clearTimeout(productLineBoxTimer); productLineBoxTimer = null };
        productLineBoxTimer = window.setTimeout(function()
        {
            $("#rullobigBox").slideDown(1000);
        }, productLineBoxTimerDelay);
        return false;
    });
    
    
    // inizializzo big rullo nuovo
		initBigRullo2();
		// 
		// configurazione pannello search nuovo
		$('#showsearch2').toggle(function()
			{
				var $this = $(this);
				$('#toppanel').animate({
					height: '660'
				}, 300, function() { $this.html("X chiudi"); $("#searchTitle").show(); });

				$('.slice.search.boxhide').animate({
					bottom: '70'
				}, 300);

			}, function()
			{
				var $this = $(this);
				$('#toppanel').animate({
					height: '560'
				}, 300, function() { $this.html("Reimposta ricerca"); $("#searchTitle").hide(); });

				$('.slice.search.boxhide').animate({
					bottom: '-30'
			}, 300);
		});

		//
		// configurazione bottone a sinistra nuovo
		$('#scrollarrowleft').click(function() {
			
			var lung = $('.rullobig2 img:first').width();
			$('.rullobig2').animate({
				left: '-=' + lung
			}, 500);
			$('.rullobig2').append($('.rullobig2 img:first').clone());
			var offset = $('.rullobig2 img:first').width();
			$('.rullobig2').animate({
				left: '+=' + offset
			}, 0, function()
			{
				$('.rullobig2 img:first').remove();
			});

		});

		//
		// configurazione bottone a destra nuovo
		$('#scrollarrowright').click(function() {
			var lung = $('.rullobig2 img:last').width();
			$('.rullobig2').prepend($('.rullobig2 img:last').clone());
			var offset = $('.rullobig2 img:first').width();
			$('.rullobig2').animate({
				left: '-=' + offset
			}, 0, function()
			{
				$('.rullobig2 img:last').remove();
			});
			$('.rullobig2').animate({
				left: '+=' + lung
			}, 500);
		});

		//
		// configurazione click sulle immagini thumbs
		$('div.rullothumbs img').click(function() {
			var vaia= parseInt($(this).attr("rel"));
			var sonoalla=parseInt($('div.rullobig2 img:first').attr("rel"));
			var q = Math.abs(sonoalla-vaia)
			//alert(sonoalla +" "+ vaia);
			if (sonoalla > vaia) {
				var offset = 0;
				var totGiri = 0;
				for(i=sonoalla-1; i>=vaia; i--) {
					offset += parseInt( $('div.rullobig2 img[rel="'+i+'"]').width() );
					totGiri++;
				}
				
				$('.rullobig2').animate({
					left: '+=' + offset
				}, 500, function()
				{
					for(i=1;i<=totGiri;i++) {
						$('.rullobig2').prepend($('.rullobig2 img:last').clone());
						var lung = $('.rullobig2 img:last').width();
						$('.rullobig2 img:last').remove();
						$('.rullobig2').animate({
							left: '-=' + lung
						}, 0);
					}
				});

			
				//for(i=0;i<q;i++) setTimeout(function() {$('#scrollarrowright').click();},550*i);
				
			} else if (sonoalla < vaia) {
				var offset = 0;
				var totGiri = 0;
				for(i=sonoalla; i<vaia; i++) {
					offset += parseInt( $('div.rullobig2 img[rel="'+i+'"]').width() );
					totGiri++;
				}
				$('.rullobig2').animate({
					left: '-=' + offset
				}, 500, function()
				{
					for(i=1;i<=totGiri;i++) {
						$('.rullobig2').append($('.rullobig2 img:first').clone());
						var lung = $('.rullobig2 img:first').width();
						$('.rullobig2 img:first').remove();
						$('.rullobig2').animate({
							left: '+=' + lung
						}, 0);
					}
				});
				
				//for(i=0;i<q;i++) setTimeout(function() {$('#scrollarrowleft').click();},550*i);
			}
			
			$(this).clearQueue();	
		});
    
    
});

function initbigrullo() {
	var firstlunghezza = $('.rullobig img:first').width();
	var lunghezzaultimetre = $('.rullobig img:last').width() + $('.rullobig img:last').prev().width() + $('.rullobig img:last').prev().prev().width();
	$('.rullobig').prepend( $('.rullobig img:last').clone() ).prepend( $('.rullobig img:last').prev().clone() ).prepend( $('.rullobig img:last').prev().prev().clone() );
	var spostamento = - lunghezzaultimetre + ( 950 - firstlunghezza) + 2;
	$('.rullobig').css('left', spostamento + 'px', function() { });
	$('.rullobig').show();
}

function initBigRullo2() {
		$('.rullobig2').show();
		
		var lunghezza = 4;
		$('.rullobig2 img').each(function() {
			lunghezza += $(this).width();
			$('.rullobig2').append( $(this).clone() );
		});
		
		$('.rullobig2').css('left', '-'+lunghezza + 'px', function() { });
	}

function reservideo() {
	$('#videocanvas').fadeOut('fast');
	$('#videocanvas').html( '' );
}

function changevideo(target) {
	$('#videocanvas').html( /*setVideoplayer($(target).attr('class'))*/ $('#video'+$(target).attr('class')).clone().css('display','block') );
	$('#videocanvas').fadeIn('slow');
}

function setmetadati(target) {
	$('#videotitle').html( $(target).attr('title') );
	$('#videodesc .content p').html( $(target).attr('alt') );
}
			
function setVideoplayer(clip_id) {
	var player = '<embed width="504" height="290" allowscriptaccess="always" allowfullscreen="true" type="application/x-shockwave-flash" src="http://vimeo.com/moogaloop.swf?clip_id='+clip_id+'&amp;server=vimeo.com&amp;show_title=1&amp;show_byline=0&amp;show_portrait=0&amp;color=0F8681&amp;fullscreen=0&amp;autoplay=1">';
				
	return player;
}
