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