/* class for individual slides */
function SimplePageSliderItem( content ){
	this.content = content;
	this.parentBrowser = null;
}						
							
/* class for the slide player */
function SimplePageSliderBrowser( elementId, leftArrowId, rightArrowId, imgSrc, loop, browserID )
{
	this.elementId = elementId;
	this.leftArrowId = leftArrowId;
	this.rightArrowId = rightArrowId;
	this.displayItemsCount = 1;		// total number of items to be displayed at one time
	this.scrollItemsCount = 1;		// how many items to scroll by when clicking next or previous
	this.itemArray = new Array();
	this.browserIndex = browserID;
	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;
}


SimplePageSliderBrowser.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"+this.browserIndex+".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"+this.browserIndex+".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
	}
}


SimplePageSliderItem.prototype.write = function(  ){
	if( document.createElement ){
		parentElement = document.getElementById( this.parentBrowser.elementId );
		if( parentElement.tagName == "TABLE" && this.parentBrowser.getBodyRow ){
			
			tdObj = document.createElement( "TD" );
			tdObj.className = "td_label";
				
			contentObj = document.createElement( "DIV" );
			contentObj.className = "content";
			contentObj.innerHTML = this.content;
				
			
/*		IE hates this method
			imgObj = document.createElement( "TABLE" );
			imgObj.className = "image";
			
				rowimgObj = document.createElement( "TR" );
				imgObj.appendChild( rowimgObj );
				
					cellimgObj = document.createElement( "TD" );

						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( "onMouseOver" , this.mouseOver );
							
							labelLinkObj.appendChild( labelImgObj );
						cellimgObj.appendChild( labelLinkObj );
					rowimgObj.appendChild( cellimgObj );
		
		IE Likes this method							
					imgObj = document.createElement( "DIV" );
					imgObj.className = "image";
						labelLinkObj = document.createElement( "A" );
						labelLinkObj.setAttribute( "href" , this.url );
							labelImgObj = document.createElement( "IMG" );
							labelImgObj.setAttribute( "onMouseOver" , this.mouseOver );
							labelImgObj.setAttribute( "src" , this.imgUrl );
							labelImgObj.setAttribute( "alt" , this.headline );
							labelLinkObj.appendChild( labelImgObj );
						imgObj.appendChild( labelLinkObj );
*/
			
			tdObj.appendChild( contentObj );
			this.parentBrowser.getBodyRow().appendChild( tdObj );	
		}
	}
}							
							
SimplePageSliderBrowser.prototype.addItem = function( item ){
	if( item instanceof SimplePageSliderItem ){
		item.parentBrowser = this;
		this.itemArray.push( item );
	}
}			
							
SimplePageSliderBrowser.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.upperBound = this.itemArray.length - this.displayItemsCount;
		if(this.itemIndex< -1*this.upperBound) this.itemIndex = this.upperBound;
		
		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();
			}
		}
	}
	
}



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

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

