/* class for individual slides */
function SliderItem( url , imgUrl ,  headline, footline, display_url ){
	this.url = url;
	this.imgUrl = imgUrl;
	this.headline = headline; // header
	this.footline = footline; // footer
	this.display_url = display_url;	
	this.parentBrowser = null;
}						
							
/* class for the slide player */
function SliderBrowser( elementId, leftArrowId, rightArrowId, leftArrow1Id, rightArrow1Id, displayItemsCount , scrollItemsCount, imgSrc, loop ){
	this.elementId = elementId;
	this.leftArrowId = leftArrowId;
	this.rightArrowId = rightArrowId;
	this.leftArrow1Id = leftArrow1Id;
	this.rightArrow1Id = rightArrow1Id;
	this.displayItemsCount = displayItemsCount;		// total number of items to be displayed at one time
	this.scrollItemsCount = scrollItemsCount;		// how many items to scroll by when clicking next or previous
	this.itemArray = new Array();
	this.itemIndex = 0;
	this.imgSrc = imgSrc;
	this.leftImgOff = this.imgSrc + "leftArrow.gif"; // image bt return
	this.leftImgOn = this.imgSrc + "leftArrow_on.gif";// image bt return on
	this.rightImgOff = this.imgSrc + "rightArrow.gif"; // image bt forward
	this.rightImgOn = this.imgSrc + "rightArrow_on.gif"; // image bt forward on
	this.loop = loop;
}


SliderBrowser.prototype.showButtons = function(){

	if (document.createElement){
	
		//begin left arrow
		ButtonleftArrow = document.getElementById(this.leftArrowId);
		//create image
		isOff = (this.itemIndex == 0);
		ButtonleftArrowImg = document.createElement("IMG");
		state = (isOff && !this.loop) ? this.leftImgOff : this.leftImgOn;
		ButtonleftArrowImg.setAttribute("src", state );
		ButtonleftArrowImg.setAttribute("name", "leftArrow"); 
		ButtonleftArrowImg.setAttribute("border", "0"); 
				
		//create anchor
		if (!isOff || this.loop){
			ButtonleftArrowAnchor = document.createElement( "A" );
			ButtonleftArrowAnchor.setAttribute("href", "javascript:browser.update(0)");
			ButtonleftArrowAnchor.appendChild(ButtonleftArrowImg);
			ButtonleftArrow.appendChild(ButtonleftArrowAnchor);
		} else {
			ButtonleftArrow.appendChild(ButtonleftArrowImg);
		}
		//end left arrow

//begin left arrow1
		ButtonleftArrow1 = document.getElementById(this.leftArrow1Id);
		//create image
		isOff = (this.itemIndex == 0);
		ButtonleftArrow1Img = document.createElement("IMG");
		state = (isOff && !this.loop) ? this.leftImgOff : this.leftImgOn;
		ButtonleftArrow1Img.setAttribute("src", state );
		ButtonleftArrow1Img.setAttribute("name", "leftArrow"); 
		ButtonleftArrow1Img.setAttribute("border", "0"); 
				
		//create anchor
		if (!isOff || this.loop){
			ButtonleftArrow1Anchor = document.createElement( "A" );
			ButtonleftArrow1Anchor.setAttribute("href", "javascript:browser.update(0)");
			ButtonleftArrow1Anchor.appendChild(ButtonleftArrow1Img);
			ButtonleftArrow1.appendChild(ButtonleftArrow1Anchor);
		} else {
			ButtonleftArrow1.appendChild(ButtonleftArrow1Img);
		}
		//end left arrow1

		//begin right arrow
		ButtonrightArrow = document.getElementById(this.rightArrowId);
		//create image
		isOff = ((this.itemIndex + this.displayItemsCount) >= this.itemArray.length);
		ButtonrightArrowImg = document.createElement("IMG");
		state = (isOff && !this.loop) ? this.rightImgOff : this.rightImgOn;
		ButtonrightArrowImg.setAttribute("src", state );
		ButtonrightArrowImg.setAttribute("name", "rightArrow"); 
		ButtonrightArrowImg.setAttribute("border", "0"); 
		//create anchor
		if (!isOff || this.loop){	
			ButtonrightArrowAnchor = document.createElement("A");
			ButtonrightArrowAnchor.setAttribute("href" , "javascript:browser.update(1)");
			ButtonrightArrowAnchor.appendChild(ButtonrightArrowImg);
			ButtonrightArrow.appendChild(ButtonrightArrowAnchor);
		} else {
			ButtonrightArrow.appendChild(ButtonrightArrowImg);
		}	
		//end right arrow
		
	//begin right arrow1
			ButtonrightArrow1 = document.getElementById(this.rightArrow1Id);
		//create image
		isOff = ((this.itemIndex + this.displayItemsCount) >= this.itemArray.length);
		ButtonrightArrow1Img = document.createElement("IMG");
		state = (isOff && !this.loop) ? this.rightImgOff : this.rightImgOn;
		ButtonrightArrow1Img.setAttribute("src", state );
		ButtonrightArrow1Img.setAttribute("name", "rightArrow"); 
		ButtonrightArrow1Img.setAttribute("border", "0"); 
		
		//create anchor
		if (!isOff || this.loop){	
			ButtonrightArrow1Anchor = document.createElement("A");
			ButtonrightArrow1Anchor.setAttribute("href" , "javascript:browser.update(1)");
			ButtonrightArrow1Anchor.appendChild(ButtonrightArrow1Img);
			ButtonrightArrow1.appendChild(ButtonrightArrow1Anchor);
		} else {
			ButtonrightArrow1.appendChild(ButtonrightArrow1Img);
		}	
		//end right arrow1
	}
}


