﻿/* ***********************************************************************************

	CJ Simple Slideshow jQuery Plugin
	Written by: Doug Jones (www.cjboco.com)
	
	Copyright (c) 2011, Creative Juices Bo. Co. All rights reserved.
	
	Redistribution and use in source and binary forms, with or without
	modification, are permitted provided that the following conditions
	are met:
	
	a) Redistributions of source code must retain the above copyright
	   notice, this list of conditions and the following disclaimer.
	  
	b) Redistributions in binary form must reproduce the above copyright
	   notice, this list of conditions and the following disclaimer in the
	   documentation and/or other materials provided with the distribution. 
	  
	c) Neither the name of the Creative Juices, Bo. Co. nor the names of its
	   contributors may be used to endorse or promote products derived from
	   this software without specific prior written permission.
	
	THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
	"AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
	LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
	A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
	OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
	SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
	LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
	DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
	THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
	(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
	OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
	
	Version History
	
	3.1 	(02-24-2011) - Added trigger method to manual set slide.
				Added better argument handling for methods.
				Removed coded styling for captions & pause. (Use CSS instead)
				Added pagination for slides.
	3.0 	(01-08-2011) - Complete rewrite of the plugin structure. 
				Added better link detection.
				Using jQuery to handle dissolves now (Dissolve amount
				is now tied in directly with fadeIn/fadeOut).
				Added pause options.
	2.1.1 	(06-27-2009) - Fixed the IE bugs.
	2.1 	(06-24-2009) - Stripped out a lot of the style features.
				It really didn't make sense to do it here, since it
				could be done easily with CSS. Plus is was causing
				issues with IE.
	2.0 	(06-14-2009) - Converted it to a JQuery plug-in.
	1.0 	(10-02-2006) - Initial release.

*********************************************************************************** */
(function (jQuery) {
	jQuery.fn.extend({

		cjSimpleSlideShow: function (opts) {

			var methods = {

				// sets the slide and handle the dissolve
				setSlide: function (jQueryobj) {
					var data = jQueryobj.data('cj'),
						o = data.options,
						s = data.sys,
						jQueryshow = jQueryobj.find(".cj_slideshow_slide"),
						jQueryslide = jQueryshow.eq(s.current),
						jQueryslideNext = jQueryshow.eq(s.current + 1).length > 0 ? jQueryshow.eq(s.current + 1) : jQueryshow.eq(0);
					if (s.inited && s.started) {
						jQueryslide.stop().fadeOut(o.dissolve);
						jQueryslideNext.stop().fadeIn(o.dissolve, function () {
							s.current = jQueryshow.eq(s.current + 1).length > 0 ? s.current + 1 : 0;
							if (!s.paused) {
								s.timer = window.setTimeout(function () {
									methods.setSlide(jQueryobj);
								}, o.delay);
							}
						});
					}
				},
				
				triggerSlide: function (jQueryobj, idx) {
					var data = jQueryobj.data('cj'),
						o = data.options,
						s = data.sys,
						jQueryshow = jQueryobj.find(".cj_slideshow_slide"),
						jQueryslide = jQueryshow.eq(s.current),
						jQueryslideNext = jQueryshow.eq(idx).length > 0 ? jQueryshow.eq(idx) : jQueryshow.eq(s.current + 1).length > 0 ? jQueryshow.eq(s.current + 1) : jQueryshow.eq(0);
					if (typeof idx === "number" && s.inited && s.started && idx !== s.current) {
						window.clearTimeout(s.timer);
						jQueryslide.stop().fadeOut(o.dissolve);
						jQueryslideNext.stop().fadeIn(o.dissolve, function () {
							s.current = jQueryshow.eq(idx).length > 0 ? idx : 0;
							if (!s.paused) {
								s.timer = window.setTimeout(function () {
									methods.setSlide(jQueryobj);
								}, o.delay);
							}
						});
					}
				},

				// pauses the slideshow
				pause: function (jQueryobj) {
					var data = jQueryobj.data('cj'),
						s = data.sys;
					if (s.started) {
						if (s.timer) {
							window.clearTimeout(s.timer);
							s.timer = null;
						}
						jQueryobj.find(".cj_slideshow_pause").stop().fadeIn("fast");
						s.paused = true;
					}
				},

				// resumes the slideshow
				resume: function (jQueryobj) {
					var data = jQueryobj.data('cj'),
						o = data.options,
						s = data.sys;
					if (s.started) {
						jQueryobj.find(".cj_slideshow_pause").stop().fadeOut("fast");
						s.timer = window.setTimeout(function () {
							methods.setSlide(jQueryobj);
						}, o.delay);
						s.paused = false;
					}
				},

				// starts the slideshow
				start: function (jQueryobj) {
					var data = jQueryobj.data('cj'),
						o = data.options,
						s = data.sys;
					if (s.inited) {
						s.timer = window.setTimeout(function () {
							methods.setSlide(jQueryobj);
						}, o.delay);
						s.started = true;
					}
				}

			};

			// handle calls to our methods
			if (typeof opts === "string" && methods[opts]) {

				var jQueryobj = jQuery(this),
					data = jQueryobj.data('cj'),
					s = data.sys;

				// accessible methods
				if (s.inited) {
					if (arguments[1]) {
						return methods[opts](jQueryobj, arguments[1]);
					} else {
						return methods[opts](jQueryobj);
					}
				}

			// call to initialize
			} else if (typeof opts === "object" || !opts) {

				// plugin main
				return this.each(function () {

					var jQueryobj = jQuery(this),
						data = jQueryobj.data('cj'),
						o, s;

					// set our options (if not set)
					if (!data) {
						jQueryobj.data('cj', {
							sys: {
								version: '3.0',
								timer: null,
								current: 0,
								paused: false,
								inited: false,
								started: false
							},
							options: {
								autoRun: true,
								delay: 5000,
								dissolve: 500,
								showCaptions: false,
								centerImage: false,
								paginate: false,
								paginateMax: 5,
								paginatePause: false,
								allowPause: false,
								pauseText: "Paused"
							}
						});
						data = jQueryobj.data('cj');
					}

					// make sure we aren't already inited and started
					if (!data.sys.inited && !data.sys.started) {

						// user defined options
						if (opts) {
							data.options = jQuery.extend(data.options, opts);
						}
						// simplify our data variables
						o = data.options;
						s = data.sys;
						// the dissolve can't be greater than the delay.
						if (typeof o.dissolve === "number" && typeof o.delay === "number" && o.dissolve > o.delay) {
							o.dissolve = o.delay;
						}
						// init: do a check of the slide count, no slideshows of 1!
						if (jQueryobj.find("img").length > 1) {
							var jQuerywrap = jQuery("<div>").css({
									"position": "relative",
									"display": "block",
									"width": jQueryobj.width() + "px",
									"height": jQueryobj.height() + "px",
									"overflow": "hidden",
									"cursor": "pointer"
								}).addClass("cj_slideshow_wrapper"),
								jQuerypage;
							// add pagination?
							if (o.paginate) {
								jQuerypage = jQuery('<div>').addClass('cj_slideshow_pagination');
								jQuerywrap.append(jQuerypage);
							}
							// find all the IMG tags (plus A tags)
							jQueryobj.find("img").each(function (a, b) {
								var jQueryimg = jQuery(b),
									jQueryslide = jQuery('<div>').css({
										"position": "absolute",
										"top": "0px",
										"left": "0px",
										"display": a > 0 ? "none" : "block",
										"width": jQueryobj.width() + "px",
										"height": jQueryobj.height() + "px"
									}).addClass("cj_slideshow_slide"),
									jQuerynum;
								// handle any links wrapping out image
								if (jQueryimg.parent().get(0).nodeName === "A") {
									var url = jQueryimg.parent().attr("href");
									jQueryslide.bind("click", function () {
										document.location.href = url;
										return false;
									});
								}
								// set up pause?
								if (o.allowPause) {
									jQueryslide.bind("mouseenter", function () {
										methods.pause(jQueryobj);
									}).bind("mouseleave", function () {
										methods.resume(jQueryobj);
									});
								}
								// center our image?
								if (o.centerImage) {
									jQueryimg.css({
										"position": "absolute",
										"top": o.centerImage ? parseInt((jQueryobj.height() - jQueryimg.height()) / 2, 10) + "px" : "0px",
										"left": o.centerImage ? parseInt((jQueryobj.width() - jQueryimg.width()) / 2, 10) + "px" : "0px"
									});
								}
								// apppend the image to our slide
								jQueryslide.append(jQueryimg);
								// do we need to show captions?
								if (o.showCaptions && jQueryimg.attr("alt").length > 0) {
									var jQuerycaption = jQuery("<span>").addClass("cj_slideshow_caption").html(jQueryobj.find('.slideCaption[data-idx="' + jQueryimg.attr('data-idx')  + '"]').html());
									jQueryobj.find('.slideCaption[data-idx="' + jQueryimg.attr('data-idx')  + '"]').remove();
									jQueryslide.append(jQuerycaption);
								}
								// should we paginate?
								if (o.paginate) {
									jQuerynum = jQuery('<div>').addClass('cj_slideshow_page');
									jQuerynum.append(jQuery('<a>').bind('click', function() {
										// do we need to pause the slideshow on click?
										if (o.allowPause && o.paginatePause) {
											methods.pause(jQueryobj);
										}
										methods.triggerSlide(jQueryobj, a);
										return false;
									}).html(a + 1));
									jQuerypage.append(jQuerynum);
								}
								// add the slide to the wrapper
								jQuerywrap.append(jQueryslide);
							});
							// prepare the elemnt to show the slides
							jQueryobj.html("").append(jQuerywrap);
							// do we need to add the pause?
							if (o.allowPause && o.pauseText.length > 0) {
								var jQuerypause = jQuery("<div>").addClass("cj_slideshow_pause").html(o.pauseText);
								jQueryobj.append(jQuerypause);
							}
							s.inited = true;
						} else {
							s.inited = false;
						}
						// autostart if option set and we are inited
						if (s.inited && o.autoRun) {
							methods.start(jQueryobj);
						}

					}
				});

			// unknown call to our plugin
			} else {
			
				jQuery.error('Method ' + opts + ' does not exist on jQuery.cjSimpleSlideShow');

			}
		}
	});
}(jQuery));
