/*globals clearTimeout,jQuery,setTimeout */
/* ***********************************************************************************
 
	 CJ Simpler Slideshow JavaScript framework
	 
	 Copyright (c) 2010, Doug Jones. 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.
	 
	 For further information, visit the Creative Juices website: www.cjboco.com.
	 
	 Version History
	 
	 1.0 (04-05-2010) - Initial release.
	 					(A simpler version based off of CJ Simple Slideshow 2.1.1)
 
 *********************************************************************************** */
(function ($) {
	$.fn.cjSimplerSlideShow = function (options) {

		var settings = {
			// user editable settings
			autoRun: true,
			delay: 3000,
			dissolve: 70,
			showCaptions: false
		};

		var sys = {
			// function parameters
			version: '1.0',
			elem: null,
			slides: [],
			captions: [],
			links: [],
			current: 0,
			timer: null
		};

		/* 
		 sets a slides opacity
		 ***************************************/

		function setOpacity(o) {
			if (parseFloat(o.t) > 1) {
				o.t = 1.0;
				return;
			}
			$(o).css({
				opacity: parseFloat(o.t),
				MozOpacity: parseFloat(o.t),
				filter: "alpha(opacity=" + (parseFloat(o.t) * 100) + ")"
			});
		}

		/* 
		 sets the slide and handles dissolves
		 ***************************************/

		function setSlide() {
			var o = parseFloat(sys.slides[sys.current].t);
			var x = sys.slides[sys.current + 1] ? sys.current + 1 : 0;
			var no = parseFloat(sys.slides[x].t);
			o -= 0.05;
			no += 0.05;
			$(sys.slides[x]).css({
				display: "block"
			});
			sys.slides[sys.current].t = parseFloat(o);
			sys.slides[x].t = parseFloat(no);
			setOpacity(sys.slides[sys.current]);
			setOpacity(sys.slides[x]);
			if (sys.captions[sys.current] && sys.captions[x]) {
				$(sys.captions[x]).css({
					display: "block"
				});
				sys.captions[sys.current].t = parseFloat(o);
				sys.captions[x].t = parseFloat(no);
				setOpacity(sys.captions[sys.current]);
				setOpacity(sys.captions[x]);
			}
			if (sys.links[sys.current] !== null) {
				// cursor: "pointer"
			}
			if (o <= 0) {
				$(sys.slides[sys.current]).css({
					display: "none"
				});
				if (sys.captions[x]) {
					$(sys.captions[x]).css({
						display: "block"
					});
					$(sys.captions[sys.current]).css({
						display: "none"
					});
				}
				sys.current = x;
				sys.timer = setTimeout(setSlide, settings.delay);
			} else {
				sys.timer = setTimeout(setSlide, settings.dissolve);
			}
		}

		/* 
		 initialize the slideshow & slides
		 ***************************************/

		function init() {

			// need to make sure the positioning is set for the main container.
			// it needs to be set to anything other than STATIC
			if ($(sys.elem).css("position") === "" || $(sys.elem).css("position") === "static") {
				$(sys.elem).css("position", "relative");
			}

			// create our main slideshow wrapper
			var wrapper = $("<div>").css({
				display: "block",
				width: "0px",
				height: "0px",
				position: "absolute",
				overflow: "hidden"
			}).addClass("cj_slideshow_wrapper");

			// loop through the images and initialize some settings
			$(sys.elem).find("img").each(function () {
				var slide = $(this)[0].parentNode.nodeName === "A" ? $(this).parent()[0] : $(this)[0],
					slideImg = $(this)[0];
				// set positioning
				$(slide).css({
						display: "none",
						position: "absolute",
						top: "0px",
						left: "0px",
						"z-index": "1"
					});
				// add slides
				sys.slides.push(slide);
				sys.slides[sys.slides.length - 1].t = 0.0;
				$(slide).appendTo($(wrapper));
				// captions?
				if (settings.showCaptions) {
						if ($(slideImg).attr("alt") !== "") {
							var cap = $("<span>").css({
								position: "absolute",
								display: "none",
								width: "100%",
								height: "auto"
							}).addClass("cj_slideshow_caption_wrapper").html($(slideImg).attr("alt"));
							$(wrapper).append(cap);
							sys.captions.push(cap);
							sys.captions[sys.captions.length - 1].t = 0.0;
						} else {
							sys.captions.push(null);
							sys.captions[sys.captions.length - 1].t = 0.0;
						}
					}
			});
			$(sys.elem).append($(wrapper));
			$(sys.elem).find("br").remove();
			$(sys.elem).css({
				display: "block"
			});

			// make the first slide and/or caption visisable
			if (sys.slides.length > 0) {
				$(sys.slides[0]).css({
					display: "block"
				});
				sys.slides[0].t = 1.0;
				if (sys.captions[0]) {
					$(sys.captions[0]).css({
						display: "block"
					});
					sys.captions[0].t = 1.0;
				}
				// show our main elem, if it was hidden
				$(sys.elem).find(".cj_slideshow_wrapper").css({
					width: $(sys.elem).width(),
					height: $(sys.elem).height()
				});
			}

		}

		function start() {
			sys.timer = setTimeout(setSlide, settings.delay);
		}

		/* 
		 set up any user passed variables
		 ***************************************/
		if (options) {
			$.extend(settings, options);
		}

		/* 
		 begin
		 ***************************************/
		return this.each(function () {
			sys.elem = this;
			init();
			if (settings.autoRun) {
				start();
			}
		});

	};
})(jQuery);
