$=jQuery.noConflict();

// Coda Bubble
$(function () {
  $('.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 = $('.trigger', this);
    var popup = $('.popup', this).css('opacity', 0);

    // set the mouseover and mouseout on both element
    $([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({
          top: 75,
          left: 12,
          display: 'block' // brings the popup back in to view
        })

        // (we're using chaining on the popup) now animate it's opacity and position
        .animate({
          top: '-=' + 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({
          top: '-=' + 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);
    });
  });
});

// Coda Bubble Primomaggio
$(function () {
  $('.bubbleInfoMaggio').each(function () {
    // options
    var distance = 10;
    var time = 250;
    var hideDelay = 0;
    var hideDelayTimer = null;

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

    // set the mouseover and mouseout on both element
    $([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({
          top: 65,
          left: -35,
          display: 'block' // brings the popup back in to view
        })

        // (we're using chaining on the popup) now animate it's opacity and position
        .animate({
          top: '-=' + 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({
          top: '-=' + 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);
    });
  });
});

// when the DOM is ready...
$(document).ready(function () {

// Sidebar Box Toggle
$('.sidebar-box-content').show();
$('.sidebar-box h1').toggle(function() {
	$(this).addClass("active");
	}, function () {
	$(this).removeClass("active");
});
$('.sidebar-box h1').click(function() {
	$(this).next(".sidebar-box-content").slideToggle("slow");
});

// News Box Toggle
$('.news-box-content').hide();
$('.news-box h1.toggler').toggle(function() {
	$(this).addClass("active");
	}, function () {
	$(this).removeClass("active");
});
$('.news-box h1.toggler').click(function() {
	$(this).next(".news-box-content").slideToggle("slow");
});

//Smooth Menu
$("#navigator li").mouseover(function(){
	$(this).stop().animate({height:'300px'},{queue:false, duration:400, easing: 'jswing'})
});
$("#navigator li").mouseout(function(){
	$(this).stop().animate({height:'45px'},{queue:false, duration:400, easing: 'jswing'})
});

//Thumb Magnifier
$("ul.thumb li").hover(function() {
	$(this).css({'z-index' : '20'}); /*Add a higher z-index value so this image stays on top*/ 
	$(this).find('img').addClass("hover").stop() /* Add class of "hover", then stop animation queue buildup*/
		.animate({
			marginTop: '-50px', /* The next 4 lines will vertically align this image */ 
			marginLeft: '-50px',
			top: '50%',
			left: '50%',
			width: '100px', /* Set new width */
			height: '100px', /* Set new height */
			padding: '0'
		}, 200); /* this value of "200" is the speed of how fast/slow this hover animates */

	} , function() {
	$(this).css({'z-index' : '0'}); /* Set z-index back to 0 */
	$(this).find('img').removeClass("hover").stop()  /* Remove the "hover" class , then stop animation queue buildup*/
		.animate({
			marginTop: '0', /* Set alignment back to default */
			marginLeft: '0',
			top: '0',
			left: '0',
			width: '75px', /* Set width back to default */
			height: '75px', /* Set height back to default */
			padding: '0'
		}, 400);
});

// Cycle
$("#slideshow").cycle({fx: 'scrollLeft', timeout: 6000, delay:  -2000});
$("#slideshow-description").cycle({fx: 'fade', timeout: 6000, delay: -1000, cleartypeNoBg: true });
/* cleartypeNoBg: true per bug internet explorer */

// Pretty Photo
$("a[rel^='prettyPhoto']").prettyPhoto({
	animationSpeed: 'normal', /* fast/slow/normal */
	padding: 40, /* padding for each side of the picture */
	opacity: 0.35, /* Value betwee 0 and 1 */
	showTitle: true, /* true/false */
	allowresize: true, /* true/false */
	counter_separator_label: '/', /* The separator for the gallery counter 1 "of" 2 */
	theme: 'light_rounded', /* light_rounded / dark_rounded / light_square / dark_square */
	callback: function(){}
	});
	
// Coda Better Slider
var $panels = $('#topstory-slider .scrollContainer > div');
var $container = $('#topstory-slider .scrollContainer');
var horizontal = true;
if (horizontal) {
  $panels.css({
    'float' : 'left',
    'position' : 'relative' // IE fix to ensure overflow is hidden
  });
  $container.css('width', $panels[0].offsetWidth * $panels.length);
}
var $scroll = $('#topstory-slider .scroll').css('overflow', 'hidden');
$scroll
.before('<img class="scrollButtons left" src="http://www.agrariaclub.it/wp-content/themes/agrariaclub/images/scroll_left.png" />')
.after('<img class="scrollButtons right" src="http://www.agrariaclub.it/wp-content/themes/agrariaclub/images/scroll_right.png" />');
function selectNav() {
  $(this)
    .parents('ul:first')
      .find('a')
        .removeClass('selected')
      .end()
    .end()
    .addClass('selected');
}
$('#topstory-slider .navigation').find('a').click(selectNav);
function trigger(data) {
  var el = $('#topstory-slider .navigation').find('a[href$="' + data.id + '"]').get(0);
  selectNav.call(el);
}
if (window.location.hash) {
  trigger({ id : window.location.hash.substr(1) });
} else {
  $('ul.navigation a:first').click();
}
var offset = parseInt((horizontal ? 
  $container.css('paddingTop') : 
  $container.css('paddingLeft')) 
  || 0) * -1;
var scrollOptions = {
  target: $scroll,
  items: $panels,
  navigation: '.navigation a',
  prev: 'img.left', 
  next: 'img.right',
  axis: 'xy',
  onAfter: trigger,
  offset: offset,
  duration: 400,
  easing: 'swing'
};
$('#topstory-slider').serialScroll(scrollOptions);
$.localScroll(scrollOptions);
scrollOptions.duration = 1;
$.localScroll.hash(scrollOptions);

});