var move_flag = false;

(function($){
	$.fn.innova_carousel = function(){

    	var f = 0; // 3 - вначале немного двигать за курсором, иначе - 0
        var movefrom = 50; // сколько нужно отодвинуть мышкой чтобы не вернулась назад
        var duration = 500;
        var duration_back = 2000;
        var display_count = 4;

    	var oViewport = $(this);
        var oContent = $(".overview", oViewport);
    	var oPages = oContent.children();
        var iPageSize = $(oPages[0]).outerWidth(true);

       if(oPages.length < display_count) // если их меньше чем нужно выводить
            return;

        var nechet = oPages.length % display_count; // если на экране нечетное количество страниц

        if(nechet) { 
            for(i=1; i<display_count; i++)
                oPages.clone().appendTo(oContent);
            oPages = oContent.children();
        }
        else
           oPages.clone().appendTo(oContent);

        oPages.clone().appendTo(oContent);
/*
        if(oPages.length == display_count) // если их меньше чем нужно выводить
            oPages.clone().appendTo(oContent);
*/    

    	ml = -iPageSize * display_count;

        oContent.width(oContent.children().length * iPageSize);
        oContent.css('margin-left',ml);

        var iContentSize = oContent.width();
    
        if ($('#icursorw').length == 0)
            $('body').append('<div id="icursorw"></div>');
        cursor = $('#icursorw');
    
        oContent.mouseover(function(event) {
            cursor.show();
            return true;
        });
        oContent.mousemove(function(event) {
            cursor.css({
                top:event.pageY,
                left:event.pageX
            });
            return true;
        });
        oContent.mouseout(function(event) {
            cursor.hide();
            return true;
        });
    
    
    	if ($.browser.mozilla) {
    		oContent.css({
    			'MozUserSelect': 'none'
    		})
    	} else if ($.browser.msie) {
    		oContent.bind('selectstart', function () {
    			return false
    		})
    	} else {
    		oContent.mousedown(function () {
    			return false
    		})
    	}
        
    	d = 0; // считает насколько отодвинулся курсор
        mp = 0;
        
     	oContent.mouseup(function (e) {

  			d = mp - e.pageX;
            move_flag = d;

    		if (f != 2) {
    			f = 0;
    			if (Math.abs(d) < movefrom+1) {
                    d = 0;
    				oContent.animate({
    					marginLeft: ml
    				},
    				duration_back, function () {
    					f = 0; 
    				})
    			}
    		}
    	}).mousedown(function (e) {
    		if (f != 2) {
    			oContent.stop();
    			mp = e.pageX;
    			ml2 = this.offsetLeft;
    			f = 1
    		}
    	}).mousemove(function (e) {
    		if (f == 1) {
    			d = mp - e.pageX;
    			if (d > movefrom) {
    				f = 2;
    				ml -= iPageSize * display_count;
    				$(this).animate({
    					marginLeft: ml
    				},
    				{
    					duration: duration,
    					easing: "easeOutQuad",
    					complete: function () {
    						f = 0;
    						if (iPageSize * display_count-this.offsetLeft == iContentSize) {
                                ml = -iPageSize * (oPages.length - display_count);
    							$(this).css({
    								marginLeft: ml
    							});
    						}
    					}
    				})
    			} else if (d < -movefrom) {
    				f = 2;
    				ml += iPageSize * display_count;
    				$(this).animate({
    					marginLeft: ml
    				},
    				{
    					duration: duration,
    					easing: "easeOutQuad",
    					complete: function () {
    						f = 0;
    						if (this.offsetLeft == 0) {
    							ml -= oPages.length * iPageSize;
    							$(this).css({
    								marginLeft: ml
    							});
    						}
    					}
    				})
    			} else {
    				oContent.css({
    					marginLeft: ml2 - d
    				})
    			}
    		} else if (f == 3) {
    			$(this).css({
    				marginLeft: -iPageSize * display_count - (e.pageX - iPageSize) / (iPageSize * display_count / 100)
    			})
    		}
    	});        

	};
})(jQuery);


$(document).ready(function () {
    $('#slider-partners').innova_carousel();
});
