var inside = 0;
var onleft = -1;
var onright = 1;
var outside = -10;


function Segment(left, right){
	this.left = left;
	this.right = right;
}

Segment.prototype.expandToLeft = function(v)
{
	this.left--;
}

Segment.prototype.concatOnRight= function(seg)
{
	this.right = seg.right;
}

Segment.prototype.concatOnLeft= function(seg)
{
	this.left = seg.left;
}

Segment.prototype.expandToRight = function(v)
{
	this.right++;
}
Segment.prototype.relation = function(v)
{
	if (this.left<=v && v<= this.right) return inside;
	if (v == (parseInt(this.left)-1)) return onleft;
	if (v == (parseInt(this.right)+1)) return onright;
	return outside;
}

Segment.prototype.toString = function(v)
{
	return ('['+this.left+' '+this.right+']');
}

/////////////////////////

function VisitedRange(containerDiv, imgCnt, lineWidth)
{
  this.visitedArray = new Array();
  this.containerDiv = containerDiv;//document.getElementById('sliderAmount');//containerDiv;
  this.document = containerDiv.ownerDocument || containerDiv.document;
  this.imgCnt = imgCnt;
  this.lineWidth = lineWidth;
  this.scale = lineWidth/imgCnt;
  this.scale = this.scale < 1 ? 1 : Math.round(this.scale);
}

VisitedRange.prototype.visited = function(visitorValue)
{
	var v = parseInt(visitorValue);
	 for( var i = 0; i <  this.visitedArray.length; i++ ) {
		 var seg = this.visitedArray[i];
		 switch (seg.relation(v)) {
			case outside: //is outside: do nothing
				break;
			case onleft: //v touches a seg on its left
			    seg.expandToLeft();
				var leftSeg = this.visitedArray[i-1];
				if (leftSeg){
					if (seg.right == leftSeg.left) {
						seg.concatOnLeft(leftSeg);
						this.visitedArray.splice(i-1,1);
					}
				}
				this.updateVisuals();
				return;
			case inside: 
				return;
			case onright: //v touches seg on its right
			    seg.expandToRight();
				var rightSeg = this.visitedArray[i+1];
				if (rightSeg){
					if ((seg.right +1) == rightSeg.left) {
						seg.concatOnRight(rightSeg);
						this.visitedArray.splice(i+1,1);
					}
				}
				this.updateVisuals();
				return;
			default: //do nothing
			    break;
		 }
	 }
	 //if we are here this is a brandnew visit
	 
	 var newSegment = new Segment(v,v);
	 this.update(newSegment)
	 this.updateVisuals();
}

VisitedRange.prototype.update = function(newSegment){
	 this.visitedArray.push(newSegment);
	 this.visitedArray.sort(segmentComparator);
}

VisitedRange.prototype.isVisited = function(visitorValue){
	 var v = parseInt(visitorValue);
	 for( var i = 0; i <  this.visitedArray.length; i++ ) {
		 if (this.visitedArray[i].relation(v) == inside) return true;
	 }
	 return false;
}

VisitedRange.prototype.updateVisuals = function(){
	this.containerDiv.innerHTML = '';
	for( var i = 0; i <  this.visitedArray.length; i++ ) {
		var seg = this.visitedArray[i];
		var segDiv = this.document.createElement("DIV");
		segDiv.className = 'segment';
		segDiv.style.left = (seg.left*this.scale) + 'px';
		segDiv.style.top = '0px';
		segDiv.style.width= ((seg.right-seg.left)*this.scale)+ 'px';
		segDiv.style.position = 'absolute';
		this.containerDiv.appendChild(segDiv);
	}
}

function segmentComparator(seg1, seg2){
	return (parseInt(seg1.right)<parseInt(seg2.left))? -1:+1;
}
