// set css rules for $items, one of main functions
function setOrder() {
// call method onChange for inner callback out of script
getObject.onChange($items.eq(itemsOrder[middleIndex]), itemsOrder[middleIndex]===0 ? length : itemsOrder[middleIndex], length);
getObject.pagination.enable&&paginationSet(itemsOrder[middleIndex]);
for (var j=0; j<length; j++){
var i = itemsOrder[j],
$this = $items.eq(i),
img = $this.find('img'),
w = img.width(),
h = img.height(),
diffIndex = j - (middleIndex-1),
diffIndexAbs = Math.abs(j - (middleIndex-1)),
newIndex = (diffIndex % middleIndex),
prop = getObject.cssProperty,
coeff = 0,
classToAdd = '',
cssProperties = {
'width': w,
'height': h,
'left' : '50%'
};
if (diffIndex > 0) {
coeff = 1;
classToAdd = ' ' + getObject.rightSideClass.substr(1);
}
else if (diffIndex < 0) {
coeff = -1;
classToAdd = ' ' + getObject.leftSideClass.substr(1);
}
cssProperties[prop] = (-w*.5 + newIndex*getObject.itemOffset) + coeff*getObject.itemOffsetCenter;
if (getObject.useCSS3Animation) {
cssProperties['zIndex'] = (middleIndex - diffIndexAbs);
$this
.attr('class', getObject.transformClasses[diffIndexAbs] + classToAdd)
.css(cssProperties);
} else {
$this
.attr('class', getObject.transformClasses[diffIndexAbs] + classToAdd)
.css({'zIndex': (middleIndex - diffIndexAbs)})
.stop()
.animate(cssProperties);
}
}
}
// create reflections with $items
function createReflections(){
$items.each(function(i){
var $this = $(this),
img = $this.find('img'),
rClass = getObject.reflections.reflectionClass.substr(1);
$.when( img.parent().after('<canvas class="'+rClass+'"></canvas>') )
.then(function(d){
var canvas = d.siblings(getObject.reflections.reflectionClass);
if (canvas[0].getContext) {
var refl = {
context: canvas[0].getContext("2d"),
width: canvas.width(),
height: canvas.height()
}
canvas.attr({'width': refl.width, 'height': refl.height});
// draw a piece of image
refl.context.drawImage(img[0], 0, img.height()-refl.height, refl.width, refl.height, 0, 0, refl.width, refl.height);
// draw gradient for reflection
refl.context.globalCompositeOperation = "destination-out";
var gradient = refl.context.createLinearGradient(0, 0, 0, refl.height);
gradient.addColorStop(0, getObject.reflections.startColor);
gradient.addColorStop(1, getObject.reflections.endColor);
refl.context.fillStyle = gradient;
refl.context.fillRect(0, 0, refl.width, refl.height);
}
})
})
}
// function for move to previous item
function prev(e){
itemsOrder.unshift(itemsOrder.pop());
setOrder();
return false;
}
// function for move to next item
function next(e){
itemsOrder.push(itemsOrder.shift());
setOrder();
return false;
}
// function for move to previous item with stop autoplay
function clickPrev(e){
$this.trigger('firstAction');
stopAutoplay();
prev(e);
return false;
}
// function for move to next item with stop autoplay
function clickNext(e){
$this.trigger('firstAction');
stopAutoplay();
next(e);
return false;
}
function firstAction(e){
if (!isFirstActionDone) {
isFirstActionDone = true;
getObject.onUserActivate();
}
}
// function for d(param) step shift
function go(e, d){
var diff, wayFunc;
function getFunc(diff){
// return function for moving items (previous or next)
return (diff > 0) ? clickNext : clickPrev;
}
if (!d) {
var ind = $(e.target).parents('li').index();
diff = ind - $items.eq(itemsOrder[middleIndex-1]).index();
} else {
diff = parseInt(d);
}
wayFunc = getFunc(diff);
diff = Math.abs(diff);
while (diff > 0){
wayFunc();
diff--;
}
}
// move by index
function goTo(e, d){
if (d) {
d = parseInt(d);
$items.eq(d) && $a.eq(d).trigger('click');
}
}
// item click listener
function clickItem(e){
var objClasses = $(this).parent().attr('class').split(' ');
for (var i=0;i < objClasses.length;i++){
if ($.inArray(objClasses[i], getObject.clickableClasses)) {
go(e);
return false;
} else {
go(e);
}
}
}
// initialize pagination
function paginationInit(){
getObject.pagination = $.extend(paginationDefaults, o.pagination);
if (getObject.pagination.enable) {
getObject.pagination.$this = $(getObject.pagination.paginationClass)
getObject.pagination.$li = getObject.pagination.$this.find('li');
getObject.pagination.$a = getObject.pagination.$this.find('a');
getObject.pagination.$a
.on('click', function(e){
clickItem(e);
return false;
});
}
}
// set active pagination item
function paginationSet(ind){
if (getObject.pagination.enable && getObject.pagination.$a) {
var $li = getObject.pagination.$li,
actClass = getObject.pagination.activeClass.substr(1);
$li
.siblings().removeClass(actClass).end()
.eq(ind-1).addClass(actClass);
}
}
// autoplay start function
function startAutoplay(){
getObject.autoplay.id = setTimeout(function(){
if(getObject.autoplay.enable){
next();
startAutoplay();
}
}, getObject.autoplay.timeout);
}
// autoplay start function
function stopAutoplay(){
clearTimeout(getObject.autoplay.id);
}
}
}
data?object=data:$this.data({tm3DCircleCarousel: getObject});
typeof o=='object' && $.extend(getObject, o);
getObject.me || getObject.constructor(getObject.me=$this);
});
}
})(jQuery);