/*
* jwSlider v1 - http://www.jeffrey-way.com
* Last Updated: October 29, 2009

* This is a very simple and convenient slider/fader to transition between images or content. it offers only fade and slide transitions to keep the script
* as lightweight as possible.

* Developed by Jeffrey Way
* http://www.jeffrey-way.com
* jeffrey@effrey-way.com
*/

(function($) {
	
	$.fn.jwSlider = function(options) {
		$('#lorem').html('out');

		// set default options
		var defaults = {
			div : 'scroll',
			speed : 1000,
			pause : 2000,
			transition : 'slide',
			id : 1
		},
		

		// Take the options that the user selects, and merge them with defaults.
		options = $.extend(defaults, options);
		
		// Needed to fix a tiny bug. If the pause is less than speed, it'll cause a flickr.
		// This will check for that, and if it is smaller, it increases it to just about the options.speed.
		if(options.pause <= options.speed) options.pause = options.speed + 100;
		
		// only if there are more than 1 objects to slide
		if($(this).find('.'+options.div).length > 1) {
		
		// for each item in the wrapped set
		return this.each(function() {
		
			var $this = $(this);
			var width = $this.width();
			
			$this.wrap('<div class="slider-wrap-'+options.id+'" />');
			
			// Set the width to a really high number. Adjusting the "left" css values, so need to set positioning.
			$this.css({
				'width' : '99999px',
				'position' : 'relative',
				'padding' : 0
			});
			
			var page = {
				obj : $this.parent().parent().find('.pages'),
				length : $this.find('div.'+options.div).length,   //$('slider-wrap-'+options.id).  //$('div.server').find('div.scroll').length,
				akt : 1,
				prev : 0,
			}
			page.prev = page.length;
			for(i = 1; i<=page.length; i++){
				page.obj.append('<a class="page page-' + options.id + '-' + i + '" href="javascript:void(0)">&nbsp;</a>');
			}
			page.obj.find('a.page-' + options.id + '-' + page.akt).addClass('currentpage');
			
			$this.children().css({
				'float' : 'left',
				'list-style' : 'none'
			});
			
			$('.slider-wrap-'+options.id).css({
				'width' : width,//$this.children().width(),
				'overflow' : 'hidden'
			});				
			
			//slide();
			//function slide() {
			
			var slideinterval = setInterval( slideani, options.pause );
				
			//} // end slide
			
			function slideani() {
			
				// Animate to the left the width of the image/div
				var parentwidth = $this.children().width();   //$this.parent().width();
				$this.animate({'left' : '-' + parentwidth}, options.speed, function() {
					// Return the "left" CSS back to 0, and append the first child to the very end of the list.
					$this
					   .css('left', 0)
					   .children(':first')
					   .appendTo($this) // move it to the end of the line.
					   .css('display', 'none')
					   .fadeIn();
					if(page.akt==page.length) {page.akt=1} else {page.akt++}
					if(page.prev==page.length) {page.prev=1} else {page.prev++}
					page.obj.find('a.page-' + options.id + '-' + page.prev).removeClass('currentpage');
					page.obj.find('a.page-' + options.id + '-' + page.akt).addClass('currentpage');
				});
			}
			
			
			

			$this.parent().mouseover(function() {
				clearTimeout(slideinterval);
			}).mouseout(function() {
				slideinterval = setInterval( slideani, options.pause );
			});
			
			
			/* $('.slider-wrap-' + options.id).mouseover(function{
				// clearInterval(sliding);
				$('#lorem').html('hover');
			});
			$('.slider-wrap-' + options.id).mouseout(
				// slide()
				$('#lorem').html('out');
			); */
			
			// $('div.page').click(function { 
				// $(this).
			// });
			

		}); // end each		
		
		}
	
	} // End plugin. Go eat cake.
	
})(jQuery);
