web/wp-content/themes/bgw/js/slider.js
changeset 1 0d28b7c10758
equal deleted inserted replaced
0:0d9a58d2c515 1:0d28b7c10758
       
     1 
       
     2 //** Created: March 19th, 08'
       
     3 //** Aug 16th, 08'- Updated to v 1.4:
       
     4 	//1) Adds ability to set speed/duration of panel animation (in milliseconds)
       
     5 	//2) Adds persistence support, so the last viewed panel is recalled when viewer returns within same browser session
       
     6 	//3) Adds ability to specify whether panels should stop at the very last and first panel, or wrap around and start all over again
       
     7 	//4) Adds option to specify two navigational image links positioned to the left and right of the Carousel Viewer to move the panels back and forth
       
     8 
       
     9 //** Aug 27th, 08'- Nav buttons (if enabled) also repositions themselves now if window is resized
       
    10 
       
    11 var stepcarousel={
       
    12 	ajaxloadingmsg: '<div style="margin: 1em; font-weight: bold"><img src="ajaxloadr.gif" style="vertical-align: middle" /> Fetching Content. Please wait...</div>', //customize HTML to show while fetching Ajax content
       
    13 	defaultbuttonsfade: 0.4, //Fade degree for disabled nav buttons (0=completely transparent, 1=completely opaque)
       
    14 	configholder: {},
       
    15 
       
    16 	getCSSValue:function(val){ //Returns either 0 (if val contains 'auto') or val as an integer
       
    17 		return (val=="auto")? 0 : parseInt(val)
       
    18 	},
       
    19 
       
    20 	getremotepanels:function($, config){ //function to fetch external page containing the panel DIVs
       
    21 		config.$belt.html(this.ajaxloadingmsg)
       
    22 		$.ajax({
       
    23 			url: config.contenttype[1], //path to external content
       
    24 			async: true,
       
    25 			error:function(ajaxrequest){
       
    26 				config.$belt.html('Error fetching content.<br />Server Response: '+ajaxrequest.responseText)
       
    27 			},
       
    28 			success:function(content){
       
    29 				config.$belt.html(content)
       
    30 				config.$panels=config.$gallery.find('.'+config.panelclass)
       
    31 				stepcarousel.alignpanels($, config)
       
    32 			}
       
    33 		})
       
    34 	},
       
    35 
       
    36 	getoffset:function(what, offsettype){
       
    37 		return (what.offsetParent)? what[offsettype]+this.getoffset(what.offsetParent, offsettype) : what[offsettype]
       
    38 	},
       
    39 
       
    40 	getCookie:function(Name){ 
       
    41 		var re=new RegExp(Name+"=[^;]+", "i"); //construct RE to search for target name/value pair
       
    42 		if (document.cookie.match(re)) //if cookie found
       
    43 			return document.cookie.match(re)[0].split("=")[1] //return its value
       
    44 		return null
       
    45 	},
       
    46 
       
    47 	setCookie:function(name, value){
       
    48 		document.cookie = name+"="+value
       
    49 	},
       
    50 
       
    51 	fadebuttons:function(config, currentpanel){
       
    52 		config.$leftnavbutton.fadeTo('fast', currentpanel==0? this.defaultbuttonsfade : 1)
       
    53 		config.$rightnavbutton.fadeTo('fast', currentpanel==config.lastvisiblepanel? this.defaultbuttonsfade : 1)
       
    54 	},
       
    55 
       
    56 	addnavbuttons:function(config, currentpanel){
       
    57 		config.$leftnavbutton=$('<img src="'+config.defaultbuttons.leftnav[0]+'">').css({zIndex:50, position:'absolute', left:config.offsets.left+config.defaultbuttons.leftnav[1]+'px', top:config.offsets.top+config.defaultbuttons.leftnav[2]+'px', cursor:'hand', cursor:'pointer'}).attr({title:'Back '+config.defaultbuttons.moveby+' panels'}).appendTo('body')
       
    58 		config.$rightnavbutton=$('<img src="'+config.defaultbuttons.rightnav[0]+'">').css({zIndex:50, position:'absolute', left:config.offsets.left+config.$gallery.get(0).offsetWidth+config.defaultbuttons.rightnav[1]+'px', top:config.offsets.top+config.defaultbuttons.rightnav[2]+'px', cursor:'hand', cursor:'pointer'}).attr({title:'Forward '+config.defaultbuttons.moveby+' panels'}).appendTo('body')
       
    59 		config.$leftnavbutton.bind('click', function(){ //assign nav button event handlers
       
    60 			stepcarousel.stepBy(config.galleryid, -config.defaultbuttons.moveby)
       
    61 		})
       
    62 		config.$rightnavbutton.bind('click', function(){ //assign nav button event handlers
       
    63 			stepcarousel.stepBy(config.galleryid, config.defaultbuttons.moveby)
       
    64 		})
       
    65 		if (config.panelbehavior.wraparound==false){ //if carousel viewer should stop at first or last panel (instead of wrap back or forth)
       
    66 			this.fadebuttons(config, currentpanel)
       
    67 		}
       
    68 	},
       
    69 
       
    70 	alignpanels:function($, config){
       
    71 		var paneloffset=0
       
    72 		config.paneloffsets=[paneloffset] //array to store upper left offset of each panel (1st element=0)
       
    73 		config.panelwidths=[] //array to store widths of each panel
       
    74 		config.$panels.each(function(index){ //loop through panels
       
    75 			var $currentpanel=$(this)
       
    76 			$currentpanel.css({float: 'none', position: 'absolute', left: paneloffset+'px'}) //position panel
       
    77 			$currentpanel.bind('click', function(e){return config.onpanelclick(e.target)}) //bind onpanelclick() to onclick event
       
    78 			paneloffset+=stepcarousel.getCSSValue($currentpanel.css('marginRight')) + parseInt($currentpanel.get(0).offsetWidth || $currentpanel.css('width')) //calculate next panel offset
       
    79 			config.paneloffsets.push(paneloffset) //remember this offset
       
    80 			config.panelwidths.push(paneloffset-config.paneloffsets[config.paneloffsets.length-2]) //remember panel width
       
    81 		})
       
    82 		config.paneloffsets.pop() //delete last offset (redundant)
       
    83 		var addpanelwidths=0
       
    84 		var lastpanelindex=config.$panels.length-1
       
    85 		config.lastvisiblepanel=lastpanelindex
       
    86 		for (var i=config.$panels.length-1; i>=0; i--){
       
    87 			addpanelwidths+=(i==lastpanelindex? config.panelwidths[lastpanelindex] : config.paneloffsets[i+1]-config.paneloffsets[i])
       
    88 			if (config.gallerywidth>addpanelwidths){
       
    89 				config.lastvisiblepanel=i //calculate index of panel that when in 1st position reveals the very last panel all at once based on gallery width
       
    90 			}
       
    91 		}
       
    92 		config.$belt.css({width: paneloffset+'px'}) //Set Belt DIV to total panels' widths
       
    93 		config.currentpanel=(config.panelbehavior.persist)? parseInt(this.getCookie(window[config.galleryid+"persist"])) : 0 //determine 1st panel to show by default
       
    94 		config.currentpanel=(typeof config.currentpanel=="number" && config.currentpanel<config.$panels.length)? config.currentpanel : 0
       
    95 		if (config.currentpanel!=0){
       
    96 			var endpoint=config.paneloffsets[config.currentpanel]+(config.currentpanel==0? 0 : config.beltoffset)
       
    97 			config.$belt.css({left: -endpoint+'px'})
       
    98 		}
       
    99 		if (config.defaultbuttons.enable==true){ //if enable default back/forth nav buttons
       
   100 			this.addnavbuttons(config, config.currentpanel)
       
   101 			$(window).bind("load, resize", function(){ //refresh position of nav buttons when page loads/resizes, in case offsets weren't available document.oncontentload
       
   102 				config.offsets={left:stepcarousel.getoffset(config.$gallery.get(0), "offsetLeft"), top:stepcarousel.getoffset(config.$gallery.get(0), "offsetTop")}
       
   103 				config.$leftnavbutton.css({left:config.offsets.left+config.defaultbuttons.leftnav[1]+'px', top:config.offsets.top+config.defaultbuttons.leftnav[2]+'px'})
       
   104 				config.$rightnavbutton.css({left:config.offsets.left+config.$gallery.get(0).offsetWidth+config.defaultbuttons.rightnav[1]+'px', top:config.offsets.top+config.defaultbuttons.rightnav[2]+'px'})
       
   105 			})
       
   106 		}
       
   107 		this.statusreport(config.galleryid)
       
   108 		config.oninit()
       
   109 		config.onslideaction(this)
       
   110 	},
       
   111 
       
   112 	stepTo:function(galleryid, pindex){ /*User entered pindex starts at 1 for intuitiveness. Internally pindex still starts at 0 */
       
   113 		var config=stepcarousel.configholder[galleryid]
       
   114 		if (typeof config=="undefined"){
       
   115 			alert("There's an error with your set up of Carousel Viewer \""+galleryid+ "\"!")
       
   116 			return
       
   117 		}
       
   118 		var pindex=Math.min(pindex-1, config.paneloffsets.length-1)
       
   119 		var endpoint=config.paneloffsets[pindex]+(pindex==0? 0 : config.beltoffset)
       
   120 		if (config.panelbehavior.wraparound==false && config.defaultbuttons.enable==true){ //if carousel viewer should stop at first or last panel (instead of wrap back or forth)
       
   121 			this.fadebuttons(config, pindex)
       
   122 		}
       
   123 		config.$belt.animate({left: -endpoint+'px'}, config.panelbehavior.speed, function(){config.onslideaction(this)})
       
   124 		config.currentpanel=pindex
       
   125 		this.statusreport(galleryid)
       
   126 	},
       
   127 
       
   128 	stepBy:function(galleryid, steps){
       
   129 		var config=stepcarousel.configholder[galleryid]
       
   130 		if (typeof config=="undefined"){
       
   131 			alert("There's an error with your set up of Carousel Viewer \""+galleryid+ "\"!")
       
   132 			return
       
   133 		}
       
   134 		var direction=(steps>0)? 'forward' : 'back' //If "steps" is negative, that means backwards
       
   135 		var pindex=config.currentpanel+steps //index of panel to stop at
       
   136 		if (config.panelbehavior.wraparound==false){ //if carousel viewer should stop at first or last panel (instead of wrap back or forth)
       
   137 			pindex=(direction=="back" && pindex<=0)? 0 : (direction=="forward")? Math.min(pindex, config.lastvisiblepanel) : pindex
       
   138 			if (config.defaultbuttons.enable==true){ //if default nav buttons are enabled, fade them in and out depending on if at start or end of carousel
       
   139 				stepcarousel.fadebuttons(config, pindex)
       
   140 			}	
       
   141 		}
       
   142 		else{ //else, for normal stepBy behavior
       
   143 		pindex=(pindex>config.paneloffsets.length-1 || pindex<0 && pindex-steps>0)? 0 : (pindex<0)? config.paneloffsets.length+steps : pindex //take into account end or starting panel and step direction
       
   144 		}
       
   145 		var endpoint=config.paneloffsets[pindex]+(pindex==0? 0 : config.beltoffset) //left distance for Belt DIV to travel to
       
   146 		if (pindex==0 && direction=='forward' || config.currentpanel==0 && direction=='back' && config.panelbehavior.wraparound==true){ //decide whether to apply "push pull" effect
       
   147 			config.$belt.animate({left: -config.paneloffsets[config.currentpanel]-(direction=='forward'? 100 : -30)+'px'}, 'normal', function(){
       
   148 				config.$belt.animate({left: -endpoint+'px'}, config.panelbehavior.speed, function(){config.onslideaction(this)})
       
   149 			})
       
   150 		}
       
   151 		else
       
   152 			config.$belt.animate({left: -endpoint+'px'}, config.panelbehavior.speed, function(){config.onslideaction(this)})
       
   153 		config.currentpanel=pindex
       
   154 		this.statusreport(galleryid)
       
   155 	},
       
   156 
       
   157 	statusreport:function(galleryid){
       
   158 		var config=stepcarousel.configholder[galleryid]
       
   159 		var startpoint=config.currentpanel //index of first visible panel 
       
   160 		var visiblewidth=0
       
   161 		for (var endpoint=startpoint; endpoint<config.paneloffsets.length; endpoint++){ //index (endpoint) of last visible panel
       
   162 			visiblewidth+=config.panelwidths[endpoint]
       
   163 			if (visiblewidth>config.gallerywidth){
       
   164 				break
       
   165 			}
       
   166 		}
       
   167 		startpoint+=1 //format startpoint for user friendiness
       
   168 		endpoint=(endpoint+1==startpoint)? startpoint : endpoint //If only one image visible on the screen and partially hidden, set endpoint to startpoint
       
   169 		var valuearray=[startpoint, endpoint, config.panelwidths.length]
       
   170 		for (var i=0; i<config.statusvars.length; i++){
       
   171 			window[config.statusvars[i]]=valuearray[i] //Define variable (with user specified name) and set to one of the status values
       
   172 			config.$statusobjs[i].text(valuearray[i]+" ") //Populate element on page with ID="user specified name" with one of the status values
       
   173 		}
       
   174 	},
       
   175 
       
   176 	setup:function(config){
       
   177 		//Disable Step Gallery scrollbars ASAP dynamically (enabled for sake of users with JS disabled)
       
   178 		document.write('<style type="text/css">\n#'+config.galleryid+'{overflow: hidden;}\n</style>')
       
   179 		jQuery(document).ready(function($){
       
   180 			config.$gallery=$('#'+config.galleryid)
       
   181 			config.gallerywidth=config.$gallery.width()
       
   182 			config.offsets={left:stepcarousel.getoffset(config.$gallery.get(0), "offsetLeft"), top:stepcarousel.getoffset(config.$gallery.get(0), "offsetTop")}
       
   183 			config.$belt=config.$gallery.find('.'+config.beltclass) //Find Belt DIV that contains all the panels
       
   184 			config.$panels=config.$gallery.find('.'+config.panelclass) //Find Panel DIVs that each contain a slide
       
   185 			config.onpanelclick=(typeof config.onpanelclick=="undefined")? function(target){} : config.onpanelclick //attach custom "onpanelclick" event handler
       
   186 			config.onslideaction=(typeof config.onslide=="undefined")? function(){} : function(beltobj){$(beltobj).stop(); config.onslide()} //attach custom "onslide" event handler
       
   187 			config.oninit=(typeof config.oninit=="undefined")? function(){} : config.oninit //attach custom "oninit" event handler
       
   188 			config.beltoffset=stepcarousel.getCSSValue(config.$belt.css('marginLeft')) //Find length of Belt DIV's left margin
       
   189 			config.statusvars=config.statusvars || []  //get variable names that will hold "start", "end", and "total" slides info
       
   190 			config.$statusobjs=[$('#'+config.statusvars[0]), $('#'+config.statusvars[1]), $('#'+config.statusvars[2])]
       
   191 			config.currentpanel=0
       
   192 			stepcarousel.configholder[config.galleryid]=config //store config parameter as a variable
       
   193 			if (config.contenttype[0]=="ajax" && typeof config.contenttype[1]!="undefined") //fetch ajax content?
       
   194 				stepcarousel.getremotepanels($, config)
       
   195 			else
       
   196 				stepcarousel.alignpanels($, config) //align panels and initialize gallery
       
   197 		}) //end document.ready
       
   198 		jQuery(window).bind('unload', function(){ //clean up
       
   199 			if (config.panelbehavior.persist){
       
   200 				stepcarousel.setCookie(window[config.galleryid+"persist"], config.currentpanel)
       
   201 			}
       
   202 			jQuery.each(config, function(ai, oi){
       
   203 				oi=null
       
   204 			})
       
   205 			config=null
       
   206 		})
       
   207 	}
       
   208 }
       
   209 
       
   210