SliderItem.prototype.write = function(  ){
	if( document.createElement ){
		parentElement = document.getElementById( this.parentBrowser.elementId );
		if( parentElement.tagName == "TABLE" && this.parentBrowser.getBodyRow ){
			
			// create body
			tdObj = document.createElement( "TD" );
				labelObj = document.createElement( "DIV" );
				labelObj.className = "label";
					if (this.headline!='')
					{
						headObj = document.createElement( "DIV" );
						headObj.className = "title";
						headObj.innerHTML = this.headline;
						labelObj.appendChild( headObj );
					}
					imgObj = document.createElement( "DIV" );
					imgObj.className = "image";
						labelLinkObj = document.createElement( "A" );
						labelLinkObj.setAttribute( "href" , this.url );
							labelImgObj = document.createElement( "IMG" );
							labelImgObj.setAttribute( "src" , this.imgUrl );
							labelImgObj.setAttribute( "alt" , this.headline );
							labelImgObj.setAttribute( "width" , "140" );
							labelLinkObj.appendChild( labelImgObj );
						imgObj.appendChild( labelLinkObj );
					labelObj.appendChild( imgObj );
					if (this.footline!='')
					{
						footObj = document.createElement( "DIV" );
						footObj.className = "footer";
						footObj.innerHTML = this.footline;
						labelObj.appendChild( footObj );
					}
			tdObj.appendChild( labelObj );
			this.parentBrowser.getBodyRow().appendChild( tdObj );	
		}
	}
}							
							
SliderBrowser.prototype.addItem = function( item ){
	if( item instanceof SliderItem ){
		item.parentBrowser = this;
		this.itemArray.push( item );
	}
}			
							
SliderBrowser.prototype.update = function( doMoveRight ){
	tableObj = document.getElementById( this.elementId );
	
	//increment index count
	var origIndex = this.itemIndex;
	if(!this.loop)
	{
		this.itemIndex = (doMoveRight) ? this.itemIndex + this.scrollItemsCount : this.itemIndex - this.scrollItemsCount;
		
		//set upper and lower bounds
		this.upperBound = this.itemArray.length - this.displayItemsCount;
		this.itemIndex = ((this.itemIndex + this.displayItemsCount) > this.itemArray.length) ? this.itemArray.length-this.displayItemsCount : this.itemIndex;
		this.itemIndex = (this.itemIndex < 0) ? 0 : this.itemIndex;

		//update button images
		this.deleteAllChildrenOf(document.getElementById(this.leftArrowId));
		this.deleteAllChildrenOf(document.getElementById(this.rightArrowId));
		this.deleteAllChildrenOf(document.getElementById(this.leftArrow1Id));
		this.deleteAllChildrenOf(document.getElementById(this.rightArrow1Id));
		this.showButtons();

		if (origIndex != this.itemIndex){
			//clear 
			//this.deleteAllChildrenOf( this.getHeaderRow() );
			this.deleteAllChildrenOf( this.getBodyRow() );
			
			// re-populate
			for( iUpdate=this.itemIndex ; iUpdate < (this.itemIndex + this.displayItemsCount) ; iUpdate++ ){
				this.itemArray[iUpdate].write();
			}
		}
	}
	else
	{
		this.itemIndex = (doMoveRight) ? this.itemIndex + this.scrollItemsCount : this.itemIndex - this.scrollItemsCount;
		this.deleteAllChildrenOf( this.getBodyRow() );
		for (i=0;i<this.displayItemsCount;i++)
		{
			if (i+this.itemIndex>=0)
			{
				this.itemArray[((i+this.itemIndex)%(this.itemArray.length))].write();
			}
			else
			{
				this.itemArray[this.itemArray.length+((i+this.itemIndex)%(this.itemArray.length))].write();
			}
		}
	}
	
}



SliderBrowser.prototype.getBodyRow = function(){
	return document.getElementById( this.elementId ).getElementsByTagName( "TBODY" )[0].getElementsByTagName( "TR" )[0];
}

SliderBrowser.prototype.deleteAllChildrenOf = function( elementObj ){
	while (elementObj.hasChildNodes()) elementObj.removeChild(elementObj.firstChild);
}
