(function($) {
	$.fn.carouselslide = function(){
		var slidenum = 0;
		$.each($(this).find('li'), function() {
			// adds the list items to the carousel
			$('ul#carousel-slides').append('<li id="slide' + slidenum + '"><img id="slideimg' + slidenum + '" src="' + $(this).children(':first').attr('href') + '" /><span>' + $(this).find('span').html() + '</span></li>');
			
			// hides everything in the carousel: li, image, span
			$('li#slide' + slidenum).css({'opacity': '0'});
			$('li#slide' + slidenum + ' span').css({'display':'none'});
			$('img#slideimg' + slidenum).css({'opacity':'0'});
			
			// binds the unload feature to preload the data.
			$('img#slideimg' + slidenum).bind('load', slideloaded)
			
			// names the btn in the tabs list
			// binds the show slide funtion to the btn
			$(this).children(':first').attr({id: 'slidebtn' + slidenum}).bind('click', slidebtn);
			
			slidenum += 1;
		});
		$('#carousel-tabs').prepend('<li id="slidecon"><a class="slide-playing" href="#"><img src="bsws-carousel/bsws-carousel-tab-stopbtn.png" alt="STOP"></a></li>');
		slidenum -= 1;
		
		//displays the first slide
		slideshow(0);
		
		
		$('a#slidebtn0').addClass('slide-act');
		$('#carousel-tabs').css({'z-index': '500'});
		
		// pause play btn
		$('#slidecon a').bind('click', slideplaypause);
		
		var slidetimer = $.timer(7000, slidenext);
		
		function slideloaded(){			
			if ($(this).parent().hasClass('slideshown') == true){
				$(this).fadeTo('fast', 1);
			} else {
				$(this).css({'opacity': '1'});
			}
			$(this).parent().css({'background': 'none'});
		}
		
		//preloads the play button image
		var loaderplay = new Image();
		loaderplay.src = "bsws-carousel/bsws-carousel-tab-playbtn.png";
		
		function slidebtn(){
			if (!$(this).hasClass('slide-act')) {
				var tslide = $(this).attr('id');
				slidecur = parseInt(tslide.substr(8));
				slideshow(slidecur);
				
				//stops the timer
				slidetimer.stop();
				$('#slidecon a').html('<img src="bsws-carousel/bsws-carousel-tab-playbtn.png" alt="PLAY">');
				$('#slidecon a').removeClass('slide-playing');
			}
			return false;
		}
		
		function slidenext(){
			//the slide number to show
			var tslide = parseInt($('a.slide-act').attr('id').substr(8));
			
			tslide += 1;
			if (tslide > slidenum){
				tslide = 0;
			}
			slideshow(tslide);
		}
		
		function slideshow(tslide){
			$('ul#carousel-slides li').not('#slide' + tslide).stop().css({'z-index':'300'}).removeClass('slideshown').fadeTo('fast', 0, function(){
				$(this).find('span').css({'display':'none', 'z-index':'300'});
			});
			
			$('li#slide' + tslide).stop().css({'z-index':'400'}).addClass('slideshown').fadeTo('slow', 1, function(){
				$('li#slide' + tslide + ' span').slideDown('slow');
			}).find('span').css({'opacity':'1', 'z-index':'401'});
			
			$('.slide-act').removeClass('slide-act');
			$('a#slidebtn' + tslide).addClass('slide-act').blur();
		}
		
		function slideplaypause(){
			if ($('#slidecon a').hasClass('slide-playing')) {
				$('#slidecon a').html('<img src="bsws-carousel/bsws-carousel-tab-playbtn.png" alt="PLAY">');
				$('#slidecon a').removeClass('slide-playing');
				slidetimer.stop();
				
			} else {
				slidetimer.stop();
				slidetimer = $.timer(7000, slidenext);
				$('#slidecon a').html('<img src="bsws-carousel/bsws-carousel-tab-stopbtn.png" alt="STOP">');
				$('#slidecon a').addClass('slide-playing');
				slidenext();
			}
			$('#slidecon a').blur();
			return false;
		}
		
	};
		
})(jQuery);
