///////////////////////////////////////////////////////////////
//
//  Script: Basic Slider
//  Version: 1.0
//  Author: Craig Nelson / Classic Labs Development
//
//  Features: 
//    - can pass in vertical or horizontal config
//

  var Ascribe = Ascribe || {};

	Ascribe.Scroller = function (config) {
	  // scope adjustment
		var that = this;

		this.scrollerID = config.scrollerID;
		this.trackID = config.trackID;
		this.handleID = config.handleID;
		this.axis = config.axis;
		this.timers = [];

		this.scrollOrientation = function (orientation, value, element, slider) {
		  //console.log("hit");
		  if (orientation == "vertical") {
		    element.scrollTop = Math.round(value/slider.maximum*(element.scrollHeight-element.offsetHeight));
		  }
		  else {
		    element.scrollLeft = Math.round(value/slider.maximum*(element.scrollWidth-element.offsetWidth));
		  }
  	};

  	this.slider1 = new Control.Slider(this.handleID, this.trackID, {
		  axis: that.axis,
			onSlide: function (v) {
			  that.scrollOrientation(that.axis, v, $(that.scrollerID), that.slider1);
			},
			onChange: function (v) {
			  that.scrollOrientation(that.axis, v, $(that.scrollerID), that.slider1);
			}
		});
		
		// fix for IE
		this.setObservations = function (element) {
      Event.observe(element, 'mouseover', function(e){
        Event.stopObserving(document, "mousemove", that.slider1.eventMouseMove);
        that.cancelTimer(element.id.match(/\d+/));
      });
      Event.observe(element, 'mouseout', function(e){
        that.createTimer(element.id.match(/\d+/));
      });        
    }
  
    this.cancelTimer = function (id) {
      if (that.timers[id]) {
        clearTimeout(that.timers[id]);
        that.timers[id] = null;
      }
    }
  
    this.createTimer = function (id) {
      that.timers[id] = setTimeout(function(e){
        Event.observe(document, "mousemove", that.slider1.eventMouseMove);
      }, 500);
    }
    
    if ($$('.mark').length > 0) { // are we on a page that has marks?
      $$('.mark').each(function(element){
        that.setObservations(element);
      });
      $$('.mark-toggle').each(function(element){
        that.setObservations(element);
      });
    }
    // --fix for IE

  	if (this.axis == "vertical") {
			if ($(this.scrollerID).scrollHeight <= $(this.scrollerID).offsetHeight) {
				this.slider1.setDisabled();
				$(this.trackID).hide();
			}
			else {
		    $(this.trackID).style.visibility = 'visible';
		  }
		}
		else {
			if ($(this.scrollerID).scrollWidth <= $(this.scrollerID).offsetWidth) {
				this.slider1.setDisabled();
				$(this.trackID).hide();
			}
			else {
		    $(this.trackID).style.visibility = 'visible';
		  }
		}
	} // constructor
	
	Event.observe(window, "load", function () {
	  if ($("scroller")) { // does the current page have a scroller?
	    var scroller = new Ascribe.Scroller({
    	  "trackID": "scroller-track",
    	  "handleID": "scroller-handle",
    	  "scrollerID": "scroller",
    	  "axis": "vertical"
    	});
	  }
	});