function makeSelection(files,oNode,iFrameTgt){
this.files = files;
this.tgtNode = document.getElementById(oNode);
this.iFrameTgt = window.frames[iFrameTgt];
this.pos = 0;
this.previous = 0;
this.next = getNodesLength(this.tgtNode)-1;
this.tgt = "";
this.direction = "";
this.setNext();
}

makeSelection.prototype.chgNode = function(sentNode,direction){ 
	var oChild = document.createElement("TD");
	oChild.style.padding = "5px";
	oChild.innerHTML = sentNode.innerHTML;
	if (direction == "next")
		addNextChild(this.tgtNode,oChild);
	else
		addPreviousChild(this.tgtNode,oChild);
}

makeSelection.prototype.getPos = function(){
	switch(this.direction)
	{
		case "next":
		if(this.next <= this.files.length) { this.next ++;	this.previous ++;}
		return this.next;
		break;
		
		case "previous":
		if(this.previous >= 0) {	this.next --; this.previous --;	}
		return this.previous;
		break;
	}
}

makeSelection.prototype.nextNode = function(direction,srcButton){
	this.tgt = this.tgtNode;
	this.direction = direction;
	this.iFrameTgt.location.href = this.files[this.getPos()];
	this.displayButtons(srcButton.name);
}

makeSelection.prototype.displayButtons = function(buttonName,srcButton){
	document.getElementById(buttonName+"prev").style.display = (this.previous == 0)? "none" : "block";
	document.getElementById(buttonName+"next").style.display = (this.next == this.files.length-1)? "none" : "block";
}

makeSelection.prototype.setNext = function(){	
	document.getElementById("imgdemo"+"prev").style.display = "none";
	document.getElementById("imgdemo"+"next").style.display = (getNodesLength(this.tgtNode) >= this.files.length)? "none" : "block";
}

function getNodesLength(oNode){
	var count = 0;
	var oChild = oNode.firstChild;
	while(oChild){
		if (oChild.nodeType == 1) count++;
		oChild = (oChild.nextSibling)? oChild.nextSibling : null;	
		}
	return count;
}

function getLastChild(oNode){
	while(oNode.nodeType != 1)	oNode = oNode.previousSibling;	
	return oNode;
}

function getFirstChild(oNode){
	while(oNode.nodeType != 1)	oNode = oNode.nextSibling;	
	return oNode;
}

function addPreviousChild(oNode,oChild){
	oNode.insertBefore(oChild,oNode.firstChild);
	oNode.removeChild(getLastChild(oNode.lastChild));
}

function addNextChild(oNode,oChild){
	oNode.insertBefore(oChild,null);
	oNode.removeChild(getFirstChild(oNode.firstChild));
}