/*! Stellar.js v0.3 | Copyright 2012, Mark Dalgleish | http://markdalgleish.com/projects/stellar.js | http://markdalgleish.mit-license.org */ (function(a,b,c,d){function l(b,c){this.element=b,this.options=a.extend({},f,c),this._defaults=f,this._name=e,this.init()}var e="stellar",f={scrollProperty:"scroll",positionProperty:"position",horizontalScrolling:!0,verticalScrolling:!0,horizontalOffset:0,verticalOffset:0,parallaxBackgrounds:!0,parallaxElements:!0,hideDistantElements:!0,viewportDetectionInterval:1e4,hideElement:function(a){a.hide()},showElement:function(a){a.show()}},g={scroll:{getTop:function(a){return a.scrollTop()},setTop:function(a,b){a.scrollTop(b)},getLeft:function(a){return a.scrollLeft()},setLeft:function(a,b){a.scrollLeft(b)}},position:{getTop:function(a){return parseInt(a.css("top"),10)*-1},setTop:function(a,b){a.css("top",b)},getLeft:function(a){return parseInt(a.css("left"),10)*-1},setLeft:function(a,b){a.css("left",b)}},margin:{getTop:function(a){return parseInt(a.css("margin-top"),10)*-1},setTop:function(a,b){a.css("margin-top",b)},getLeft:function(a){return parseInt(a.css("margin-left"),10)*-1},setLeft:function(a,b){a.css("margin-left",b)}},transform:{getTop:function(a){return a.css(i+"transform")!=="none"?parseInt(a.css(i+"transform").match(/(-?[0-9]+)/g)[5],10)*-1:0},setTop:function(a,b){j(a,b,"Y")},getLeft:function(a){return a.css(i+"transform")!=="none"?parseInt(a.css(i+"transform").match(/(-?[0-9]+)/g)[4],10)*-1:0},setLeft:function(a,b){j(a,b,"X")}}},h={position:{setTop:function(a,b){a.css("top",b)},setLeft:function(a,b){a.css("left",b)}},transform:{setTop:function(a,b,c){j(a,b-c,"Y")},setLeft:function(a,b,c){j(a,b-c,"X")}}},i=function(){var b="";return a.browser.webkit?b="-webkit-":a.browser.mozilla?b="-moz-":a.browser.opera?b="-o-":a.browser.msie&&(b="-ms-"),b}(),j=function(a,b,c){var d=a.css(i+"transform");d==="none"?a.css(i+"transform","translate"+c+"("+b+"px)"):a.css(i+"transform",k(d,/(-?[0-9]+[.]?[0-9]*)/g,c==="X"?5:6,b))},k=function(a,b,c,e){var f,g,h;return a.search(b)===-1?a:(f=a.split(b),h=c*2-1,f[h]===d?a:(f[h]=e,f.join("")))};l.prototype={init:function(){this.options.name=e+"_"+Math.floor(Math.random()*1e4),this._defineElements(),this._defineGetters(),this._defineSetters(),this.refresh(),this._startViewportDetectionLoop(),this._startAnimationLoop()},_defineElements:function(){this.element===c.body&&(this.element=b),this.$scrollElement=a(this.element),this.$element=this.element===b?a("body"):this.$scrollElement,this.$viewportElement=this.options.viewportElement!==d?a(this.options.viewportElement):this.$scrollElement[0]===b||this.options.scrollProperty.indexOf("scroll")===0?this.$scrollElement:this.$scrollElement.parent()},_defineGetters:function(){var a=this;this._getScrollLeft=function(){return g[a.options.scrollProperty].getLeft(a.$scrollElement)},this._getScrollTop=function(){return g[a.options.scrollProperty].getTop(a.$scrollElement)}},_defineSetters:function(){var a=this;this._setScrollLeft=function(b){g[a.options.scrollProperty].setLeft(a.$scrollElement,b)},this._setScrollTop=function(b){g[a.options.scrollProperty].setTop(a.$scrollElement,b)},this._setLeft=function(b,c,d){h[a.options.positionProperty].setLeft(b,c,d)},this._setTop=function(b,c,d){h[a.options.positionProperty].setTop(b,c,d)}},refresh:function(){var c=this,d=c._getScrollLeft(),e=c._getScrollTop();this._setScrollLeft(0),this._setScrollTop(0),this._setOffsets(),this._findParticles(),this._findBackgrounds(),navigator.userAgent.indexOf("WebKit")>0&&a(b).load(function(){var a=c._getScrollLeft(),b=c._getScrollTop();c._setScrollLeft(a+1),c._setScrollTop(b+1),c._setScrollLeft(a),c._setScrollTop(b)}),c._setScrollLeft(d),c._setScrollTop(e)},_findParticles:function(){var b=this,c=this._getScrollLeft(),e=this._getScrollTop();if(this.particles!==d)for(var f=this.particles.length-1;f>=0;f--)this.particles[f].$element.data("stellar-elementIsActive",d);this.particles=[];if(!this.options.parallaxElements)return;this.$element.find("[data-stellar-ratio]").each(function(c){var e=a(this),f,g,h,i,j,k,l,m,n,o=0,p=0,q=0,r=0;if(!e.data("stellar-elementIsActive"))e.data("stellar-elementIsActive",this);else if(e.data("stellar-elementIsActive")!==this)return;b.options.showElement(e),e.data("stellar-startingLeft")?(e.css("left",e.data("stellar-startingLeft")),e.css("top",e.data("stellar-startingTop"))):(e.data("stellar-startingLeft",e.css("left")),e.data("stellar-startingTop",e.css("top"))),h=e.position().left,i=e.position().top,j=e.css("margin-left")==="auto"?0:parseInt(e.css("margin-left"),10),k=e.css("margin-top")==="auto"?0:parseInt(e.css("margin-top"),10),m=e.offset().left-j,n=e.offset().top-k,e.parents().each(function(){var b=a(this);if(b.data("stellar-offset-parent")===!0)return o=q,p=r,l=b,!1;q+=b.position().left,r+=b.position().top}),f=e.data("stellar-horizontal-offset")!==d?e.data("stellar-horizontal-offset"):l!==d&&l.data("stellar-horizontal-offset")!==d?l.data("stellar-horizontal-offset"):b.horizontalOffset,g=e.data("stellar-vertical-offset")!==d?e.data("stellar-vertical-offset"):l!==d&&l.data("stellar-vertical-offset")!==d?l.data("stellar-vertical-offset"):b.verticalOffset,b.particles.push({$element:e,$offsetParent:l,isFixed:e.css("position")==="fixed",horizontalOffset:f,verticalOffset:g,startingPositionLeft:h,startingPositionTop:i,startingOffsetLeft:m,startingOffsetTop:n,parentOffsetLeft:o,parentOffsetTop:p,stellarRatio:e.data("stellar-ratio")!==d?e.data("stellar-ratio"):1,width:e.outerWidth(!0),height:e.outerHeight(!0),isHidden:!1})})},_findBackgrounds:function(){var b=this,c=this._getScrollLeft(),e=this._getScrollTop(),f;this.backgrounds=[];if(!this.options.parallaxBackgrounds)return;f=this.$element.find("[data-stellar-background-ratio]"),this.$element.is("[data-stellar-background-ratio]")&&f.add(this.$element),f.each(function(){var f=a(this),g=f.css("background-position").split(" "),h,i,j,k,l,m,n,o,p,q=0,r=0,s=0,t=0;if(!f.data("stellar-backgroundIsActive"))f.data("stellar-backgroundIsActive",this);else if(f.data("stellar-backgroundIsActive")!==this)return;f.data("stellar-backgroundStartingLeft")?f.css("background-position",f.data("stellar-backgroundStartingLeft")+" "+f.data("stellar-backgroundStartingTop")):(f.data("stellar-backgroundStartingLeft",g[0]),f.data("stellar-backgroundStartingTop",g[1])),l=f.css("margin-left")==="auto"?0:parseInt(f.css("margin-left"),10),m=f.css("margin-top")==="auto"?0:parseInt(f.css("margin-top"),10),n=f.offset().left-l-c,o=f.offset().top-m-e,f.parents().each(function(){var b=a(this);if(b.data("stellar-offset-parent")===!0)return q=s,r=t,p=b,!1;s+=b.position().left,t+=b.position().top}),h=f.data("stellar-horizontal-offset")!==d?f.data("stellar-horizontal-offset"):p!==d&&p.data("stellar-horizontal-offset")!==d?p.data("stellar-horizontal-offset"):b.horizontalOffset,i=f.data("stellar-vertical-offset")!==d?f.data("stellar-vertical-offset"):p!==d&&p.data("stellar-vertical-offset")!==d?p.data("stellar-vertical-offset"):b.verticalOffset,b.backgrounds.push({$element:f,$offsetParent:p,isFixed:f.css("background-attachment")==="fixed",horizontalOffset:h,verticalOffset:i,startingValueLeft:g[0],startingValueTop:g[1],startingBackgroundPositionLeft:isNaN(parseInt(g[0],10))?0:parseInt(g[0],10),startingBackgroundPositionTop:isNaN(parseInt(g[1],10))?0:parseInt(g[1],10),startingPositionLeft:f.position().left,startingPositionTop:f.position().top,startingOffsetLeft:n,startingOffsetTop:o,parentOffsetLeft:q,parentOffsetTop:r,stellarRatio:f.data("stellar-background-ratio")===d?1:f.data("stellar-background-ratio")})})},destroy:function(){var b,c,d,e,f;for(f=this.particles.length-1;f>=0;f--)b=this.particles[f],c=b.$element.data("stellar-startingLeft"),d=b.$element.data("stellar-startingTop"),this._setLeft(b.$element,c,c),this._setTop(b.$element,d,d),this.options.showElement(b.$element),b.$element.data("stellar-startingLeft",null).data("stellar-elementIsActive",null).data("stellar-backgroundIsActive",null);for(f=this.backgrounds.length-1;f>=0;f--)e=this.backgrounds[f],e.$element.css("background-position",e.startingValueLeft+" "+e.startingValueTop);this._animationLoop=a.noop,clearInterval(this._viewportDetectionInterval)},_setOffsets:function(){var c=this;a(b).unbind("resize.horizontal-"+this.name).unbind("resize.vertical-"+this.name),typeof this.options.horizontalOffset=="function"?(this.horizontalOffset=this.options.horizontalOffset(),a(b).bind("resize.horizontal-"+this.name,function(){c.horizontalOffset=c.options.horizontalOffset()})):this.horizontalOffset=this.options.horizontalOffset,typeof this.options.verticalOffset=="function"?(this.verticalOffset=this.options.verticalOffset(),a(b).bind("resize.vertical-"+this.name,function(){c.verticalOffset=c.options.verticalOffset()})):this.verticalOffset=this.options.verticalOffset},_repositionElements:function(){var a=this._getScrollLeft(),b=this._getScrollTop(),c,d,e,f,g,h,i,j=!0,k=!0,l,m,n,o,p;if(this.currentScrollLeft===a&&this.currentScrollTop===b&&this.currentWidth===this.viewportWidth&&this.currentHeight===this.viewportHeight)return;this.currentScrollLeft=a,this.currentScrollTop=b,this.currentWidth=this.viewportWidth,this.currentHeight=this.viewportHeight;for(p=this.particles.length-1;p>=0;p--)e=this.particles[p],f=e.isFixed?1:0,this.options.horizontalScrolling&&(l=(a+e.horizontalOffset+this.viewportOffsetLeft+e.startingPositionLeft-e.startingOffsetLeft+e.parentOffsetLeft)*-(e.stellarRatio+f-1)+e.startingPositionLeft,n=l-e.startingPositionLeft+e.startingOffsetLeft),this.options.verticalScrolling&&(m=(b+e.verticalOffset+this.viewportOffsetTop+e.startingPositionTop-e.startingOffsetTop+e.parentOffsetTop)*-(e.stellarRatio+f-1)+e.startingPositionTop,o=m-e.startingPositionTop+e.startingOffsetTop),this.options.hideDistantElements&&(k=!this.options.horizontalScrolling||n+e.width>(e.isFixed?0:a)&&n<(e.isFixed?0:a)+this.viewportWidth+this.viewportOffsetLeft,j=!this.options.verticalScrolling||o+e.height>(e.isFixed?0:b)&&o<(e.isFixed?0:b)+this.viewportHeight+this.viewportOffsetTop),k&&j?(e.isHidden&&(this.options.showElement(e.$element),e.isHidden=!1),this.options.horizontalScrolling&&this._setLeft(e.$element,l,e.startingPositionLeft),this.options.verticalScrolling&&this._setTop(e.$element,m,e.startingPositionTop)):e.isHidden||(this.options.hideElement(e.$element),e.isHidden=!0);for(p=this.backgrounds.length-1;p>=0;p--)g=this.backgrounds[p],f=g.isFixed?0:1,h=this.options.horizontalScrolling?(a+g.horizontalOffset-this.viewportOffsetLeft-g.startingOffsetLeft+g.parentOffsetLeft-g.startingBackgroundPositionLeft)*(f-g.stellarRatio)+"px":g.startingValueLeft,i=this.options.verticalScrolling?(b+g.verticalOffset-this.viewportOffsetTop-g.startingOffsetTop+g.parentOffsetTop-g.startingBackgroundPositionTop)*(f-g.stellarRatio)+"px":g.startingValueTop,g.$element.css("background-position",h+" "+i)},_startViewportDetectionLoop:function(){var a=this,b=function(){var b=a.$viewportElement.offset();a.viewportWidth=a.$viewportElement.width(),a.viewportHeight=a.$viewportElement.height(),a.viewportOffsetTop=b!==null?b.top:0,a.viewportOffsetLeft=b!==null?b.left:0};b(),this._viewportDetectionInterval=setInterval(b,this.options.viewportDetectionInterval)},_startAnimationLoop:function(){var a=this,c=function(){return b.requestAnimationFrame||b.webkitRequestAnimationFrame||b.mozRequestAnimationFrame||b.oRequestAnimationFrame||b.msRequestAnimationFrame||function(a,c){b.setTimeout(a,1e3/60)}}();this._animationLoop=function(){c(a._animationLoop),a._repositionElements()},this._animationLoop()}},a.fn[e]=function(b){var c=arguments;if(b===d||typeof b=="object")return this.each(function(){a.data(this,"plugin_"+e)||a.data(this,"plugin_"+e,new l(this,b))});if(typeof b=="string"&&b[0]!=="_"&&b!=="init")return this.each(function(){var d=a.data(this,"plugin_"+e);d instanceof l&&typeof d[b]=="function"&&d[b].apply(d,Array.prototype.slice.call(c,1)),b==="destroy"&&a.data(this,"plugin_"+e,null)})},a[e]=function(c){var d=a(b);return d.stellar.apply(d,Array.prototype.slice.call(arguments,0))},a[e].scrollProperty=g,a[e].positionProperty=h,b.Stellar=l})(jQuery,window,document);