(function($) {

	$.fn.efSimpleSlider = function(options) {
	  	
	  	if( /template\.asp/.test(document.URL) ) {
			return false;
		}
	  	
		// default configuration properties
		var defaults = {
			continuous:		false,
			pause:			4000,
			auto:			false,
			speed: 			500,
			width:			500,
			height:			300,
			startIndex:		0,
			displayCaption: 'always',	// always|onover|none
			arrowsVAlign:	'M',		//T|M|B|<offsetTop>
			alighCapHeight:	false,
			displayArrows:	true
		}; 
		
		var options = $.extend(defaults, options);  
		var isMoving = false;
		var isActive = !!options.auto;
		var aSlidesHtml = [];
		var slidesHtml = '';
		var w = options.width;
		var h = options.height;
		var currentSlideId = 0;
		var slideCnt = 0;
		var timeout = null;
		
		
		this.each(function() {

			$obj = $(this);
			
			/*
			 * Generate slides HTML
			 */
			$slidesContentTd = $('.slides-content', $obj);
			$slidesContent = $('.slides-content > table', $obj);
			
			slideCnt = $slidesContent.length;
			
			/*
			 * Validate startIndex
			 */
			if(options.startIndex < 0 || options.startIndex >= slideCnt) {
				options.startIndex = 0;
			}
			currentSlideId = options.startIndex;
			
			
			$slidesContent.each(function(index, el) {
				
				var cells = el.rows[0].cells;
				var slideContent = '';
				var captionContent = '';
				
				if(cells[0]) {
					slideContent = cells[0].innerHTML;
				}
				
				if(cells[1]) {
					captionContent = cells[1].innerHTML;
				}
				
				aSlidesHtml.push('\
					<div class="slide">\
						<div class="content">\
							' + slideContent + '\
						</div>\
						<div class="caption"><div class="caption-wrap">\
							' + captionContent + '\
						</div></div>\
					</div>\
				');
			}); // each slide content table
			
			slidesHtml = aSlidesHtml.join('');
			
			
			
			
			/*
			 * Generate containers HTML
			 */
			var slideLineWidth = w * $slidesContent.length;
			var startOffsetLeft = -1 * options.startIndex * w;
			
			if(options.continuous) {
				slidesHtml = aSlidesHtml[slideCnt - 1] + slidesHtml + aSlidesHtml[0];
				slideLineWidth += 2 * w;
				startOffsetLeft -= w;
			}
			
			var slideLine = document.createElement('DIV');
			var container = document.createElement('DIV');
			
			slideLine.innerHTML = slidesHtml;
			$(slideLine).css({
				position:	'absolute',
				width: 		slideLineWidth,
				height: 	h,
				left: 		startOffsetLeft + 'px'
			});
			
			
			$(container).css({
				width: w,
				height: h,
				overflow: 'hidden',
				position: 'relative'
			});
			
			
			
			$('.slide', slideLine).css({width: w, height: h});
			
			$('.caption', slideLine).css({width: w, position: 'absolute'});
			
			
			
			/*
			 * Set Caption behavior
			 */
			options.displayCaption = options.displayCaption.toLowerCase();
			
			if(options.displayCaption == 'none') {
				$('.caption', slideLine).remove();
			}/* else if(options.displayCaption == 'onover') {
				$('.caption', slideLine).css({top: h});
				
				
				$(container).mouseenter(function() {
					$capt = $('.caption', slideLine)
					
					$capt.stop().animate({top: h - $capt.height()});
				});
				
				
				/*$('.caption', slideLine).hide();
				
				$(container).mouseenter(function() {
					$('.caption', slideLine).stop().fadeIn();
				});
				
				$(container).mouseleave(function() {
					$('.caption', slideLine).stop().fadeOut();
				});
				
			}*/
			
			container.appendChild(slideLine);
			
			/*
			 * Add control buttons
			 */
			if(options.displayArrows) {
				$('<div class="nav-arrow prev"></div><div class="nav-arrow next"></div>').appendTo(container);
			}
			$('<div class="play-stop-btn"></div>').appendTo(container);
			
			
			
			$slidesContentTd.html('');
			$(container).appendTo($slidesContentTd);
			
			initControls(container);
			
			$slidesContentTd.show();
			
			/*
			 * Align caption container width
			 */
			if(options.alighCapHeight) {
				var captionH = 0;
				$('.caption', slideLine).each(function(index, el) {
					var h = $(el).height();
					
					captionH = (h > captionH) ? h : captionH;
				});
				
				$('.caption', slideLine).css('height', captionH);
			}
			
			/*
			 * Start autoscrolling
			 */
			if(options.auto) {
				timeout = setTimeout(next, options.pause);
			}
			
			
			/*
			 * Functions
			 */
			function initControls(container) {
				var valign = options.arrowsVAlign;
				var $playStopBtn = $('.play-stop-btn', container);
				
				$playStopBtn.css({
					left: (options.width - $playStopBtn.width()) / 2
				});
				
				if(valign == 'M') {
					$('.prev, .next', container).each(function(index, el) {
						$(el).css('top', (options.height - $(el).height()) / 2);
					});
					
					$playStopBtn.css({
						top: (options.height - $playStopBtn.height()) / 2
					});
					
				} else if(valign == 'B') {
					$('.prev, .next', container).each(function(index, el) {
						$(el).css('bottom', 0);
					});
					
					$playStopBtn.css('bottom', 0);
				} else if( !isNaN(parseInt(valign)) ) {
					$('.prev, .next', container).css('top', parseInt(valign));
					
					$playStopBtn.css('top', parseInt(valign));
				}
				
				$('.prev', container).click(function() {
					if(isMoving)
						return false;
					
					clearTimeout(timeout);
					
					prev();
				});
				
				$('.next', container).click(function() {
					if(isMoving)
						return false;
					
					clearTimeout(timeout);
					
					next();
				});
				
				$('.prev', container).css('left', 0);
				$('.next', container).css('right', 0);
				
				
				
				$playStopBtn.click(function() {
					
					if(isActive) {
						stop();
						$(this).addClass('play-btn');
					} else {
						next();
						$(this).removeClass('play-btn');
					}
					
					isActive = !isActive;
					
					//$(this).toggleClass('play-btn');
				});
				
				//options.width
				
				
				
				$(container).mouseenter(function() {
					$playStopBtn.stop();
					$playStopBtn.show();
				}).mouseleave(function() {
					$playStopBtn.stop();
					$playStopBtn.hide();
				});
				//clearTimeout(timeout)
			}
			
			function stop() {
				clearTimeout(timeout);
			}
			
			function prev() {
				if(isMoving)
					return false;
				
				isMoving = true;
				
				if(currentSlideId == 0) {
					if(options.continuous) {
						$(slideLine).css('left', '-' + ($(slideLine).width() - w));
					} else {
						isMoving = false;
						return false;
					}
					
					currentSlideId = (slideCnt - 1);
				} else {
					--currentSlideId;
				}
				
				$(slideLine).animate(
					{left: '+='+w},
					{
						complete: function() {
							isMoving = false;
							
							if(options.auto && isActive) {
								timeout = setTimeout(next, options.pause);
							}
						},
						duration: options.speed
					}
				);
			}
			
			function next() {
				if(isMoving)
					return false;
				
				isMoving = true;
				
				// Current slide is last
				if(currentSlideId == (slideCnt - 1)) {
					if(options.continuous) {
						$(slideLine).css('left', 0);
					} else {
						isMoving = false;
						return false;
					}
					
					currentSlideId = 0;
				} else {
					++currentSlideId;
				}
				
				$(slideLine).animate(
					{left: '-='+w}, 
					{
						complete: function() {
							isMoving = false;
							
							if(options.auto && isActive) {
								timeout = setTimeout(next, options.pause);
							}
						
						},
						duration: options.speed
					}
				);
			}
		});
		
		
		
		/*function animateNext() {
			isMoving = true;
			
			$(slideLine).animate({left: '-='+w}, function() {
				
			});
		}*/
	}
	
})(jQuery);
