var $j = jQuery.noConflict();
var shift = 220;
jQuery.fn.fadeToggle = function(speed, easing, callback) {return this.animate({opacity: 'toggle'}, speed, easing, callback);};

$j(function () {
    $j.fn.infiniteCarousel = function () {
        function repeat(str, n) {
            return new Array( n + 1 ).join(str);
        }
        
        return this.each(function () {
            // magic!
            var $wrapper = $j('> div', this).css('overflow', 'hidden'),
                $slider = $wrapper.find('> ul').width(9999),
                $items = $slider.find('> li'),
                $single = $items.filter(':first')
                
                singleWidth = $single.outerWidth(),
                visible = Math.ceil($wrapper.innerWidth() / singleWidth),
                currentPage = 1,
                pages = Math.ceil($items.length / visible);
                
            /* TASKS */
            
            // 1. pad the pages with empty element if required
            if ($items.length % visible != 0) {
                // pad
                $slider.append(repeat('<li class="empty" />', visible - ($items.length % visible)));
                $items = $slider.find('> li');
            }
            
            // 2. create the carousel padding on left and right (cloned)
            $items.filter(':first').before($items.slice(-visible).clone().addClass('cloned'));
            $items.filter(':last').after($items.slice(0, visible).clone().addClass('cloned'));
            $items = $slider.find('> li');
            
            // 3. reset scroll
            $wrapper.scrollLeft(singleWidth * visible);
            
            // 4. paging function
            function gotoPage(page) {
                var dir = page < currentPage ? -1 : 1,
                    n = Math.abs(currentPage - page),
                    left = singleWidth * dir * visible * n;
                
                $wrapper.filter(':not(:animated)').animate({
                    scrollLeft : '+=' + left
                }, 500, function () {
                    // if page == last page - then reset position
                    if (page > pages) {
                        $wrapper.scrollLeft(singleWidth * visible);
                        page = 1;
                    } else if (page == 0) {
                        page = pages;
                        $wrapper.scrollLeft(singleWidth * visible * pages);
                    }
                    
                    currentPage = page;
                });
            }
            
            // 5. insert the back and forward link
            $wrapper.after('<a href="#" class="arrow back"></a><a href="#" class="arrow forward"></a>');
            
            // 6. bind the back and forward links
            $j('.back', this).click(function () {
                gotoPage(currentPage - 1);
                return false;
            });
            
            $j('.forward', this).click(function () {
                gotoPage(currentPage + 1);
                return false;
            });
            
            $j(this).bind('goto', function (event, page) {
                gotoPage(page);
            });
            
            // THIS IS NEW CODE FOR THE AUTOMATIC INFINITE CAROUSEL
            $j(this).bind('next', function () {
                gotoPage(currentPage + 1);
            });
        });
    };
});



$j(function () {
  $j('.bubbleInfo').each(function () {
    // options
    var distance = 10;
    var time = 250;
    var hideDelay = 500;

    var hideDelayTimer = null;

    // tracker
    var beingShown = false;
    var shown = false;
    
    var trigger = $j('.trigger', this);
    var popup = $j('.popup', this).css('opacity', 0);

    // set the mouseover and mouseout on both element
    $j([trigger.get(0), popup.get(0)]).mouseover(function () {
      // stops the hide event if we move from the trigger to the popup element
      if (hideDelayTimer) clearTimeout(hideDelayTimer);

      // don't trigger the animation again if we're being shown, or already visible
      if (beingShown || shown) {
        return;
      } else {
        beingShown = true;

        // reset position of popup box
        popup.css({
          bottom: 30,
          right: 25,
          display: 'block' // brings the popup back in to view
        })

        // (we're using chaining on the popup) now animate it's opacity and position
        .animate({
          bottom: '-=' + distance + 'px',
          opacity: 1
        }, time, 'swing', function() {
          // once the animation is complete, set the tracker variables
          beingShown = false;
          shown = true;
        });
      }
    }).mouseout(function () {
      // reset the timer if we get fired again - avoids double animations
      if (hideDelayTimer) clearTimeout(hideDelayTimer);
      
      // store the timer so that it can be cleared in the mouseover if required
      hideDelayTimer = setTimeout(function () {
        hideDelayTimer = null;
        popup.animate({
          bottom: '-=' + distance + 'px',
          opacity: 0
        }, time, 'swing', function () {
          // once the animate is complete, set the tracker variables
          shown = false;
          // hide the popup entirely after the effect (opacity alone doesn't do the job)
          popup.css('display', 'none');
        });
      }, hideDelay);
    });
  });
});

jQuery(document).ready(function(){


	// INFINITE CAROUSEL
    var autoscrolling = true;
    $j('.infiniteCarousel').infiniteCarousel().mouseover(function () {
        autoscrolling = false;
    }).mouseout(function () {
        autoscrolling = true;
    });
    setInterval(function () {
        if (autoscrolling) {
            $j('.infiniteCarousel').trigger('next');
        }
    }, 6000);


	jQuery("a#ad-toggle").click(function() {jQuery('#formbox').fadeToggle("slow"); return false;});	
	jQuery("a#email-toggle").click(function() {jQuery('#email_form_data').fadeToggle("slow"); return false;});
	jQuery("img.size-thumbnail").parent().fancybox({"hideOnContentClick":true,"overlayShow":true,"overlayOpacity":.5,"zoomSpeedIn":300,"zoomSpeedOut":300});
	
	//SHOW/HIDE SUB
	var toggleMinus = 'http://www.chicercatrova.it/wp-content/themes/cct/images/minus.png';
	var togglePlus = 'http://www.chicercatrova.it/wp-content/themes/cct/images/plus.png';

	var $subHead = $j('.children').parent();

	//Add the Plus image to every child by default
	$subHead.prepend('<img src="' + togglePlus + '" alt="collapse this section" /> ');

	//By Default put the Menu in collapsed state
	$j('.children').parent().children('ul').slideUp('fast');

	//Expand All Code
	$j('.expand').click(function() {
		$subHead.children('ul').slideDown('fast');
		$j('img', $subHead).attr('src', toggleMinus);
	});

	//Contract All Code
	$j('.contract').click(function() {
		$subHead.attr('src', toggleMinus).children('ul').slideUp('fast');
		$j('img', $subHead).attr('src', togglePlus);
	});

	//Expand or Contract one particular Nested ul
	$j('img', $subHead).addClass('clickable').click(function() {
	var toggleSrc = $j(this).attr('src');
	if ( toggleSrc == toggleMinus ) {
	$j(this).attr('src', togglePlus).parent().children('ul').slideUp('fast');
	} else{
	$j(this).attr('src', toggleMinus).parent().children('ul').slideDown('fast');
	};
	});
	
	// Tabs
	$j("#tabs").tabs();
	
});


	


