/*
Name: jQuery Simple Slider Plugin
Version: 0.1
*/

(function($){  
	
	// default settings
	var defaults = {
		slideEaseDuration: 	1000,
		slideEaseFunction: 	"easeInOutExpo",
		firstPanelToLoad: 1,
		playspeed: 1200
	};
	
	// Properties
	var 	myoptions, 	// plugin properties
			slider_panelWidth,
			panelCount,
			currentPanel,
			play_interval
			
			;	// descr container object

	var mymethods = {
		
		// initialize everything
		init : function( options ) { 
			
			//alert('list init');
			var slider=$(this);
			myoptions = $.extend(defaults, options);
			
			// Slide left
			$(this).find('.arrleft').click(function(e) {
				var slideto=currentPanel-1;
				if (slideto<=0) {slideto=panelCount;}
				$(this).parent().simpleslider('slide',slideto);

			});
			
			// Slide right
			$(this).find('.arrright').click(function(e) {
				var slideto= currentPanel+1;
				if (slideto>panelCount) {slideto=1;}
				$(this).parent().simpleslider('slide',slideto);
			});
			
			// Stop autoplay on mouse over
			$(this).mouseenter(function() {				
				clearInterval(play_interval);				
			});
	
			// Stop autoplay on mouse over
			$(this).mouseleave(function() {				
				if (myoptions.playspeed>0) {
					play_interval = setInterval(
						function() {
							slider.find('.arrright').click();
						},
						myoptions.playspeed
					 );				
				}
			});

			
			// 
			$(this).find('.arrow').hover( function () {$(this).addClass('hover');}, function () {$(this).removeClass('hover');} );
			
			slider_panelWidth = slider.find(".spanel").width();
			panelCount = slider.find(".spanel").size();
			var panelContainerWidth = slider_panelWidth*panelCount;
			var navClicks = 0; // Used if autoSlideStopWhenClicked = true
			
			// Surround the collection of panel divs with a container div (wide enough for all panels to be lined up end-to-end)
			//$('.panel', slider).wrapAll('<div class="panel-container"></div>');
			// Specify the width of the container div (wide enough for all panels to be lined up end-to-end)
			slider.find(".spanel-container", slider).css({ width: panelContainerWidth });
			
			// Specify the current panel.
				
			// If that's not the case, check to see if we're supposed to load a panel other than Panel 1 initially...
			if (myoptions.firstPanelToLoad != 1 && myoptions.firstPanelToLoad <= panelCount) { 
				
				currentPanel = myoptions.firstPanelToLoad;
				var offset = - (slider_panelWidth*(currentPanel - 1));
				$('.spanel-container', slider).css({ marginLeft: offset });
			// Otherwise, we'll just set the current panel to 1...
			} else { 
				
				currentPanel = 1;
			};
			
			//autoplay
			if (myoptions.playspeed>0) {
				play_interval = setInterval(
					function() {
						slider.find('.arrright').click();
					},
					myoptions.playspeed
				 );
			}
			//slider.simpleslider('slide',currentPanel);
		},
	
		slide: function (targetPanel) {
						
			if (targetPanel=='') {targetPanel=1;}
			offset = - (slider_panelWidth*(targetPanel - 1));
			//alert(slider_panelWidth+'|'+offset+'|'+targetPanel);
			//alert($(this).attr('class'));
			//alert($(this).find('.spanel-container').attr('class'));
			$(this).find('.spanel-container').animate({ marginLeft: offset });//, myoptions.slideEaseDuration, myoptions.slideEaseFunction);
			currentPanel=targetPanel;
		
		}		
	}
	
	// we can call methods like $('div').listcontrol('methodname')
	$.fn.simpleslider = function( method ) {    
		if ( mymethods[method] ) {
		  return mymethods[ method ].apply( this, Array.prototype.slice.call( arguments, 1 ));
		} else if ( typeof method === 'object' || ! method ) {
		  return mymethods.init.apply( this, arguments );
		} else {		  $.fn.listcontrol.error( 'Method ' +  method + ' does not exist on jQuery.econtrol' );
		}   
	}
	
	// Error handler
	$.fn.simpleslider.error =function(msg) {
		alert(msg);
	}
	
})(jQuery); 

