/*
 * call this function on a scrollable node to add some more clones on the right side of the carousel
 * as the jQuery Tools Scrollable plugin is build to just show one item on screen at once (not
 * multiple small elements). This function fills up the empty space right to the carousel with dummy
 * nodes
 * @param int addItems [OPTIONAL] define the number of clone items to aditionally add to the wrapper
 * @return void
 */
$.fn.scrollableAddClones = function(addItems) {
  // grab scrollable plugin
  var scrollable;
  if (!(scrollable = $(this).data('scrollable')) || !scrollable.getConf().circular)
    return;
  // grab scrollable elements and remember it's count
  var nodes = scrollable.getItems();
  var length = nodes.length;
  // grab class for the nodes
  var clonedClass = scrollable.getConf().clonedClass;
  // get wrap object to append the clones to
  var wrap = scrollable.getItemWrap();
  // fill as much nodes as needed for 500 pixels
  if (!addItems) addItems = Math.ceil(500 / nodes.eq(1).width());
  // create fake container to add the clones to (max 15 clones)
  var newNodesAppend = $('<span />');
  for (var i = 1; i <= (addItems < 15 ? addItems : 15); i++)
    nodes.eq(i % length).clone().addClass(clonedClass).appendTo(newNodesAppend);
  // insert HTML
  newNodesAppend.children().appendTo(wrap);
};

(function ($) {
    var b = $.tools.scrollable;
    b.autoscroll = {
        conf: {
            autoplay: !0,
            interval: 3e3,
            autopause: !0,
			direction: 'ltr'
        }
    }, $.fn.autoscroll = function (c) {
        typeof c == "number" && (c = {
            interval: c
        });
        var d = $.extend({}, b.autoscroll.conf, c),
            e;
        this.each(function () {
            var b = $(this).data("scrollable");
            b && (e = b);
            var c, f = !0;
            b.play = function () {
                c || (f = !1, c = setInterval(function () {
                    if (d.direction == 'rtl') { b.prev() } else { b.next() }
                }, d.interval))
            }, b.pause = function () {
                c = clearInterval(c)
            }, b.stop = function () {
                b.pause(), f = !0
            }, d.autopause && b.getRoot().add(b.getNaviButtons()).hover(b.pause, b.play), d.autoplay && b.play()
        });
        return d.api ? e : this
    }
})(jQuery);

$(document).ready(function() {
	$('#navigazioneGALLERYtitolo').click(function() {
		var $this = $(this);
		var $parent = $this.parent();
		if ($parent.height() == 30) {
			$parent.animate({ height: 180 }, 'fast', 'swing', function() {
				$parent.toggleClass('chiuso').toggleClass('aperto');
			});
		} else {
			$parent.animate({ height: 30 }, 'fast', 'swing', function() {
				$parent.toggleClass('aperto').toggleClass('chiuso');
			});
		}
	});
	$('#SCROLL').scrollable({circular: true, easing: 'swing'}).autoscroll({interval: 3000});
	$('#SCROLL').scrollableAddClones();
	if ($('#NEWShomepage .wrapper .scroller div').length > 1) {
		$('#NEWShomepage>.wrapper').scrollable({circular: true, easing: 'swing', vertical: false, steps: -1}).autoscroll({interval: 5000, direction: 'rtl'});
	}
	setTimeout(function() {
		if ($('#OFFERTEhomepage .wrapper .scroller div').length > 1) {
			$('#OFFERTEhomepage>.wrapper').scrollable({circular: true, easing: 'swing', vertical: false}).autoscroll({interval: 5000, direction: 'rtl'});
		}
	}, 2500);
});
