;(function($) {
/**
 * Run slideshow with image download on demand
 *
 * @example jQuery("img").slideshow();
 *
 * @lastmod 2011-01-13 19.21
 *
 * @name slideshow
 * @type jQuery
 * @param Object	settings
 *								
 * @return jQuery
 * @author Dharma Ferrari (http://www.basili.co)
 */
$.fn.slideshow = function(imageList, settings)
{
	settings = $.extend({}, $.fn.slideshow.defaults, settings);
	
	if(settings.shuffle) {
	  imageList.sort(function() {return 0.5 - Math.random()});
	}
	
  return this.each( function()
  {
    var $this = $(this);
	  var parent = $this.parent();
    var imageListIndex = 0;
    
    $this.hide();
    
    /**
     * Bind image load fired on change of src attribute
     *
     */
    $this.load(function () {
      $this.fadeIn(settings.fadeDuration * 1000);
      parent.toggleClass(settings.loadingClass);
      
      // swap only if there are more than one pictures
      if(imageList.length > 1) {
        setTimeout(function() {
          if(imageListIndex >= imageList.length-1) {imageListIndex = 0;}
          else {imageListIndex++;}
      
          $this.fadeOut(settings.fadeDuration * 1000, function() {
            parent.toggleClass(settings.loadingClass);
            $this.attr('src', imageList[imageListIndex]);
          });
        },
        settings.imageStillDuration * 1000);
      }
      
    }).error(function () {
      parent.removeClass(settings.loadingClass);
      parent.append('<div id="jssError">' + settings.error404Msg + '</div>');
    }).attr('src', imageList[imageListIndex]);
    
	});
};

$.fn.slideshow.defaults = {
  fadeDuration : .5,
  imageStillDuration : 5,
  loadingClass : 'ss_loading',
  error404Msg : '404: Errore nel caricamento',
  shuffle : false
};

})(jQuery);




;(function($){
/**
 * Effect blender
 *
 */
$.fn.blender = function(settings)
{
  settings = $.extend({}, $.fn.blender.defaults, settings);
  return this.each( function() {
    
    var $caption = {
      o:$(this).children('.caption'),
      init: {
        fs:$(this).children('.caption').css('font-size'),
        lh:$(this).children('.caption').css('line-height'),
        bt:$(this).children('.caption').css('bottom'),
        h:$(this).children('.caption').innerHeight()
      }
    };
    
    var $img = {
      o:$(this).children('img'),
      init: {
        op: $(this).children('img').css('opacity')
      }
    };
    
    if(!settings.onclick) {
     $(this).hover( function (e) {
        if ($caption.o.children('div').length>0) {
          $caption.o.children('div').slideDown(settings.speedIn, 'easeInOutQuad');
        }
      }, function (e) {
        if ($caption.o.children('div').length>0) {
          $caption.o.children('div').slideUp(settings.speedOut, 'easeInOutQuad');
        } else {
          // $img.o.fadeTo(settings.speedOut, $img.init.op);
          // $caption.o.animate({'height':$caption.init.h}, settings.speedOut, 'easeOutQuad');
        }
      });
    } else {
      $caption.o.children('a').click(function(event) {
        event.preventDefault();
        $caption.o.children('div').slideToggle(settings.speedIn, 'easeInOutQuad');
      })
    };

    if( $(this).children('a').length > 0 ) {
      $(this).click( function(event) {
        event.preventDefault();
        location.href = $(this).children('a').attr('href');
      });
      $(this).css('cursor', 'pointer');
    };
    
  });
};

$.fn.blender.defaults = {
  speedIn : 450,
  speedOut : 550,
  onclick : false
};

})(jQuery);




;(function($) {
/**
 * Prepare click action of <a> items with submenu
 * Open the submenu
 *
 */
$.fn.menuOpener = function(options) {
  var options = $.extend({}, $.fn.menuOpener.defaults, options);
  var speed = 300;
  
  function moveForward(root, target, speed) {
    target.show();
    root.animate({right: options.endx}, speed);        
    target.animate({right: options.startx}, speed);
  }
  
  function moveBack(root, target, speed) {
    root.show();
    root.animate({right: options.startx}, speed);        
    target.animate({right: options.endx}, speed);
  }
  
  return this.each(function() {
    var $this = $(this);
    var $root = $this.parent().parent();
    var $target = $( $this.attr('href') );
    
    if ($this.hasClass('active')) {
      moveForward($root, $target, 0);
    } else {
      $root.show();
      $target.animate({right : options.endx}, function(){$(this).show()});
    };

    $this.click( function(event) {
      event.preventDefault();
      moveForward($root, $target, speed);
    });
    
    $target.children('li.back').children('a').click( function(event) {
      event.preventDefault();
      moveBack($root, $target, speed);
    });
  });  
};

// default options
$.fn.menuOpener.defaults = {
  startx : 16,
  endx : -180
};

})(jQuery);



