var DragScrollable = Class.create();
DragScrollable.prototype = {
  initialize: function(element, tolerance, updown) {
    this.element = $(element);
    this.pending = false;
    this.active = false;
    this.scrolling = false;
    this.tolerance = Object.isNumber(tolerance) ? parseInt(tolerance) : 0;
    this.updown = Object.isUndefined(updown) ? false : updown;

    //this.element.style.cursor = 'pointer';

    this.eventMouseDown = this.startScroll.bindAsEventListener(this);
    this.eventMouseUp   = this.endScroll.bindAsEventListener(this);
    this.eventMouseMove = this.scroll.bindAsEventListener(this);

    Event.observe(this.element, 'mousedown', this.eventMouseDown);
  },
  destroy: function() {
    Event.stopObserving(this.element, 'mousedown', this.eventMouseDown);
    Event.stopObserving(document, 'mouseup', this.eventMouseUp);
    Event.stopObserving(document, 'mousemove', this.eventMouseMove);
  },
  startScroll: function(event) {
    this.startX = Event.pointerX(event);
    this.startY = Event.pointerY(event);
    var p = this.element.positionedOffset();
    if (Event.isLeftClick(event) &&
        (this.startX < p[0] + this.element.clientWidth) &&
        (this.startY < p[1] + this.element.clientHeight)) {
      this.element.style.cursor = 'move';
      Event.observe(document, 'mouseup', this.eventMouseUp);
      Event.observe(document, 'mousemove', this.eventMouseMove);
      this.pending = true;
      /*this.active = true;
      Event.stop(event);
      this.element.fire('scroll:start');*/
    }
  },
  endScroll: function(event) {
    this.element.style.cursor = 'pointer';
    var wasActive = this.active;
    if (this.active || this.pending)
    {
    	this.active = false;
    	this.pending = false;
    	Event.stopObserving(document, 'mouseup', this.eventMouseUp);
    	Event.stopObserving(document, 'mousemove', this.eventMouseMove);
    }
	if (wasActive)
	{
		this.element.fire('scroll:stop');
    	Event.stop(event);
    }
  },
  scroll: function(event) {
  	if (this.pending)
  	{
  	  if ((Math.abs(this.startY - Event.pointerY(event))>this.tolerance) ||
  	  	(Math.abs(this.startX - Event.pointerX(event))>this.tolerance))
  	  {
  	    this.pending = false;
	    this.active = true;
        this.element.fire('scroll:start');  	
      }
  	}
  	if (this.active) {
  		if (this.updown)
      		this.element.scrollTop += (this.startY - Event.pointerY(event));
      	else
      		this.element.scrollLeft += (this.startX - Event.pointerX(event));
      this.startX = Event.pointerX(event);
      this.startY = Event.pointerY(event);
      this.element.fire('scroll:scroll');
    }
    Event.stop(event);
  },
  observe: function(event, method)
  {
  	this.element.observe(event, method);
  }
};



Effect.Scroll = Class.create(); 
Object.extend(Object.extend(Effect.Scroll.prototype, Effect.Base.prototype), { 
	initialize: function(element) { 
		this.element = $(element); 
		var options = Object.extend({ 
			x:    0, 
			y:    0, 
			mode: 'absolute' 
		} , arguments[1] || {}  ); 
		this.start(options); 
	}, 
	setup: function() { 
		if (this.options.continuous && !this.element._ext ) { 
			this.element.cleanWhitespace(); 
			this.element._ext=true; 
			this.element.appendChild(this.element.firstChild); 
		} 

		this.originalLeft=this.element.scrollLeft; 
		this.originalTop=this.element.scrollTop; 

		if(this.options.mode == 'absolute') { 
			this.options.x -= this.originalLeft; 
			this.options.y -= this.originalTop; 
		} else { 
		} 
	}, 
	update: function(position) {     
		this.element.scrollLeft = this.options.x * position + this.originalLeft; 
		this.element.scrollTop  = this.options.y * position + this.originalTop; 
	} 
}); 


Effect.FadeNoHide = function(element) {
  element = $(element);
  var oldOpacity = element.getInlineOpacity();
  if (!element.getOpacity())
  	return;
  var options = Object.extend({
    from: element.getOpacity() || 1.0,
    to:   0.0,
    afterFinishInternal: function(effect) { 
      if (effect.options.to!=0) return;
      element.setOpacity(0);
    }
  }, arguments[1] || { });
  return new Effect.Opacity(element,options);
};


Effect.divSwap = function(element,container){
    var div = document.getElementById(container);
    var nodeList = div.childNodes;
    var queue = Effect.Queues.get('menuScope');

    if(queue.toArray().length<1){
        if(Element.visible(element)==false){
            for(i=0;i<nodeList.length;i++){
                if(nodeList.item(i).nodeName=="DIV" && nodeList.item(i).id!=element){
                    if(Element.visible(nodeList.item(i))==true){
                        Effect.Fade(nodeList.item(i),{queue:{position:'end',scope:'menuScope',limit:2},duration:0.3})
//                      Effect.SlideUp(nodeList.item(i),{queue:{position:'end',scope:'menuScope',limit:2},duration:0.5})
                    }
                }
            }
            Effect.Appear(element,{queue:{position:'end',scope:'menuScope',limit:2},duration:0.5})
//          Effect.SlideDown(element,{queue:{position:'end',scope:'menuScope',limit:2},duration:0.5})
            return true;
       }
   }
   return false;
}