(function($){
$.fn.SimpleSlider = function( method ) { 
		/* public settins "options" */
		var settings = {
			Direction : 'Up', //defaults to Right for Horizontal Down|Up|Left|Right
			Orientation : 'Vertical',
			InfinateSlide : true,  //slide forever
			SlideAction : 'Hover', //Click
			SlideSpeed : 5, //normal move 
			SlideDuration : 200, //timer duration
			StopOnHover : true, //stop slider when hovering over
			ControlBtns : 'overlay',
			LazyLoad : true,
			ScrollSpeed : 30, //click scroll - relevent on scroll action only
			ScrollDuration : 200, //click scroll
			ButtonImage : 'assets/css/SimpleSlider/slide*.jpg', //collection
			_timer : '',
			AutoStart :  true //automaticly run and restart on mouse leave
		};
		/* private settings */
		var defaults = {
			namespace : 'SimpleSlider'	 //namespace for the plugin data "The name of the plugin"
		};
		var methods = {
			init: function(options){
				var object = $(this);
				var data = object.data(defaults.namespace); //load object data
				
				if (!data) { //init the plugin
					object.data(defaults.namespace, settings); //save the "default settings"
					methods.Options( options, object ); // extend with the Options
					var my_settings = object.data(defaults.namespace); //reload the settings and move to local variable
					var list = object.find('ul');
					var max_width = object.width();
					if(list.size() == 0){
						alert('Error: no ul list found');
					}
					
					methods._setUp(object);	
					
					var list_width = 0;
					if(my_settings.LazyLoad){
						list.children('li').each(function(){
						list_width += $(this).width();
							if($(this).html().match(/<!--(.+)-->/)){
								
								if(list_width <= max_width + $(this).width()){
									var html = $(this).html().replace(/<!--/, '');
									html = html.replace(/-->/, '');
									$(this).html(html);
								}
							}
						});
					}
					
				}
				return object;
			},
			Options : function(options, object){
				if(object == undefined){
					var object = $(this); //if called from outside plugin
				}
				var my_settings = object.data(defaults.namespace); //move to local variable
				 
				if (options) {
					 $.extend(my_settings, options); //extend the settings
				}
				object.data(defaults.namespace, my_settings); //save the extended settings
				return object;
			},
			_setUp : function(object){
				if(object == undefined){
					var object = $(this);
				}
				my_settings = object.data(defaults.namespace);	
				
				var list = object.find('ul');
				list.wrap('<div/>');
				
				list.closest('div').css({
					'overflow': 'hidden',
					'position': 'relative' 
				});
				object.children('div:first-child').addClass('SimpleScrollContent');
			
				list.css({
					'padding': '0px',
					'margin': '0px',
					'position': 'absolute',
					'list-style': 'none',
					'top': '0px',
					'left': '0px'
				});
	
				if(my_settings.Orientation == 'Vertical'){
					methods._setUpVertical(object);
				}else if(my_settings.Orientation == 'Horizontal'){
					methods._setUpHorizontal(object);
				}
				
				if(my_settings.StopOnHover){
					list.hover(function(){
						methods.Stop(object); //stop
					},function(){
						methods.Start(null,null,null,object); //start
					});
				}
				
				if(my_settings.AutoStart){
					methods.Start(null,null,null,object);
				}
				return object;
			},
			_setUpVertical : function(object){
				if(object == undefined){
					var object = $(this);
				}
				my_settings = object.data(defaults.namespace);	
				
				var list = object.find('ul');
				var direction = my_settings.Direction.toLowerCase();
				var height = 0;
				var width = 0;
				//TODO: add hidden to this one.
				if(my_settings.ControlBtns == 'visibile'){
				//UP control
					object.prepend('<div class="SimpleScrollUp" style="cursor: pointer;" ></div>');
					var _slideUp = $('.SimpleScrollUp');
					var buttonUp = my_settings.ButtonImage.replace('*', 'Up');
					var buttonUpHover = my_settings.ButtonImage.replace('*', 'UpHover');
					_slideUp.prepend('<img src="'+buttonUp+'" alt="Slide Up" title="Slide Up" style="display:block; margin:auto;" />');
					_slideUp.css({
						'height' : _slideUp.children('img').height()+'px'
					});
					//events//
					
				//Down control
					object.append('<div class="SimpleScrollDown" style="cursor: pointer;" ></div>');
					var _slideDown = $('.SimpleScrollDown');
					var buttonDown = my_settings.ButtonImage.replace('*', 'Down');
					var buttonDownHover = my_settings.ButtonImage.replace('*', 'DownHover');
					_slideDown.prepend('<img src="'+buttonDown+'" alt="Slide Down" title="Slide Down" style="display:block; margin:auto;" />');		
					_slideDown.css({
						'height' : _slideDown.children('img').height()+'px'
					});
					//--------events-------//
					//hover//
					var li_height = 0;
					//up//
					$('.SimpleScrollUp').hover(function(){
							methods.Start('Up',null,null,object);
							$('.SimpleScrollUp>img').attr('src', buttonUpHover);
						},function(){
							methods.Start(null,null,null,object); //restart
							$('.SimpleScrollUp>img').attr('src', buttonUp);
					});
					//click//
					$('.SimpleScrollUp').click(function(){
						methods.Start('Up',my_settings.ScrollSpeed,my_settings.ScrollDuration,object);
					});
					//down//
					//hover//
					$('.SimpleScrollDown').hover(function(){
							methods.Start('Down',null,null,object);	
							$('.SimpleScrollDown>img').attr('src', buttonDownHover);
						},function(){
							methods.Start(null,null,null,object);	//restart
							$('.SimpleScrollDown>img').attr('src', buttonDown);
					});
					//click//
					$('.SimpleScrollDown').click(function(){
							methods.Start('down',my_settings.ScrollSpeed,my_settings.ScrollDuration,object);
					});
					
					
				//List Wrpper
					height = object.height() - _slideDown.height() - _slideUp.height();
				}else{
					height = object.height();
				}
			
				$('.SimpleScrollContent').css({
					'height': height+'px'
				});
				
				return object;
			},
			_setUpHorizontal : function(object){
				if(object == undefined){
					var object = $(this);
				}
				my_settings = object.data(defaults.namespace);	
				
				var list = object.find('ul');
				var direction = my_settings.Direction.toLowerCase();
				var height = 0;
				var width = 0;
				object.find('table').css({'padding': '0px', 'margin': '0px', 'border':'none', 'border-collapse':'collapse' });
				
				
				list.children('li').css({
					'float' : 'left',
					'display': 'block'
				});
				
				list.children('li').each(function(){
					width += $(this).outerWidth(); //for padding etc.
				});
				
				list.css({
					'height' : height+'px',
					'width': width+'px'
				});
			
				if(my_settings.ControlBtns  == 'visibile' || my_settings.ControlBtns  == 'overlay'){
					height = object.height();	
					var _slideLeft = "";
					var buttonLeft = "";
					var buttonLeftHover = "";
					var _slideRight = "";
					var buttonRight = "";
					var buttonRightHover = "";
					
					if(my_settings.ControlBtns  == 'visibile'){
					//LEFT control
						object.prepend('<div class="SimpleScrollLeft" style="float:left; height: '+object.height()+'px; cursor: pointer;" ></div>');
						
						_slideLeft = $('.SimpleScrollLeft');
						buttonLeft = my_settings.ButtonImage.replace('*', 'Left');
						buttonLeftHover = my_settings.ButtonImage.replace('*', 'LeftHover');
						
						_slideLeft.prepend('<img src="'+buttonLeft+'" alt="Slide Left" title="Slide Left" style="display:block; margin:auto;" />');
						_slideLeft.children('img').css({
							"display" : "block",
							"margin-top" : Math.round((object.height()-_slideLeft.children('img').height())*0.5)+"px"
						});
						
					//RIGHT control
						object.append('<div class="SimpleScrollRight" style="float:left; height: '+object.height()+'px; cursor: pointer;" ></div>');
						
						_slideRight = $('.SimpleScrollRight');
						buttonRight = my_settings.ButtonImage.replace('*', 'Right');
						buttonRightHover = my_settings.ButtonImage.replace('*', 'RightHover');
						
						_slideRight.prepend('<img src="'+buttonRight+'" alt="SlideRight" title="Slide Right" style="display:block; margin:auto;" />');		
						_slideRight.children('img').css({
							"display" : "block",
							"margin-top" : Math.round((object.height()-_slideRight.children('img').height())*0.5)+"px"
						});
						//List Wrpper
						width = object.width() - _slideLeft.width() - _slideRight.width();
					}else if(my_settings.ControlBtns  == 'overlay'){
						//LEFT control
						object.prepend('<div class="SimpleScrollLeft" style="background: red; position: absolute; top: 0px; left: 0px; z-index: 99999; height: '+object.height()+'px; width: 50px; cursor: pointer;" ></div>');
						
						_slideLeft = $('.SimpleScrollLeft');
						buttonLeft = my_settings.ButtonImage.replace('*', 'Left');
						buttonLeftHover = my_settings.ButtonImage.replace('*', 'LeftHover');
						
						_slideLeft.prepend('<img src="'+buttonLeft+'" alt="Slide Left" title="Slide Left" style="display:block; margin:auto;" />');
						_slideLeft.children('img').css({
							
						});
						
					//RIGHT control
						object.prepend('<div class="SimpleScrollRight" sstyle="background: red; position: absolute; top: 0px; right: 0px; z-index: 99999; height: '+object.height()+'px; width: 50px; cursor: pointer;" ></div>');
						
						_slideRight = $('.SimpleScrollRight');
						buttonRight = my_settings.ButtonImage.replace('*', 'Right');
						buttonRightHover = my_settings.ButtonImage.replace('*', 'RightHover');
						
						_slideRight.prepend('<img src="'+buttonRight+'" alt="SlideRight" title="Slide Right" style="display:block; margin:auto;" />');		
						_slideRight.children('img').css({
							
						});
						width = object.width();
					}
					//--------events-------//
					//hover//
					//up//
					$('.SimpleScrollRight').hover(function(){
							methods.Start('Right',null,null,object);
							$('.SimpleScrollRight>img').attr('src', buttonUpHover);
						},function(){
							methods.Start(null,null,null,object); //restart
							$('.SimpleScrollRight>img').attr('src', buttonUp);
					});
					//click//
					$('.SimpleScrollRight').click(function(){
						methods.Start('Right',my_settings.ScrollSpeed,my_settings.ScrollDuration,object);
					});
					//left//
					//hover//
					$('.SimpleScrollLeft').hover(function(){
							methods.Start('Left',null,null,object);	
							$('.SimpleScrollLeft>img').attr('src', buttonDownHover);
						},function(){
							methods.Start(null,null,null,object);	//restart
							$('.SimpleScrollLeft>img').attr('src', buttonDown);
					});
					//click//
					$('.SimpleScrollLeft').click(function(){
							methods.Start('Left',my_settings.ScrollSpeed,my_settings.ScrollDuration,object);
					});
					
				}else{
					width = object.width();
				}
				
			
				$('.SimpleScrollContent').css({
					'width': width+'px',
					'height' : object.height()+'px',
					'float': 'left'
				});
			
				object.append('<div style="clear:both; height: 1px; width: '+object.width()+'px;" ></div>');
				
				
				return object;
			},
			Stop : function(object){
				if(object == undefined){
					var object = $(this);
				}
				var list = object.find('ul');
				my_settings = object.data(defaults.namespace);
				list.stop(true, false)
				clearInterval(my_settings._timer);
			},
			Start : function(direction, speed, time, object){
				if(object == undefined){
					var object = $(this);
				}
				var my_settings = object.data(defaults.namespace);	
				
				methods.Stop(object);
				
				if(direction == undefined || direction == null ){
					direction = my_settings.Direction;
				}
				if(speed == undefined || speed == null ){
					speed = my_settings.SlideSpeed;
				}
				if(time == undefined || time == null ){
					time = my_settings.SlideDuration;
				}
				
				switch(direction.toLowerCase()){
						case 'down' : default :
							if(my_settings.Orientation == 'Vertical'){
								methods.SlideDown(speed, time, object);
							}else if(my_settings.Orientation == 'Horizontal'){
								methods.SlideRight(speed, time, object);
							}
						break;
						case 'up':
							if(my_settings.Orientation == 'Vertical'){
								methods.SlideUp(speed, time, object);
							}else if(my_settings.Orientation == 'Horizontal'){
								methods.SlideLeft(speed, time, object);
							}
						break;
						case 'right':
							if(my_settings.Orientation == 'Vertical'){
								methods.SlideDown(speed, time, object);
							}else if(my_settings.Orientation == 'Horizontal'){
								methods.SlideRight(speed, time, object);
							}
						break;
						case 'left' :
							if(my_settings.Orientation == 'Vertical'){
								methods.SlideUp(speed, time, object);
							}else if(my_settings.Orientation == 'Horizontal'){
								methods.SlideLeft(speed, time, object);
							}
						break;
					}
				return object;
			},
			_getLiHeight : function(index, object){
				if(object == undefined){
					var object = $(this);
				}
				my_settings = object.data(defaults.namespace);
				var list = object.find('ul');
				var li = list.children('li')[index];
				var height = $(li).height();
				return height;
			},
			_getLiWidth : function(index, object){
				if(object == undefined){
					var object = $(this);
				}
				my_settings = object.data(defaults.namespace);
				var list = object.find('ul');
				var li = list.children('li')[index];
				var width = $(li).width();
				return width;
			},
			_getPos : function(object){
				if(object == undefined){
					var object = $(this);
				}
				my_settings = object.data(defaults.namespace);
				var list = object.find('ul');
				var pos_dim = 'top';
				if(my_settings.Orientation == 'Horizontal'){
					pos_dim = 'left';
				}
				var pos = Math.round(list.css(pos_dim).replace('px', ''));
				
				return pos;
			},
			SlideDown : function(speed, time, object){
				if(object == undefined){
					var object = $(this);
				}
				my_settings = object.data(defaults.namespace);
				//overload defaults..
				if(speed == undefined || speed == null ){
					speed = my_settings.SlideSpeed;
				}
				if(time == undefined || time == null ){
					time = my_settings.SlideDuration;
				}
				//..
				var list = object.find('ul');
				var run = false;
				if(my_settings.InfinateSlide){
					run = true;
				}else{
					var pos = methods._getPos(object);
					var max_pos = 0;
					//alert('1 '+pos+' '+max_pos);
					if(pos < max_pos){
						run = true;
					}
				}
				
				if(run){
					my_settings._timer = setInterval(function() {
						methods._checkBounds(object);
						list.stop(true, false).animate({"top": "+="+speed+"px"}, time, "linear");
					}, time);
				}
				
			},
			SlideUp : function(speed, time, object){
				if(object == undefined){
					var object = $(this);
				}
				my_settings = object.data(defaults.namespace);
				
				//overload defaults..
				if(speed == undefined || speed == null ){
					speed = my_settings.SlideSpeed;
				}
				if(time == undefined || time == null ){
					time = my_settings.SlideDuration;
				}
				//..
				var run = false;
				if(my_settings.InfinateSlide){
					run = true;
				}else{
					var pos = methods._getPos(object);
					var min_pos = Math.round(object.find('.SimpleScrollContent').height()-object.find('ul').height());
					
					if(pos > min_pos){
						run = true;
					}
				}
				
				if(run){
					var list = object.find('ul');
					my_settings._timer = setInterval(function() {
						methods._checkBounds(object);
						list.stop(true, false).animate({"top": "-="+speed+"px"}, time, "linear");
					}, time);
				}

			},	
			SlideLeft : function(speed, time, object){
				if(object == undefined){
					var object = $(this);
				}
				my_settings = object.data(defaults.namespace);
				
				//overload defaults..
				if(speed == undefined || speed == null ){
					speed = my_settings.SlideSpeed;
				}
				if(time == undefined || time == null ){
					time = my_settings.SlideDuration;
				}
				//..
				var run = false;
				if(my_settings.InfinateSlide){
					run = true;
				}else{
					var pos = methods._getPos(object);
					var min_pos = Math.round(object.find('.SimpleScrollContent').width()-object.find('ul').width());
					
					if(pos > min_pos){
						run = true;
					}
				}
				
				if(run){
					var list = object.find('ul');
					my_settings._timer = setInterval(function() {
						methods._checkBounds(object);
						list.stop(true, false).animate({"left": "-="+speed+"px"}, time, "linear");
					}, time);
				}
			},
			SlideRight : function(speed, time, object){
				if(object == undefined){
					var object = $(this);
				}
				my_settings = object.data(defaults.namespace);
				
				//overload defaults..
				if(speed == undefined || speed == null ){
					speed = my_settings.SlideSpeed;
				}
				if(time == undefined || time == null ){
					time = my_settings.SlideDuration;
				}
				//..
				var run = false;
				if(my_settings.InfinateSlide){
					run = true;
				}else{
					var pos = methods._getPos(object);
					var min_pos = Math.round(object.find('.SimpleScrollContent').width()-object.find('ul').width());
					
					if(pos > min_pos){
						run = true;
					}
				}
				
				if(run){
					var list = object.find('ul');
					my_settings._timer = setInterval(function() {
						methods._checkBounds(object);
						list.stop(true, false).animate({"left": "+="+speed+"px"}, time, "linear");
					}, time);
				}
	
			},
			_checkBounds : function(object){
				if(object == undefined){
					var object = $(this);
				}
				my_settings = object.data(defaults.namespace);	
				
				var pos = methods._getPos(object);
				var list = object.find('ul');
				var child = '';
				if(my_settings.Orientation == 'Vertical'){
					if(pos > 5 ){ //out bottom
						methods._swapBottomToTop(object);
					}else if((pos < (list.height()-list.parent().height())*-1)){ //out top
						methods._swapTopToBottom(object);
					}
				}else if(my_settings.Orientation == 'Horizontal'){
					var max_width = object.width()+Math.abs(pos);
					if(pos > 5 ){ //out left
						methods._swapRightToLeft(object);
					}else if((pos < (list.width()-list.parent().width())*-1)){ //out right
						methods._swapLeftToRight(object);
					}
					//alert(max_width);
					if(my_settings.LazyLoad){
						var list_width = 0;
						list.children('li').each(function(){						
							list_width += $(this).width();
							if($(this).html().match(/<!--(.+)-->/)){
								if(list_width <= max_width+$(this).width()){
									var html = $(this).html().replace(/<!--/, '');
									html = html.replace(/-->/, '');
									$(this).html(html);
								}
							}
						});
					}

					
				}
				return object;
			},
			_swapTopToBottom : function(object){
				if(object == undefined){
					var object = $(this);
				}
				my_settings = object.data(defaults.namespace);
				//move top to bottom
				var pos = methods._getPos(object);
				var list = object.find('ul');
				
				var adjusted_pos = 0;
				var li_height = 0;
				
				if(my_settings.InfinateSlide){
					list.children('li:last').after('<li>'+list.children('li:first').html()+'</li>'); //copy
					list.children('li:first').remove(); //remove
					li_height = methods._getLiHeight(list.children('li').length-1, object);
					adjusted_pos = Math.round(pos+li_height);
				}else{
					li_height = methods._getLiHeight(list.children('li').length-1, object);
					methods.Stop(object);
					adjusted_pos = Math.round(object.find('.SimpleScrollContent').height()-object.find('ul').height());
				}
				
				
				list.css({'top':  adjusted_pos+'px'}); //adjust
				return object;
			},
			_swapBottomToTop : function(object){
				if(object == undefined){
					var object = $(this);
				}
				my_settings = object.data(defaults.namespace);	
				//move bottom to top
				var pos = methods._getPos(object);
				var list = object.find('ul');
				var adjusted_pos = 0;
				var li_height = 0;
				
				if(my_settings.InfinateSlide){
					list.children('li:first').before('<li>'+list.children('li:last').html()+'</li>'); //copy
					list.children('li:last').remove(); //remove
					li_height = methods._getLiHeight(0,object);
					adjusted_pos = Math.round(pos-li_height);
				}else{
					li_height = methods._getLiHeight(0,object);
					methods.Stop(object);	
					adjusted_pos = 0;
				}

				list.css({'top':  adjusted_pos+'px'}); //adjust
				return object;
			},
			_swapLeftToRight : function(object){
				if(object == undefined){
					var object = $(this);
				}
				my_settings = object.data(defaults.namespace);
				//move top to bottom
				var pos = methods._getPos(object);
				var list = object.find('ul');
				
				var adjusted_pos = 0;
				var li_width = 0;
				
				if(my_settings.InfinateSlide){
					list.children('li:last').after('<li style="float: left; display: block;" >'+list.children('li:first').html()+'</li>'); //copy
					list.children('li:first').remove(); //remove
					li_width = methods._getLiWidth(list.children('li').length-1, object);
					adjusted_pos = Math.round(pos+li_width);
				}else{
					li_width = methods._getLiWidth(list.children('li').length-1, object);
					methods.Stop(object);
					adjusted_pos = Math.round(object.find('.SimpleScrollContent').width()-object.find('ul').width())-2;
				}
				
				
				list.css({'left':  adjusted_pos+'px'}); //adjust
				return object;
			},
			_swapRightToLeft : function(object){
				if(object == undefined){
					var object = $(this);
				}
				my_settings = object.data(defaults.namespace);	
				//move bottom to top
				var pos = methods._getPos(object);
				var list = object.find('ul');
				var adjusted_pos = 0;
				var li_width = 0;
				
				if(my_settings.InfinateSlide){
					list.children('li:first').before('<li style="float: left; display: block;" >'+list.children('li:last').html()+'</li>'); //copy
					list.children('li:last').remove(); //remove
					li_width = methods._getLiWidth(0,object);
					adjusted_pos = Math.round(pos-li_width);
				}else{
					li_width = methods._getLiWidth(0,object);
					methods.Stop(object);	
					adjusted_pos = 0;
				}

				list.css({'left':  adjusted_pos+'px'}); //adjust
				return object;	
			},
			Remove : function(object){
				if(object == undefined){
					var object = $(this);
				}
				my_settings = object.data(defaults.namespace);	
			}
		
		};
		
		if ( methods[method] ) {
			return methods[method].apply( this, Array.prototype.slice.call( arguments, 1 ));
		} else if ( typeof method === 'object' || ! method ) {
			return methods.init.apply( this, arguments );
		} else {
			$.error( 'Method ' +  method + ' does not exist on jQuery.'+defaults.namespace );
		}    
	};
})(jQuery);
