(function($)
{
	$.fn.extend(
	{ 
		zAccordion: function(options)
		{
			// options
			var defaults =
			{
				timeout:		4000,		// time between each slide (in ms)
				width:			990,		// width of the container (in px)
				height:			78,		// height of the container (in px)
				slideWidth:		125,		// width of each slide (in px)
				slideHeight:		340,		// height of each slide (in px)
				tabWidth:		100,		// width of each slide's tab (when clicked it opens the slide)
				startingSlide:		0,		// zero-based index of which slide should be displayed
				slideClass:		"slide",	// class of each slide
				slideOpenClass:		"slide-open",	// class of open slides
				slideClosedClass:	"slide-closed",	// class of closed slides
				easing:			null,		// easing method
				speed:			700,		// speed of the slide transition (in ms)
				open:			null,		// callback function for opening slides
				close:			null,		// callback function for closing slides
				trigger:		"mouseover",	// event type that will bind to the tab (click, mouseover, etc.)
				pause:			true,		// pause on hover
				clickx:			null		// function called on click
			};

			// measuring the height
			if ((options.height == undefined) && (options.slideHeight == undefined))
			{
				options.height = defaults.height;
				options.slideHeight = defaults.slideHeight;
			}
			else if ((options.height != undefined) && (options.slideHeight == undefined))
			{
				options.slideHeight = options.height;
			}
			else if ((options.height == undefined) && (options.slideHeight != undefined))
			{
				options.height = options.slideHeight;
			}

			// measure the width
			// nothing is defined
			if ((options.width == undefined) && (options.slideWidth == undefined) && (options.tabWidth == undefined))
			{
				options.width = defaults.width;
				options.tabWidth = defaults.tabWidth;
				options.slideWidth = defaults.slideWidth;
			}
			/* One of three is defined */
			else if ((options.width != undefined) && (options.slideWidth == undefined) && (options.tabWidth == undefined))
			{
				options.tabWidth = 100;
				options.slideWidth = options.width - ((this.children().size() - 1) * options.tabWidth);
			}
			else if ((options.width == undefined) && (options.slideWidth != undefined) && (options.tabWidth == undefined))
			{
				options.width = defaults.width;
				options.tabWidth = (defaults.width - options.slideWidth) / (this.children().size() - 1);
			}
			else if ((options.width == undefined) && (options.slideWidth == undefined) && (options.tabWidth != undefined))
			{
				options.width = defaults.width;
				options.slideWidth  = options.width - ((this.children().size() - 1) * options.tabWidth);
			}
			// two of three are defined
			else if ((options.width != undefined) && (options.slideWidth != undefined) && (options.tabWidth == undefined))
			{
				options.tabWidth = (options.width - options.slideWidth) / (this.children().size() - 1);
			}
			else if ((options.width != undefined) && (options.slideWidth == undefined) && (options.tabWidth != undefined))
			{
				options.slideWidth = options.width - ((this.children().size() - 1) * options.tabWidth);
			}
			else if ((options.width == undefined) && (options.slideWidth != undefined) && (options.tabWidth != undefined))
			{
				options.width = ((this.children().size() - 1) * options.tabWidth) + options.slideWidth;
			}
			defaults.animate	= options.slideWidth - options.tabWidth; // number of pixels yet do be displayed on a hidden slide
			var interval		= null;
			defaults.stop		= false;
			defaults.inside		= false; // determines whether or not the mouse is inside the container.  container should pause on hover if needed.
			defaults.current	= defaults.startingSlide;
			var options		= $.extend(defaults, options);
			this.clickx		= function(num)
			{
				clearTimeout(interval);
				num++;
				if ((num > $(this).children().size()) || (num < 1))
				{
					num = 1;
				}
				$(this).children($(this).children().get(0).tagName + ":nth-child(" + num + ")").trigger(defaults.trigger);
			};
			this.stop = function() // this will stop the accordion unless the slides are clicked, however, it will not resume the autoplay
			{
				clearTimeout(interval);
				defaults.stop = true;
			};
			this.start = function()  // this will start the accordion back up if it has been stopped
			{
				clearTimeout(interval);
				defaults.stop = false;
				var s = $(this).children().get(0).tagName + "." + defaults.slideOpenClass;
				this.clickx($(this).children(s).index() + 1);
			};
			return this.each(function()
			{
				var o = options;
				var obj = $(this);
				var x; // used to set up each slide's position
				// count the number of slides
				var originals = [];
				// loop through each of the slides
				obj.children().each(function(index)
				{
					var z; // used to set the z-index of a slide
					x = index * o.tabWidth; // used for the position of each slide
					originals[index] = x;
					z = index * 10; // increase each slide's z-index by 10 so they sit on top of each other
					$(this).addClass(o.slideClass); // add the slide class to each of the slides
					$(this).css(
					{
						"left": x + "px",
						"top": 0,
						"z-index": z,
						"margin": 0,
						"padding": 0,
						"display": "block",
						"position": "absolute",
						"overflow": "hidden",
						"width": o.slideWidth + "px",
						"height": o.slideHeight + "px",
						"float": "left"
					});
					if (index == (o.startingSlide))
					{
						$(this).addClass(o.slideOpenClass).css("cursor", "pointer");
					}
					else
					{
						$(this).addClass(o.slideClosedClass).css("cursor", "pointer");
						if (index > (o.startingSlide))
						{
							var y = index + 1;
							obj.children(obj.children().get(0).tagName + ":nth-child(" + y + ")").css(
							{
								left: originals[y-1] + o.animate + "px"
							});
						}
					}
				});
				// modify the css of the main container
				obj.css(
				{
					"display": "block",
					"list-style": "none",
					"height": o.height + "px",
					"overflow": "hidden",
					"width": o.width + "px",
					"padding": 0,
					"position": "relative",
					"overflow": "hidden"
				});
				// if the container is a list, get rid of any bullets
				if ((obj.get(0).tagName == "UL") || (obj.get(0).tagName == "OL"))
				{
					obj.css(
					{
						"list-style": "none"
					});
				}
				
				$("#slidercontainer").hover(function()
				{
					obj.fadeIn(700);
				}, function()
				{
					obj.fadeOut(700);
				});
				
				// set up the listener to change slides when clicked
				obj.children(obj.children().get(0).tagName + "." + o.slideClass).bind(o.trigger, function()
				{
					// don't do anything if the slide is already open
					if ($(this).hasClass(o.slideOpenClass))
					{
					}
					// if the slide is not open...
					else
					{
						try
						{
							clearTimeout(interval);
						}
						catch(e)
						{
						}
						
						obj.children(obj.children().get(0).tagName + "." + o.slideClass).addClass(o.slideClosedClass).removeClass(o.slideOpenClass).css("cursor", "pointer"); // remove the open class from all the slide tabs
						$(this).addClass(o.slideOpenClass).removeClass(o.slideClosedClass).css("cursor", "pointer"); // add the open class to the slide tab that was just clicked
						var index = $(this).index() + 1; // the index refers to the actual slide number that was clicked (no zeros)
						if (o.clickx != null)
						{
							o.clickx();
						}
						$(this).click(function()
						{
							$(document).bgStretcher.playNext(index-1);
						});
						interval = setTimeout(function()
						{
							obj.children(obj.children().get(0).tagName + ":nth-child(" + index + ")").animate(
							{ left: originals[index-1] + "px" }, o.speed, o.easing, o.open);
							
							// closing other slides
							for (var i = 1;i <= originals.length;i++)
							{
								if (i < index)
								{
									obj.children(obj.children().get(0).tagName + ":nth-child(" + i + ")").animate(
									{ left: originals[i-1] + "px" }, o.speed, o.easing, o.close);
								}
								if (i > index)
								{
									obj.children(obj.children().get(0).tagName + ":nth-child(" + i + ")").animate(
									{ left: originals[i-1] + o.animate + "px" }, o.speed, o.easing, o.close);
								}
							}
						}, 300 );
						return false; // this is important. if a visible link is clicked within the slide, it will open the slide instead of redirecting the link
					}
				});
			});
		}
	});
})(jQuery);
