var slideTimer;

function gotoSlide(sPanelID,iSlideRate)
{
	$(function()
	{
		$(".slidePanel").removeClass("onShow");
		$(".slideLink").removeClass("active");
		$(sPanelID).addClass("onShow");
		$("#slideStrip").animate({marginLeft: $(sPanelID).attr("rel")}, iSlideRate, function() { $(".slideLink[rel='"+sPanelID+"']").addClass("active"); } );
	});
}

function autoSlide(iSlideRate,iAutoTimer)
{
	$(function()
	{
		if($(".slidePanel.onShow").attr("id")!=$(".slidePanel:last").attr("id"))
			gotoSlide("#"+$(".slidePanel.onShow").next(".slidePanel").attr("id"),iSlideRate);
		else
			gotoSlide("#"+$(".slidePanel:first").attr("id"),iSlideRate);
		slideTimer = setTimeout("autoSlide("+iSlideRate+","+iAutoTimer+")",iAutoTimer);
	});
}

$(function()
{
	$.fn.cycleSlide = function(options)
	{
		$(this).click(function()
						{
							clearTimeout(slideTimer);
							var iSlideRate = options["slideRate"];
							if($(this).attr("rel")=="prevSlide")
							{
								if($(".slidePanel.onShow").attr("id")!=$(".slidePanel:first").attr("id"))
									gotoSlide("#"+$(".slidePanel.onShow").prev(".slidePanel").attr("id"),options["slideRate"]);
								else
									gotoSlide("#"+$(".slidePanel:last").attr("id"),options["slideRate"]);
							}
							else if($(this).attr("rel")=="nextSlide")
							{
								if($(".slidePanel.onShow").attr("id")!=$(".slidePanel:last").attr("id"))
									gotoSlide("#"+$(".slidePanel.onShow").next(".slidePanel").attr("id"),options["slideRate"]);
								else
									gotoSlide("#"+$(".slidePanel:first").attr("id"),options["slideRate"]);
							}
							if(options["autoSlide"]) slideTimer = setTimeout("autoSlide("+options["slideRate"]+","+options["autoTimer"]+")",options["autoTimer"]);
							return false;
						});
	}
	
	$.fn.pickSlide = function(options)
	{
		$(this).click(function()
						{
							clearTimeout(slideTimer);
							var iSlideRate = options["slideRate"];
							gotoSlide($(this).attr("rel"),options["slideRate"]);
							if(options["autoSlide"]) slideTimer = setTimeout("autoSlide("+options["slideRate"]+","+options["autoTimer"]+")",options["autoTimer"]*options["extTimer"]);
							return false;
						});
	}
	
	$.fn.SlideStrip = function(options)
	{
		var defaults = 	{
							slideRate: 500,
							defaultSlide: 0,
							autoSlide: false,
							autoTimer: 5000,
							extTimer: 1
						};
		options = $.extend(defaults, options); //adds default values to the options array where they haven't been specified
		
		var iPlayWidth = options["playWidth"];
		var iPlayHeight = options["playHeight"];
		var oNavList = $(this).children("ul:first");
		var iSlideCount = oNavList.children("li").length;
		var iDefaultSlide = options["defaultSlide"];
		
		if(iDefaultSlide=="rand") iDefaultSlide = Math.floor(Math.random()*iSlideCount); //picks a random default slide
		
		$(this).css("width",iPlayWidth);
		
		oNavList.addClass("slideList");
		oNavList.children("li").addClass("slideItem");
		$(".slideItem").children("a").addClass("slideLink");
		$(".slideLink").pickSlide({
																slideRate: options["slideRate"],
																autoSlide: options["autoSlide"],
																autoTimer: options["autoTimer"],
																extTimer: options["extTimer"]
															});
		$(".slideLink").each(function(i)
												{
													$($(this).attr("rel")).addClass("slidePanel");
												}); //finds the panels that are linked from a tags and adds the slidePanel Class 
												
		$(".slidePanel").css("width",iPlayWidth);
		$(".slidePanel").css("height",iPlayHeight);
		$(".slidePanel").each(function(i)
													{
														$(this).attr("rel",-iPlayWidth*i);
													});
		
		$(".slidePanel").wrapAll("<div id=\"slideStrip\"></div>");

		$("#slideStrip").css("width",iPlayWidth*iSlideCount);
		$("#slideStrip").css("height",iPlayHeight);
		
		$("#slideStrip").after("<div id=\"cycleSlide\"><a id=\"prevSlide\" href=\"#prevSlide\" rel=\"prevSlide\" class=\"cycleSlide\"><span>Prev</span></a><a id=\"nextSlide\" href=\"#nextSlide\" rel=\"nextSlide\" class=\"cycleSlide\"><span>Next</span></a></div>");
		$("#cycleSlide").css("width",iPlayWidth);
		$("#cycleSlide").css("marginTop",-iPlayHeight);
		$(".cycleSlide").cycleSlide({
										slideRate: options["slideRate"],
										autoSlide: options["autoSlide"],
										autoTimer: options["autoTimer"]
									});
		$(".slideLink:eq("+iDefaultSlide+")").addClass("active");
		$($(".slideLink.active").attr("rel")).addClass("onShow");
		$("#slideStrip").css("marginLeft",parseFloat($(".onShow").attr("rel")));
		
		if(options["autoSlide"]) slideTimer = setTimeout("autoSlide("+options["slideRate"]+","+options["autoTimer"]+")",options["autoTimer"]);
	}
});
