// Requires array.js and Dynapi

function DynViewer(id,x,y,w,h,nexta,preva,viewerlabel,images,imageSizes,path,bResize,lPad) {
	this.dynlayer=DynLayer
	this.dynlayer(id,x,y,w,h)
	this.canvas=null;
	this.VisiblePanel=0;
	this.LeftPanel=0;
	this.RightPanel=0;
	this.images=images;
	this.imageSizes=imageSizes;
	this.portpct=.70;
	this.nextAmt=0;
	this.nexta=nexta;
	this.preva=preva;
	this.viewerlabel=viewerlabel;
	this.imageLayers=[];
	this.bResizePort = bResize;
	this.path=path;
	this.imagObj=[];
	this.imgLpad=lPad;
	this.imagesshowing=-1;
	this.animDelta = 20;
	this.animSpeed = 10;
	this.imagecount=this.images.length;
	this.imagewidth=imageSizes[0];
	this.height=h;
	this.canvas=null;
	this.onResize();

	DynAPI.addResizeFunction(this+".onResize()");

	return this;
}
DynViewer.prototype=new DynLayer();

DynViewer.prototype.unloadImages=function(){ if (this.canvas) this.canvas.deleteFromParent() }
DynViewer.prototype.attachImages=function() {
	if (!this.canvas) {
		var isum=this.imagecount*this.imagewidth;
		this.canvas = new DynLayer(null,0,0,isum+((this.imagecount-1)*this.imgLpad),this.height);
	}

	this.unloadImages();
	this.nextAmt=0;
	this.imageLayers = new Array();
	
	var x = 0;
	for (var i=0;i<this.imagecount; i++) {
		var thislayer = new DynLayer(null,x,0,this.imageSizes[i],this.height);
		x += this.imageSizes[i]+this.imgLpad;
		thislayer.addChild(new DynImage(imagObj[i]));
		this.imageLayers.push(thislayer);
		this.canvas.addChild(thislayer);
	}
	
	this.addChild(this.canvas);

	DynAPI.document.addChild(this);

}
DynViewer.prototype.calcWidth=function() {
	var w=Math.floor(((this.bResizePort)?(DynAPI.document.getWidth() * this.portpct):this.getWidth()) / this.imagewidth); 
	return ( (w<1)?1:w );
}
DynViewer.prototype.WidthSet=function() {
	if (this.bResizePort) {
		var www=(this.imagesshowing*this.imagewidth)+(this.imagesshowing-1)*this.imgLpad;
		if (www<this.imagewidth) www=this.imagewidth;
		this.setWidth(www);
	}
}

DynViewer.prototype.onResize=function() {
	var winW=DynAPI.document.getWidth();
	var winH=DynAPI.document.getHeight();
	var y = (winH-this.height)/2;
	var w = this.calcWidth();

	if (this.imagesshowing != w) {
		this.imagesshowing=w;
		this.WidthSet();
		this.RightPanel = this.LeftPanel + this.imagesshowing - 1;
		this.nexta.setVisible((this.RightPanel<(this.imagecount-1)));
	}

	w = this.getWidth();
	var x=(winW-w)/2; 
	if ((this.getX()!=x)||(this.getY()!=y))this.moveTo(x,y);
	
	// position arrows and label
	x=this.getX(); y=this.getY();
	this.viewerlabel.moveTo(x,y-30);
	this.viewerlabel.setVisible(true);
	
	y=y+this.height+10;
	this.preva.moveTo(x, y );
	nexta.moveTo(x+w-this.nexta.getWidth(), y );
}
