
function scrollNav(objectType, args){

   this.initInheritedParams(args);
   this.initHelp(objectType);
   var self = this;

   // Initialize Parameters // 
   this.params.navStyle = 'navStyle' in args? args.navStyle: "pan"; // Pan Show, Slide Show
   this.params.clickToScroll = 'clickToScroll' in args? args.clickToScroll:0;
   this.params.selectContent = 'selectContent' in args? args.selectContent:0;
   this.params.disableContext = 'disableContext' in args? args.disableContext:0;

   this.frontDiv = document.createElement("div");
   this.frontDiv.style.height = "600px";
   this.frontDiv.style.width = "400px";
   this.frontDiv.style.position = "absolute";
   this.frontDiv.style.top = "0px";
   this.frontDiv.style.left = "0px";
   this.frontDiv.style.backgroundColor = "black";

   this.backDiv = document.createElement("div");
   this.backDiv.style.height = "600px";
   this.backDiv.style.width = "400px";
   this.backDiv.style.position = "absolute";
   this.backDiv.style.top = "0px";
   this.backDiv.style.left = "0px";
   this.backDiv.style.backgroundColor = "black";

   this.container = document.createElement("div");
   this.container.style.overflow = "hidden";
   this.container.style.position = "relative";

   this.sectionList;
   this.currentSection;

   this.scrollerWidth = 0;
   this.paddingSize = 10000;

   this.currentPosition = 0;
   this.currentSection;
   this.destinationPosition = 0;
   this.timer = "";
   this.params.panStep = 1;
  
   this.scroller = document.createElement("div");
   this.scroller.appendChild(this.frontDiv);
   this.scroller.appendChild(this.backDiv);

   this.setDestinationPosition = function(position){
      this.destinationPosition = position;
   }


   this.addPadding = function() {
      this.scrollPadding = document.createElement("div"); 
      this.scrollPadding.style.width = this.paddingSize + "px"; 
      this.scrollPadding.style.backgroundColor = "green";
      this.scrollPadding.style.cssFloat = "left";
      this.scrollPadding.style.styleFloat = "left";
      this.scroller.appendChild(this.scrollPadding);
      this.scroller.style.width = this.scrollerWidth + this.paddingSize + "px";
   }

   this.container.appendChild(this.scroller);

   this.dragState = 0;
   this.Xpos;

   if(this.params.clickToScroll == 1) {
      this.scroller.onmouseover = function(event) {
         document.body.style.cursor = "pointer";
         return false;
      }

      this.scroller.onmouseout = function(event) {
         document.body.style.cursor = "";
         return false;
      }
      
      this.scroller.onmousedown = function(event) { event = event || window.event; self.dragState = 1; self.Xpos = event.x; return false; }
      this.scroller.onmouseup = function(event) { event = event || window.event; self.dragState = 0; return false; }
      this.scroller.onmouseout = function(event) { event = event || window.event; self.dragState = 0; return false; }
      this.scroller.onmousemove = function(event) { 
         event = event || window.event; 
         if (self.dragState == 1) { 
            var distance = self.Xpos - event.x; 
            if(self.currentPosition + distance <= (self.scrollerWidth-self.paddingSize) && self.currentPosition - distance >= 0) {
               self.setDestinationPosition(self.currentPosition + distance ); 
               self.Xpos = event.x;
               self.skipToSection(); 
            }
            
         } 
         return false;
      } 
   } else if(this.params.selectContent == 0) {
      this.scroller.onmousedown = function(event) {return false;}
      this.scroller.onmouseup = function(event) {return false;}
      this.scroller.onmousedown = function(event) {return false;}
      this.scroller.onmousemove = function(event) {return false;}
   }
   
   if(this.params.disableContext == 1) {
      this.scroller.oncontextmenu = function(event) {return false;}
   }

   this.isFirst = 1;

   /* Add front book end to allow image centering */ 
   this.addFrontBookEnd = function() {
      this.frontBookEnd = document.createElement("div");
      this.frontBookEnd.style.position = "relative";
      this.frontBookEnd.style.cssFloat = "left"; this.frontBookEnd.style.styleFloat = "left";
      this.frontBookEnd.style.left = "0px";
      this.frontBookEnd.style.width = this.paddingSize + "px";
      this.scrollerWidth = this.scrollerWidth + this.paddingSize;
      this.frontBookEnd.style.height = "1px";
      this.scroller.appendChild(this.frontBookEnd);
      this.scroller.style.width = this.scrollerWidth + "px";
   }

  /* Add rear book end to allow image centering */ 
  this.addRearBookEnd = function() {
     this.rearBookEnd = document.createElement("div");
     this.rearBookEnd.style.position = "relative";
     this.rearBookEnd.style.cssFloat = "left"; this.frontBookEnd.style.styleFloat = "left";
     this.rearBookEnd.style.left = "0px";
     this.rearBookEnd.style.width = this.paddingSize + "px";
     this.scrollerWidth = this.scrollerWidth + this.paddingSize;
     this.rearBookEnd.style.height = "1px";
     this.scroller.appendChild(this.rearBookEnd);
     this.scroller.style.width = this.scrollerWidth + "px";
  }

  this.removeRearBookEnd = function() {
      this.scroller.removeChild(this.rearBookEnd);
      this.scrollerWidth = this.scrollerWidth - this.paddingSize;
      this.scroller.style.width = this.scrollerWidth + "px";
  }

   /* Add a new section to the scroll Navigator */
   this.appendSection = function(section, borderWidth) {
        
      if(this.isFirst == 1) {
         this.currentSection = section; self.firstSection = section; this.sectionList = section;
         this.isFirst = 0; 
         section.previous = "none";
         this.addFrontBookEnd();
         this.addRearBookEnd();
      } else {
         section.previous = this.currentSection;
         this.currentSection.next = section;
         this.currentSection = section;
      }

      this.removeRearBookEnd();
      section.position = this.scrollerWidth; section.next = "none";
      section.getNext=function(){if(this.next!="none"){return this.next;}else{return "none";}}
      section.getPrevious=function(){if(this.previous!="none"){return this.previous;}else{return "none";}}

      this.scrollerWidth = this.scrollerWidth + section.width + borderWidth*2; 
      this.scroller.style.width = this.scrollerWidth +  "px";
      section.style.cssFloat = "left"; section.style.styleFloat = "left";
      
      this.scroller.appendChild(section);
      this.addRearBookEnd();

      if(self.params.navStyle == "pan") {
         section.action = function() {
            self.setDestinationPosition(section.position);
            self.scrollToSection();
         }
         return section.action;
      } else {
         var mySec = section;
         section.action = function() {
            self.currentSection = mySec;
            var position = section.position - Math.round(parseFloat(self.styleParams.width)-section.width)/2 - borderWidth;

            self.setDestinationPosition(position);
            self.frontDiv.style.width = section.position +"px";
            self.backDiv.style.width = self.scrollerWidth - (section.width + borderWidth*2) + "px";
            self.backDiv.style.left = section.position + section.width + borderWidth*2 + "px";
            self.skipToSection();

            return false; 
         }
         return section.action;
      }
   }

   this.skipToSection = function() {
      clearTimeout(this.timer);
      var distance =  this.destinationPosition - this.currentPosition;
      this.container.scrollLeft += distance;
      this.currentPosition = this.destinationPosition;
   }

   this.scrollToSection = function() {
      clearTimeout(this.timer);
      var sign = 1;
      var distance = this.destinationPosition - this.currentPosition;
      if (distance < 0) {
         sign = -1 ;
         distance = distance * sign;
      }
      if(distance < 5 ) {
         this.params.scrollStep = 1 * sign;
      } else { 
         this.params.scrollStep = Math.round(distance/2) * sign;
      }
      if(distance >= 1) {
         this.container.scrollLeft += this.params.scrollStep;
         this.currentPosition = this.currentPosition + this.params.scrollStep ;
         this.timer = setTimeout(function() {self.scrollToSection();} , 5);
      }
   }

   this.setDestinationPosition(0);
   this.skipToSection();
   this.setGuts(this.container);
}
  
scrollNav.prototype = new control;
