var _hasscrollbar = true;

window.addEvent('domready', function(){
//                        var scroll2 = new Scroller('products', {area: 300, velocity: 1});
//
//                        function scroller()
//                        {
//                            function start()
//                            {
//                                console.log('start');
//                            }
//
//                            function stop()
//                            {
//                                console.log('stop');
//                            }
//                        }
//
//
//
//                        var scr = document.getElementById('scrollbar-scrollright');
//                        var scl = document.getElementById('scrollbar-scrollleft');
//                        var pro = document.getElementById('products');
//			// container
//			$(pro).addEvent('mouseover', scroll2.start.bind(scroller));
//			$(pro).addEvent('mouseout', scroll2.stop.bind(scroller));
		});


function makeScrollbar(content,scrollbar,handle,horizontal,ignoreMouse,startpoint){
	var fx = new Fx.Styles(content, {duration: 1000, wait: false});

	if(content != null && scrollbar != null && handle != null){
		var steps = (horizontal?(content.getSize().scrollSize.x - content.getSize().size.x):(content.getSize().scrollSize.y - content.getSize().size.y));

		var slider = new Slider(scrollbar, handle, {
			steps: steps,

			mode: (horizontal?'horizontal':'vertical'),
			onChange: function(step){
				// Scrolls the content element in x or y direction.
				var x = (horizontal?step:0);
				//x = startpoint + x;
				var y = (horizontal?0:step);
				content.scrollTo(x,y);
			}
		}).set(0);


		if( !(ignoreMouse) ){
			// Scroll the content element when the mousewheel is used within the
			// content or the scrollbar element.

			$$(content, scrollbar).addEvent('mousewheel', function(e){
				e = new Event(e).stop();
				var step = slider.step - e.wheel * 30;
				slider.set(step);
			});
		}
		//RW - 04/03/2010
		//Sets the sliders starting position
		slider.set(slider.step + Number(startpoint))


		//DA 18/01/2010
		//attach key listener


		document.addEvent('keydown', function(k){
			//console.log(k.keyCode);
			switch(k.keyCode) {
				//catch left btn
				case 37:
					slider.set(slider.step-249);
				break;
				//catch right btn
				case 39:
					slider.set(slider.step+249);
				break;
				//anything else
				default:
			}
		});

		// Stops the handle dragging process when the mouse leaves the document body.
		//$(document.body).addEvent('mouseleave',function(){slider.drag.stop();});
		//attach the left and right clicks :D
		if($('scrollbar-scrollleft') == null){
			$('scrollbar-scroll-up').addEvent('click',function(){
				slider.set(slider.step-50);
			});
			$('scrollbar-scroll-down').addEvent('click',function(){
				slider.set(slider.step+50);
			});
		}
		else {


                var scr = false;
                var scl = false;
                var time = 0;
                var stid = 0;
                
                $('scrollbar-scrollright').addEvent('mousedown',function(){
                        if (scr == false)
                            {
                                
                                scr = true;
                                scrollDirection("left");
                                //stid = setTimeout(function() { scrollDirection("left") },1);
                              
                          } else {
                                clearTimeout(stid);
                                scr = false;
                                scl = false;
                          }
				
			}).addEvent('mouseup',function(){ scr = false;
                                scl = false; });

                $('scrollbar-scrollleft').addEvent('mousedown',function(){
			 if (scl == false)
                            {
                             scl = true;
                             scrollDirection("right");
                             //stid = setTimeout(function() { scrollDirection("right") },1);
                          } else {
                             clearTimeout(stid);
                             scr = false;
                                scl = false;
                          }

			}).addEvent('mouseup',function(){scr = false;
                                scl = false; });


                var flow = {
                    startleft: function() {
                        while (scl == true)
                            {
                                scrollDirection(left);
                            }
                    },
                    startright: function() {
                        while (scr == true)
                            {
                                scrollDirection(right);
                            }
                    },
                    stop: function() {
                        scr = false;
                        scl = false;
                    }
                }

                function scrollDirection(direction)
                {
                    if (direction == "left")
                        {
                            for ($i = 0; $i < 2; $i++)
                                {
                                    slider.set(slider.step+3);

                                }
                        }
                        else
                        {
                             for ($i = 0; $i < 2; $i++)
                                {
                                    slider.set(slider.step-3);

                                }
                        }

            
                        if (scl != false)
                        {
                         stid = setTimeout(function() { scrollDirection(direction) },1);
                        }

                        if (scr != false)
                        {
                         stid = setTimeout(function() { scrollDirection(direction) },1);
                        }
                     
                }



            

            }

        }



		


}

/* preloading */
var images = ['/public/images/scroller/scrollbar-arrow-left-norm.gif','/public/images/scroller/scrollbar-arrow-left-over.gif','/public/images/scroller/scrollbar-arrow-right-norm.gif','/public/images/scroller/scrollbar-arrow-right-over.gif','/public/images/scroller/scrollbar-handle.gif','/public/images/scroller/scrollbar-bg.gif'];
var loader = new Asset.images(images, {});