/*
 * jQuery Nivo Slider v2.1
 * http://nivo.dev7studios.com
 *
 * Copyright 2010, Gilbert Pellegrom
 * Free to use and abuse under the MIT license.
 * http://www.opensource.org/licenses/mit-license.php
 * 
 * October 2010 - Added "foldLeft", "unfold", and "unfoldLeft" effects, settings.effect is pulled from $.data so that the effect can be changed on the fly by thirdender
 * May 2010 - Pick random effect from specified set of effects by toronegro
 * May 2010 - controlNavThumbsFromRel option added by nerd-sh
 * May 2010 - Do not start nivoRun timer if there is only 1 slide by msielski
 * April 2010 - controlNavThumbs option added by Jamie Thompson (http://jamiethompson.co.uk)
 * March 2010 - manualAdvance option added by HelloPablo (http://hellopablo.co.uk)
 */

(function(c){c.fn.nivoSlider=function(E){function r(a,d,b,j){var i=a.data("nivo:vars");if((!i||i.stop)&&!j)return false;b.effect=i.effect;var l=Math.round(a.width()/b.slices),k=c(".nivo-slice",a);b.beforeChange.call(this);if(!j||j==="prev"||j==="next")a.css("background","url("+i.currentImage.attr("src")+") no-repeat");i.currentSlide++;if(i.currentSlide===i.totalSlides){i.currentSlide=0;b.slideshowEnd.call(this)}if(i.currentSlide<0)i.currentSlide=i.totalSlides-1;i.currentImage=c(d[i.currentSlide]).is("img")?
c(d[i.currentSlide]):c(d[i.currentSlide]).find("img:first");b.controlNav&&c(".nivo-controlNav a",a).removeClass("active").filter(":eq("+i.currentSlide+")").addClass("active");if(i.currentImage.attr("title")!==""){var q=i.currentImage.attr("title");if(q.substr(0,1)==="#")q=c(q).html();c(".nivo-caption",a).css("display")=="block"?c(".nivo-caption p",a).fadeOut(b.animSpeed,function(){c(this).html(q);c(this).fadeIn(b.animSpeed)}):c(".nivo-caption p",a).html(q);c(".nivo-caption",a).fadeIn(b.animSpeed)}else c(".nivo-caption",
a).fadeOut(b.animSpeed);var m=0;k.each(function(g){m+=c(this).css({height:"0px",opacity:"0",left:m,background:"url("+i.currentImage.attr("src")+") no-repeat -"+(l+g*l-l)+"px 0%"}).width()});var e;d=b.effect==="random"?["sliceDownRight","sliceDownLeft","sliceUpRight","sliceUpLeft","sliceUpDown","sliceUpDownLeft","fold","foldLeft","unfold","unfoldLeft","fade"]:b.effect.split(",");e=d[0|Math.random()*d.length];if(b.effect==="random")i.randAnim=e;i.running=true;if(e==="sliceDown"||e==="sliceDownRight"||
e==="sliceDownLeft"){if(e==="sliceDownLeft")k=k._reverse();k.each(function(g){var f=c(this).css({top:0,bottom:"auto"});setTimeout(g===b.slices-1?function(){f.animate({height:"100%",opacity:"1.0"},b.animSpeed,"",function(){a.trigger("nivo:animFinished")})}:function(){f.animate({height:"100%",opacity:"1.0"},b.animSpeed)},100+g*50)})}else if(e==="sliceUp"||e==="sliceUpRight"||e==="sliceUpLeft"){if(e==="sliceUpLeft")k=k._reverse();k.each(function(g){var f=c(this).css({top:"auto",bottom:0});setTimeout(g===
b.slices-1?function(){f.animate({height:"100%",opacity:"1.0"},b.animSpeed,"",function(){a.trigger("nivo:animFinished")})}:function(){f.animate({height:"100%",opacity:"1.0"},b.animSpeed)},100+g*50)})}else if(e==="sliceUpDown"||e==="sliceUpDownRight"||e==="sliceUpDownLeft"){if(e==="sliceUpDownLeft")k=k._reverse();k.each(function(g){var f=c(this).css(g%2===0?{top:0,bottom:"auto"}:{top:"auto",bottom:0});setTimeout(g===b.slices-1?function(){f.animate({height:"100%",opacity:"1.0"},b.animSpeed,"",function(){a.trigger("nivo:animFinished")})}:
function(){f.animate({height:"100%",opacity:"1.0"},b.animSpeed)},100+g*50)})}else if(e==="zoom"||e==="zoomRight"||e==="zoomLeft"){d=a.width();var v=e==="zoomLeft",t=0,z={opacity:1,transform:"scale(1)","-moz-transform":"scale(1)","-webkit-transform":"scale(1)"};k.each(function(){var g=c(this).css({transform:"scale(1.5)","-moz-transform":"scale(1.5)","-webkit-transform":"scale(1.5)"}).css(v?"left":"right",t),f="all "+b.animSpeed/1E3+"s ease-in";g.css({position:"absolute",top:0,height:"100%",transition:f,
"-moz-transition":f,"-webkit-transition":f});t+=g.width()});if(e==="zoomLeft")k=k._reverse();k.each(function(g){var f=c(this);setTimeout(g===b.slices-1?function(){f.css(z);setTimeout(function(){k.css({transition:"none","-moz-transition":"none","-webkit-transition":"none"});a.trigger("nivo:animFinished")},b.animSpeed)}:function(){f.css(z)},100+g*50)})}else if(e==="fold"||e==="unfoldLeft"){if(e==="unfoldLeft")k=k._reverse();k.each(function(g){var f=c(this),n=f.width();f.css({top:0,height:"100%",width:0});
setTimeout(g===b.slices-1?function(){f.animate({width:n,opacity:"1.0"},b.animSpeed,"",function(){a.trigger("nivo:animFinished")})}:function(){f.animate({width:n,opacity:"1.0"},b.animSpeed)},100+g*50)})}else if(e==="unfold"||e==="foldLeft"){var F=/\D/g;if(e==="foldLeft")k=k._reverse();k.each(function(g){var f=c(this),n={width:f.width(),opacity:1,left:Number(f.css("left").replace(F,""))};if(e==="foldLeft"){n["background-position"]="-"+(b.slices-g-1)*l+"px 0%";f.css("background-position","-"+(b.slices-
g)*l+"px 0%")}else{n["background-position"]="-"+g*l+"px 0%";f.css("background-position","-"+(g+1)*l+"px 0%")}f.css({top:0,height:"100%",width:0,left:n.left+n.width});setTimeout(g===b.slices-1?function(){f.animate(n,b.animSpeed,"",function(){a.trigger("nivo:animFinished")})}:function(){f.animate(n,b.animSpeed)},100+g*50)})}else if(e==="slide"||e==="slideLeft"){d=a.width();j=k.filter(":first");var w=j.clone().css({top:0,left:0,height:"100%",opacity:1,width:a.width(),background:a.css("background")});
j.before(w);a.css("background","url("+i.currentImage.attr("src")+")");w.animate({left:e==="slideLeft"?-d:d,opacity:0},b.animSpeed*2,"",function(){w.remove();a.trigger("nivo:animFinished")})}else if(e==="disintegrate"||e==="disintegrateLeft"){d=a.width();var G=a.height();j=k.filter(":first").css("background",a.css("background"));var u=[],x=30,s,p,A,o;for(s=0;s<d;s+=32){u.push(A=[]);for(p=0;p<G;p+=32){j.before(o=j.clone().css({left:s,top:p,width:32,height:32,"background-position":"-"+s+"px -"+p+"px",
opacity:1}));A.push({element:o,left:s,top:p,bgLeft:-s,bgTop:-p})}}a.css("background","url("+i.currentImage.attr("src")+")");e==="disintegrateLeft"&&u.reverse();c.each(u,function(g,f){var n=32,y=+new Date,B,C;setTimeout(function(){function D(){if(n>0){n-=1;for(p=0;o=f[p];p++){if(n%2){o.left++;o.top++;o.bgTop--;o.bgLeft--}o.element.css({left:o.left,top:o.top,width:n,height:n,"background-position":o.bgLeft+"px "+o.bgTop+"px",opacity:n/24})}B=y;y=+new Date;C=y-(B+x);setTimeout(D,Math.max(0,x-C))}else if(g+
1===u.length){for(g=0;f=u[g];g++)for(p=0;o=f[p];p++)o.element.remove();a.trigger("nivo:animFinished")}}setTimeout(D,x)},g*40)})}else e==="fade"&&k.each(function(g){var f=c(this);f.css("height","100%");g===b.slices-1?f.animate({opacity:"1.0"},b.animSpeed*2,"",function(){a.trigger("nivo:animFinished")}):f.animate({opacity:"1.0"},b.animSpeed*2)})}var h=c.extend({},c.fn.nivoSlider.defaults,E);return this.each(function(){var a={currentSlide:0,currentImage:"",effect:h.effect,totalSlides:0,randAnim:"",running:false,
paused:false,stop:false},d=c(this).data("nivo:vars",a).css("position","relative").addClass("nivoSlider"),b=d.children();b.each(function(){var m=c(this),e="";if(!m.is("img")){if(m.is("a")){m.addClass("nivo-imageLink");e=m}m=m.find("img:first")}var v=m.width()||m.attr("width"),t=m.height()||m.attr("height");v>d.width()&&d.width(v);t>d.height()&&d.height(t);e!==""&&e.css("display","none");m.css("display","none");a.totalSlides++});if(h.startSlide>0){if(h.startSlide>=a.totalSlides)h.startSlide=a.totalSlides-
1;a.currentSlide=h.startSlide}a.currentImage=c(b[a.currentSlide]).is("img")?c(b[a.currentSlide]):c(b[a.currentSlide]).find("img:first");c(b[a.currentSlide]).is("a")&&c(b[a.currentSlide]).css("display","block");d.css("background","url("+a.currentImage.attr("src")+") no-repeat");for(var j=0;j<h.slices;j++){var i=Math.round(d.width()/h.slices);d.append(c('<div class="nivo-slice"></div>').css({left:i*j,width:j===h.slices-1?d.width()-i*j:i}))}d.append(c('<div class="nivo-caption"><p></p></div>').css({display:"none",
opacity:h.captionOpacity}));if(a.currentImage.attr("title")!==""){j=a.currentImage.attr("title");if(j.substr(0,1)==="#")j=c(j).html();c(".nivo-caption p",d).html(j);c(".nivo-caption",d).fadeIn(h.animSpeed)}var l=0;if(!h.manualAdvance&&b.length>1)l=setInterval(function(){r(d,b,h,false)},h.pauseTime);if(h.directionNav){d.append('<div class="nivo-directionNav"><a class="nivo-prevNav">Prev</a><a class="nivo-nextNav">Next</a></div>');if(h.directionNavHide){var k=c(".nivo-directionNav",d).hide();d.hover(function(){k.show()},
function(){k.hide()})}c("a.nivo-prevNav, a.nivo-nextNav",d).live("click",function(){if(a.running)return false;clearInterval(l);l="";if(c(this).is("a.nivo-prevNav")){a.currentSlide-=2;r(d,b,h,"prev")}else r(d,b,h,"next")})}if(h.controlNav){i=c('<div class="nivo-controlNav"></div>');d.append(i);for(j=0;j<b.length;j++)if(h.controlNavThumbs){var q=b.eq(j);q.is("img")||(q=q.find("img:first"));h.controlNavThumbsFromRel?i.append('<a class="nivo-control" rel="'+j+'"><img src="'+q.attr("rel")+'" alt="" /></a>'):
i.append('<a class="nivo-control" rel="'+j+'"><img src="'+q.attr("src").replace(h.controlNavThumbsSearch,h.controlNavThumbsReplace)+'" alt="" /></a>')}else i.append('<a class="nivo-control" rel="'+j+'">'+(j+1)+"</a>");c(".nivo-controlNav a:eq("+a.currentSlide+")",d).addClass("active");c(".nivo-controlNav a",d).live("click",function(){if(a.running)return false;if(c(this).hasClass("active"))return false;clearInterval(l);l="";d.css("background","url("+a.currentImage.attr("src")+") no-repeat");a.currentSlide=
c(this).attr("rel")-1;r(d,b,h,"control")})}h.keyboardNav&&c(window).keypress(function(m){if(m.keyCode==="37"){if(a.running)return false;clearInterval(l);l="";a.currentSlide-=2;r(d,b,h,"prev")}if(m.keyCode==="39"){if(a.running)return false;clearInterval(l);l="";r(d,b,h,"next")}});h.pauseOnHover&&d.hover(function(){a.paused=true;clearInterval(l);l=""},function(){a.paused=false;if(l==""&&!h.manualAdvance)l=setInterval(function(){r(d,b,h,false)},h.pauseTime)});d.bind("nivo:animFinished",function(){a.running=
false;c(b).each(function(){c(this).is("a")&&c(this).css("display","none")});c(b[a.currentSlide]).is("a")&&c(b[a.currentSlide]).css("display","block");if(l==""&&!a.paused&&!h.manualAdvance)l=setInterval(function(){r(d,b,h,false)},h.pauseTime);h.afterChange.call(this)})})};c.fn.nivoSlider.defaults={effect:"random",slices:15,animSpeed:500,pauseTime:3E3,startSlide:0,directionNav:true,directionNavHide:true,controlNav:true,controlNavThumbs:false,controlNavThumbsFromRel:false,controlNavThumbsSearch:".jpg",
controlNavThumbsReplace:"_thumb.jpg",keyboardNav:true,pauseOnHover:true,manualAdvance:false,captionOpacity:0.8,beforeChange:function(){},afterChange:function(){},slideshowEnd:function(){}};c.fn._reverse=[].reverse})(jQuery);

