/* ------------------------------------------------------------------------
	Peel Slider
	
	Developped By: Alex Calko
	Version: 1.0
	
	Copyright: Feel free to redistribute the script/modify it, as
			   long as you leave my infos at the top.
------------------------------------------------------------------------- */

(function($){  

    $.fn.PeelSlider = function(vars) {       

    	var visible_images	= (vars.showImages != undefined) ? vars.showImages : 5;
    	var midle_pos		= Math.ceil(visible_images/2);
    	var current_center	= 0;

    	var speed_img		= (vars.timeOut != undefined) ? vars.timeOut : 200;
    	
    	var slide_id		= $(this).attr('id');
    	var prev_btn_id		= (vars.prevBtn != undefined) ? vars.prevBtn : 'prev_btn';
    	var next_btn_id		= (vars.nextBtn != undefined) ? vars.nextBtn : 'next_btn';
    	
    	
    	var img_width		= 0;
    	var count_images	= 0;
    	var total_width		= 0;

    	$('#'+slide_id+' li').each(function(i){
    		if(img_width==0)
    			img_width	= $(this).outerWidth(true);
    		count_images+=1;
    		
    		var el_obj	= $(this);
    		el_obj.find('img').mouseover(function() {
    			el_obj.find('div').slideDown('fast');
             });
            
    		el_obj.find('img').mouseout(function() {
    			el_obj.find('div').slideUp('fast');
            });
    		
    	});

    	total_width	= count_images*img_width;

    	$('#'+slide_id).width(total_width);
    	
    	current_center	= Math.ceil(count_images/2);
    	if(count_images<visible_images)
    	{
    		if(current_center==(count_images/2))
    			current_center+=1;
    	}

    	var cp	= midle_pos-current_center; 
    	leftpos	= cp*img_width;
    	
    	$('#'+slide_id).css('left', leftpos );
    	
    	// check which navigation buttons show/hide
    	var show_hide_nav = function(){
    		
    		if(current_center>midle_pos)	
    			$('#'+prev_btn_id).show();
    		else
    			$('#'+prev_btn_id).hide();

    		if((count_images-current_center)>=midle_pos)	
    			$('#'+next_btn_id).show();
    		else
    			$('#'+next_btn_id).hide();

    	}
    	
    	$('#'+next_btn_id+' a').click(function(){

    		var cn = count_images - (current_center+(midle_pos-1));
    		if(cn > visible_images)
    			cn	= visible_images;

    		var left_dec = cn*img_width;

    		$('#'+slide_id).animate({
    			left: '-='+left_dec
    		}, (speed_img*cn), function() {
    			
    		    current_center+=cn;

    		    show_hide_nav();

    		});
    	});    	
    	
    	$('#'+prev_btn_id+' a').click(function(){

    		var cp = current_center-midle_pos;
    		if(cp > visible_images)
    			cp	= visible_images;

    		var left_inc = cp*img_width;

    		$('#'+slide_id).animate({
    			left: '+='+left_inc
    		}, (speed_img*cp), function() {
    			
    		    current_center-=cp;

    		    show_hide_nav();

    		});
    	});

    	show_hide_nav();

    };  

})(jQuery);  
