/*
 * jQuery Peepshow
 * Copyright 2011 ZULIUS LLC All rights reserved. 
 *
 * jQuery plugin for creating an image gallery. The plugin centers/crops/expands groups of images
 * contained in unordered list elements <li>.
 *
 * Demo: http://demos.zulius.com/jquery/peepshow
 *
 * Dev notes:
 * - An image group is simply a set of images contained within a list element <li>.
 * - Groups of images are wrapped with a relatively positioned image-wrap div for cropping/centering purposes.
 * - Auto centering wraps the entire jq-peepshow element with a relatively positioned
 *   div.jq-peepshow-auto-center so floated elements can be centered.
 * - Hover events are bound to the image-wrap div's. Public methods 'expand' and 'shrink'
 *   can be called on either the image-wrap div's or their parent li's.
 * - Expansion/shrinkage is done to the image-wrap div's.
 * - Once the image-wrap div has been added to the DOM, the plugin assumes
 *   it has no child images of class "exclude". 
 *
 *
 * @fileOverview jQuery Peepshow 
 * @author ZULIUS LLC 
 * @version 1.0.0
 */
(function(k){var c,E={id:null,length:null,firstImageLength:null},H=false,D={init:function(J){c=k.extend({},jQuery.fn.peepshow.defaults,J);o();if(c.autoCenter){k(this).wrap(k("<div></div>").addClass("jq-peepshow-auto-center"))}if(c.autoMargin){d(this)}if(typeof(c.itemsPerRow)=="string"&&c.itemsPerRow.toLowerCase()=="auto"){u(this)}i();return this.each(function(){var K=0,L=k(this).children("ul").children("li");L.each(function(){K++;if(parseInt(c.itemsPerRow)){K=g(this,K)}var M=k(this).find("img").not(c.excludeSelector);M.hide();B(this);b(this);if(c.preloadImages){if(c.preloadImages.toLowerCase()=="first"){G(M.first())}else{G(M)}}else{j(M)}z(this)})})},expand:function(){var N=k(this),M=N.hasClass(c.wrapClass)?N:N.find("."+c.wrapClass);if(M.data("expanded")){return}var K=k(M.find("img")[0]),L=K.width(),O=K.height(),Q=A(L,O);M.stop(true,true);var P={"margin-left":"-"+((parseInt(Q[0])/2))+"px","margin-top":"-"+((parseInt(Q[1])/2))+"px",width:Q[0],height:Q[1],top:"50%",left:"50%"};C(M,P.width,P.height);x(M,K,c.playPauseButton);function J(R,S){k(R).data("expanded",true).parent("li").addClass("expanded");k(S).addClass("shown")}if(!c.expandSpeed){M.css(P);J(M,K);return}M.animate(P,{duration:c.expandSpeed,easing:c.expandEasing,complete:function(R){J(this,K)}})},shrink:function(){l(this)},destroy:function(){if(c.autoCenter){this.unwrap()}return this.find("."+c.wrapClass).each(function(){var J=k(this);J.unbind("mousemove"+c.hoverIntentOptions.bindNamespace+" mouseenter"+c.hoverIntentOptions.bindNamespace+" mouseleave"+c.hoverIntentOptions.bindNamespace);J.removeData("expanded").removeData("displayedImageIndex").removeData("rotateState").removeData("maxDim");J.find("."+c.wrapClass).unbind("load error load"+c.bindNamespace+"-center");J.find(".rotate-state-button").unbind("click"+c.bindNamespace)})}};function o(){c.itemsPerRow=parseInt(c.itemsPerRow);c.crop[0]=r(c.crop[0]);c.crop[1]=r(c.crop[1]);c.minExpansion[0]=r(c.minExpansion[0]);c.minExpansion[1]=r(c.minExpansion[1]);c.maxExpansion[0]=r(c.maxExpansion[0]);c.maxExpansion[1]=r(c.maxExpansion[1]);c.autoMarginRatio=parseFloat(c.autoMarginRatio);c.rotateSpeed=parseInt(c.rotateSpeed);if(c.crop[0]>c.minExpansion[0]){c.minExpansion[0]=c.crop[0]}if(c.crop[1]>c.minExpansion[1]){c.minExpansion[1]=c.crop[1]}if(c.crop[0]>c.maxExpansion[0]){c.maxExpansion[0]=c.crop[0]}if(c.crop[1]>c.maxExpansion[1]){c.maxExpansion[1]=c.crop[1]}}function r(J){return parseInt(J,10)||0}function u(K){var M=k(K).parent().width(),O=k(K).children("ul").children("li"),J=0,N=0,L=0;if(!O.length){return false}O.each(function(){var P=k(this);J+=c.crop[0]+r(P.css("margin-right"))+r(P.css("margin-left"))+r(P.css("padding-right"))+r(P.css("padding-left"))});N=J/O.length;if(N<=0){return false}L=Math.floor(M/N);if(L<=0){return false}c.itemsPerRow=L;return true}function d(J){var K=k(J).parent().width(),O=k(J).children("ul").children("li"),R=0,N=0,L=0;if(!c.autoMarginRatio){return false}if(c.crop[1]){L=parseInt(c.crop[1])*parseFloat(c.autoMarginRatio)}if(c.crop[0]){N=parseInt(c.crop[0])*parseFloat(c.autoMarginRatio);if(parseInt(c.itemsPerRow)&&c.itemsPerRow>0){for(var M=0;M<c.itemsPerRow;M++){if(!O[M]){continue}var Q=k(O[M]);R+=parseInt(c.crop[0])+r(Q.css("padding-left"))+r(Q.css("padding-right"))}var P=N*(c.itemsPerRow-1);R+=P;if(R>K){N=(P-(R-K))/(c.itemsPerRow-1);if(N<1){N=1}L=N}}}if(N){O.css("margin-right",N)}if(L){O.css("margin-bottom",L)}}function g(K,J){if(J==1||(J==(c.itemsPerRow+1))){k(K).addClass("first")}if(J==c.itemsPerRow){k(K).addClass("last");J=0}return J}function x(L,K,M){var J=k(L).children(".overlay"),N=k(K).attr(c.captionAttribute);if(!M){if(!N||typeof(N)!="string"||!N.length){if(J.is(":visible")){J.hide()}return}}J.children("span").text(N);if(k.browser.msie){J.css("filter",J.css("filter"))}if(J.is(":visible")){return}J.fadeIn(f())}function y(J){k(J).children(".overlay").hide()}function z(J){if(c.bindNamespace){c.hoverIntentOptions.bindNamespace=c.bindNamespace+"-hover"}c.hoverIntentOptions.over=function(K){k(this).peepshow("expand");h(this)};c.hoverIntentOptions.out=function(K){k(this).peepshow("shrink")};return k(J).children("."+c.wrapClass).each(function(){var K=k(this);if(!K.find("img").length){return}K.hoverIntent(c.hoverIntentOptions)})}function i(){var J=2.5,K=parseInt(c.rotateSpeed-(J*c.hoverIntentOptions.interval));E.firstImageLength=K>c.hoverIntentOptions.interval?K:c.rotateSpeed}function h(M){var L=k(this),J;if(!M){J=L.hasClass(c.wrapClass)?L:L.find("."+c.wrapClass)}else{J=M.target==undefined?k(M):k(M.target);J=J.hasClass(c.wrapClass)?J:J.find("."+c.wrapClass)}var K=J.find("img");if(c.preloadImages.toLowerCase()=="first"){G(K)}if(E.id!=null){l(J);return}if(K.length<2){return}a(J,"play",J.children(".overlay").children(".rotate-state-button"));E.length=E.firstImageLength;E.id=setInterval(function(){if(v(J)=="pause"){return}if(E.length==E.firstImageLength){E.length=parseInt(c.rotateSpeed);clearTimeout(E.id);E.id=setInterval(arguments.callee,E.length)}var Q=J.data("displayedImageIndex")==undefined?0:J.data("displayedImageIndex"),N=K[Q+1]==undefined?0:Q+1,P=k(K[Q]),O=k(K[N]);if(!O.complete){if(c.showLoader&&(c.playPauseButton)){n(J);return}}P.addClass("last-shown");O.show().css({opacity:0}).addClass("shown");var R=function(){if(!c.transitionEffect||(typeof(c.transitionEffect)!="string")||c.transitionEffect.toLowerCase()=="none"){P.css({opacity:0}).removeClass("shown last-shown");O.css({opacity:1});return}var S=f();switch(c.transition){case"fade":default:O.animate({opacity:1},S,function(){P.removeClass("shown last-shown")});P.animate({opacity:0},S);break}};J.data("displayedImageIndex",(N));x(J,O,c.playPauseButton);s(J,O,R)},E.length)}function a(K,L,J){k(K).data("rotateState",L);if(J!=undefined){if(L=="pause"){k(J).addClass("paused");return}k(J).removeClass("paused")}}function v(J){return k(J).data("rotateState")}function t(K,J){if(v(K)=="play"){a(K,"pause",J);return}a(K,"play",J)}function f(){var J=300;if(c.transitionEffectSpeedRatio&&c.rotateSpeed){J=c.transitionEffectSpeedRatio*c.rotateSpeed}return J}function l(N){var M=(this),K;if(!N){K=M.hasClass(c.wrapClass)?M:M.find("."+c.wrapClass)}else{K=N.target==undefined?k(N):k(N.target);K=K.hasClass(c.wrapClass)?K:K.find("."+c.wrapClass)}clearInterval(E.id);E.id=null;var L=K.find("img");K.stop(true,true);L.stop(false,true).removeClass("last-shown shown").css({opacity:0});var J=k(L[0]);J.css({opacity:1});K.data("displayedImageIndex",0);F(K)}function F(K){var J=k(K);if(!J.data("expanded")){return}var L={left:0,top:0,width:"100%",height:"100%","margin-top":0,"margin-left":0};J.stop(true,true);if(!c.shrinkSpeed){J.css(L).find("img").removeClass("shown")}else{J.stop(true,true).animate(L,{duration:c.shrinkSpeed,easing:c.shrinkEasing,complete:function(){k(this).find("img").removeClass("shown")}})}y(K);J.data("expanded",false).removeData("maxDim").parent("li").removeClass("expanded")}function C(M,L,J){var K=k(M),O=K.data("maxDim"),N=false;if(O==undefined){K.data("maxDim",[L,J]);return}if(L>O[0]){O[0]=L;N=true}if(J>O[1]){O[1]=J;N=true}if(!N){return}K.data("maxDim",O)}function b(N){if(H){return false}var K=[],M=k(N).children("."+c.wrapClass).first(),O=M.css("background-image");if(O){O=O.replace(/"/g,"").replace(/url\(|\)$/ig,"");K.push(O)}for(var L=0,J=K.length;L<J;++L){if(typeof(K[L])!="string"||K[L].toLowerCase()=="none"){continue}k("<img/>")[0].src=K[L]}H=true}function G(J){k(J).each(function(){var K=this,L=k(this);if(K.complete){j(K);return}if(k.browser.msie){L.attr("src",p(K))}L.load(function(M){j(this)}).error(w)})}function w(K){if(this.src==c.imageNotFound){return false}var J=k(this).attr("src");k(this).attr({"orig-src":J,src:c.imageNotFound});j(this);return true}function j(J){return k(J).each(function(){var K=k(this);K.unbind("load");m(this);q(K.parents("."+c.wrapClass));if(!K.siblings().length&&K.parent("a").length){if(!K.parent("a").prev().length){K.show();e(this)}return}if(!K.prev().length){K.show();e(this)}})}function n(J){k(J).children(".overlay").children(".rotate-state-button").addClass("loading")}function q(J){k(J).children(".overlay").children(".rotate-state-button").removeClass("loading")}function e(K){var J=k(K);if(!J.hasClass(c.wrapClass)){J.parents("."+c.wrapClass).removeClass("loading");return}J.removeClass("loading")}function B(O){var L=k(O);L.css({width:c.crop[0],height:c.crop[1]});if(L.children("."+c.wrapClass).length){return}var N=k("<div></div>"),Q=c.wrapClass,P=L.find("a").has("img").not(c.excludeSelector);if(c.roundCorners){Q+=" round"}if(c.showLoader){Q=Q+" loading"}N.attr({"class":Q});if(P.length){k(P).wrapAll(N)}else{L.find("img").not(c.excludeSelector).wrapAll(N)}var J=k('<div class="overlay"></div>').css("display","none"),M=k("<span></span>");J.append(M);if(c.playPauseButton){var K=k('<a title="pause"></a>').addClass("rotate-state-button");J.append(K)}J.append(M);L.children("."+c.wrapClass).append(J);if(c.playPauseButton){L.children("."+c.wrapClass).children(".overlay").children(".rotate-state-button").bind("click"+c.bindNamespace,function(R){R.preventDefault();if(c.showLoader&&k(this).hasClass("loading")){return false}t(k(this).parents("."+c.wrapClass),this);return false})}}function m(J){var L=k(J),K="load"+c.bindNamespace+"-center";function M(O){var Q=k(O),P=Q.width(),N=Q.height();Q.css({"margin-top":"-"+(parseInt(N)/2)+"px","margin-left":"-"+(parseInt(P)/2)+"px",top:"50%",left:"50%"});Q.unbind(K)}return L.each(function(){if(this.complete){M(this);return}var N=k(this);if(k.browser.msie){N.attr("src",p(this))}N.bind(K,function(O){M(this)}).error(w)})}function p(J){if(k.browser.version==7||k.browser.version==8){var K=Math.floor(Math.random()*(Math.pow(2,32)-1));return k(J).attr("src")+"?"+K}return k(J).attr("src")}function I(){return this.each(function(){k(this).css({"margin-top":0,"margin-left":0,left:0,top:0})})}function s(X,Q,O){var S=k(X),K=k(Q),U=K.width(),P=K.height(),W=S.width(),R=S.height(),N=typeof O==="function"?O:undefined;if(c.retainMaxExpansion&&L&&(L[0]<=imgWrapW)&&(L[1]<=imgWrapH)){if(N!==undefined){N()}return}var V=U,M=P,J={"margin-top":0,"margin-left":0,top:"50%",left:"50%",width:U,height:P};if(c.retainMaxExpansion){var L=S.data("maxDim"),Y=false;if(L==undefined||L.length!=2){Y=true}else{if(L[0]>J.width){J.width=L[0];V=L[0]}else{Y=true}if(L[1]>J.height){J.height=L[1];M=L[1]}else{Y=true}}if(Y){C(S,J.width,J.height)}}var T=A(J.width,J.height);J.width=T[0];V=T[0];J.height=T[1];M=T[1];J["margin-left"]="-"+(parseInt(V)/2)+"px";J["margin-top"]="-"+(parseInt(M)/2)+"px";S.stop(true,true);if(!c.expandSpeed){S.css(J);if(N!==undefined){N()}return}S.animate(J,{duration:c.expandSpeed,easing:c.expandEasing,complete:function(){if(N!==undefined){N()}}})}function A(K,J){if(K<c.minExpansion[0]){K=c.minExpansion[0]}else{if(K>c.maxExpansion[0]){K=c.maxExpansion[0]}}if(J<c.minExpansion[1]){J=c.minExpansion[1]}else{if(J>c.maxExpansion[1]){J=c.maxExpansion[1]}}return[K,J]}jQuery.fn.peepshow=function(J){if(D[J]){return D[J].apply(this,Array.prototype.slice.call(arguments,1))}else{if(typeof J==="object"||!J){return D.init.apply(this,arguments)}else{k.error("Method "+J+" does not exist on jQuery.peepshow")}}};jQuery.fn.peepshow.defaults={autoCenter:true,autoMargin:true,autoMarginRatio:0.25,bindNamespace:".jq-peepshow",captionAttribute:"caption",crop:[200,200],excludeSelector:"img.exclude",expandEasing:"swing",expandSpeed:0,hoverIntentOptions:{sensitivity:9,interval:100,timeout:0},imageNotFound:"img/image-not-found.jpg",itemsPerRow:3,maxExpansion:[500,500],minExpansion:[300,300],playPauseButton:false,preloadImages:"all",retainMaxExpansion:true,rotateSpeed:1700,roundCorners:false,showLoader:false,shrinkEasing:"swing",shrinkSpeed:0,transitionEffect:"fade",transitionEffectSpeedRatio:0.27,wrapClass:"image-wrap"}})(jQuery);
/*
* hoverIntent is similar to jQuery's built-in "hover" function except that
* instead of firing the onMouseOver event immediately, hoverIntent checks
* to see if the user's mouse has slowed down (beneath the sensitivity
* threshold) before firing the onMouseOver event.
* 
* hoverIntent r6 // 2011.02.26 // jQuery 1.5.1+
* <http://cherne.net/brian/resources/jquery.hoverIntent.html>
* 
* hoverIntent is currently available for use in all personal or commercial 
* projects under both MIT and GPL licenses. This means that you can choose 
* the license that best suits your project, and use it accordingly.
* 
* // basic usage (just like .hover) receives onMouseOver and onMouseOut functions
* $("ul li").hoverIntent( showNav , hideNav );
* 
* // advanced usage receives configuration object only
* $("ul li").hoverIntent({
*   sensitivity: 7, // number = sensitivity threshold (must be 1 or higher)
*   interval: 100,   // number = milliseconds of polling interval
*   over: showNav,  // function = onMouseOver callback (required)
*   timeout: 0,   // number = milliseconds delay before onMouseOut function call
*   out: hideNav    // function = onMouseOut callback (required)
* });
* 
* @param  f  onMouseOver function || An object with configuration options
* @param  g  onMouseOut function  || Nothing (use configuration options object)
* @author    Brian Cherne brian(at)cherne(dot)net
* @modifiedBy  Timbo White, ZULIUS LLC
*
*
*/
(function(a){a.fn.hoverIntent=function(k,j){var l={sensitivity:7,interval:100,timeout:0,bindNamespace:".hoverIntent"};l=a.extend(l,j?{over:k,out:j}:k);var n,m,h,d;var e=function(f){n=f.pageX;m=f.pageY};var c=function(g,f){f.hoverIntent_t=clearTimeout(f.hoverIntent_t);if((Math.abs(h-n)+Math.abs(d-m))<l.sensitivity){a(f).unbind("mousemove"+l.bindNamespace,e);f.hoverIntent_s=1;return l.over.apply(f,[g])}else{h=n;d=m;f.hoverIntent_t=setTimeout(function(){c(g,f)},l.interval)}};var i=function(g,f){f.hoverIntent_t=clearTimeout(f.hoverIntent_t);f.hoverIntent_s=0;return l.out.apply(f,[g])};var b=function(o){var g=jQuery.extend({},o);var f=this;if(f.hoverIntent_t){f.hoverIntent_t=clearTimeout(f.hoverIntent_t)}if(o.type=="mouseenter"){h=g.pageX;d=g.pageY;a(f).bind("mousemove"+l.bindNamespace,e);if(f.hoverIntent_s!=1){f.hoverIntent_t=setTimeout(function(){c(g,f)},l.interval)}}else{a(f).unbind("mousemove"+l.bindNamespace,e);if(f.hoverIntent_s==1){f.hoverIntent_t=setTimeout(function(){i(g,f)},l.timeout)}}};return this.bind("mouseenter"+l.bindNamespace,b).bind("mouseleave"+l.bindNamespace,b)}})(jQuery);
