// JavaScript Document
var smothScroll={
	_smothScroll:function(inicio,fin){
		var _this=this;
		if(window['globalIntervalo'])
			return;
		window['globalIntervalo']=1;
		var t=new Transition(SineCurve, 1000, function(percentage) {
			var delta=fin-inicio;
			if(fin<inicio){
				var delta=inicio-fin;
    			scrollTo(0,inicio-(percentage*delta));
			}
			else
				scrollTo(0,inicio+(percentage*delta));
			});
		t.run();
		return this;
	},
	getElementPosition:function() {
		var offsetTrail = this;
		var offsetLeft = 0;
		var offsetTop = 0;
		while (offsetTrail) {
			offsetLeft += offsetTrail.offsetLeft;
			offsetTop += offsetTrail.offsetTop;
			offsetTrail = offsetTrail.offsetParent;
		}
		
		return {left:offsetLeft, top:offsetTop};
	} 
}
panino.add(smothScroll); 
var Transicion={
		transition:function(curve, milliseconds, callback) {
			this.done_=false;
    		this.curve_ = curve;
    		this.milliseconds_ = milliseconds;
    		this.callback_ = callback;
    		this.start_ = new Date().getTime();
    		var me = this;
    		this.runCallback_ = function() {
       			me.run();
   		 	};
		},
		run : function() {
   			if(!this.hasNext()) {
				window['globalIntervalo']=0;
				return;
			}
    		this.callback_(this.next());
    		setTimeout(this.runCallback_, 30);
		},
		hasNext : function() {
    		if(this.done_)
				return this.oneLeft_;
    		var now = new Date().getTime();
    		if ((now - this.start_) > this.milliseconds_) {
       			this.done_ = true;
        		this.oneLeft_ = true;
    		}
    		return true;
		},
		next : function() {
    		this.oneLeft_ = false;
    		var now = new Date().getTime();
    		var percentage = Math.min(1, (now - this.start_) / this.milliseconds_);
			return this.curve_(percentage);
		},
		SineCurve:function(percentage) {
			return (1 - Math.cos(percentage * Math.PI)) / 2;
		},
		LinearCurve:function(percentage) {
    		return percentage;
		}
}

panino.add(Transicion);
function agregarSuavizado(idObjeto,idObjetoDestino){
	$(idObjeto).addEvent('click',function(){this._smothScroll(this.getElementPosition().top,$(idObjetoDestino).getElementPosition().top);});
}

