jQuery.fn.is_simple_slider = function(options){
    var items_wrapper = $(this);
    
    var defaults = {
        arr_left: false,// object of left arrow
        arr_right: false,// object of right arrow
        arr_top: false,// object of top arrow
        arr_bottom: false,// object of bottom arrow
        
        items_to_scroll_x: 0,
        add_scroll_to_x: 0,// this value adds to scroll width
        items_to_scroll_y: 0,
        add_scroll_to_y: 0,// this value adds to scroll width
        
        visible_items_x: 4, // number of items that user can see on a time
/*
        scroll_x_width: 0,// horizontal displacement of items_wrapper after click
        scroll_y_width: 0,// vertical displacement of items_wrapper after click
*/        
        duration: 'slow',// daration of item's movement
        animate_param: 'margin-',// wich css param to use for items_wrapper movement (ex.: margin-left: -20px or left: -20px)
        item_filter: '> div'// how to find item object from items_wrapper
    };
    
    // creating a common settings
    var settings = jQuery.extend({}, defaults, options);
    
    /* for error alert */
    var error = 'Simple photo slider Error:\n';
    var show_error = false;
    
    if(!settings.items_to_scroll_x && !settings.items_to_scroll_y){
        show_error = true;
        error += 'You must set the "items_to_scroll_x" or "items_to_scroll_y" option \n'
    }
    
    if(show_error){
        alert(error);
    }
    /* end of error alert */

    // binding a click event for objects
    if(settings.arr_left){
        settings.arr_left.click(function(){ move('left') });
    }
    if(settings.arr_right){
        settings.arr_right.click(function(){ move('right') });
    }
    if(settings.arr_top){
        settings.arr_top.click(function(){ move('top') });
    }
    if(settings.arr_bottom){
        settings.arr_botttom.click(function(){ move('botttom') });
    }
        

    
    // How many items do we have?
    var total_items = items_wrapper.find(settings.item_filter).length;
    // Object of first item
    var first_item = items_wrapper.find(settings.item_filter).filter(':first');
    if(settings.items_to_scroll_x){
        // horizontal displacement of items_wrapper after click
        var scroll_width_x = settings.items_to_scroll_x * (first_item.get(0).offsetWidth + settings.add_scroll_to_x);
    }
    if(settings.items_to_scroll_y){
        // vertical displacement of items_wrapper after click
        var scroll_width_y = settings.items_to_scroll_y * (first_item.get(0).offsetHeight + settings.add_scroll_to_y);
    }

    check_end();
    
    function check_end(){
        if(scroll_width_x){// if exists x-scroll
            behind_left_side = items_left('left');
            //TODO: если остаются два элемента, а скролят по три штуки за раз - тогда стрелочку все равно спрячет
            if(behind_left_side>0){
                settings.arr_right.css('visibility','visible');
            }else{
                settings.arr_right.css('visibility','hidden');
            }

            behind_right_side = items_left('right');
            if(behind_right_side>0){
                settings.arr_left.css('visibility','visible');
            }else{
                settings.arr_left.css('visibility','hidden');
            }
        }
        if(scroll_width_y){
            //TODO: create 'top' and 'bottom' checkes

        }
    }
    
    //counts how many items left on the side
    function items_left(side){
        items_left_count = 0;// remaining items behind the visible area
        items_wrapper_shift = Math.abs(parseInt(items_wrapper.css(settings.animate_param+side)));

        switch(side){
            case 'left':    items_left_count = Math.round(items_wrapper_shift / (scroll_width_x / settings.items_to_scroll_x));
                            //items_left_count = (items_left_count<0) ? 0 : items_left_count;
                            break;

            case 'right':   items_left_count = total_items - settings.visible_items_x - items_left('left');
                            break;
                            
            //TODO: create 'top' and 'bottom' cases 
        }
        
        return items_left_count;
    }
    
    function move(side){
        var animate_properties = {};
        var scroll_width = scroll_width_x;
        
        switch(side){
            case 'left':    var prefix = '-';
                            break;
                            
            case 'right':   var prefix = '+';
                            side = 'left';
                            break;

            case 'top':     var prefix = '-';
                            scroll_width = scroll_width_y;
                            break;
                            
            case 'bottom':  var prefix = '+';
                            side = 'top';
                            scroll_width = scroll_width_y;
                            break;
        }

        animate_properties[settings.animate_param+side] = prefix + "=" + scroll_width + "px";
        items_wrapper.animate(animate_properties, settings.duration, check_end);
    }
}
