function Timeline(config) {
		if (!(this instanceof arguments.callee))
			return new arguments.callee();
		
		this.nextMouseDown = false;
		this.previousMouseDown = false;
		this.move = false;
		this.currentRotation=null;
		this.draggableId = config.draggableId;
		this.itemClass = config.itemClass;
		this.width = config.width;
		this.height = config.height;
		this.itemWidth = config.itemWidth;
		
		this.nextBtn = $("#" + config.nextBtn);
		this.prevBtn = $("#" + config.prevBtn);	
		this.dragHandle = $("#"+this.draggableId);	
				
		this.init();

	}

	Timeline.prototype.init = function(){
		
		var me = this;
		this.itemsCount = $("#" + this.draggableId + " > div." + this.itemClass).length;
		this.totalWidth = this.itemsCount * this.itemWidth;	
		this.dragHandle.css("width" , this.totalWidth + "px");
		this.dragHandle.css("height", this.height + "px");
		this.dragHandle.css("overflow" , "hidden");	
		this.dragHandle.css("left" ,"0px");
		
		this.nextBtn.mousedown(function(){me.nextMouseDown = true;me.stepRight();});
		this.nextBtn.mouseup(function(){me.nextMouseDown =false;});
		this.nextBtn.mouseout(function(){me.nextMouseDown = false;});
		
		this.prevBtn.mousedown(function(){me.previousMouseDown = true;me.stepLeft();});
		this.prevBtn.mouseup(function(){me.previousMouseDown =false;});
		this.prevBtn.mouseout(function(){me.previousMouseDown = false;});
		
		if(this.isTouch()){
			setInterval(me.checkOrientation,1000);
			this.dragHandle.touch({ 
				animate: false,
				sticky: true,
				dragx: true,
				dragy: false,
				rotate: false,
				resort: false,
				scale: false,
				stopHandler:function(){me.posTimeline();}
			});
		}else{
			this.dragHandle.draggable({ axis: "x",stop: function(){me.posTimeline();}});
		}	
	};
	
	Timeline.prototype.checkOrientation = function(){
		if(this.currentRotation != window.orientation)
		    this.setOrientation();
	};
	
	Timeline.prototype.setOrientation = function(){
	    switch(window.orientation){
		    case 0:
			    orient = "portrait";
			    break;
		    case 90:
			    orient = "landscape";
			    break;
		    case -90:
			    orient = "landscape";
			    break;
	    }
	    this.currentRotation = window.orientation;
	    document.body.setAttribute("orient",orient);
	    setTimeout(scrollTo,0,0,1);
	};
 
	Timeline.prototype.isTouch = function(){
	  
	    if(navigator.userAgent.indexOf("iPhone") != -1 )
		return true;
	    
	    if(navigator.userAgent.indexOf("iPad") != -1 ) 
		return true;
		    
	    var userag = navigator.userAgent.toLowerCase();		
	    return userag.indexOf("android") > -1; 
	};
 
	Timeline.prototype.stepLeft = function(){
		
		var me = this;

		if(this.move)
			return;
		
		var curLeft = parseInt(this.dragHandle.css("left")) ;
		
		if(curLeft>=0){
			this.posTimeline();
			return;
		} else{
			this.move = true;
			this.dragHandle.animate({
			    left: "+="+this.itemWidth
			  }, 500 ,function(){
			  		if(me.previousMouseDown){
			  			me.move = false;
			  			me.stepLeft();
			  		}else{
			  			me.move = false;
			  			me.posTimeline();
			  		}
			  } );
		}
	};
	
	Timeline.prototype.stepRight = function(){
		
		var me = this;
		
		if(this.move)
			return;
		
		var curLeft = parseInt(this.dragHandle.css("left")) ;
		
		if(curLeft  <=  - this.totalWidth + this.width){
			this.posTimeline();
			return;
		}else{
			this.move = true;
			this.dragHandle.animate({
			    left: "-="+this.itemWidth
			  }, 500 ,function(){
					if(me.nextMouseDown){
						me.move = false;
						me.stepRight();
					}else{
						me.move = false;
						me.posTimeline();
					}
			  } );
		}
	};
	
	Timeline.prototype.posTimeline = function(){
	      var me = this;
		
	      if(this.move)
		      return;
	       
	      var curLeft = parseInt(this.dragHandle.css("left")) ;
	      
	      if(curLeft  <  - this.totalWidth + this.width){
	    	  this.dragHandle.draggable("disable");
		      this.move = true;
		      this.dragHandle.animate({
		    	  left: "+="+(  -this.totalWidth + this.width - curLeft)},
		    	  500 , 
		    	  function(){
			      	me.move = false;
			      	me.dragHandle.draggable("enable");
			  });	
	      }
	      
	      if(curLeft > 0){
	    	  this.dragHandle.draggable("disable");
		      this.move = true;
		      this.dragHandle.animate({
					  left: "-=" + curLeft
					},
					500, 
					function(){
					    me.move = false;
					    me.dragHandle.draggable("enable");
			   });	
	      }		 
	};

