jquery plugin template

quite good jquery plugin template

http://www.whatheaven.com/fileupload/upload/jquery_slideshow.js


/*
* jQuery Slideshow Plug-in
*
* Writen By Chan Chun Yin
*
* Date: 2011-04-14
* Version: 1.0.0
*/
(function($){

$.fn.slideshow = function (options) {
var method = typeof arguments[0] == "string" && arguments[0];
var args = method && Array.prototype.slice.call(arguments, 1) || arguments;
// get a reference to the first slideshow found
var self = (this.length == 0) ? null : $.data(this[0], "slideshow");

// if a method is supplied, execute it for non-empty results
if( self && method && this.length ){

// if request a copy of the object, return it
if( method.toLowerCase() == "object" ) return self;
// if method is defined, run it and return either it's results or the chain
else if( self[method] ){
// define a result variable to return to the jQuery chain
var result;
this.each(function (i){
// apply the method to the current element
var r = $.data(this, "slideshow")[method].apply(self, args);
// if first iteration we need to check if we're done processing or need to add it to the jquery chain
if( i == 0 && r ){
// if this is a jQuery item, we need to store them in a collection
if( !!r.jquery ){
result = $([]).add(r);
// otherwise, just store the result and stop executing
} else {
result = r;
// since we're a non-jQuery item, just cancel processing further items
return false;
}
// keep adding jQuery objects to the results
} else if( !!r && !!r.jquery ){
result = result.add(r);
}
});

// return either the results (which could be a jQuery object) or the original chain
return result || this;
// everything else, return the chain
} else return this;
// initializing request
} else {
// create a new slideshow for each object found
return this.each(function (){
new $.Slideshow(this, options);
});
};
}

$.Slideshow = function (slideshow, options){
options = $.extend({}, $.Slideshow.defaults, options);

var self = this;
var $slideshow = $(slideshow);
var $divs = $slideshow.find("> div");
var current = 0;
var hard_paused = false;
var paused = false;
var isScroll = true;
var loop_count = 0;
var t = 0;

// store a reference to this slideshow
$.data($slideshow[0], "slideshow", self);

// the scrolling items init position
var params = {
left: (options.direction == "rtl" ? "+" : "-") + ($slideshow.innerWidth()) + "px"
};

// pause the slideshow
this.pause = function (){
// mark as hard pause (no resume on hover)
hard_paused = true;
// pause scrolling
pause();
}

// resume the slideshow
this.resume = function (){
// mark as hard pause (no resume on hover)
hard_paused = false;
// resume scrolling
resume();
}

// show next slide
this.next = function (){
if (t > 0) {
clearTimeout(t);
t = 0;
options.direction = "rtl";
scroll(0);
} else if (paused) {
paused = false;
scroll(0);
}
}

// show previous slide
this.previous = function (){
if (t > 0) {
clearTimeout(t);
t = 0;
options.direction = "ltr";
slideTo(current);
} else if (paused) {
paused = false;
options.direction = "ltr";
slideTo(current);
}
}

// go to target slide
this.slideTo = function (index){
if ( (index-1 != current) && (t > 0) ) {
clearTimeout(t);
options.direction = (current > index-1)?"ltr":"rtl";
slideTo(index);
} else if (paused) {
paused = false;
options.direction = (current > index-1)?"ltr":"rtl";
slideTo(index);
}
}

function initPosition() {
$divs.css(params);
$divs.eq(current).css("left", 0);

$("img[id^='btn_']").attr("src", "images/dot.gif");
$("img[id='btn_"+(current+1)+"']").attr("src", "images/dot_on.gif");
}

function scroll(delay) {
// if paused, stop processing
if ( paused ) return false;

if ( delay == null )
delay = options.delayShowNext?options.pauseTime:0; // get the delay speed

var $div = $divs.eq(current); // get the item which needed to scroll
var $divNext = $divs.eq((current+1)%$divs.length); // get the next item which needed to scroll

var slideshowWidth = $slideshow.innerWidth(); // get the slideshow area width

t = setTimeout(function () {
clearTimeout(t);
t = 0;
$divs.draggable( "option", "delay", 2000 );

if (options.direction == "rtl") {
var width = $div.outerWidth(); // get the scroll item width
var endPos = (width * -1); // get the end position
var curPos = parseInt($div.css("left"), 10); // get the current position
var scrollSpeed = curPos * options.scrollSpeed;

$divNext.css({"left": ($slideshow.innerWidth()) + "px"});
var nextWidth = $divNext.outerWidth(); // get the scroll item width
var nextEndPos = 0; // get the end position
var nextCurPos = parseInt($divNext.css("left"), 10); // get the current position
var nextScrollSpeed = nextCurPos * options.scrollSpeed;
/*
$div.animate({left: endPos + "px"}, scrollSpeed, "linear", function (){ finish($div); });
$divNext.animate({left: nextEndPos + "px"}, nextScrollSpeed, "linear");
*/
$div.animate({left: endPos + "px"}, options.scrollSpeed, "swing", function (){ finish($div); });
$divNext.animate({left: nextEndPos + "px"}, options.scrollSpeed, "swing");

} else if (options.direction == "ltr") {
var width = $div.outerWidth(); // get the scroll item width
var endPos = width; // get the end position
var curPos = parseInt($div.css("left"), 10); // get the current position
//var scrollSpeed = (width + curPos) * options.scrollSpeed;

$divNext.css({"left": "-" + ($slideshow.innerWidth()) + "px"});
var nextWidth = $divNext.outerWidth(); // get the scroll item width
var nextEndPos = 0; // get the end position
var nextCurPos = parseInt($divNext.css("left"), 10); // get the current position
//var nextScrollSpeed = (nextWidth + nextCurPos) * options.scrollSpeed;

$div.animate({left: endPos + "px"}, options.scrollSpeed, "swing", function (){ finish($div); });
$divNext.animate({left: nextEndPos + "px"}, options.scrollSpeed, "swing");
}
}, delay);
}

function slideTo(index) {
index = index-1>-1?((index-1)%$divs.length):$divs.length-1;

var $div = $divs.eq(current); // get the item which needed to scroll
var $divTarget = $divs.eq(index); // get the item which needed to scroll

t = setTimeout(function () {
clearTimeout(t);
t = 0;
$divs.draggable( "option", "delay", options.scrollSpeed );

if (options.direction == "rtl") {
var width = $div.outerWidth(); // get the scroll item width
var endPos = (width * -1); // get the end position
var curPos = parseInt($div.css("left"), 10); // get the current position
//var scrollSpeed = (width + curPos) * options.scrollSpeed;

$divTarget.css({"left": ($slideshow.innerWidth()) + "px"});
var nextWidth = $divTarget.outerWidth(); // get the scroll item width
var nextEndPos = 0; // get the end position
var nextCurPos = parseInt($divTarget.css("left"), 10); // get the current position
//var nextScrollSpeed = (nextWidth + nextCurPos) * options.scrollSpeed;

$div.animate({left: endPos + "px"}, options.scrollSpeed, "swing", function (){ finishSlideTo(index); });
$divTarget.animate({left: nextEndPos + "px"}, options.scrollSpeed, "swing");

} else if (options.direction == "ltr") {
var width = $div.outerWidth(); // get the scroll item width
var endPos = width; // get the end position
var curPos = parseInt($div.css("left"), 10); // get the current position
//var scrollSpeed = (width + curPos) * options.scrollSpeed;

$divTarget.css({"left": "-" + ($slideshow.innerWidth()) + "px"});
var nextWidth = $divTarget.outerWidth(); // get the scroll item width
var nextEndPos = 0; // get the end position
var nextCurPos = parseInt($divTarget.css("left"), 10); // get the current position
//var nextScrollSpeed = (nextWidth + nextCurPos) * options.scrollSpeed;

$div.animate({left: endPos + "px"}, options.scrollSpeed, "swing", function (){ finishSlideTo(index); });
$divTarget.animate({left: nextEndPos + "px"}, options.scrollSpeed, "swing");
}
}, 0);
}

function dragTo() {
var $div = $divs.eq(current); // get the item which needed to scroll
var $divNext = $divs.eq((current+1)%$divs.length); // get the next item which needed to scroll

t = setTimeout(function () {
clearTimeout(t);
t = 0;
$divs.draggable( "option", "delay", options.scrollSpeed );

if (options.direction == "rtl") {
var width = $div.outerWidth(); // get the scroll item width
var endPos = (width * -1); // get the end position
var curPos = parseInt($div.css("left"), 10); // get the current position
var scrollSpeed = (width - Math.abs(curPos)) * (width / options.scrollSpeed);

$divNext.css({"left": ($slideshow.innerWidth()) + "px"});
var nextWidth = $divNext.outerWidth(); // get the scroll item width
var nextEndPos = 0; // get the end position
var nextCurPos = parseInt($divNext.css("left"), 10); // get the current position
//var nextScrollSpeed = (nextWidth + nextCurPos) * options.scrollSpeed;

$div.animate({left: endPos + "px"}, scrollSpeed, "swing", function (){ finish($div); });
$divNext.animate({left: nextEndPos + "px"}, options.scrollSpeed, "swing");

} else if (options.direction == "ltr") {
var width = $div.outerWidth(); // get the scroll item width
var endPos = width; // get the end position
var curPos = parseInt($div.css("left"), 10); // get the current position
var scrollSpeed = (width - curPos) * (width / options.scrollSpeed);

$divNext.css({"left": "-" + ($slideshow.innerWidth()) + "px"});
var nextWidth = $divNext.outerWidth(); // get the scroll item width
var nextEndPos = 0; // get the end position
var nextCurPos = parseInt($divNext.css("left"), 10); // get the current position
//var nextScrollSpeed = (nextWidth + nextCurPos) * options.scrollSpeed;

$div.animate({left: endPos + "px"}, scrollSpeed, "swing", function (){ finish($div); });
$divNext.animate({left: nextEndPos + "px"}, options.scrollSpeed, "swing");
}
}, 0);
}

function finish($div){
if (!options.autoSlide) {
paused = true;
}
$divs.draggable( "option", "delay", 0 );
$divs.draggable( "option", "revert", true );
// mark that we're done scrolling this element
//$div.css(params);
// show the next message
next();
}

function finishSlideTo(index) {
if (!options.autoSlide) {
paused = true;
}
$divs.draggable( "option", "delay", 0 );
$divs.draggable( "option", "revert", true );
current = index;
options.direction = "rtl";
initPosition();
scroll();
}

function pause() {
// mark the message as paused
paused = true;
clearTimeout(t);
t = 0;
}

function resume() {
// mark the message as resumed
paused = false;
scroll();
}

if( options.pauseOnHover ){
$slideshow.hover(
function (){
// if hard paused, prevent hover events
if ( hard_paused ) return false;
// pause scrolling
pause();
}
, function (){
// if hard paused, prevent hover events
if ( hard_paused ) return false;
// resume scrolling
resume();
}
);
}
/*
if( options.isDraggable ){
$divs.draggable({
axis: "x",
distance: 50,
revert: true,
start: function(event, ui) {
clearTimeout(t);
t = 0;
},
drag: function(event, ui) {
if (ui.position.left < -200 ) { $(this).draggable( "option", "revert", false ); dragTo(); return false; } else if (ui.position.left > 200 ) {
$(this).draggable( "option", "revert", false );
options.direction = "ltr";
dragTo();
return false;
}
},
stop: function(event, ui) {
},
});
}
*/
function previous() {
//options.direction = (options.direction == "rtl")?"ltr":"rtl";
options.direction = "ltr";
current = current-1>-1?((current-1)%$divs.length):$divs.length-1;
initPosition();
scroll();
}

function next() {
options.direction = "rtl";
current = (current+1) % $divs.length;
initPosition();
scroll();
}

initPosition();

if (!options.autoStart) {
paused = true;
}

scroll();
};

$.Slideshow.defaults = {
direction: "rtl", // the direction of the slideshow scroll (can be "ltr" and "rtl" only)
scrollSpeed: 1000, // the speed of the scrolling (must > 1) (smaller number is faster)
pauseTime: 4000, // the time to wait before showing the next message (in millionseconds)
delayShowNext: true, // determine if we needed to wait before showing the next message
autoStart: true, // the slideshow to be shown or not when the page is loaded
autoSlide: true, // the slideshow to be shown or not when the page is loaded
isDraggable: true, // the slide to be drag or not to next/previous slide
pauseOnHover: false, // determine if we should pause on mouse over
loop: -1 // determine how many times to loop through the slideshows ( < 0 = infinite) }; })(jQuery);