/[drupal]/contributions/modules/content_slider/contentslider.js
ViewVC logotype

Diff of /contributions/modules/content_slider/contentslider.js

Parent Directory Parent Directory | Revision Log Revision Log | View Revision Graph Revision Graph | View Patch Patch

revision 1.1, Thu Dec 4 18:11:03 2008 UTC revision 1.2, Sat Jan 10 19:22:22 2009 UTC
# Line 0  Line 1 
1    //** Featured Content Slider script- (c) Dynamic Drive DHTML code library: http://www.dynamicdrive.com.
2    //** May 2nd, 08'- Script rewritten and updated to 2.0.
3    //** June 12th, 08'- Script updated to v 2.3, which adds the following features:
4                            //1) Changed behavior of script to actually collapse the previous content when the active one is shown, instead of just tucking it underneath the later.
5                            //2) Added setting to reveal a content either via "click" or "mouseover" of pagination links (default is former).
6                            //3) Added public function for jumping to a particular slide within a Featured Content instance using an arbitrary link, for example.
7    
8    //** July 11th, 08'- Script updated to v 2.4:
9                            //1) Added ability to select a particular slide when the page first loads using a URL parameter (ie: mypage.htm?myslider=4 to select 4th slide in "myslider")
10                            //2) Fixed bug where the first slide disappears when the mouse clicks or mouses over it when page first loads.
11    
12    var featuredcontentslider={
13    
14    //3 variables below you can customize if desired:
15    ajaxloadingmsg: '<div style="margin: 20px 0 0 20px"><img src="loading.gif" /> Fetching slider Contents. Please wait...</div>',
16    bustajaxcache: true, //bust caching of external ajax page after 1st request?
17    enablepersist: true, //persist to last content viewed when returning to page?
18    
19    settingcaches: {}, //object to cache "setting" object of each script instance
20    
21    jumpTo:function(fcsid, pagenumber){ //public function to go to a slide manually.
22            this.turnpage(this.settingcaches[fcsid], pagenumber)
23    },
24    
25    ajaxconnect:function(setting){
26            var page_request = false
27            if (window.ActiveXObject){ //Test for support for ActiveXObject in IE first (as XMLHttpRequest in IE7 is broken)
28                    try {
29                    page_request = new ActiveXObject("Msxml2.XMLHTTP")
30                    }
31                    catch (e){
32                            try{
33                            page_request = new ActiveXObject("Microsoft.XMLHTTP")
34                            }
35                            catch (e){}
36                    }
37            }
38            else if (window.XMLHttpRequest) // if Mozilla, Safari etc
39                    page_request = new XMLHttpRequest()
40            else
41                    return false
42            var pageurl=setting.contentsource[1]
43            page_request.onreadystatechange=function(){
44                    featuredcontentslider.ajaxpopulate(page_request, setting)
45            }
46            document.getElementById(setting.id).innerHTML=this.ajaxloadingmsg
47            var bustcache=(!this.bustajaxcache)? "" : (pageurl.indexOf("?")!=-1)? "&"+new Date().getTime() : "?"+new Date().getTime()
48            page_request.open('GET', pageurl+bustcache, true)
49            page_request.send(null)
50    },
51    
52    ajaxpopulate:function(page_request, setting){
53            if (page_request.readyState == 4 && (page_request.status==200 || window.location.href.indexOf("http")==-1)){
54                    document.getElementById(setting.id).innerHTML=page_request.responseText
55                    this.buildpaginate(setting)
56            }
57    },
58    
59    buildcontentdivs:function(setting){
60            //alert(document.getElementById(setting))
61            var alldivs=document.getElementById(setting.id).getElementsByTagName("div")
62            for (var i=0; i<alldivs.length; i++){
63                    if (this.css(alldivs[i], "contentdiv", "check")){ //check for DIVs with class "contentdiv"
64                            setting.contentdivs.push(alldivs[i])
65                                    alldivs[i].style.display="none" //collapse all content DIVs to begin with
66                    }
67            }
68    },
69    
70    buildpaginate:function(setting){
71            this.buildcontentdivs(setting)
72            var sliderdiv=document.getElementById(setting.id)
73            var pdiv=document.getElementById("paginate-"+setting.id)
74            var phtml=""
75            var toc=setting.toc
76            var nextprev=setting.nextprev
77            if (typeof toc=="string" && toc!="markup" || typeof toc=="object"){
78                    for (var i=1; i<=setting.contentdivs.length; i++){
79                            phtml+='<a href="#'+i+'" class="toc">'+(typeof toc=="string"? toc.replace(/#increment/, i) : toc[i-1])+'</a> '
80                    }
81                    phtml=(nextprev[0]!=''? '<a href="#prev" class="prev">'+nextprev[0]+'</a> ' : '') + phtml + (nextprev[1]!=''? '<a href="#next" class="next">'+nextprev[1]+'</a>' : '')
82                    pdiv.innerHTML=phtml
83            }
84            var pdivlinks=pdiv.getElementsByTagName("a")
85            var toclinkscount=0 //var to keep track of actual # of toc links
86            for (var i=0; i<pdivlinks.length; i++){
87                    if (this.css(pdivlinks[i], "toc", "check")){
88                            if (toclinkscount>setting.contentdivs.length-1){ //if this toc link is out of range (user defined more toc links then there are contents)
89                                    pdivlinks[i].style.display="none" //hide this toc link
90                                    continue
91                            }
92                            pdivlinks[i].setAttribute("rel", ++toclinkscount) //store page number inside toc link
93                            pdivlinks[i][setting.revealtype]=function(){
94                                    featuredcontentslider.turnpage(setting, this.getAttribute("rel"))
95                                    return false
96                            }
97                            setting.toclinks.push(pdivlinks[i])
98                    }
99                    else if (this.css(pdivlinks[i], "prev", "check") || this.css(pdivlinks[i], "next", "check")){ //check for links with class "prev" or "next"
100                            pdivlinks[i].onclick=function(){
101                                    featuredcontentslider.turnpage(setting, this.className)
102                                    return false
103                            }
104                    }
105            }
106            this.turnpage(setting, setting.currentpage, true)
107            if (setting.autorotate[0]){ //if auto rotate enabled
108                    pdiv[setting.revealtype]=function(){
109                            featuredcontentslider.cleartimer(setting, window["fcsautorun"+setting.id])
110                    }
111                    sliderdiv["onclick"]=function(){ //stop content slider when slides themselves are clicked on
112                            featuredcontentslider.cleartimer(setting, window["fcsautorun"+setting.id])
113                    }
114                    setting.autorotate[1]=setting.autorotate[1]+(1/setting.enablefade[1]*50) //add time to run fade animation (roughly) to delay between rotation
115             this.autorotate(setting)
116            }
117    },
118    
119    urlparamselect:function(fcsid){
120            var result=window.location.search.match(new RegExp(fcsid+"=(\\d+)", "i")) //check for "?featuredcontentsliderid=2" in URL
121            return (result==null)? null : parseInt(RegExp.$1) //returns null or index, where index (int) is the selected tab's index
122    },
123    
124    turnpage:function(setting, thepage, autocall){
125            var currentpage=setting.currentpage //current page # before change
126            var totalpages=setting.contentdivs.length
127            var turntopage=(/prev/i.test(thepage))? currentpage-1 : (/next/i.test(thepage))? currentpage+1 : parseInt(thepage)
128            turntopage=(turntopage<1)? totalpages : (turntopage>totalpages)? 1 : turntopage //test for out of bound and adjust
129            if (turntopage==setting.currentpage && typeof autocall=="undefined") //if a pagination link is clicked on repeatedly
130                    return
131            setting.currentpage=turntopage
132            setting.contentdivs[turntopage-1].style.zIndex=++setting.topzindex
133            this.cleartimer(setting, window["fcsfade"+setting.id])
134            setting.cacheprevpage=setting.prevpage
135            if (setting.enablefade[0]==true){
136                    setting.curopacity=0
137                    this.fadeup(setting)
138            }
139            if (setting.enablefade[0]==false){ //if fade is disabled, fire onChange event immediately (verus after fade is complete)
140                    setting.contentdivs[setting.prevpage-1].style.display="none" //collapse last content div shown (it was set to "block")
141                    setting.onChange(setting.prevpage, setting.currentpage)
142            }
143            setting.contentdivs[turntopage-1].style.visibility="visible"
144            setting.contentdivs[turntopage-1].style.display="block"
145            if (setting.prevpage<=setting.toclinks.length) //make sure pagination link exists (may not if manually defined via "markup", and user omitted)
146                    this.css(setting.toclinks[setting.prevpage-1], "selected", "remove")
147            if (turntopage<=setting.toclinks.length) //make sure pagination link exists (may not if manually defined via "markup", and user omitted)
148                    this.css(setting.toclinks[turntopage-1], "selected", "add")
149            setting.prevpage=turntopage
150            if (this.enablepersist)
151                    this.setCookie("fcspersist"+setting.id, turntopage)
152    },
153    
154    setopacity:function(setting, value){ //Sets the opacity of targetobject based on the passed in value setting (0 to 1 and in between)
155            var targetobject=setting.contentdivs[setting.currentpage-1]
156            if (targetobject.filters && targetobject.filters[0]){ //IE syntax
157                    if (typeof targetobject.filters[0].opacity=="number") //IE6
158                            targetobject.filters[0].opacity=value*100
159                    else //IE 5.5
160                            targetobject.style.filter="alpha(opacity="+value*100+")"
161            }
162            else if (typeof targetobject.style.MozOpacity!="undefined") //Old Mozilla syntax
163                    targetobject.style.MozOpacity=value
164            else if (typeof targetobject.style.opacity!="undefined") //Standard opacity syntax
165                    targetobject.style.opacity=value
166            setting.curopacity=value
167    },
168    
169    fadeup:function(setting){
170            if (setting.curopacity<1){
171                    this.setopacity(setting, setting.curopacity+setting.enablefade[1])
172                    window["fcsfade"+setting.id]=setTimeout(function(){featuredcontentslider.fadeup(setting)}, 50)
173            }
174            else{ //when fade is complete
175                    if (setting.cacheprevpage!=setting.currentpage) //if previous content isn't the same as the current shown div (happens the first time the page loads/ script is run)
176                            setting.contentdivs[setting.cacheprevpage-1].style.display="none" //collapse last content div shown (it was set to "block")
177                    setting.onChange(setting.cacheprevpage, setting.currentpage)
178            }
179    },
180    
181    cleartimer:function(setting, timervar){
182            if (typeof timervar!="undefined"){
183                    clearTimeout(timervar)
184                    clearInterval(timervar)
185                    if (setting.cacheprevpage!=setting.currentpage){ //if previous content isn't the same as the current shown div
186                            setting.contentdivs[setting.cacheprevpage-1].style.display="none"
187                    }
188            }
189    },
190    
191    css:function(el, targetclass, action){
192            var needle=new RegExp("(^|\\s+)"+targetclass+"($|\\s+)", "ig")
193            if (action=="check")
194                    return needle.test(el.className)
195            else if (action=="remove")
196                    el.className=el.className.replace(needle, "")
197            else if (action=="add")
198                    el.className+=" "+targetclass
199    },
200    
201    autorotate:function(setting){
202     window["fcsautorun"+setting.id]=setInterval(function(){featuredcontentslider.turnpage(setting, "next")}, setting.autorotate[1])
203    },
204    
205    getCookie:function(Name){
206            var re=new RegExp(Name+"=[^;]+", "i"); //construct RE to search for target name/value pair
207            if (document.cookie.match(re)) //if cookie found
208                    return document.cookie.match(re)[0].split("=")[1] //return its value
209            return null
210    },
211    
212    setCookie:function(name, value){
213            document.cookie = name+"="+value
214    
215    },
216    
217    
218    init:function(setting){
219            var persistedpage=this.getCookie("fcspersist"+setting.id) || 1
220            var urlselectedpage=this.urlparamselect(setting.id) //returns null or index from: mypage.htm?featuredcontentsliderid=index
221            this.settingcaches[setting.id]=setting //cache "setting" object
222            setting.contentdivs=[]
223            setting.toclinks=[]
224            setting.topzindex=0
225            setting.currentpage=urlselectedpage || ((this.enablepersist)? persistedpage : 1)
226            setting.prevpage=setting.currentpage
227            setting.revealtype="on"+(setting.revealtype || "click")
228            setting.curopacity=0
229            setting.onChange=setting.onChange || function(){}
230            if (setting.contentsource[0]=="inline")
231                    this.buildpaginate(setting)
232            if (setting.contentsource[0]=="ajax")
233                    this.ajaxconnect(setting)
234    }
235    
236    }

Legend:
Removed from v.1.1  
changed lines
  Added in v.1.2

  ViewVC Help
Powered by ViewVC 1.1.2