| 1 |
//Featured Content Glider: By http://www.dynamicdrive.com
|
| 2 |
//Created: Dec 22nd, 07'
|
| 3 |
//Updated (Jan 29th, 08): Added four possible slide directions: "updown", "downup", "leftright", or "rightleft"
|
| 4 |
//Updated (Feb 1st, 08): Changed glide behavior to reverse direction when previous button is clicked
|
| 5 |
//Updated (Feb 12th, 08): Added ability to retrieve gliding contents from an external file using Ajax ("remotecontent" variable added to configuration)
|
| 6 |
|
| 7 |
var featuredcontentglider={
|
| 8 |
csszindex: 100,
|
| 9 |
ajaxloadingmsg: '<b>Fetching Content. Please wait...</b>',
|
| 10 |
glide:function(config, showpage, isprev){
|
| 11 |
var selected=parseInt(showpage)
|
| 12 |
if (selected>=config.$contentdivs.length){ //if no content exists at this index position
|
| 13 |
// alert("No content exists. Please check: "+'\n'
|
| 14 |
// +"1. Make sure you type correct content-type in the setting page"+'\n'
|
| 15 |
// +"2. Try to clear your browser cache!")
|
| 16 |
selected=0
|
| 17 |
}
|
| 18 |
var $target=config.$contentdivs.eq(selected)
|
| 19 |
//Test for toggler not being initialized yet, or user clicks on the currently selected page):
|
| 20 |
if (config.$togglerdiv.attr('lastselected')==null || parseInt(config.$togglerdiv.attr('lastselected'))!=selected){
|
| 21 |
var $selectedlink=config.$toc.eq(selected)
|
| 22 |
config.$next.attr('loadpage', (selected<config.$contentdivs.length-1)? selected+1+'pg' : 0+'pg')
|
| 23 |
config.$prev.attr('loadpage', (selected==0)? config.$contentdivs.length-1+'pg' : selected-1+'pg')
|
| 24 |
var startpoint=(isprev=="previous")? -config.startpoint : config.startpoint
|
| 25 |
$target.css(config.leftortop, startpoint).css("zIndex", this.csszindex++) //hide content so it's just out of view before animating it
|
| 26 |
var endpoint=(config.leftortop=="left")? {left:0} : {top:0} //animate it into view
|
| 27 |
$target.animate(endpoint, config.speed)
|
| 28 |
config.$toc.removeClass('selected')
|
| 29 |
$selectedlink.addClass('selected')
|
| 30 |
config.$togglerdiv.attr('lastselected', selected+'pg')
|
| 31 |
}
|
| 32 |
},
|
| 33 |
|
| 34 |
getremotecontent:function(config){
|
| 35 |
config.$glider.html(this.ajaxloadingmsg)
|
| 36 |
$.ajax({
|
| 37 |
url: config.remotecontent,
|
| 38 |
error:function(ajaxrequest){
|
| 39 |
config.$glider.html('Error fetching content.<br />Server Response: '+ajaxrequest.responseText)
|
| 40 |
},
|
| 41 |
success:function(content){
|
| 42 |
config.$glider.html(content)
|
| 43 |
featuredcontentglider.setuptoggler(config)
|
| 44 |
}
|
| 45 |
})
|
| 46 |
},
|
| 47 |
|
| 48 |
aligncontents:function(config){
|
| 49 |
config.$contentdivs=$("#"+config.gliderid+" ."+config.contentclass)
|
| 50 |
config.$contentdivs.css(config.leftortop, config.startpoint).css({height: config.$glider.height(), visibility: 'visible'}) //position content divs so they're out of view:
|
| 51 |
},
|
| 52 |
|
| 53 |
setuptoggler:function(config){
|
| 54 |
this.aligncontents(config)
|
| 55 |
config.$togglerdiv.hide()
|
| 56 |
config.$toc.each(function(index){
|
| 57 |
$(this).attr('pagenumber', index+'pg')
|
| 58 |
if (index > (config.$contentdivs.length-1))
|
| 59 |
$(this).css({display: 'none'}) //hide redundant "toc" links
|
| 60 |
})
|
| 61 |
var $nextandprev=$("#"+config.togglerid+" .next, #"+config.togglerid+" .prev")
|
| 62 |
$nextandprev.click(function(event){ //Assign click behavior to 'next' and 'prev' links
|
| 63 |
featuredcontentglider.glide(config, this.getAttribute('loadpage'), this.getAttribute('buttontype'))
|
| 64 |
event.preventDefault() //cancel default link action
|
| 65 |
})
|
| 66 |
config.$toc.click(function(event){ //Assign click behavior to 'toc' links
|
| 67 |
featuredcontentglider.glide(config, this.getAttribute('pagenumber'))
|
| 68 |
event.preventDefault()
|
| 69 |
})
|
| 70 |
config.$togglerdiv.fadeIn(1000, function(){
|
| 71 |
featuredcontentglider.glide(config, config.selected)
|
| 72 |
if (config.autorotate==true){ //auto rotate contents?
|
| 73 |
config.stepcount=0 //set steps taken
|
| 74 |
config.totalsteps=config.$contentdivs.length*config.autorotateconfig[1] //Total steps limit: num of contents x num of user specified cycles)
|
| 75 |
featuredcontentglider.autorotate(config)
|
| 76 |
}
|
| 77 |
})
|
| 78 |
config.$togglerdiv.click(function(){
|
| 79 |
featuredcontentglider.cancelautorotate(config.togglerid)
|
| 80 |
})
|
| 81 |
},
|
| 82 |
|
| 83 |
autorotate:function(config){
|
| 84 |
var rotatespeed=config.speed+config.autorotateconfig[0]
|
| 85 |
window[config.togglerid+"timer"]=setInterval(function(){
|
| 86 |
if (config.totalsteps>0 && config.stepcount>=config.totalsteps){
|
| 87 |
clearInterval(window[config.togglerid+"timer"])
|
| 88 |
}
|
| 89 |
else{
|
| 90 |
config.$next.click()
|
| 91 |
config.stepcount++
|
| 92 |
}
|
| 93 |
}, rotatespeed)
|
| 94 |
},
|
| 95 |
|
| 96 |
cancelautorotate:function(togglerid){
|
| 97 |
if (window[togglerid+"timer"])
|
| 98 |
clearInterval(window[togglerid+"timer"])
|
| 99 |
},
|
| 100 |
|
| 101 |
getCookie:function(Name){
|
| 102 |
var re=new RegExp(Name+"=[^;]+", "i") //construct RE to search for target name/value pair
|
| 103 |
if (document.cookie.match(re)) //if cookie found
|
| 104 |
return document.cookie.match(re)[0].split("=")[1] //return its value
|
| 105 |
return null
|
| 106 |
},
|
| 107 |
|
| 108 |
setCookie:function(name, value){
|
| 109 |
document.cookie = name+"="+value
|
| 110 |
},
|
| 111 |
|
| 112 |
init:function(config){
|
| 113 |
$(document).ready(function(){
|
| 114 |
config.$glider=$("#"+config.gliderid)
|
| 115 |
config.$togglerdiv=$("#"+config.togglerid)
|
| 116 |
config.$toc=config.$togglerdiv.children('.toc')
|
| 117 |
config.$next=config.$togglerdiv.children('.next')
|
| 118 |
config.$prev=config.$togglerdiv.children('.prev')
|
| 119 |
config.$prev.attr('buttontype', 'previous')
|
| 120 |
var selected=(config.persiststate)? featuredcontentglider.getCookie(config.gliderid) : config.selected
|
| 121 |
config.selected=(isNaN(parseInt(selected))) ? config.selected : selected //test for cookie value containing null (1st page load) or "undefined" string
|
| 122 |
config.leftortop=(/up/i.test(config.direction))? "top" : "left" //set which CSS property to manipulate based on "direction"
|
| 123 |
config.heightorwidth=(/up/i.test(config.direction))? config.$glider.height() : config.$glider.width() //Get glider height or width based on "direction"
|
| 124 |
config.startpoint=(/^(left|up)/i.test(config.direction))? -config.heightorwidth : config.heightorwidth //set initial position of contents based on "direction"
|
| 125 |
if (typeof config.remotecontent!="undefined" && config.remotecontent.length>0)
|
| 126 |
featuredcontentglider.getremotecontent(config)
|
| 127 |
else
|
| 128 |
featuredcontentglider.setuptoggler(config)
|
| 129 |
$(window).bind('unload', function(){ //clean up and persist
|
| 130 |
config.$togglerdiv.unbind('click')
|
| 131 |
config.$toc.unbind('click')
|
| 132 |
config.$next.unbind('click')
|
| 133 |
config.$prev.unbind('click')
|
| 134 |
if (config.persiststate)
|
| 135 |
featuredcontentglider.setCookie(config.gliderid, config.$togglerdiv.attr('lastselected'))
|
| 136 |
config=null
|
| 137 |
|
| 138 |
})
|
| 139 |
})
|
| 140 |
}
|
| 141 |
}
|