//requires twsack.js,  bbbasics.js and tween.js
//Note the viewport's initial class is reassigned.
//requires 1st div be tray to hold ajax-genned divs. this has attrib ajurls="path/to/SS-script.php"
//generates ol/li's for 1/2/3/etc buttons and pause/play; gives them CSS class EHUrotBanCTRLS 
// buttons li's have class EHUbannerSelButOff which cycles to EHUbannerSelButOn when matching banner is on
//button graphic takes "playButSrc" url and appends "Play" or "Pause" + ".png"
// button graphic has classes EHUbannerPPbuttonPlay / EHUbannerPPbuttonPause

function EHUrotBan(VPid, secs, name, count, twnTm, playButSrc, butW){

	this.loader = function(spot){
		if((spot < this.count) && (this.allLoaded == 0)){
			var rotBan = this;

			var Tajax = new sack();
			Tajax.requestFile = rotBan.ajurl;
			Tajax.method = 'GET';
			Tajax.onLoading = function(){};
			Tajax.onCompletion = function(){
				var response = Tajax.response || "";
				eval(Tajax.response);
				rotBan.msecs[spot] = tempsecs * 1000;
				rotBan.buttons[spot].innerHTML = "<span>" + tempname + "</span>";
				//add to tray
				var tempd = document.createElement("div");
				//var cleanedHTML = tempHTML.replace("&quot;","'");
				tempd.innerHTML = tempHTML;
				rotBan.container.appendChild(tempd);
				spot++;
				rotBan.loader(spot);
				//check if queued waiting for this one
				if(rotBan.queued == spot) rotBan.jumpTo(spot);
			};
			Tajax.onError = function(){ };
			var temp = "position=" + spot; 
			Tajax.runAJAX(temp);
		} else {
			this.allLoaded = 1;
			this.placeControls();
		}
	}

	this.placeControls = function() {
		this.viewport.appendChild(this.controls);
	
		//place controls bottom left of vp
		var vpX = findPosX(this.viewport);
		var vpY = findPosY(this.viewport);
		var vpW = this.viewport.offsetWidth;
		var vpH = this.viewport.offsetHeight;
		var ctrlH = this.controls.offsetHeight;
		var ctrlW = this.controls.offsetWidth;
		this.controls.style.left = vpX + vpW - ctrlW + "px";
		this.controls.style.top = vpY + vpH - ctrlH + "px";
		this.controls.style.zIndex = 100;
	}

	this.startTimer = function() {
		this.timeHandle = setTimeout(this.name + ".timeTo()", this.msecs[this.position]);
	}

	this.timeTo = function() {
		var temp = 1 + this.position;
		if(temp >= this.count) temp = 0;
		var temp2 = this.goTo(temp);
		this.timeHandle = setTimeout(this.name + ".timeTo()", this.msecs[temp2]);
	}

	
	this.pause = function(){
		clearTimeout(this.timeHandle);
		this.playbutton.className = "EHUbannerPPbuttonPlay";
		this.playbutton.src = this.playbutton.srcRoot + "Play.png";
	}
	
	this.jumpTo = function(where){
		this.pause();
		this.goTo(where);
	}
	
	this.goTo = function(where){
		//use this.msecs array to check for loaded
		if(this.msecs[where]){
			//first change back class
			this.buttons[this.position].className = "EHUbannerSelButOff";
			this.position = where;
			var newM = -1 * (this.position * this.vpwidth); // + "px";
			var tempTm = this.twnTm;
			var tempC = this.container;
			var tempM = this.curMarginLeft;
			var tempTween = new Tween(tempC.style,'marginLeft',Tween.strongEaseOut,parseInt(tempM), parseInt(newM),tempTm,'px');
			tempTween.start();
			tempTween.onMotionFinish = function(){};
			this.buttons[this.position].className = "EHUbannerSelButOn";
			this.curMarginLeft = newM;

		} else this.queued = where; //queue this request
		
		return this.position;
	}
	
	var rotBan = this;

	
	this.count = count;
	this.twnTm = twnTm;
	this.name = name;
	this.msecs = new Array(); //set of delay times
	this.buttons = new Array(); //set of button elements
	this.msecs[0] = secs * 1000;
	this.viewport = document.getElementById(VPid);
	this.viewport.className = "";
	var temp = this.viewport.getElementsByTagName("div");
	this.container = temp[0]; //first div is tray
	this.position = 0;
	this.curMarginLeft = 0;
	this.ajurl = this.container.getAttribute("ajurl");
	this.timeHandle = 0;
	this.allLoaded = 0;
	this.currentHTML = "";
	this.queued = -1;//no banners queued

	//setup viewport/container
	this.vpwidth = this.container.offsetWidth;
	this.container.style.width = (this.vpwidth * (this.count + 1))  + "px";
	
	//build play controls
	this.controls = document.createElement("ol");
	this.controls.className = "EHUrotBanCTRLS";
	
	//build each button
	for(var j=0;j<count;j++) {
		var temp = document.createElement("li");
		if(j==0) temp.className = 'EHUbannerSelButOn';
		else temp.className = 'EHUbannerSelButOff';
		if(j==0) temp.innerHTML = "<span>" + EHUfirstlinkname + "</span>";
		else temp.innerHTML = "<span>" + (j + 1) + "</span>";
		this.buttons[j] = temp;
		this.buttons[j].rotBan = this; //ref on DOM but to rotBan
		this.buttons[j].num = j;//buttons knows it's own number
		this.buttons[j].onclick = function(){this.rotBan.jumpTo(this.num);}
		this.controls.appendChild(temp);
	}
	
	//build play/pause
	var temp = document.createElement("div");
	this.controls.appendChild(temp);
	if(butW) ww = butW;
	else ww = 16;
	temp.style.width = ww + "px";
	temp.style.overflow = "hidden";
	var temp2 = document.createElement("img");	
	temp2.srcRoot = playButSrc;
	temp2.width = ww;
	temp2.src = temp2.srcRoot + "Pause.png";
	temp2.className = 'EHUbannerPPbuttonPause';
	temp2.rotBan = this; //ref on DOM button back to this
	this.playbutton = temp2;
	temp2.onclick = function(){
		if(this.className == "EHUbannerPPbuttonPause"){
			this.className = "EHUbannerPPbuttonPlay";
			this.src = this.srcRoot + "Play.png";
			this.rotBan.pause();
		} else {
			this.className = "EHUbannerPPbuttonPause";
			this.src = this.srcRoot + "Pause.png";
			this.rotBan.timeTo();
		}
	}	
	temp.appendChild(temp2);

	this.startTimer();	
	this.loader(1);
}
