(function($){
	$.fn.jslideshow = function(ops) {
		var defaults = {
			speed : 0,
			start : 0,
			pager : true,
			equalHeights : true,
			showTitles : true,
			pauseOnHover : false,
			effect : 'fade',
			itemsClass : '.slide',
			titlesClass : '.slide_title',
			titlesContainer : '',
			activetitleClass : 'bg3'
		};

		var slideshow_interval;
		var options = $.extend(defaults, ops);

		var container = this;
		var slides = container.children(options.itemsClass).addClass('slide');
		var total_slides = slides.size();
		var current_slide = options.start;
		var last_slide = (current_slide > 0) ? current_slide-1 : 0;
		var slideWidth = container.width();
		var slideHeight = 0;
		var paused = false;
		var sObj = {};

		var slideshow_rotate = function() {
			current_slide = (last_slide + 1) % total_slides;

			switch(options.effect) {
				case 'slideLeft':
					var scrollAmount = current_slide * slideWidth;
					sObj.animate({scrollLeft: scrollAmount}, 1000);
				break;
				case 'slideUp':
					scrollAmount = current_slide * slideHeight;
					sObj.animate({scrollTop: scrollAmount}, 1000);
				break;
				case 'show':
					slides.eq(last_slide).css('display', 'none');
					slides.eq(current_slide).css('display', 'block');
				break;
				case 'fade':
					slides.eq(last_slide).fadeOut('fast', function(){
						slides.eq(current_slide).fadeIn('slow');
					});
				break;
			}

			last_slide = current_slide;
			container.children('.slideshow_pager').text( (current_slide+1) + '/' + total_slides);

			if (options.showTitles) {
				$(options.titlesContainer + ' > li').removeClass(options.activetitleClass).eq(current_slide).addClass(options.activetitleClass);
			}

			return false;
		};

		var initialize = function() {
			slides.each(function() {
				slideHeight = Math.max(slideHeight, $(this).height());
			}).width(slideWidth);
	
			if (options.equalHeights || options.effect !== 'fade' || options.effect !== 'show') {
				slides.height(slideHeight);
			}
	
			var totalWidth = total_slides * slideWidth;
			var totalHeight = total_slides * slideHeight;

			switch (options.effect) {
				case 'slideLeft':
				case 'slideUp':
					var cId = container.attr('id');
					container.addClass('slideshow').wrapInner('<div class="slideshow-scroller"><div class="slideshow-holder"></div></div>');
					sObj = container.children('.slideshow-scroller');
	
					switch (options.effect) {
						case 'slideLeft':
							container.children('.slideshow-scroller').attr({scrollLeft: 0}).width(slideWidth).children('.slideshow-holder').width(totalWidth).children(options.itemsClass).width(slideWidth).css('float', 'left');
						break;
						case 'slideUp':
							container.children('.slideshow-scroller').attr({scrollTop: 0}).height(slideHeight).children('.slideshow-holder').height(totalHeight).children(options.itemsClass).height(slideHeight);
						break;
					}
				break;
	
				default:
					slides.css('display', 'none').eq(current_slide).css('display', 'block');
				break;
			}

			if (options.pager || options.showTitles) {
				container.append('<div class="clear"></div><br />');
			}

			if (options.pager) {
				var controls = '<span class="slideshow_controls"><a href="#" class="slideshow_prev">&nbsp;</a>';
				controls += (options.speed) ? '<a href="#" class="slideshow_pause">&nbsp;</a>' : '';
				controls += '<a href="#" class="slideshow_next">&nbsp;</a></span>';
				container.append('<span class="slideshow_pager">' + (current_slide+1) + '/' + total_slides + '</span>' + controls + '<div class="clear"></div>');

				container.children('.slideshow_controls').find('a').click(function() {
					var control = $(this).attr('class');

					switch(control) {
						case 'slideshow_next':
							clearInterval(slideshow_interval);
							if (paused === false && options.speed) {
								slideshow_interval = window.setInterval(slideshow_rotate, options.speed);
							}
							slideshow_rotate();
						break;
						case 'slideshow_pause':
							clearInterval(slideshow_interval);
							$(this).attr('class', 'slideshow_play');
							paused = true;
						break;
						case 'slideshow_play':
							slideshow_interval = window.setInterval(slideshow_rotate, options.speed);
							$(this).attr('class', 'slideshow_pause');
							paused = false;
						break;
						case 'slideshow_prev':
							clearInterval(slideshow_interval);

							current_slide = ((current_slide - 1 < 0) ? total_slides : current_slide) - 1;
							last_slide = ((current_slide - 1 < 0) ? total_slides : current_slide) - 1;
				
							if (options.effect === 'show' || options.effect === 'fade') {
								slides.css('display', 'none');
							}
				
							if (paused === false) {
								slideshow_interval = window.setInterval(slideshow_rotate, options.speed);
							}
							slideshow_rotate();
						break;
					}
				
					return false;
				});
			}

			if (options.showTitles) {
				if (!options.titlesContainer) {
					var el = container.attr('id') || container.attr('class');
					options.titlesContainer = '#titles_' + el;
					container.append('<div id="titles_' + el + '"></div>');
				}

				var titlesObj = $(options.titlesContainer);
				slides.find(options.titlesClass).each(function(i) {
					var t = $(this).text();
					var c = (current_slide === i) ? ' class="' + options.activetitleClass + '"' : '';
					titlesObj.append('<li id="slideshow_title' + i + '"' + c + '><a href="#">' + t + '</a></li>');
				});

				titlesObj.wrap('<ul class="post"></ul>').find('li').click(function(i) {
					clearInterval(slideshow_interval);
	
					current_slide = $(this).attr('id').replace('slideshow_title', '');
					last_slide = ((current_slide - 1 < 0) ? total_slides : current_slide) - 1;
	
					$(this).children('li').removeClass(options.activetitleClass);
					$(this).addClass(options.activetitleClass);
	
					if (options.effect === 'show' || options.effect === 'fade') {
						slides.css('display', 'none');
					}
	
					if (paused === false) {
						slideshow_interval = window.setInterval(slideshow_rotate, options.speed);
					}
					slideshow_rotate();
	
					return false;
				});
			}

			if (options.speed) {
				slideshow_interval = window.setInterval(slideshow_rotate, options.speed);
			}

			if (options.pauseOnHover) {
				slides.hover(
					function() {
						clearInterval(slideshow_interval);
					},
					function() {
						slideshow_interval = window.setInterval(slideshow_rotate, options.speed);
					}
				);
			}

			return false;
		};

		container.preloader({
			ondone: function() {
				initialize();
				container.show();
			}
		});
   };
})(jQuery);
