(function($){
$.fn.slides = function( option ) {
option = $.extend( {}, $.fn.slides.option, option );
return this.each(function(){
$('.' + option.container, $(this)).children(".par").wrapAll('<div class="slides_control"/>');
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, pauseTimeout, playInterval;
if (total < 2) {
$('.' + option.container, $(this)).fadeIn(option.fadeSpeed, option.fadeEasing, function(){
loaded = true;
option.slidesLoaded();
});
$('.' + option.next + ', .' + option.prev).fadeOut(0);
return false;
}
function animate(direction, effect, clicked) {
if (!active && loaded) {
active = true;
option.animationStart(current + 1);
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.'+ option.currentClass +' 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') {
if (option.crossfade) {
control.children(':eq('+ next +')', elem).css({
zIndex: 10
}).fadeIn(option.fadeSpeed, option.fadeEasing, 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 {
control.children(':eq('+ prev +')', elem).fadeOut(option.fadeSpeed,  option.fadeEasing, function(){
if (option.autoHeight) {
control.animate({
height: control.children(':eq('+ next +')', elem).outerHeight()
}, option.autoHeightSpeed,
function(){
control.children(':eq('+ next +')', elem).fadeIn(option.fadeSpeed, option.fadeEasing);
});
} else {
control.children(':eq('+ next +')', elem).fadeIn(option.fadeSpeed, option.fadeEasing, 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) {
control.animate({
left: direction,
height: control.children(':eq('+ next +')').outerHeight()
},option.slideSpeed, option.slideEasing, 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 {
control.animate({
left: direction
},option.slideSpeed, option.slideEasing, 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.' + option.currentClass, elem).removeClass(option.currentClass);
$('.' + option.paginationClass + ' li:eq('+ next +')', elem).addClass(option.currentClass);
}
}
} // end animate function
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();
}
}
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:eq(' + start + ')').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, option.fadeEasing, function(){
$(this).css({
zIndex: 5
});
$('.' + option.container, elem).css({
background: ''
});
loaded = true;
option.slidesLoaded();
});
});
} else {
control.children(':eq(' + start + ')').fadeIn(option.fadeSpeed, option.fadeEasing, function(){
loaded = true;
option.slidesLoaded();
});
}
if (option.bigTarget) {
control.children().css({
cursor: 'pointer'
});
control.children().click(function(){
animate('next', effect);
return false;
});
}
if (option.hoverPause && option.play) {
control.bind('mouseover',function(){
stop();
});
control.bind('mouseleave',function(){
pause();
});
}
if (option.generateNextPrev) {
$('.' + option.container, elem).after('<a href="#" class="'+ option.prev +'">Prev</a>');
$('.' + option.prev, elem).after('<a href="#" class="'+ option.next +'">Next</a>');
}
$('.' + 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) {
if (option.prependPagination) {
elem.prepend('<ul class='+ option.paginationClass +'></ul>');
} else {
elem.append('<ul class='+ option.paginationClass +'></ul>');
}
control.children().each(function(){
$('.' + option.paginationClass, elem).append('<li><a href="#'+ number +'">'+ (number+1) +'</a></li>');
number++;
});
} else {
$('.' + option.paginationClass + ' li a', elem).each(function(){
$(this).attr('href', '#' + number);
number++;
});
}
$('.' + option.paginationClass + ' li:eq('+ start +')', elem).addClass(option.currentClass);
$('.' + 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);
}
});
};
$.fn.slides.option = {
preload: false, // boolean, Set true to preload images in an image based slideshow
preloadImage: '/img/loading.gif', // string, Name and location of loading image for preloader. Default is "/img/loading.gif"
container: 'slides_container', // string, Class name for slides container. Default is "slides_container"
generateNextPrev: false, // boolean, Auto generate next/prev buttons
next: 'next', // string, Class name for next button
prev: 'prev', // string, Class name for previous button
pagination: true, // boolean, If you're not using pagination you can set to false, but don't have to
generatePagination: true, // boolean, Auto generate pagination
prependPagination: false, // boolean, prepend pagination
paginationClass: 'pagination', // string, Class name for pagination
currentClass: 'current', // string, Class name for current class
fadeSpeed: 350, // number, Set the speed of the fading animation in milliseconds
fadeEasing: '', // string, must load jQuery's easing plugin before http://gsgd.co.uk/sandbox/jquery/easing/
slideSpeed: 350, // number, Set the speed of the sliding animation in milliseconds
slideEasing: '', // string, must load jQuery's easing plugin before http://gsgd.co.uk/sandbox/jquery/easing/
start: 1, // number, Set the speed of the sliding animation in milliseconds
effect: 'slide', // string, '[next/prev], [pagination]', e.g. 'slide, fade' or simply 'fade' for both
crossfade: false, // boolean, Crossfade images in a image based slideshow
randomize: false, // boolean, Set to true to randomize slides
play: 0, // number, Autoplay slideshow, a positive number will set to true and be the time between slide animation in milliseconds
pause: 0, // number, Pause slideshow on click of next/prev or pagination. A positive number will set to true and be the time of pause in milliseconds
hoverPause: false, // boolean, Set to true and hovering over slideshow will pause it
autoHeight: false, // boolean, Set to true to auto adjust height
autoHeightSpeed: 350, // number, Set auto height animation time in milliseconds
bigTarget: false, // boolean, Set to true and the whole slide will link to next slide on click
animationStart: function(){}, // Function called at the start of animation
animationComplete: function(){}, // Function called at the completion of animation
slidesLoaded: function() {} // Function is called when slides is fully loaded
};
$.fn.randomize = function(callback) {
function randomizeOrder() { return(Math.round(Math.random())-0.5); }
return($(this).each(function() {
var $this = $(this);
var $children = $this.children(".par");
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);

