// D. Kadrioski
// 9/13/11

//------------------------------------------------------------------------------
function WebinarScroller(id, ds, visibleRowCount){
	this.id = id;
	this.ds = ds;
	this.visibleRowCount = visibleRowCount;
	this.currentIndex    = 0;

	this.firstUpdate = true;

	this.linksA = [];
	this.linksB = [];

	// Setup link Elms
	for(var i=0;i<this.visibleRowCount;i++){
		this.linksA.push( domapi.Elm( { id: this.id + "Link"+ i + "a" } ) );
		this.linksB.push( domapi.Elm( { id: this.id + "Link"+ i + "b" } ) );
	}
};
//------------------------------------------------------------------------------
WebinarScroller.prototype.initialRender = function(){
	this.fillLinks();
};
//------------------------------------------------------------------------------
WebinarScroller.prototype.renderNext = function(){
	this.fillLinks();
};
//------------------------------------------------------------------------------
WebinarScroller.prototype.fillLinks = function(){
	// Loop over the visible links.
	var self = this;
	for(var i=0;i<this.visibleRowCount;i++){
		var item = null;

		// Find the item to show in this link (or null if we are all out).
		if(this.currentIndex < this.ds.length){
			item = this.ds[ this.currentIndex ];
			this.currentIndex++;
		}else{
			item = null;
		}

		// Closures in a loop, keeping index intact:  http://www.mennovanslooten.nl/blog/post/62
		var closureFuncA = (function(linkIdx, item){
			return function(){
				self.fadeOut( linkIdx, item );
			}
		})( i, item );

		if(this.firstUpdate){
			// First time through, just update all the links immediately.
			this.fillLinkText(i, item);
		}else{
			setTimeout( closureFuncA, 100 * i );
		}
	}

	if(this.firstUpdate){
		this.firstUpdate = false;
	}

	// If after this cycle we are out of events, reset.
	if(this.currentIndex >= this.ds.length){
		this.currentIndex = 0;
	}
};
//------------------------------------------------------------------------------
WebinarScroller.prototype.fadeOut = function(linkIdx, item){
	var A = this.linksA[ linkIdx ];
	var B = this.linksB[ linkIdx ];
	var self = this;

	A.alphaTo(
		0, 2, 20, 50,
		function(){
			self.fillLinkText( linkIdx, item );
			A.alphaTo( 100, 2, 20, 50 );
		}
	);

	B.alphaTo(
		0, 2, 20, 50,
		function(){
			B.alphaTo( 100, 2, 20, 50 );
		}
	);
};
//------------------------------------------------------------------------------
WebinarScroller.prototype.fillLinkText = function(linkIdx, item){
	// Null items just blank out the anchors, otherwise they are updated here.
	var A = this.linksA[ linkIdx ];
	var B = this.linksB[ linkIdx ];

	if(item == null){
		A.innerHTML = "";
		B.innerHTML = "";

	}else{
		A.innerHTML = "&#8226; " + item.event_name + " w/ " + item.host;
		B.innerHTML = item.event_date;

		A.href      = "/index.cfm?page=webinars&id=" + item.user_id + "&wid=" + item.id
		A.title     = item.description;

		B.href      = "/index.cfm?page=webinars&id=" + item.user_id + "&wid=" + item.id
		B.title     = item.description;
	}
};
//------------------------------------------------------------------------------

