(function($){$.fn.jsquares=$.fn.jSquares=function(options){var main=this,image_counter=0,dropin_int=0,defaults={image:'.js-image',caption:'.js-small-caption',caption_overlay_spacing:12,caption_width:400,caption_height:130,fade_to:.3,fade_start:1,fade_speed:'fast',shuffle_in:true,shuffle_in_speed:130,fade_on_hover:true,caption_slide_down:true,caption_slidedown_speed:'fast',_fade_selectors_on_hover:'img, .js-small-caption span',_overlay_selector_class:'js-caption-overlay'},options=$.extend(defaults,options),$image=$(options.image,main),$image_children=$image.find(options._fade_selectors_on_hover),window_width=$(window).width();$(window).resize(function(){window_width=$(window).width();});function _revealBinds(e,i,evt){_caption(i);_fadeInOutImage('in',options.fade_to,i);}
function _fadeInOutImage(in_out,opacity_val,i){var chain=(in_out=='in')?$image.not(':eq('+i+')').find(options._fade_selectors_on_hover):$image_children;(options.fade_on_hover)?$(chain).fadeTo(options.fade_speed,opacity_val):$(chain).css('opacity',opacity_val);}
function _caption(index){$('.'+options._overlay_selector_class).remove();var $current_image=$(options.image+':eq('+index+')',main),overlay_sett=_getCaptionSettings($current_image),contents=$current_image.find('.js-overlay-caption-content').html()||'',caption_options=(options.caption_slide_down)?{display:'none'}:{};if(contents){var conts=contents,img=$current_image.find('img'),a=img.parent().attr('href')||'',wrap_a_start=(a!='')?'<a href="'+a+'">':'',wrap_a_end=(a!='')?'</a>':'';contents=wrap_a_start+'<img src="'+img.attr('src')+'" class="js-overlay-image" />'+wrap_a_end;contents+='<div class="js-overlay-html">'+conts+'</div><div style="clear:both"></div>';}
var caption_options=$.extend({top:overlay_sett.y,left:overlay_sett.x,width:overlay_sett.width,height:overlay_sett.height},caption_options);$('<div id="js-overlay-id-'+index+'">'+contents+'</div>').css(caption_options).addClass(options._overlay_selector_class).appendTo('body').bind('mouseleave',function(){_fadeInOutImage('out',options.fade_start)
$('#js-overlay-id-'+index).remove();});if(options.caption_slide_down){$('#js-overlay-id-'+index).slideDown(options.caption_slidedown_speed)}}
function _getCaptionSettings(image){var loc=_elementLocation($(image));var loc_x=loc.x-options.caption_overlay_spacing;var loc_y=loc.y-options.caption_overlay_spacing;var img_height=options.caption_height+(options.caption_overlay_spacing*2);var img_width=options.caption_width+(options.caption_overlay_spacing*2);var check_window_width=loc_x+img_width+(options.caption_overlay_spacing*2);if(check_window_width>=window_width){var hard_img_width=$(image).outerWidth();loc_x=loc_x-(options.caption_width+(options.caption_overlay_spacing*2))+hard_img_width;}
return{x:loc_x,y:loc_y,width:img_width,height:img_height};}
function _setImageTimeoutSlideDown(images){var images=_shuffle(images);dropin_int=setInterval(function(){if(image_counter==images.length){clearInterval(dropin_int);return false;}
var $img=$(options.image+':eq('+images[image_counter]+')',main)
var img_height=$img.height();$img.fadeIn(options.fade_speed);image_counter++},options.shuffle_in_speed);}
function _elementLocation(obj){var curleft=0;var curtop=0;do{curleft+=obj.attr('offsetLeft');curtop+=obj.attr('offsetTop');obj=obj.offsetParent();}while(obj.attr('tagName')!='BODY');return{x:curleft,y:curtop};}
function _shuffle(v){for(var j,x,i=v.length;i;j=parseInt(Math.random()*i),x=v[--i],v[i]=v[j],v[j]=x);return v;};return this.each(function(){var images=[];$image.each(function(i){$(this).hide();images.push(i);});(options.shuffle_in)?_setImageTimeoutSlideDown(images):$image.show();$image.each(function(i){$(this).hoverIntent(function(evt){_revealBinds(this,i,evt);},function(evt){});});});};})(jQuery);if(typeof $.fn.hoverIntent!='function'){(function($){$.fn.hoverIntent=function(f,g){var cfg={sensitivity:7,interval:100,timeout:0};cfg=$.extend(cfg,g?{over:f,out:g}:f);var cX,cY,pX,pY;var track=function(ev){cX=ev.pageX;cY=ev.pageY;};var compare=function(ev,ob){ob.hoverIntent_t=clearTimeout(ob.hoverIntent_t);if((Math.abs(pX-cX)+Math.abs(pY-cY))<cfg.sensitivity){$(ob).unbind("mousemove",track);ob.hoverIntent_s=1;return cfg.over.apply(ob,[ev]);}else{pX=cX;pY=cY;ob.hoverIntent_t=setTimeout(function(){compare(ev,ob);},cfg.interval);}};var delay=function(ev,ob){ob.hoverIntent_t=clearTimeout(ob.hoverIntent_t);ob.hoverIntent_s=0;return cfg.out.apply(ob,[ev]);};var handleHover=function(e){var p=(e.type=="mouseover"?e.fromElement:e.toElement)||e.relatedTarget;while(p&&p!=this){try{p=p.parentNode;}catch(e){p=this;}}if(p==this){return false;}var ev=jQuery.extend({},e);var ob=this;if(ob.hoverIntent_t){ob.hoverIntent_t=clearTimeout(ob.hoverIntent_t);}if(e.type=="mouseover"){pX=ev.pageX;pY=ev.pageY;$(ob).bind("mousemove",track);if(ob.hoverIntent_s!=1){ob.hoverIntent_t=setTimeout(function(){compare(ev,ob);},cfg.interval);}}else{$(ob).unbind("mousemove",track);if(ob.hoverIntent_s==1){ob.hoverIntent_t=setTimeout(function(){delay(ev,ob);},cfg.timeout);}}};return this.mouseover(handleHover).mouseout(handleHover);};})(jQuery);}