/**
 * @author Ronen
 */


(function($){

	$.gallery_slider = function(options){	
		
		/* Variables
		----------------------------*/
		var $imgCount;
		var $imgWidth = options.width_of_frame; 
		var $imgHeight = options.height_of_frame; 
		var _curImg = 1;
		var _prevImg = 1;
		
        var changeTimer;
        		
		var _howMuch = 1;
		
        var base = this;
        	
    	var _gallery = options.holder;
        var page_dir = options.direction; 
       
        var _img_holder_type = options.image_holder_type;
        var _auto = options.auto;        		
        var _useBtns = options.useBtns;        		
        if (_useBtns) {
        	var btn_left = (page_dir == 'left') ? options.btn_left : options.btn_right;  
        	var btn_right = (page_dir == 'left') ? options.btn_right : options.btn_left;  
        }
        
        var _useImageNums = options.useImageNumbers;
        var _imgNumsHolder = options.image_numbers_holder;
        
        var timeBetween = options.timeBetween;
		var transSpeed = options.transSpeed;
		var easeType = options.easeType;   
		
		var pause_on_hover = options.pause_on_hover;   
		
		var mouseIsOver = false;
		        		
		base.init = function(){
        	
        	//$(_gallery + " " + _img_holder_type).height($imgHeight)
        	       	
            //$imgWidth = $(_gallery + " " + _img_holder_type).first().outerWidth();//read the image width
		   	$imgCount = $(_gallery + " " + _img_holder_type).length;//count the images
		  	 //alert ($imgWidth + " : " + $imgCount)
		  	$(_gallery).width($imgWidth*($imgCount+2));//set the width of the container to the number of images - plus 2 to account for the cloned images
		  	$(_gallery + " " + _img_holder_type).first().addClass('endless_slider_first');//identify the first and last images
		   	$(_gallery + " " + _img_holder_type).last().addClass('endless_slider_last');
		    $(_gallery + " " + '.endless_slider_first').clone().appendTo(_gallery);//clone the first image and put it at the end
		    $(_gallery + " " + '.endless_slider_last').clone().prependTo(_gallery);//clone the last image and put it at the front
		    $(_gallery).css({'left':-1*$imgWidth+'px'});//reset the slider so the first image is still visible
		   
		    /*img_to_grayscale.each(function(){
				$(this).load(function(){
		        	setGreyscale ()
		        })
		    	if (this.complete) $(this).trigger("load");
			});*/
		   
			if (_auto) changeTimer = setInterval(base.goRight,timeBetween)	   
			if (_useBtns) base.setButtons();
			if (_useImageNums) base.setImageNumbers();
			if (pause_on_hover) base.setPauseOnHover();
						
        };

		
	
	
		base.setButtons = function () {
				
			$(btn_left).click(function(){
		   		//alert ($(this).attr('class') + " : " + (_curImg+1) +":"+ $imgCount)
		   		var curToSend = ((_curImg+1) > $imgCount) ? 1 : (_curImg+1);
				base.setCurrentBtn (curToSend)
		     	base.goRight(1);
		   });
		   
		   $(btn_right).click(function(){
		   		//alert ((_curImg-1) +":"+ $imgCount)
		   		var curToSend = ((_curImg-1) < 1) ? $imgCount : (_curImg-1);
				base.setCurrentBtn (curToSend)
		      	base.goLeft(1);
		   });
		}

		base.setImageNumbers = function () {
			$(_imgNumsHolder + ' li:first-child').addClass('current');
			$(_imgNumsHolder + ' li').click(function() {   		
   		
		   		var dif = $(this).html() - _curImg;
		   		
		   		if (_curImg < $(this).html() )
		   			base.goRight( $(this).html() - _curImg  );   
		      	else 
		      		base.goLeft( _curImg - $(this).html() ); 
		      	   
		   		base.setCurrentBtn ($(this).html())
		   		   	
		   });
		}

		base.setCurrentBtn = function (wO) {
			_curImg = Number(wO);
			$(_imgNumsHolder + " li:nth-child("+_curImg+")").addClass('current');
			$(_imgNumsHolder + " li:nth-child("+_prevImg+")").removeClass('current');
			_prevImg = _curImg;
		}

		base.setPauseOnHover = function () {
			$(_gallery).parent().hover(function () {
				base.mouseIsOver = true;
			}, function () {
				base.mouseIsOver = false;
			})
			
		}

		base.goRight = function (_hM) {	
			if (!base.mouseIsOver) {
				if (!_hM) _hM = 1;
				$(_gallery).stop('true','true'); //complete any animation still running - in case anyone's a bit click happy... 
		    	base.rePos();
		      	var $newLeft = $(_gallery).position().left-(_hM*$imgWidth);//calculate the new position which is the current position minus the width of one image
		      
		      	$(_gallery).animate({'left':$newLeft+'px'},{duration:transSpeed, easing:easeType, complete: function(){//slide to the new position...
		        if (Math.abs($newLeft) == (($imgCount+_hM)*$imgWidth)) //...and if the slider is displaying the last image, which is the clone of the first image...
		         	{
		            $(_gallery).css({'left':-_hM*$imgWidth+'px'});//...reset the slider back to the first image without animating 
		            }
		    }});
		    
		    return false;
		    
			}		
			
		}
		
		base.goLeft = function (_hM) {
			if (!_hM) _hM = 1;
			$(_gallery).stop('true','true'); //complete any animation still running  
			base.rePos();
		      var $newLeft = $(_gallery).position().left+(_hM*$imgWidth);//calculate the new position which is the current position plus the width of one image
		      $(_gallery).animate({'left':$newLeft+'px'}, {duration:transSpeed, easing:easeType, complete: function(){//slide to the new position
		         if (Math.abs($newLeft) == (0)) //if the slider is displaying the first image, which is the clone of the last image
		            {
		            $(_gallery).css({'left':-($imgCount)*$imgWidth+'px'});//reset the slider back to the last image without animating 
		            }
			}});
				
		   	return false;
		}


		base.rePos = function () {
			/*var i = $(_gallery + " li:nth-child("+_curImg+") img");
		
			if (i.width() > i.height()) {				
				i.css('width',i.width()+'px')
			} else {
				
			}*/
		}	
		
		
        base.init();
	};
	
})(jQuery);





