blob: 94aa7c64d3b10fac94b05c45bcb345ff9a21325f [file] [log] [blame]
!function(a){"function"==typeof define&&define.amd?define(["jquery"],a):a(jQuery)}(function(a){"use strict";a(document);var b=a(window),c={container:"fsr-container",image:"fsr-image",hidden:"fsr-hidden"},d={useCSS:!0,container:"parent",position:{x:"center",y:"center"},throttleTimeout:100},e="backgroundSize"in document.createElement("div").style,f=function(b,c){if(this.image=b,this.$image=a(b),this.settings=a.extend({},d,c),this.params={image:{width:null,height:null},container:{width:null,height:null},ratios:{image:null,container:null}},"parent"===this.settings.container)this.$container=this.$image.parent();else{if(!(this.settings.container instanceof jQuery&&this.settings.container.length))return this.log("The container is missing."),void 0;this.$container=this.settings.container}this.id=this.uuid(),this.init()};f.fn=f.prototype,f.fn.init=function(){if(this.$image.addClass(c.image),this.$container.addClass(c.container),e&&this.settings.useCSS)this.setBackground();else{if(e&&this.settings.useCSS)return this.log("No resize method."),void 0;this.listen(),this.resize()}},f.fn.setBackground=function(){var a=this.$image,b=this.$container,d=this.settings.position;a.addClass(c.hidden),b.css({"background-image":"url("+a[0].src+")","background-position":d.x+" "+d.y})},f.fn.listen=function(){var a=this;b.on("resize."+this.id+" orientationchange."+this.id,this.throttle(function(){a.resize()},this.settings.throttleTimeout))},f.fn.resize=function(){this.calcRatios(),this.update()},f.fn.calcRatios=function(){var a,b,c,d,e=this.$image,f=this.$container,g=this.params;if(!g.ratios.image){if(!e.attr("width")||!e.attr("height"))return this.log("The image should have width/height attributes."),void 0;a=parseInt(e.attr("width"),10),b=parseInt(e.attr("height"),10),g.ratios.image=b/a,g.image.width=a,g.image.height=b}c=f.width(),d=f.height(),g.ratios.container=d/c,g.container.width=c,g.container.height=d},f.fn.update=function(){var a=this.$image,b=this.params.ratios,c=this.params.container,d=this.settings,e={width:0,height:0,top:"auto",right:"auto",bottom:"auto",left:"auto"};switch(b.image<b.container?(e.width=c.height/b.image,e.height=c.height):(e.width=c.width,e.height=c.width*b.image),d.position.y){case"top":e.top=0;break;case"bottom":e.bottom=0;break;case"center":e.top=(c.height-e.height)/2;break;default:return this.log("The vertical position cannot be calculated."),void 0}switch(d.position.x){case"left":e.left=0;break;case"right":e.right=0;break;case"center":e.left=(c.width-e.width)/2;break;default:return this.log("The horizontal position cannot be calculated."),void 0}a.css(e)},f.fn.destroy=function(){var a=this.$image,d=this.$container;a.removeClass(c.hidden).removeClass(c.image).removeAttr("style"),d.removeClass(c.container).removeAttr("style"),b.off("resize."+this.id+" orientationchange."+this.id),a.removeData("fullscreener")},f.fn.throttle=function(a,b,c){function d(){i=0==c.leading?0:new Date,h=null,g=a.apply(e,f)}var e,f,g,h=null,i=0,c=c||{};return function(){var j,k=new Date;return i||0!=c.leading||(i=k),j=b-(k-i),e=this,f=arguments,0>=j?(clearTimeout(h),h=null,i=k,g=a.apply(e,f)):h||0==c.leading||(h=setTimeout(d,j)),g}},f.fn.uuid=function(){return"fsr"+parseInt((new Date).getTime()/1e3,10)},f.fn.log=function(a){alert("Fullscreener "+a)},a.fn.fullscreener=function(b){return this.each(function(){var c=a(this),d=c.data("fullscreener");d?"string"==typeof b&&(d[b]?d[b]():f.fn.log("No valid method.")):"string"!=typeof b&&c.data("fullscreener",new f(this,b))})}});