// wpvSlider.js
/**
* A tipical HTML structure after the script has ran ( NOT FINAL!):
*
');
var _outerWrapper = $elem.parent();
$elem.wrapInner('
');
var _innerWrapper = $elem.find("> .wpv-htmlslide-wrap");
// Resize it and make it invisible while initializing
$elem.css({
visibility: "hidden",
width : calcWidth(),
height: _cfg.height
});
// Attach the pause_on_hover listeners
if ( !! _cfg.pause_on_hover) {
_outerWrapper.hover(function () {
_inst.pause();
}, function () {
_inst.resume();
});
}
// Collect all slides now ==================================================
var slides = $elem.find(".wpv-slide").wrap('
'),
len = slides.length,
initialSlide, loadCallback = function (el, type, initial) {
var slide = new Slide(el, type, _inst);
_inst.slides.push(slide);
if (initial) {
initialSlide = slide;
}
// When ALL the slides are ready fire the load event
if (_inst.slides.length === len) {
$elem.css("visibility", "visible");
_inst.setExpanded(_cfg.expand);
_outerWrapper.addClass("wpv-loaded");
_inst.hideLoadingMask(function () {
$elem.trigger("sliderload", [_inst]);
initialSlide.getCaption();
// Do I need these ?
$elem.trigger("beforeChange", [_inst, $(initialSlide.wrapper)]);
//elem.trigger("afterChange", [_inst, $(initialSlide.wrapper)]);
if (len > 1 && (_cfg.auto_direction == "left" || _cfg.auto_direction == "right")) {
_inst.start(_cfg.pause_time);
}
initialSlide = null;
});
}
slide = null;
};
// Add some classNames to the outer wrapper to help the CSS styling
if ( !! _cfg.show_arrows && len > 1) {
_outerWrapper.addClass("wpv-show-arrows");
}
if ( !! _cfg.show_bullets && len > 1) {
_outerWrapper.addClass("wpv-show-bullets");
}
if ( !! _cfg.show_caption) {
_outerWrapper.addClass("wpv-show-caption");
}
slides.each(function (i, o) {
var $o = $(o),
type = "html";
switch (o.nodeName.toLowerCase()) {
case "img":
type = "image";
break;
case "iframe":
type = "frame";
break;
case "object":
type = "object";
break;
case "embed":
type = "embed";
$o.attr("wmode", "transparent").removeAttr("bgcolor");
o.wmode = "transparent";
break;
}
if (type == "image" || type == "frame") {
if ( !! o.complete || o.readyState == "complete" || o.readyState == "loaded") {
loadCallback(o, type, i === (_cfg.start_slide || 0));
} else {
$o.bind("load readystatechange", function (e) {
if (e.type == "load") {
$o.unbind("load").unbind("readystatechange");
loadCallback(o, type, i === (_cfg.start_slide || 0));
} else {
if (o.readyState == "complete" || o.readyState == "loaded") {
$o.unbind("load").unbind("readystatechange");
loadCallback(o, type, i === (_cfg.start_slide || 0));
}
}
});
}
} else {
loadCallback(o, type, i === (_cfg.start_slide || 0));
}
});
// Find and show the initial slide
var startSlide = $(".wpv-slide-wrapper.active", elem);
if (!startSlide.length) {
startSlide = $(".wpv-slide-wrapper", elem).eq(_cfg.start_slide || 0);
}
startSlide.addClass("active").show(); //.closest(".wpv-view").css("visibility", "visible");
_inst.setExpanded(_cfg.expand);
_inst.setFX(_cfg.effect);
// Show the arrows if needed
_inst.setShowArrows(_cfg.show_arrows && len > 1);
// Show the pager if needed
_inst.setShowBullets(_cfg.show_bullets && len > 1);
// Show the captions?
_inst.setShowCaption(_cfg.show_caption);
// Autorun the slideshow if needed
//if (_cfg.auto_direction == "left" || _cfg.auto_direction == "right") {
// _inst.start(_cfg.pause_time);
//}
//
startSlide.closest(".wpv-view").css("visibility", "visible");
_outerWrapper.addClass("wpv-ready");
// Trigger the "sliderready" event
$elem.trigger("sliderready", [_inst]);
}
WPVSlider.getInstance = function (elem, options) {
var inst = $.data(elem, "WPVSlider");
if (!inst) {
inst = new WPVSlider(elem, options);
$.data(elem, "WPVSlider", inst);
}
return inst;
};
// Expose the constructor
$.WPVSlider = WPVSlider;
$.WPVSlider.Util = {
getDirection: function (from, to, all) {
var fromIndex = all.index(from);
var toIndex = all.index(to);
if (fromIndex === 0 && toIndex == all.length - 1) {
return "prev";
}
if (fromIndex == all.length - 1 && toIndex === 0) {
return "next";
}
if (fromIndex < toIndex) {
return "next";
}
if (fromIndex > toIndex) {
return "prev";
}
}
};
/**
* Public container for transition effects (available as jQuery.WPVSlider.fx)
*/
WPVSlider.fx = {};
// Built-in Effects ----------------------------------------------------------
WPVSlider.fx.fade = {
run: function (cfg) {
cfg = $.extend({
duration: 1000,
easing: "linear"
}, cfg);
cfg.toShow.css({
opacity: 0,
zIndex: 3
}).show();
//var w1 = cfg.toHide ? $("> .wpv-slide", cfg.toHide).width() : 0;
//var h1 = cfg.toHide ? $("> .wpv-slide", cfg.toHide).height() : 0;
//var w2 = $("> .wpv-slide", cfg.toShow).width();
//var h2 = $("> .wpv-slide", cfg.toShow).height();
//if (cfg.toHide && (w1 > w2 || h1 > h2)) {
cfg.toHide.fadeTo(cfg.duration, 0, "linear", function () {
cfg.toHide.css({
display: "none",
opacity: 1
});
});
//}
cfg.toShow.animate({
opacity: 1
}, cfg.duration, cfg.easing, function () {
cfg.toHide.hide();
cfg.callback();
});
cfg.toHide.css({
zIndex: 2
});
}
};
WPVSlider.fx.slide = {
run: function (cfg) {
cfg = $.extend({
duration: 800,
easing: "easeOutExpo"
}, cfg);
//console.log(cfg.toShow.data("Slide").width)
cfg.toShow.css({
//left: cfg.toShow.width() * (cfg.dir == "next" ? 1 : -1),
left: cfg.toShow.data("Slide").width * (cfg.dir == "next" ? 1 : -1),
zIndex: 3
}).show();
//var w1 = cfg.toHide ? $("> .wpv-slide", cfg.toHide).width() : 0;
//var h1 = cfg.toHide ? $("> .wpv-slide", cfg.toHide).height() : 0;
//var w2 = $("> .wpv-slide", cfg.toShow).width();
//var h2 = $("> .wpv-slide", cfg.toShow).height();
//if (cfg.toHide && (w1 > w2 || h1 > h2)) {
cfg.toHide.fadeTo(cfg.duration, 0, "linear", function () {
cfg.toHide.css({
display: "none",
opacity: 1
});
});
//}
cfg.toShow.animate({
left: 0
}, cfg.duration, cfg.easing, function () {
cfg.toHide.stop(1, 0).hide();
cfg.callback();
});
if (cfg.toHide) {
cfg.toHide.css({
zIndex: 2
});
}
}
};
WPVSlider.fx.random = {
run: function () {
var fx = [];
for (var name in WPVSlider.fx) {
if (name != "random") {
fx.push(name);
}
}
return WPVSlider.fx[fx[Math.floor(Math.random() * fx.length)]].run.apply(this, arguments);
}
};
// ---------------------------------------------------------------------------
/**
* And some public static caption effects that the effects may refer to
*/
WPVSlider.captionFx = {};
/**
* Caption effects factory
*/
WPVSlider.createCaptionFx = function (cfg, hideFn, showFn, onComplete) {
var queue = (function () {
var q = [];
function next() {
if (q.length) {
q.shift()(next);
} else {
onComplete();
}
}
function add(fn) {
q.push(fn);
}
return {
next: next,
add: add
};
})();
if (!$.isArray(hideFn)) {
hideFn = hideFn ? [hideFn] : [];
}
if (!$.isArray(showFn)) {
showFn = showFn ? [showFn] : [];
}
return function () {
$.each(hideFn, function (i, fn) {
queue.add(function (next) {
setTimeout(function () {
fn(next);
}, cfg.delay || 0);
});
});
$.each(showFn, function (i, fn) {
queue.add(function (next) {
setTimeout(function () {
fn(next);
}, cfg.delay || 0);
});
});
queue.next();
};
};
/**
* Just show/hide captions without animation (but thay still can have delays)
*/
WPVSlider.captionFx.toggle = function (cfg, callback) {
if (cfg.newCaption.length) {
$(cfg.newCaption).hide();
}
WPVSlider.createCaptionFx(
cfg, function (next) { // hideFn
if (cfg.oldCaption.length) {
$(cfg.oldCaption).hide();
}
next();
}, function (next) { // showFn
if (cfg.newCaption.length) {
$(cfg.newCaption).show();
}
next();
}, callback || $.noop)();
};
/**
* Fade-in/out both captions at the same time
*/
WPVSlider.captionFx.fadeToggle = function (cfg, callback) { //console.log(arguments)
if (cfg.newCaption.length) {
$(cfg.newCaption).fadeOut(0);
}
WPVSlider.createCaptionFx(
cfg, function (next) { // hideFn
if (cfg.oldCaption.length) {
$(cfg.oldCaption).fadeIn(0).fadeOut(cfg.speed, "linear");
}
next();
}, function (next) { // showFn
if (cfg.newCaption.length) {
$(cfg.newCaption || []).fadeTo(cfg.speed, cfg.slider.options.caption_opacity, "linear", next);
} else {
next();
}
}, callback || $.noop)();
};
// The jQuery Plugin ---------------------------------------------------------
$.fn.wpvSlider = function (opt) {
var _args = arguments,
result;
var isMethodCall = typeof opt == "string";
this.each(function (i, o) {
// Method call
if (isMethodCall) {
if (opt.charAt(0) != "_") {
var _inst = WPVSlider.getInstance(o);
if (_inst && $.isFunction(_inst[opt])) {
result = _inst[opt].apply(_inst, Array.prototype.slice.call(_args, 1));
if (result !== undefined) {
return false; // break each
}
}
_inst = null; // free object refs
}
}
// Create instance
else {
WPVSlider.getInstance(o, opt);
}
});
_args = null; // free object refs
return isMethodCall ? result : this;
};
$(window).bind("keydown", function(e) {
if (e.shiftKey) {
//console.log(e.keyCode);
switch (e.keyCode) {
case 67: // Shift + C
jQuery('.wpv-view').wpvSlider("setResizing", "crop");
break;
case 70: // Shift + F
jQuery('.wpv-view').wpvSlider("setResizing", "fit");
break;
case 83: // Shift + S
jQuery('.wpv-view').wpvSlider("setResizing", "stretch");
break;
}
}
})
})(jQuery);