// JavaScript Document
//
//  Some Items MUST match up with the HTML
//
//  scroller width = numPerPage * blockSize + padding
//  span id  = WibSpan
//  left arrow = 'objTitle'+_left_arrow
//  right_arrow = 'objTitle'+_right_arrow
//

function Class_WIB()
{
	this.delay = 15
	this.PageStep = 1
	this.Page = 1
	this.bTurned = false
	this.bMoving = false
	this.Interval = false
	this.accelleratie = (document.all && document.getElementById ? 3 : 8)
	this.arrObjects = new Array()
	this.Breadth = 540
	this.v = 0
	this.vmax = 0
	this.a = 0
	this.x = 0
	this.t = 0
	this.nNumberInitialObjects = 0
	this.nNumberObjects = 0
	this.showPageNumbers = false
	this.pageDesignator = "http://nutsie-public.s3.amazonaws.com/images/iphone_thumb_indicator_fill_yellow.gif"
	// some constants that are configurable
	//
	// Number of blocks per page...displayed blocks
	this.numPerPage = 1
	//
	// pixel width of the blocks
	//
	this.blockSize = 540

  // Initialize the movement controls
	for(var i=0;i<this.blockSize;i++)
	{
		this.t += this.accelleratie
		this.x += this.t
		if ( this.x * 2 > this.Breadth && this.vmax == 0 )
		{
			this.vmax = this.t
			this.Factor = this.Breadth / (this.x*2)
     		//alert('vmax='+this.vmax+'\nFactor='+this.Factor)
 		}
	}
	this.x = 0
	this.xLocation = 0

	this.Start = function(page, numObjects, objTitle, numInitial)
	{
		this.objTitle = objTitle
		this.nNumberObjects = numObjects //6this.arrObjects.length
		this.nNumberInitialObjects = numInitial
		this.xMax = 0
		this.NumberPages = Math.ceil(this.nNumberObjects / this.numPerPage)
		this.xMin = - (this.NumberPages-1) * (this.blockSize * this.numPerPage)

		this.SetButtons()
		if (page) {
			this.FirstLoad = true
			this.FirstPress = true
			this.goToSlide(page)
		}
	}

	this.SetButtons = function()
	{
		if ( this.nNumberObjects > 1 )
		{
			document.getElementById(this.objTitle+'_right_arrow').style.display = ( this.Page < this.NumberPages ? 'block' : 'none' )
			document.getElementById(this.objTitle+'_left_arrow').style.display = ( this.Page > 1 ? 'block' : 'none' )
			var s = ''
			for ( var i=1; i<=this.NumberPages; i++ ) {
		     s += '<div onclick="javascript:WIB.goToSlide(' + i + ')" class="fl' + (this.Page == i ? ' sel' : '') + '">'
			 if (this.showPageNumbers) {
			 	s += i
		     } else if(i == this.Page){
			 	s += '<img src="' + this.pageDesignator + '" alt="page'+ i + '"/>'
			 } else {
			 	s += '<img src="/images/spacer.gif" alt="page'+ i + '"/>'
			 }
			 s += '</div>'
		   }
			 if(document.getElementById(this.objTitle+'_pagenumber')) document.getElementById(this.objTitle+'_pagenumber').innerHTML = s
		}
	}
	//
	//  For our site... this specifies the addition to the current page to view:
	//  -1 = previous page
	//   1 = next page
	//
	this.MouseDown = function(_ax)
	{
		this.goToSlide(this.Page-_ax)
	}
	//
	// arg specifies absolute page number: 1-n
	//
	this.goToSlide = function(n)
	{
		//alert("goToSlide: "+n)
		//-------------------------------------------------
		// place to hook in database paging
		//
		// call link_to_remote to get the correct page(s) of data
		//  2 philospies:
		//    1) load set for next display just before moving.
		//    2) have prev/cur/next loaded
		//       a) move
		//       b) load the new set
		//-------------------------------------------------
		//if ( this.FirstLoad == false )
		//{
			// this does the full load check on the first friends grab after load....
		//	if ( this.FirstPress == true )
		//	{
		//		if ( this.nNumberInitialObjects < this.nNumberObjects )
		//		{
		//			// different way...small initial load, then first time here... load ALL the rest
		//			preq = new Ajax.Request('/myhomepage/page_friends', {asynchronous:true, evalScripts:true})
		//		}
		//		this.FirstPress = false
		//	}
		//	//preq = new Ajax.Request('/myhomepage/page_friends?friend_page='+n, {asynchronous:true, evalScripts:true})
		//}
		//else
		//{
			//if ( this.nNumberInitialObjects < this.nNumberObjects )
			//{
				// different way...small initial load, then first time here... load ALL the rest
			//	preq = new Ajax.Request('/myhomepage/page_friends', {asynchronous:true, evalScripts:true})
			//}
			this.FirstLoad = false
		//}
		if ( this.bMoving == false )
		{
			this.bMoving = true
			this.a = ( this.Page>n ? 1 : -1 ) * this.accelleratie
			this.PageMove = ( this.Page>n ? this.Page - n : n - this.Page )
			this.Page = n
			if ( this.Page > this.NumberPages ) this.Page = this.NumberPages
			if ( this.Page < 1 ) this.Page = 1
			sStats = ''
			// call the move function every 15ms until interval is cleared
			if ( !this.Interval ) this.Interval = setInterval('WIB.Move()',15)
		}
	}
	// this function is called until Interface is cleared.
	//
	// xLocation is recalculated and substituted into the WibSpan px location
	this.Move = function()
	{
		this.x += this.v * this.Factor * this.PageMove
		this.xLocation = Math.round(this.x)
		if ( this.x > this.xMax ) this.x = this.xMax
		if ( this.x < this.xMin ) this.x = this.xMin
		if ( this.bTurned == false && ( this.v >= this.vmax || this.v <= -this.vmax ))
		{
			this.a = -this.a
			this.bTurned = true
		}
		else
		{
			this.v += this.a
		}
		if ( this.v == 0 )
		{
			this.bTurned = false
			this.a = 0
			clearInterval(this.Interval)
			this.bMoving = false
			this.Interval = false
			this.SetButtons()
			this.PageStep = 1
			this.FirstLoad = false
		}

		document.getElementById('WibSpan').style.left = this.xLocation + 'px'
	}
}
//
// entry instances for this object
//

//	var beginWIB = WIBStart(1, <%=@friends.length%>, 'wib');

function WIBStart(pageNum, numObjects, objTitle, numInitial){
	WIB = new Class_WIB()
	WIB.Start(pageNum, numObjects, objTitle, numInitial)
	//alert("WIB Created "+pageNum+":"+numObjects+":"+numInitial)
}	

var WIB;
function WIBMouseDown(n) { if(WIB)WIB.MouseDown(n) }