;(function($){
/**
 * Resizer v1
 *
 */
$.fn.resizer = function(settings)
{
  settings = $.extend({}, $.fn.resizer.defaults, settings);
	
  // execute on selected
  return this.each( function() {
    var pw, ph, iw, ih, $parent;
    
    $parent = $(this).parent();
        
    iw = $(this).width();
    ih = $(this).height();
    
    if(!settings.fullscreen.w) {
      pw = $parent.width();
    } else {
      pw = $(window).width();
    }
    
    if(!settings.fullscreen.h) {
      ph = $parent.height();
    } else {
      ph = $(window).height();
    }
    
    if (ih >= ph && iw < pw) {
      $(this).css('width', pw+'px');
      $(this).css('height', ((ih*pw)/iw)+'px');
    } else if (ih < ph && iw > pw) {
      $(this).css('height', ph+'px');
      $(this).css('width', ((ph*iw)/ih)+'px');
    } else if (ih > ph && iw > pw) {
      if (((ih*pw)/iw) >= ph) {
        $(this).css('width', pw+'px');
        $(this).css('height', ((ih*pw)/iw)+'px');
      } else {
        $(this).css('height', ph+'px');
        $(this).css('width', ((ph*iw)/ih)+'px');
      }
    } else if (ih < ph && iw < pw) {
      if (((ih*pw)/iw) >= ph) {
        $(this).css('width', pw+'px');
        $(this).css('height', ((ih*pw)/iw)+'px');
      } else {
        $(this).css('height', ph+'px');
        $(this).css('width', ((ph*iw)/ih)+'px');
      }
    }
  });
};

$.fn.resizer.defaults = {
  fullscreen : {w:false, h:false}
};

})(jQuery);




;(function($){
/**
 * Resizer v2 (the light one)
 *
 */
$.fn.resizer2 = function(settings)
{
  settings = $.extend({}, $.fn.resizer2.defaults, settings);

  // execute on selected
  return this.each( function() {
    var pw, ph, iw, ih, $parent;

    $parent = $(this).parent();

    iw = 4;
    ih = 3;

    if(!settings.fullscreen.w) {
      pw = $parent.width();
    } else {
      pw = $(window).width();
    }

    if(!settings.fullscreen.h) {
      ph = $parent.height();
    } else {
      ph = $(window).height();
    }

    var pratio = pw/ph;
    var iratio = iw/ih;

    if (pratio < iratio) {
      $(this).css('height', '100%');
      $(this).css('width', '');
    } else if (pratio > iratio) {
      $(this).css('height', '');
      $(this).css('width', '100%');
    }
    
  });
};

$.fn.resizer2.defaults = {
  fullscreen : {w:false, h:false}
};

})(jQuery);




;(function($) {
/**
 * Clear input and textarea placeholder values on focus in and replaces them on focus out if no edit was made
 *
 * @example jQuery("input").placeholder();
 *
 * @lastmod 2011-01-12 23.40
 *
 * @name placeholder
 * @type jQuery
 * @param Object	settings (not used yet)
 *								
 * @return jQuery
 * @author Dharma Ferrari (http://www.basili.co)
 */
$.fn.placeholder = function(settings)
{  
	settings = $.extend({}, $.fn.placeholder.defaults, settings);
	
	return this.each(function() {
  	var $this = $(this);
    $this.addClass(settings.cssClass);
  	
    $this.data('old', $this.val());
	
    // On focus in
    $this.focusin(function() {
      if($this.data('old') == $this.val()) {
        $this.val('');
        $this.removeClass(settings.cssClass);
      }
    });
    
    // On focus out
    $this.focusout(function() {
      if('' == $this.val()) {
        $this.val($this.data('old'));
        $this.addClass(settings.cssClass);
      }
    });
    
  });
  
}

$.fn.placeholder.defaults = {
	cssClass : 'placeholder_in'
};

})(jQuery);

