/*
* slides, a slideshow plugin for jquery
* intructions: http://slidesjs.com
* by: nathan searles, http://nathansearles.com
* version: 1.1.2
* updated: february 14th, 2011
*/
(function($) {
$.fn.slides = function(option) {
option = $.extend({},
$.fn.slides.option, option);
return this.each(function() {
$('.' + option.container, $(this)).children().wrapall('
');
var elem = $(this),
control = $('.slides_control', elem),
total = control.children().size(),
width = control.children().outerwidth(),
height = control.children().outerheight(),
start = option.start - 1,
effect = option.effect.indexof(',') < 0 ? option.effect: option.effect.replace(' ', '').split(',')[0],
paginationeffect = option.effect.indexof(',') < 0 ? effect: option.effect.replace(' ', '').split(',')[1],
next = 0,
prev = 0,
number = 0,
current = 0,
loaded,
active,
clicked,
position,
direction,
imageparent;
if (total < 2) {
return;
}
if (start < 0) {
start = 0;
};
if (start > total) {
start = total - 1;
};
if (option.start) {
current = start;
};
if (option.randomize) {
control.randomize();
}
$('.' + option.container, elem).css({
overflow: 'hidden',
position: 'relative'
});
control.children().css({
position: 'absolute',
top: 0,
left: control.children().outerwidth(),
zindex: 0,
display: 'none'
});
control.css({
position: 'relative',
width: (width * 3),
height: height,
left: -width
});
$('.' + option.container, elem).css({
display: 'block'
});
if (option.autoheight) {
control.children().css({
height: 'auto'
});
control.animate({
height: control.children(':eq(' + start + ')').outerheight()
},
option.autoheightspeed);
}
if (option.preload && control.find('img').length) {
$('.' + option.container, elem).css({
background: 'url(' + option.preloadimage + ') no-repeat 50% 50%'
});
var img = control.find('img:eq(' + start + ')').attr('src') + '?' + (new date()).gettime();
if ($('img', elem).parent().attr('class') != 'slides_control') {
imageparent = control.children(':eq(0)')[0].tagname.tolowercase();
} else {
imageparent = control.find('img:eq(' + start + ')');
}
control.find('img:eq(' + start + ')').attr('src', img).load(function() {
control.find(imageparent + ':eq(' + start + ')').fadein(option.fadespeed,
function() {
$(this).css({
zindex: 5
});
elem.css({
background: ''
});
loaded = true;
});
});
} else {
control.children(':eq(' + start + ')').fadein(option.fadespeed,
function() {
loaded = true;
});
}
if (option.bigtarget) {
control.children().css({
cursor: 'pointer'
});
control.children().click(function() {
animate('next', effect);
return false;
});
}
if (option.hoverpause && option.play) {
control.children().bind('mouseover',
function() {
stop();
});
control.children().bind('mouseleave',
function() {
pause();
});
}
if (option.generatenextprev) {
$('.' + option.container, elem).after('prev');
$('.' + option.prev, elem).after('next');
}
$('.' + option.next, elem).click(function(e) {
e.preventdefault();
if (option.play) {
pause();
};
animate('next', effect);
});
$('.' + option.prev, elem).click(function(e) {
e.preventdefault();
if (option.play) {
pause();
};
animate('prev', effect);
});
if (option.generatepagination) {
elem.append('');
control.children().each(function() {
$('.' + option.paginationclass, elem).append('' + (number + 1) + '');
number++;
});
} else {
$('.' + option.paginationclass + ' li a', elem).each(function() {
$(this).attr('href', '#' + number);
number++;
});
}
$('.' + option.paginationclass + ' li:eq(' + start + ')', elem).addclass('current');
$('.' + option.paginationclass + ' li a', elem).click(function() {
if (option.play) {
pause();
};
clicked = $(this).attr('href').match('[^#/]+$');
if (current != clicked) {
animate('pagination', paginationeffect, clicked);
}
return false;
});
$('a.link', elem).click(function() {
if (option.play) {
pause();
};
clicked = $(this).attr('href').match('[^#/]+$') - 1;
if (current != clicked) {
animate('pagination', paginationeffect, clicked);
}
return false;
});
if (option.play) {
playinterval = setinterval(function() {
animate('next', effect);
},
option.play);
elem.data('interval', playinterval);
};
function stop() {
clearinterval(elem.data('interval'));
};
function pause() {
if (option.pause) {
cleartimeout(elem.data('pause'));
clearinterval(elem.data('interval'));
pausetimeout = settimeout(function() {
cleartimeout(elem.data('pause'));
playinterval = setinterval(function() {
animate("next", effect);
},
option.play);
elem.data('interval', playinterval);
},
option.pause);
elem.data('pause', pausetimeout);
} else {
stop();
}
};
function animate(direction, effect, clicked) {
if (!active && loaded) {
active = true;
switch (direction) {
case 'next':
prev = current;
next = current + 1;
next = total === next ? 0: next;
position = width * 2;
direction = -width * 2;
current = next;
break;
case 'prev':
prev = current;
next = current - 1;
next = next === -1 ? total - 1: next;
position = 0;
direction = 0;
current = next;
break;
case 'pagination':
next = parseint(clicked, 10);
prev = $('.' + option.paginationclass + ' li.current a', elem).attr('href').match('[^#/]+$');
if (next > prev) {
position = width * 2;
direction = -width * 2;
} else {
position = 0;
direction = 0;
}
current = next;
break;
}
if (effect === 'fade') {
option.animationstart();
if (option.crossfade) {
control.children(':eq(' + next + ')', elem).css({
zindex: 10
}).fadein(option.fadespeed,
function() {
if (option.autoheight) {
control.animate({
height: control.children(':eq(' + next + ')', elem).outerheight()
},
option.autoheightspeed,
function() {
control.children(':eq(' + prev + ')', elem).css({
display: 'none',
zindex: 0
});
control.children(':eq(' + next + ')', elem).css({
zindex: 0
});
option.animationcomplete(next + 1);
active = false;
});
} else {
control.children(':eq(' + prev + ')', elem).css({
display: 'none',
zindex: 0
});
control.children(':eq(' + next + ')', elem).css({
zindex: 0
});
option.animationcomplete(next + 1);
active = false;
}
});
} else {
option.animationstart();
control.children(':eq(' + prev + ')', elem).fadeout(option.fadespeed,
function() {
if (option.autoheight) {
control.animate({
height: control.children(':eq(' + next + ')', elem).outerheight()
},
option.autoheightspeed,
function() {
control.children(':eq(' + next + ')', elem).fadein(option.fadespeed);
});
} else {
control.children(':eq(' + next + ')', elem).fadein(option.fadespeed,
function() {
if ($.browser.msie) {
$(this).get(0).style.removeattribute('filter');
}
});
}
option.animationcomplete(next + 1);
active = false;
});
}
} else {
control.children(':eq(' + next + ')').css({
left: position,
display: 'block'
});
if (option.autoheight) {
option.animationstart();
control.animate({
left: direction,
height: control.children(':eq(' + next + ')').outerheight()
},
option.slidespeed,
function() {
control.css({
left: -width
});
control.children(':eq(' + next + ')').css({
left: width,
zindex: 5
});
control.children(':eq(' + prev + ')').css({
left: width,
display: 'none',
zindex: 0
});
option.animationcomplete(next + 1);
active = false;
});
} else {
option.animationstart();
control.animate({
left: direction
},
option.slidespeed,
function() {
control.css({
left: -width
});
control.children(':eq(' + next + ')').css({
left: width,
zindex: 5
});
control.children(':eq(' + prev + ')').css({
left: width,
display: 'none',
zindex: 0
});
option.animationcomplete(next + 1);
active = false;
});
}
}
if (option.pagination) {
$('.' + option.paginationclass + ' li.current', elem).removeclass('current');
$('.' + option.paginationclass + ' li:eq(' + next + ')', elem).addclass('current');
}
}
};
});
};
$.fn.slides.option = {
preload: false,
preloadimage: '/img/loading.gif',
container: 'slides_container',
generatenextprev: false,
next: 'next',
prev: 'prev',
pagination: true,
generatepagination: true,
paginationclass: 'pagination',
fadespeed: 350,
slidespeed: 350,
start: 1,
effect: 'slide',
crossfade: false,
randomize: false,
play: 0,
pause: 0,
hoverpause: false,
autoheight: false,
autoheightspeed: 350,
bigtarget: false,
animationstart: function() {},
animationcomplete: function() {}
};
$.fn.randomize = function(callback) {
function randomizeorder() {
return (math.round(math.random()) - 0.5);
}
return ($(this).each(function() {
var $this = $(this);
var $children = $this.children();
var childcount = $children.length;
if (childcount > 1) {
$children.hide();
var indices = [];
for (i = 0; i < childcount; i++) {
indices[indices.length] = i;
}
indices = indices.sort(randomizeorder);
$.each(indices,
function(j, k) {
var $child = $children.eq(k);
var $clone = $child.clone(true);
$clone.show().appendto($this);
if (callback !== undefined) {
callback($child, $clone);
}
$child.remove();
});
}
}));
};
})(jquery);