/*
* jquery.elastislide.js
*
* Slider Functions
*/
(function (e, h, g) {
h.fn.touchwipe = function (a) {
var b = {
min_move_x: 20,
min_move_y: 20,
wipeLeft: function () {},
wipeRight: function () {},
wipeUp: function () {},
wipeDown: function () {},
preventDefaultEvents: true
};
if (a) h.extend(b, a);
this.each(function () {
var o;
var p;
var c = false;
function d() {
this.removeEventListener("touchmove", n);
o = null;
c = false
}
function n(k) {
if (b.preventDefaultEvents) k.preventDefault();
if (c) {
var i = k.touches[0].pageX;
var j = k.touches[0].pageY;
var l = o - i;
var r = p - j;
if (Math.abs(l) >= b.min_move_x) {
d();
if (l > 0) {
b.wipeLeft()
} else {
b.wipeRight()
}
} else {
if (Math.abs(r) >= b.min_move_y) {
d();
if (r > 0) {
b.wipeDown()
} else {
b.wipeUp()
}
}
}
}
}
function m(i) {
if (i.touches.length == 1) {
o = i.touches[0].pageX;
p = i.touches[0].pageY;
c = true;
this.addEventListener("touchmove", n, false)
}
}
if ("ontouchstart" in document.documentElement) {
this.addEventListener("touchstart", m, false)
}
});
return this
};
h.elastislide = function (b, a) {
this.$el = h(a);
this._init(b)
};
h.elastislide.defaults = {
speed: 450,
easing: "",
imageW: 190,
margin: 3,
border: 0,
minItems: 1,
current: 0,
onClick: function () {
return false
}
};
h.elastislide.prototype = {
_init: function (a) {
this.options = h.extend(true, {}, h.elastislide.defaults, a);
this.$slider = this.$el.find("ul");
this.$items = this.$slider.children("li");
this.itemsCount = this.$items.length;
this.$esCarousel = this.$slider.parent();
this._validateOptions();
this._configure();
this._addControls();
this._initEvents();
this.$slider.show();
this._slideToCurrent(false)
},
_validateOptions: function () {
if (this.options.speed < 0) this.options.speed = 450;
if (this.options.margin < 0) this.options.margin = 4;
if (this.options.border < 0) this.options.border = 1;
if (this.options.minItems < 1 || this.options.minItems > this.itemsCount) this.options.minItems = 1;
if (this.options.current > this.itemsCount - 1) this.options.current = 0;
},
_configure: function () {
this.current = this.options.current;
this.visibleWidth = this.$esCarousel.width();
if (this.visibleWidth < this.options.minItems * (this.options.imageW + 2 * this.options.border) + (this.options.minItems - 1) * this.options.margin) {
this._setDim((this.visibleWidth - (this.options.minItems - 1) * this.options.margin) / this.options.minItems);
this._setCurrentValues();
this.fitCount = this.options.minItems
} else {
this._setDim();
this._setCurrentValues()
}
this.$slider.css({
width: this.sliderW
})
},
_setDim: function (a) {
this.$items.css({
marginRight: this.options.margin,
width: (a) ? a : this.options.imageW + 2 * this.options.border
}).children("a").css({
borderWidth: this.options.border
})
},
_setCurrentValues: function () {
this.itemW = this.$items.outerWidth(true);
this.sliderW = this.itemW * this.itemsCount;
this.visibleWidth = this.$esCarousel.width();
this.fitCount = Math.floor(this.visibleWidth / this.itemW)
},
_addControls: function () {
this.$navNext = h('');
this.$navPrev = h('');
h('
').append(this.$navPrev).append(this.$navNext).appendTo(this.$el)
},
_toggleControls: function (a, b) {
if (a && b) {
if (b === 1) {
(a === "right") ? this.$navNext.show() : this.$navPrev.show()
} else {
(a === "right") ? this.$navNext.hide() : this.$navPrev.hide()
}
} else {
if (this.current === this.itemsCount - 1 || this.fitCount >= this.itemsCount) {
this.$navNext.hide()
}
}
},
_initEvents: function () {
var a = this;
h(e).bind("resize.elastislide", function (b) {
a._setCurrentValues();
if (a.visibleWidth < a.options.minItems * (a.options.imageW + 2 * a.options.border) + (a.options.minItems - 1) * a.options.margin) {
a._setDim((a.visibleWidth - (a.options.minItems - 1) * a.options.margin) / a.options.minItems);
a._setCurrentValues();
a.fitCount = a.options.minItems
} else {
a._setDim();
a._setCurrentValues()
}
a.$slider.css({
width: a.sliderW + 10
});
clearTimeout(a.resetTimeout);
a.resetTimeout = setTimeout(function () {
a._slideToCurrent()
}, 200)
});
this.$navNext.bind("click.elastislide", function (b) {
a._slide("right")
});
this.$navPrev.bind("click.elastislide", function (b) {
a._slide("left")
});
this.$items.bind("click.elastislide", function (b) {
a.options.onClick(h(this));
return false
});
a.$slider.touchwipe({
wipeLeft: function () {
a._slide("right")
},
wipeRight: function () {
a._slide("left")
}
})
},
_slide: function (q, r, p, a) {
if (this.$slider.is(":animated")) return false;
var o = parseFloat(this.$slider.css("margin-left"));
if (r === g) {
var n = this.fitCount * this.itemW, r;
if (n < 0) return false;
if (q === "right" && this.sliderW - (Math.abs(o) + n) < this.visibleWidth) {
n = this.sliderW - (Math.abs(o) + this.visibleWidth) - this.options.margin;
this._toggleControls("right", -1);
this._toggleControls("left", 1)
} else {
if (q === "left" && Math.abs(o) - n < 0) {
n = Math.abs(o);
this._toggleControls("left", -1);
this._toggleControls("right", 1)
} else {
var c;
(q === "right") ? c = Math.abs(o) + this.options.margin + Math.abs(n) : c = Math.abs(o) - this.options.margin - Math.abs(n);
if (c > 0) {
this._toggleControls("left", 1)
} else {
this._toggleControls("left", -1)
} if (c < this.sliderW - this.visibleWidth) {
this._toggleControls("right", 1)
} else {
this._toggleControls("right", -1)
}
}
}(q === "right") ? r = "-=" + n : r = "+=" + n
} else {
var c = Math.abs(r);
if (Math.max(this.sliderW, this.visibleWidth) - c < this.visibleWidth) {
r = -(Math.max(this.sliderW, this.visibleWidth) - this.visibleWidth);
if (r !== 0) r += this.options.margin;
this._toggleControls("right", -1);
c = Math.abs(r)
}
if (c > 0) {
this._toggleControls("left", 1)
} else {
this._toggleControls("left", -1)
} if (Math.max(this.sliderW, this.visibleWidth) - this.visibleWidth > c + this.options.margin) {
this._toggleControls("right", 1)
} else {
this._toggleControls("right", -1)
}
}
h.fn.applyStyle = (p === g) ? h.fn.animate : h.fn.css;
var d = {
marginLeft: r
};
var b = this;
this.$slider.applyStyle(d, h.extend(true, [], {
duration: this.options.speed,
easing: this.options.easing,
complete: function () {
if (a) a.call();
}
}))
},
_slideToCurrent: function (a) {
var b = this.current * this.itemW;
this._slide("", -b, a)
},
add: function (b, a) {
this.$items = this.$items.add(b);
this.itemsCount = this.$items.length;
this._setDim();
this._setCurrentValues();
this.$slider.css({
width: this.sliderW
});
this._slideToCurrent();
if (a) a.call(b);
},
destroy: function (a) {
this._destroy(a)
},
_destroy: function (a) {
this.$el.unbind(".elastislide").removeData("elastislide");
h(e).unbind(".elastislide");
if (a) a.call();
}
};
var f = function (a) {
if (this.console) console.error(a);
};
h.fn.elastislide = function (a) {
if (typeof a === "string") {
var b = Array.prototype.slice.call(arguments, 1);
this.each(function () {
var c = h.data(this, "elastislide");
if (!c) {
f("cannot call methods on elastislide prior to initialization; attempted to call method '" + a + "'");
return
}
if (!h.isfunction (c[a]) || a.charAt(0) === "_") {
f("no such method '" + a + "' for elastislide instance");
return
}
c[a].apply(c, b)
})
} else {
this.each(function () {
var c = h.data(this, "elastislide");
if (!c) h.data(this, "elastislide", new h.elastislide(a, this));
})
}
return this
}
})(window, jQuery);