/** * TOSOM S.R.L. * Background Slider. * * This library is property of Tosom s.r.l. * It's not free for use, modify, redistribution or whatever. * It can only be used within the residenza bistrot subsite developed by tosom. * You cannot sell, modify, re-use, distribute or re-distribute without explicit permission of tosom. * * * @version:1.0 * @author: Toniolo Marco. * * */ function trace(m) {window.console.log(m)} var bgslider = function(opts){ this.options = { slides : null , timingsTransition : 500 , timingsSlide : 3000 , empty : null }; this.images = null ; this.sliderId = null ; this.sliderEl = null ; this.sliderLoader = null ; this.sliderImage = null ; this.currentSlide = 0 ; this.status = "default" ; this.timer = null ; this.loaderTimer = null ; var self = this ; // Private methods self scoping hack /*public methods*/ this.getWinSize = function() { return { w : $(window).width() , h : $(window).height() }; } /*LOADER!!!*/ this.loaderSetPos = function() { var winSize = this.getWinSize(); this.sliderLoader.css({ left : parseInt(winSize.w/2) - parseInt( this.sliderLoader.width()/2 ), top : parseInt(winSize.h/2) - parseInt( this.sliderLoader.height()/2 ) }); } this.loaderShow = function() { this.sliderLoader.css({display:"block"}); } this.loaderHide = function() { this.sliderLoader.css({display:"none"}); } /*CONTAINER AND IMAGE*/ this.sliderResize = function(bg) { var winSize = this.getWinSize(); this.sliderEl.css({ width : winSize.w , height : winSize.h }); var finalW = winSize.w ; var finalH = winSize.h ; var ratio = finalW/finalH ; var ci = this.images[ this.currentSlide ] ; var imageW = ci.width; var imageH = ci.height; var imgRatio = imageW /imageH ; var margin = "" ; var tmp = "" ; if(ci) { } if( ratio <= imgRatio ) { finalW = parseInt( finalH*imgRatio ) ; } else { finalH = parseInt( finalW/imgRatio ) ; } var ml = 0 ; var mt = 0 ; if( finalW>winSize.w ) { ml = parseInt( Math.abs( winSize.w-finalW ) / 2 ) ; } if( finalH>winSize.h ) { mt = parseInt( Math.abs( winSize.h-finalH ) / 2 ) ; } this.sliderImage.css({ width : finalW , height : finalH , margin : "-"+mt+"px 0 0 -"+ml+"px" }); if( typeof bg!="undefined" && bg==true) { this.sliderEl.children(".bgslider_slide").css({ width : finalW , height : finalH , margin : "-"+mt+"px 0 0 -"+ml+"px" }); } } this.sliderStop = function() { if( this.timer !=="null" ) { window.clearTimeout(this.timer); this.timer = null ; this.sliderImage.stop() ; this.status = "stopped" ; this.currentSlide = 0 ; //this.sliderImage.css({display:"none"}) ; if( this.loaderTimer ) { window.clearTimeout(this.loaderTimer) ; this.loaderTimer = null ; } } } this.slidesChange = function(slides) { /*window.clearTimeout(this.timer); this.timer = null ; //this.status = "default" ; this.currentSlide = 0 ;*/ this.sliderStop(); if( this.loaderTimer ) { window.clearTimeout(this.loaderTimer) ; this.loaderTimer = null ; } //this.sliderEl.children(".bgslider_slide").css({opacity:0}); this.sliderImage.stop(); this.sliderImage.animate({opacity:0},this.options.timingsTransition,function(){ //self.sliderStop(); self.options.slides = slides ; //trace("CS : "+self.status ) self.build(); }) ; } /*animation methods*/ this.build = function() { //trace("B1 : "+this.status) // STOP THE ANIMATION(if running) if( this.status=="running" ) { this.sliderStop(); } // LOAD OR RELOAD SLIDES this.images = null ; this.images = new Array(); if( typeof(this.options.slides)!="undefined" ) { for( var i=0 ; i1 ) { ++self.currentSlide ; if( self.currentSlide >= self.images.length ) { self.currentSlide =0 ; } // START SLIDESHOW ONLY IF MORE THAN 1 IMAGE self.slideshowPlay(); } }, self.options.timingsSlide ); }) ; } else if( this.status == "changed" ) // FIRST CHANGE EXECUTION !!! { this.status = "running" ; this.sliderImage.css({opacity:0,display:"block"}); self.sliderResize(); this.sliderImage.attr("src",currImage.src) ; this.sliderImage.animate({ opacity:1 },this.options.timingsTransition , function(){ // RESET BG self.sliderEl.children(".bgslider_slide").attr("src",currImage.src) ; self.sliderEl.children(".bgslider_slide").css({ width : self.sliderImage.css("width") , height : self.sliderImage.css("height") , margin : self.sliderImage.css("marginTop")+" 0 0 "+self.sliderImage.css("marginLeft") }) ; self.timer = window.setTimeout( function(){ if( self.images.length>1 ) { ++self.currentSlide ; if( self.currentSlide >= self.images.length ) { self.currentSlide =0 ; } // PLAY SLIDESHOW ONLY IF MORE THAN 1 IMAGE //trace(self.currentSlide+" "+self.images.length) self.slideshowPlay(); } }, self.options.timingsSlide ); }); } else if( this.status == "running" ) { this.sliderImage.stop().animate({ opacity:0 },this.options.timingsTransition , function(){ self.sliderResize(); $(this).attr("src",currImage.src) ; $(this).animate({ opacity:1 },self.options.timingsTransition , function(){ // RESET BG self.sliderEl.children(".bgslider_slide").attr("src",currImage.src) ; self.sliderEl.children(".bgslider_slide").css({ width : self.sliderImage.css("width") , height : self.sliderImage.css("height") , margin : self.sliderImage.css("marginTop")+" 0 0 "+self.sliderImage.css("marginLeft") }) ; self.timer = window.setTimeout( function(){ if( self.images.length>1 ) { ++self.currentSlide ; if( self.currentSlide >= self.images.length ) { self.currentSlide =0 ; } // PLAY SLIDESHOW ONLY IF MORE THAN 1 IMAGE //trace(self.currentSlide+" "+self.images.length) self.slideshowPlay(); } }, self.options.timingsSlide ); }); }) ; } } this.imageHide = function( callback ) { var fn = null ; //trace("imageHide("+typeof callback+") => "+this.currentSlide) if( typeof callback=="function" ) { fn = callback } this.sliderImage.stop().animate({ opacity:0 },this.options.timingsTransition , fn) ; } this.imageShow = function( callback ) { var fn = null ; //trace("imageShow("+typeof callback+") => "+this.currentSlide) if( typeof callback=="function" ) { fn = callback } this.sliderImage.stop().animate({ opacity:1 },this.options.timingsTransition , fn ) ; } /*Constructor*/ this.init = function(opts) { if( typeof opts.sliderId != "undefined" ) { this.sliderId = opts.sliderId ; this.sliderEl = $("#"+this.sliderId) ; if( this.sliderEl.length > 0 ) { var winSize = this.getWinSize(); this.sliderEl.append('
') ; this.sliderLoader = $("#bgslider_loader") ; this.sliderImage = this.sliderEl.children(".bgslider_slide_active") ; this.sliderImage.css({display:"none"}) this.loaderSetPos(); this.loaderShow(); this.sliderEl.css({ display:"block" , position:"absolute", zIndex : -1 , width : winSize.w, height : winSize.h }); //trace(this.options.sliderEl.css() ) } } if( typeof opts.timingsTransition != "undefined" ) { this.options.timingsTransition = opts.timingsTransition ; } if( typeof opts.timingsSlide != "undefined" ) { this.options.timingsSlide = opts.timingsSlide ; } if( typeof opts.slides != "undefined" ) { this.options.slides = opts.slides ; } // add the resize method $(window).resize(function(){ self.sliderResize(true); }); this.build(); } this.init(opts) ; }