(function($) {

	$.event.special.mousewheel = {

		setup: function() {

			if (this.addEventListener)
				this.addEventListener('DOMMouseScroll', $.event.special.mousewheel.handler, false);

			this.onmousewheel = $.event.special.mousewheel.handler;

		},

		tearright: function() {

			if (this.removeEventListener)
				this.removeEventListener('DOMMouseScroll', $.event.special.mousewheel.handler, false);

			this.onmousewheel = null;
		},

		handler: function( event ) {

			event = $.event.fix(event || window.event);

			event.type = "mousewheel";
			event.delta = 0;

			if ( event.wheelDelta < 0 || event.detail > 0 )
				event.delta = -1;
			else if (  event.wheelDelta > 0 || event.detail < 0 )
				event.delta = 1;

			$.event.handle.apply( this, [event] );

			return ;
		}
	};

	
	
	$.fn.scroll = function(options) {
		
		options = $.extend({
			width: 600,
			height: 100,
			
			button_img_left: "/bitrix/templates/reeferservice/img/main/scroll/btn-left.gif",
			button_img_right: "/bitrix/templates/reeferservice/img/main/scroll/btn-right.gif",
			button_width: 17,
			button_height: 14,
			
			bar_img_left: "/bitrix/templates/reeferservice/img/main/scroll/btn1.gif",
			bar_img_middle: "/bitrix/templates/reeferservice/img/main/scroll/btn2.gif",
			bar_img_right: "/bitrix/templates/reeferservice/img/main/scroll/btn3.gif",
			bar_drag: "/bitrix/templates/reeferservice/img/main/scroll/dragbar.gif",
			bar_height: 14,
			bar_width: 17,
			
			scroll_speed: 600
		}, options);
		

		if (options.button_width * 2 > options.width)
		{
			options.width = options.button_width * 2;
		}
		
		$(this).each( function(i) {

			if ( $(this).attr("id") == '' )
			{
				$(this).attr("id", 'scrollingrnd' + i);
			}
			
			var id_prosc = 'prosc';
			var id_rnd = $(this).attr("id") + id_prosc;
			var id_box = 'box' + id_rnd;
			var id_content = 'content' + id_rnd;
			var id_bar = "bar" + id_rnd;
			var id_line = "line" + id_rnd;
			var id_left = "up" + id_rnd;
			var id_right = "down" + id_rnd;

			code_bar  = '<table width="100%" height="100%" id="' + id_bar + '"  style="background: url(' + options.bar_img_middle + ') repeat-x;"><tr>';
			code_bar += '<td height="14" width="4" style="background: url(' + options.bar_img_left + ') no-repeat left top;"></td>';
			code_bar += '<td></td>';
			code_bar += '<td height="14" width="4" style="background: url(' + options.bar_img_right + ') no-repeat right top;"></td>';
			code_bar += '</tr></table>';

			code_scrollbar  = '<div style="position:absolute; top:-10px;"><table height="' + options.bar_height + '" width="' + options.width + '"><tr>';
			code_scrollbar += '<td width="' + options.button_width + '" height="' + options.button_height + '"><a href="" id="' + id_left + '"><img src="' + options.button_img_left + '" border="0"></a></td>';
			code_scrollbar += '<td style="background: url(' + options.bar_drag + ') repeat-x;" valign="top" id="' + id_line + '">' + code_bar + '</td>';
			code_scrollbar += '<td width="' + options.button_width + '" height="' + options.button_height + '"><a href="" id="' + id_right + '"><img src="' + options.button_img_right + '" border="0"></a></td>';
			code_scrollbar += '</tr></table></div>';

			code_box  = '<table id="' + id_box + '" width="' + options.width + '" height="' + options.height + '">';
			code_box += '<tr><td><div style="position:relative; overflow:hidden; height:' + (options.height - options.bar_height) + 'px; width:' + options.width + 'px; ' + options.box_style + ' ">';
			code_box += '<div id="' + id_content + '" style="position:absolute; left:0px; top:6px; visibility: visible;">' + $(this).html() + '</div></div></td></tr><tr><td width="' + options.bar_width + '"><div style="position: relative;"><div style="height:' + options.bar_height + '">&nbsp;</div>' + code_scrollbar + '</div></td></tr><table>';

			$(this).replaceWith(code_box);
			
			var bar_left = $('#' + id_line).position().left;		

			var content_width = 0;
			var content_left = 0;
			var bar_width_max = options.width - options.button_width * 2;
			var bar_width_cur = 0;
			var bar_left_max = 0;
			var bar_left_cur = 0;
			var bar_move = false;
			var bar_left_max = 0;
			var headline_interval;
			var interval;
            var max_x;
            var isRunning = false;        
            var bar_dest = 1;
            var bar_dist = 2;
            var bar_sec = 25;     
            
			setInterval(function() {

				var content_width_true = parseInt($('#' + id_content).innerWidth());

				if (content_width == content_width_true)
					return;
				
				content_width = content_width_true;
				
				content_left = options.width - content_width;

				if (content_width > options.width)
				{
					bar_width_cur = bar_width_max * options.width / (content_width*3);
				} else {
					bar_width_cur = bar_width_max;
				}
				bar_left_max = bar_width_max - bar_width_cur;
				max_x = bar_left_max;

				$('#' + id_bar).css({
					'width' : bar_width_cur
				});
				
				 moveBar(bar_dest, bar_dist, bar_sec);
			}, 500);         
             
            function moveBar(d, dist, sec)
            {
                //если нужно, чтобы скролл ходил туда-обратно, раскомментируйте условия
                /*if(bar_left_cur == 0 && d==0)
                {
                    clearInterval(headline_interval);
                    headline_interval = setInterval(function(){moveBar(1, bar_dist, bar_sec)}, 600);                
                    return;    
                }                    
                if(bar_left_cur >= Math.floor(max_x) && d==1)
                {
                    clearInterval(headline_interval);
                    headline_interval = setInterval(function(){moveBar(0, bar_dist, bar_sec)}, 600); 
                    return;              
                }*/

                /*if(bar_left_cur >= Math.floor(max_x) && d==1)
                {
                    bar_move = false;
                    return;
                }*/
                
                if(isRunning)
                {
                    bar_move = false;
                    return;
                }
                    
                bar_left_get();

				$('#' + id_content).stop();
				$('#' + id_bar).stop();	

                if(d == 1)
				    move_content_bar(bar_left_cur + dist, sec, false);
                else				        
                    move_content_bar(bar_left_cur - dist, sec, false); 
                                     
                clearInterval(headline_interval);
                headline_interval = setInterval(function(){moveBar(d, dist, sec)}, sec);                
            }
            
			$('#' + id_right).bind("click", function(e) {

				bar_left_get();

				$('#' + id_content).stop();
				$('#' + id_bar).stop();

				move_content_bar(bar_left_cur + 100, options.scroll_speed, false);

				return false;
			});
			
			$('#' + id_left).bind("click", function(e) {

				bar_left_get();

				$('#' + id_content).stop();
				$('#' + id_bar).stop();

				move_content_bar(bar_left_cur - 100, options.scroll_speed, false);

				return false;
			});		
		
           $('#' + id_left).bind("mousedown", function(e) {           		
                StartScrollPics(0, 100, 600);                
                
				$("body").one("mouseup", function(e) {	
				    clearInterval(headline_interval);
					$("body").unbind("mousemove");					
					bar_move = false;
					isRunning = false;
					moveBar(0, bar_dist, bar_sec);
				}); 
				                
				return false;		
			});	
											
           $('#' + id_right).bind("mousedown", function(e) {                 			
                StartScrollPics(1, 100, 600);                
                
				$("body").one("mouseup", function(e) {
					$("body").unbind("mousemove");
					clearInterval(headline_interval);
					bar_move = false;
					isRunning = false;
					moveBar(1, bar_dist, bar_sec);
				}); 
				                
				return false;		
			});		
			
            function StartScrollPics(d, dist, sec)
            { 
                bar_move = true;
                var dest = d;
                bar_left_get();

				$('#' + id_content).stop();
				$('#' + id_bar).stop();				

                if(dest == 1)
				    move_content_bar(bar_left_cur + dist, sec, false);
                else				        
                    move_content_bar(bar_left_cur - dist, sec, false); 
                                     
                clearInterval(headline_interval);
                headline_interval = setInterval(function(){ StartScrollPics(dest, dist, sec)}, sec);                                       
            }

			$('#' + id_bar).bind("mousedown", function(e) {
				if (bar_move) return false;
				bar_move = true;
				isRunning = true;
                clearInterval(headline_interval);	
				
				bar_left_get();
				bar_left_cur = (e.pageX - bar_left) - bar_left_cur;

				$("body").bind("mousemove", function(e) {
					bar_move = true;
                    isRunning = true;
                    
					move_margin = (e.pageX - bar_left) - bar_left_cur;
					move_content_bar(move_margin, 0, true);

					return false;
				});

				$("body").one("mouseup", function(e) {
					$("body").unbind("mousemove");
					bar_move = true;
					isRunning = true;
					
                    interval = setInterval(function(){
                        clearInterval(interval);                
                        var d = ((e.pageX-((screen.width - 990)/2)) > bar_left_cur)? 1 : 0;
                        isRunning = false;                  
                        moveBar(d, bar_dist, bar_sec);
                        bar_move = false;
                        clearInterval(interval);
                    }, 600); 					
				});

				return false;
			});


			$('#' + id_line).bind("click", function(e) {
				if (bar_move) return false;	
                isRunning = true;
                clearInterval(headline_interval);
				bar_move = true;

				bar_left_get();			
						
				move_margin = ((e.pageX-((screen.width - 990)/2)) - bar_left - bar_width_cur / 2);
				move_content_bar(move_margin, 600, false);				
				
                interval = setInterval(function(){
                    clearInterval(interval);                
                    var d = ((e.pageX-((screen.width - 990)/2)) > bar_left_cur)? 1 : 0;
                    isRunning = false;                  
                    moveBar(d, bar_dist, bar_sec);
                    bar_move = false;
                    }, 700);               
                
				return false;
			});


			$('#' + id_box).bind('mousewheel', function (event) {	
			    var f = event.delta;			    
			    fun(f);
			});
			
			function fun(f)
			{
                var d = (-1 * f > 0)? 1 : 0;
                bar_dest = d;
                clearInterval(headline_interval);               
                moveBar(d, bar_dist, bar_sec);   
                
                return false;		
			}

			function bar_left_get()
			{
                    if (bar_left != $('#' + id_line).position().left )				
					    bar_left = $('#' + id_line).position().left;

				    bar_left_cur = parseInt($('#' + id_bar).css('marginLeft'));
				    bar_left_cur = (isNaN(bar_left_cur) ? 0 : bar_left_cur);    				    
                
				    return;
			}

			function move_content_bar(left, sec, move)
			{
    			if (left > bar_left_max)
					left = bar_left_max;
				else if (left < 0)
					left = 0;
					
                if(left>770)					
                    $('.phone').fadeOut('slow');
                else                    		
                    $('.phone').fadeIn('slow');
              
                if(left>100 && left < 500)
                    $('.faq p').fadeOut('slow');
                else                    		
                    $('.faq p').fadeIn('slow');                                                        

				$('#' + id_bar).animate({marginLeft: left + "px"}, sec)

    			left = left * content_left / bar_left_max;
				
				$('#' + id_content).animate({"left": left + "px"}, sec, function () {
					bar_move = move;
				});		
			}
		});
	}		
})(jQuery);


