jQuery.fn.dgallery = function(options) {


    var options = jQuery.extend({
        scroll: true
    }, options);

    var Pressed = false;
    var Scrolling = false;
    $(document).bind('mouseup', function(e) {
        Pressed = false;
        Scrolling = false;
    })

    return this.each(function() {

        var el = jQuery(this);

        var scrollbox = $("div.double_gallery_slider_stroke div", el);
        options.scroll = scrollbox.size() < 1 ? false : true;

        if (options.scroll) {
            var scrollBoxW = $(scrollbox).width();
            var scrollbar = $("div.double_gallery_slider_stroke", el);
            var scrollBarW = $(scrollbar).width();
            //l("scrollBarW = " + scrollBarW);
            var curLeft = $(scrollbar).offset().left;
        } else {
            var scrollBoxW = 0;
            var scrollbar = $("div.double_gallery_clip", el);
            var scrollBarW = $(scrollbar).width();
            var curLeft = 0;
        }

        var pC = $("div.double_gallery_lenta", el);
        var pCwidth = $(pC).width();
        var Xtouched = 0;
        var ScrollbarTimer;
        var scrollW = 0;
        //var scrollBarW=0;

        var productsContainterW = $('div.double_gallery_lenta_item', el).size() * ( $('div.double_gallery_lenta_item:last', el).width() + parseInt($('div.double_gallery_lenta_item:first', el).css("margin-right")) + parseInt($('div.double_gallery_lenta_item:first', el).css("padding-right")) );//+12+20;
        l(productsContainterW + " Size: " + $('div.double_gallery_lenta_item', el).size());
        $(pC).width(productsContainterW);//margin-right=12px!!!!
        pCwidth = productsContainterW;
        
        el.find('div.double_gallery_arrow').not(".double_gallery_arrow_right").addClass("inact");                                             
        if (scrollBarW >= pCwidth) {
            //alert("Нихуя не выйдет");
            l(scrollBarW + "  > " + pCwidth);
            //el.find('div.double_gallery_arrow').not(".double_gallery_arrow_right").addClass("inact");
            el.find(".double_gallery_arrow_right").addClass("inact");
        } else {
            if (!options.scroll) {
                el.find('div.double_gallery_arrow').not(".double_gallery_arrow_right").bind('mousedown', function () {
                    Scrolling = true;
                    if (ScrollbarTimer) {
                        clearTimeout(ScrollbarTimer);
                    }
                    scroll();
                    Scrolling = true;
                    if (ScrollbarTimer) {
                        clearTimeout(ScrollbarTimer);
                    }
                    scroll();
                    function scroll() {
                        if (Scrolling) {
                            /*curLeft=parseFloat($(pC).css('left'));
                             newLeft = curLeft + 100; // pCwidth/6;

                             // if(newLeft < scrollBarW - pCwidth) { newLeft = scrollBarW - pCwidth;}
                             if(newLeft > 0) { newLeft = 0;}
                             */

                            curLeft = parseFloat($(pC).css('left'));
                            item = $('div.double_gallery_lenta_item:first', el);
                            //l(item)
                            newLeft = curLeft + ($('div.double_gallery_lenta_item:first', el).width() + parseInt($('div.double_gallery_lenta_item:first', el).css("margin-right")) + parseInt($('div.double_gallery_lenta_item:first', el).css("padding-right")) ); // pCwidth/6;
                            l("left");
                            if (newLeft > 0) {
                                newLeft = 0;
                                el.find('div.double_gallery_arrow').not(".double_gallery_arrow_right").addClass("inact");
                                l("0px -40px left");
                            } else {
                                l("0px 0px left");
                                el.find('div.double_gallery_arrow').not(".double_gallery_arrow_right").removeClass("inact");
                            }


                            $(pC).animate({ left:  (newLeft) }, 300, function() {
                                l(newLeft);
                            });

                            ScrollbarTimer = setTimeout(function () {
                                scroll()
                            }, 300);
                        }
                    }

                    return false;
                });

                el.find('div.double_gallery_arrow_right').bind('mousedown', function () {
                    Scrolling = true;
                    el.find('div.double_gallery_arrow').not(".double_gallery_arrow_right").removeClass("inact");
                    if (ScrollbarTimer) {
                        clearTimeout(ScrollbarTimer);
                    }
                    scroll();
                    function scroll() {
                        if (Scrolling) {
                            /*curLeft=parseFloat($(pC).css('left'));
                             newLeft = curLeft - 100; // pCwidth/6;

                             if(newLeft < scrollBarW - pCwidth) { newLeft = scrollBarW - pCwidth;}
                             */
                            curLeft = parseFloat($(pC).css('left'));
                            item = $('div.double_gallery_lenta_item:first', el);
                            //l(item)
                            newLeft = curLeft - ($('div.double_gallery_lenta_item:first', el).width() + parseInt($('div.double_gallery_lenta_item:first', el).css("margin-right")) + parseInt($('div.double_gallery_lenta_item:first', el).css("padding-right")) ); // pCwidth/6;
                            if (newLeft < scrollBarW - pCwidth) {
                                newLeft = scrollBarW - pCwidth;
                                el.find(".double_gallery_arrow_right").addClass("inact");
                            } else {
                                el.find(".double_gallery_arrow_right").removeClass("inact");
                            }
                            // newLeft = scrollBarW - pCwidth;

                            $(pC).animate({ left:  (newLeft) }, 300, function() {
                                l(newLeft);
                            });

                            ScrollbarTimer = setTimeout(function () {
                                scroll()
                            }, 300);
                        }
                    }

                    return false;
                });


            }
            else {


                function ScrollTo(params) {
                    scrollBarW = $(scrollbar).width();
                    var newX = params.newX;
                    // l("f " + scrollBarW + " | " + params.newX);
                    if (params.animation) {
                        $(scrollbox).stop(0, 0);
                        $(pC).stop(0, 0);

                        //l($(scrollbar).width() * (newX) / $(pC).width());
                        //var Scroll = ($(pC).width()-scrollBarW) * (scrollBoxL / scrollBarW);
                        $(scrollbox).animate(
                        {
                            left: - ($(scrollbar).width() - scrollBoxW) * ( (newX) / ($(pC).width() - $(scrollbar).width()) )
                        }, params.animation);

                        /*                    if(productsContainterW - $(pC).width() < newX){
                         newX = -(productsContainterW - $(pC).width());
                         }*/
                        $(pC).animate(
                        {
                            left: (newX)
                        }, params.animation,
                                function() {
                                    //l("left = " + $(pC).css("left"));
                                });

                    }
                    else {
                        $(scrollbox).css('left', - $(scrollbar).width() * ( (newX) / $(pC).width()));
                        $(pC).css('left', newX);
                    }


                }

                $(scrollbox).bind('mousedown', function(e) {
                    Xtouched = (e.pageX - $(scrollbox).offset().left);
                    Pressed = true;
                    //            delement =
                    return false;
                });

                $(scrollbox).bind('mousemove', function () {
                    Scrolling = false;
                });
                $(scrollbar).bind('mouseleave', function () {
                    Scrolling = false;
                });

                if (el.parent().is(".gallery_saver")) {
                    moveel = el.parent();
                } else {
                    moveel = el;
                }
                $(moveel).bind('mousemove', function(e) {
                    if (Pressed) {
                        scrollBarW = $(scrollbar).width();
                        var newPos = e.pageX - el.offset().left - Xtouched;// - Xtouched - curLeft;
                        if (newPos < 0) {
                            newPos = 0
                            el.find('div.double_gallery_arrow').not(".double_gallery_arrow_right").addClass("inact");
                        } else {
                            el.find('div.double_gallery_arrow').not(".double_gallery_arrow_right").removeClass("inact");
                        }

                        if (newPos + scrollBoxW > scrollBarW) {
                            newPos = scrollBarW - scrollBoxW;
                            el.find(".double_gallery_arrow_right").addClass("inact");
                        } else {
                            el.find(".double_gallery_arrow_right").removeClass("inact");
                        }
                        var Scroll = ((pCwidth - scrollBarW) * (newPos) / (scrollBarW - scrollBoxW));
                        $(scrollbox).css('left', newPos);
                        //l(scrollBarW);
                        //l($(pC).length);

                        $(pC).css('left', -Scroll);
                        return false;
                    }
                    else {
                        return true;
                    }
                });


                el.find('div.double_gallery_arrow').not(".double_gallery_arrow_right").bind('mousedown', function () {
                el.find(".double_gallery_arrow_right").removeClass("inact");
                    Scrolling = true;
                    if (ScrollbarTimer) {
                        clearTimeout(ScrollbarTimer);
                    }

                    function scroll() {
                        if (Scrolling) {
                            curLeft = parseFloat($(pC).css('left'));
                            // var item = $('div.double_gallery_lenta_item',el).first();
                            //l(item)
                            newLeft = curLeft + ($('div.double_gallery_lenta_item:first', el).width() + parseInt($('div.double_gallery_lenta_item:first', el).css("margin-right")) + parseInt($('div.double_gallery_lenta_item:first', el).css("padding-right")) ); // pCwidth/6;
                            //if(newLeft > 0) { newLeft = 0;}
                            if (newLeft >= 0) {
                                newLeft = 0;
                                el.find('div.double_gallery_arrow').not(".double_gallery_arrow_right").addClass("inact");
                            } else {
                                el.find('div.double_gallery_arrow').not(".double_gallery_arrow_right").removeClass("inact");
                            }
                            el.find(".double_gallery_arrow_right").removeClass("inact");
                            Scroll = -newLeft;

                            //	log('leftarrow scroll');
                            /*curLeft=parseFloat($(scrollbox).css('left'));
                             var maxScroll = scrollBarW / 6;
                             var scrollRatio=curLeft-maxScroll;
                             if(scrollRatio <0) {scrollRatio=0;}
                             var Scroll =  ($(pC).width()-scrollBarW) * (scrollRatio / (scrollBarW-scrollBoxW));
                             */

                            ScrollTo({'newX':-Scroll,'animation':300});
                            curLeft = $(scrollbar).offset().left;
                            ScrollbarTimer = setTimeout(function () {
                                scroll()
                            }, 300);
                        }

                    }

                    scroll();
                    return false;
                });


                el.find('div.double_gallery_arrow_right').bind('mousedown', function () {
                    Scrolling = true;
                    if (ScrollbarTimer) {
                        clearTimeout(ScrollbarTimer);
                    }
                    function scroll() {
                        if (Scrolling) {
                            curLeft = parseFloat($(pC).css('left'));
                            //  item = $('div.double_gallery_lenta_item:first',el);
                            //l(item)
                            newLeft = curLeft - ($('div.double_gallery_lenta_item:first', el).width() + parseInt($('div.double_gallery_lenta_item:first', el).css("margin-right")) + parseInt($('div.double_gallery_lenta_item:first', el).css("padding-right")) ); // pCwidth/6;

                            //alert(newLeft);
                            
                            //if(newLeft < scrollBarW - pCwidth) { newLeft = scrollBarW - pCwidth;}
                            if (newLeft <= scrollBarW - pCwidth) {
                                newLeft = scrollBarW - pCwidth;
                                el.find(".double_gallery_arrow_right").addClass("inact");

                            }
                            else
                            {
                                el.find(".double_gallery_arrow_right").removeClass("inact");
                            }
                            el.find('div.double_gallery_arrow').not(".double_gallery_arrow_right").removeClass("inact");

                            /*
                             curLeft=parseFloat($(scrollbox).css('left'));
                             var maxScroll = scrollBarW / 6;
                             var scrollRatio=curLeft+maxScroll;
                             if(scrollRatio+(scrollBoxW)>scrollBarW) { scrollRatio=scrollBarW - (scrollBoxW);}
                             var Scroll =  ($(pC).width()-scrollBarW) * (scrollRatio / (scrollBarW-scrollBoxW));
                             */
                            Scroll = -newLeft;

                            ScrollTo({'newX':-Scroll,'animation':300});
                            curLeft = $(scrollbar).offset().left;
                            ScrollbarTimer = setTimeout(function () {
                                scroll()
                            }, 300);
                        }
                    }

                    scroll();

                    return false;
                });


                function setSize() {
                    //scrollW=$(window).width()- $(".leftbox").width()-13;
                    //$('.scroll').width(scrollW);
                    //$(scrollbox).width(scrollBarW* (scrollW /productsContainterW) );
                    /*
                     l("---");
                     l("scrollBarW = " + scrollBarW);
                     l("( parseFloat($(pC).css('left'))/productsContainterW) = " + ( parseFloat($(pC).css('left'))/productsContainterW));
                     l("---");
                     */

                    scrollBarW = $(scrollbar).width();
                    $(scrollbox).css('left', -scrollBarW * ( parseFloat($(pC).css('left')) / productsContainterW));
                    scrollBoxW = $(scrollbox).width();
                    pCwidth = productsContainterW;
                    var newPos = parseFloat($(scrollbox).css('left'));
                    if (newPos + scrollBoxW > scrollBarW) {
                        newPos = scrollBarW - scrollBoxW;
                    }
                    var Scroll = (productsContainterW * (newPos) / scrollBarW);
                    $(scrollbox).css('left', newPos);
                    $(pC).css('left', -Scroll);
                }

                setSize();
                el.find('div.double_gallery_arrow').not(".double_gallery_arrow_right").addClass("inact");

                $(window).bind('resize', function() {
                    setSize();
                });

            }
        }
    });

};