/*! responsiveslides.js v1.54
* http://responsiveslides.com
* http://viljamis.com
*
* copyright (c) 2011-2012 @viljamis
* available under the mit license
*/
/*jslint browser: true, sloppy: true, vars: true, plusplus: true, indent: 2 */
(function ($, window, i) {
$.fn.responsiveslides = function (options) {
// default settings
var settings = $.extend({
"auto": true, // boolean: animate automatically, true or false
"speed": 500, // integer: speed of the transition, in milliseconds
"timeout": 4000, // integer: time between slide transitions, in milliseconds
"pager": false, // boolean: show pager, true or false
"nav": false, // boolean: show navigation, true or false
"random": false, // boolean: randomize the order of the slides, true or false
"pause": false, // boolean: pause on hover, true or false
"pausecontrols": true, // boolean: pause when hovering controls, true or false
"prevtext": "previous", // string: text for the "previous" button
"nexttext": "next", // string: text for the "next" button
"maxwidth": "", // integer: max-width of the slideshow, in pixels
"navcontainer": "", // selector: where auto generated controls should be appended to, default is after the
"manualcontrols": "", // selector: declare custom pager navigation
"namespace": "rslides", // string: change the default namespace used
"before": $.noop, // function: before callback
"after": $.noop // function: after callback
}, options);
return this.each(function () {
// index for namespacing
i++;
var $this = $(this),
// local variables
vendor,
selecttab,
startcycle,
restartcycle,
rotate,
$tabs,
// helpers
index = 0,
$slide = $this.children(),
length = $slide.size(),
fadetime = parsefloat(settings.speed),
waittime = parsefloat(settings.timeout),
maxw = parsefloat(settings.maxwidth),
// namespacing
namespace = settings.namespace,
namespaceidx = namespace + i,
// classes
navclass = namespace + "_nav " + namespaceidx + "_nav",
activeclass = namespace + "_here",
visibleclass = namespaceidx + "_on",
slideclassprefix = namespaceidx + "_s",
// pager
$pager = $(""),
// styles for visible and hidden slides
visible = {"float": "left", "position": "relative", "opacity": 1, "zindex": 2},
hidden = {"float": "none", "position": "absolute", "opacity": 0, "zindex": 1},
// detect transition support
supportstransitions = (function () {
var docbody = document.body || document.documentelement;
var styles = docbody.style;
var prop = "transition";
if (typeof styles[prop] === "string") {
return true;
}
// tests for vendor specific prop
vendor = ["moz", "webkit", "khtml", "o", "ms"];
prop = prop.charat(0).touppercase() + prop.substr(1);
var i;
for (i = 0; i < vendor.length; i++) {
if (typeof styles[vendor[i] + prop] === "string") {
return true;
}
}
return false;
})(),
// fading animation
slideto = function (idx) {
settings.before(idx);
// if css3 transitions are supported
if (supportstransitions) {
$slide
.removeclass(visibleclass)
.css(hidden)
.eq(idx)
.addclass(visibleclass)
.css(visible);
index = idx;
settimeout(function () {
settings.after(idx);
}, fadetime);
// if not, use jquery fallback
} else {
$slide
.stop()
.fadeout(fadetime, function () {
$(this)
.removeclass(visibleclass)
.css(hidden)
.css("opacity", 1);
})
.eq(idx)
.fadein(fadetime, function () {
$(this)
.addclass(visibleclass)
.css(visible);
settings.after(idx);
index = idx;
});
}
};
// random order
if (settings.random) {
$slide.sort(function () {
return (math.round(math.random()) - 0.5);
});
$this
.empty()
.append($slide);
}
// add id's to each slide
$slide.each(function (i) {
this.id = slideclassprefix + i;
});
// add max-width and classes
$this.addclass(namespace + " " + namespaceidx);
if (options && options.maxwidth) {
$this.css("max-width", maxw);
}
// hide all slides, then show first one
$slide
.hide()
.css(hidden)
.eq(0)
.addclass(visibleclass)
.css(visible)
.show();
// css transitions
if (supportstransitions) {
$slide
.show()
.css({
// -ms prefix isn't needed as ie10 uses prefix free version
"-webkit-transition": "opacity " + fadetime + "ms ease-in-out",
"-moz-transition": "opacity " + fadetime + "ms ease-in-out",
"-o-transition": "opacity " + fadetime + "ms ease-in-out",
"transition": "opacity " + fadetime + "ms ease-in-out"
});
}
// only run if there's more than one slide
if ($slide.size() > 1) {
// make sure the timeout is at least 100ms longer than the fade
if (waittime < fadetime + 100) {
return;
}
// pager
if (settings.pager && !settings.manualcontrols) {
var tabmarkup = [];
$slide.each(function (i) {
var n = i + 1;
tabmarkup +=
"- " +
"" + n + "" +
"
";
});
$pager.append(tabmarkup);
// inject pager
if (options.navcontainer) {
$(settings.navcontainer).append($pager);
} else {
$this.after($pager);
}
}
// manual pager controls
if (settings.manualcontrols) {
$pager = $(settings.manualcontrols);
$pager.addclass(namespace + "_tabs " + namespaceidx + "_tabs");
}
// add pager slide class prefixes
if (settings.pager || settings.manualcontrols) {
$pager.find('li').each(function (i) {
$(this).addclass(slideclassprefix + (i + 1));
});
}
// if we have a pager, we need to set up the selecttab function
if (settings.pager || settings.manualcontrols) {
$tabs = $pager.find('a');
// select pager item
selecttab = function (idx) {
$tabs
.closest("li")
.removeclass(activeclass)
.eq(idx)
.addclass(activeclass);
};
}
// auto cycle
if (settings.auto) {
startcycle = function () {
rotate = setinterval(function () {
// clear the event queue
$slide.stop(true, true);
var idx = index + 1 < length ? index + 1 : 0;
// remove active state and set new if pager is set
if (settings.pager || settings.manualcontrols) {
selecttab(idx);
}
slideto(idx);
}, waittime);
};
// init cycle
startcycle();
}
// restarting cycle
restartcycle = function () {
if (settings.auto) {
// stop
clearinterval(rotate);
// restart
startcycle();
}
};
// pause on hover
if (settings.pause) {
$this.hover(function () {
clearinterval(rotate);
}, function () {
restartcycle();
});
}
// pager click event handler
if (settings.pager || settings.manualcontrols) {
$tabs.bind("click", function (e) {
e.preventdefault();
if (!settings.pausecontrols) {
restartcycle();
}
// get index of clicked tab
var idx = $tabs.index(this);
// break if element is already active or currently animated
if (index === idx || $("." + visibleclass).queue('fx').length) {
return;
}
// remove active state from old tab and set new one
selecttab(idx);
// do the animation
slideto(idx);
})
.eq(0)
.closest("li")
.addclass(activeclass);
// pause when hovering pager
if (settings.pausecontrols) {
$tabs.hover(function () {
clearinterval(rotate);
}, function () {
restartcycle();
});
}
}
// navigation
if (settings.nav) {
var navmarkup =
"" + settings.prevtext + "" +
"" + settings.nexttext + "";
// inject navigation
if (options.navcontainer) {
$(settings.navcontainer).append(navmarkup);
} else {
$this.after(navmarkup);
}
var $trigger = $("." + namespaceidx + "_nav"),
$prev = $trigger.filter(".prev");
// click event handler
$trigger.bind("click", function (e) {
e.preventdefault();
var $visibleclass = $("." + visibleclass);
// prevent clicking if currently animated
if ($visibleclass.queue('fx').length) {
return;
}
// adds active class during slide animation
// $(this)
// .addclass(namespace + "_active")
// .delay(fadetime)
// .queue(function (next) {
// $(this).removeclass(namespace + "_active");
// next();
// });
// determine where to slide
var idx = $slide.index($visibleclass),
previdx = idx - 1,
nextidx = idx + 1 < length ? index + 1 : 0;
// go to slide
slideto($(this)[0] === $prev[0] ? previdx : nextidx);
if (settings.pager || settings.manualcontrols) {
selecttab($(this)[0] === $prev[0] ? previdx : nextidx);
}
if (!settings.pausecontrols) {
restartcycle();
}
});
// pause when hovering navigation
if (settings.pausecontrols) {
$trigger.hover(function () {
clearinterval(rotate);
}, function () {
restartcycle();
});
}
}
}
// max-width fallback
if (typeof document.body.style.maxwidth === "undefined" && options.maxwidth) {
var widthsupport = function () {
$this.css("width", "100%");
if ($this.width() > maxw) {
$this.css("width", maxw);
}
};
// init fallback
widthsupport();
$(window).bind("resize", function () {
widthsupport();
});
}
});
};
})(jquery, this, 0);