/* manages the tab navigation */
var PlayerControls={
	containerclass:'player-controls',
	itemclass:'player-control',
	activeclass:'on',
	overclass:'over'
};
Object.extend(PlayerControls,Page);

/* Player Control Methods */

/* process
 * fired when the dom is ready
 * if an "on" class is found, the player is invoked
 */
PlayerControls.process=function(item)
{
	var parent=this.traverse(item,this.containerclass).identify();
	var href=item.down().readAttribute("href");
	
	if(this[parent]==undefined)
		this[parent]=new Object();

	//
	if(this[parent][item.identify()]==undefined)
		this[parent][item.identify()]=new Object();
	else
		return;
		
	//if it's already "on"
	if(item.hasClassName(this.activeclass))
	{
		this[parent].selected=item;
		$('video_player').loadPlaylist(href + '/playlist');
	}
	
	//if the item is the selected item
	if(this[parent].selected && this[parent][this[parent].selected.id].playlist==href)
	{
		item.addClassName(this.activeclass);
		this[parent].selected=item;
	}
	
	//store and replace the hrefs
	this[parent][item.identify()].playlist=href;
	item.down().setAttribute("href","javascript:void(0);");
	//add the onclick handler
	this.bfx_click=this.click.bindAsEventListener(this);
	this.bfx_over=this.over.bindAsEventListener(this);
	this.bfx_out=this.out.bindAsEventListener(this);
	
	Event.observe(item,"click",this.bfx_click);
	Event.observe(item,"mouseover",this.bfx_over);
	Event.observe(item,"mouseout",this.bfx_out);
};

/* mouse overs */
PlayerControls.over=function(evt)
{
	var e=this.claim(evt);
	e.addClassName(this.overclass);
};

/* mouse outs */
PlayerControls.out=function(evt)
{
	var e=this.claim(evt);
	e.removeClassName(this.overclass);
};

PlayerControls.cleanup=function(p,item)
{
	//remove listeners
	Event.stopObserving(item,"click",this.bfx_click);
	Event.stopObserving(item,"mouseover",this.bfx_over);
	Event.stopObserving(item,"mouseout",this.bfx_out);
	//remove the obj
	this[p][item.id]=null;	
};

/* add to the page listener */
PlayerControls.load=function()
{
	$$("."+PlayerControls.itemclass).each(PlayerControls.process.bind(PlayerControls));
};

/* user clicks on a control */
PlayerControls.click=function(evt)
{
	var e=this.claim(evt);
	//search and identify the element
	var e_id=e.identify();
	//find the container
	var p=this.traverse(e,this.containerclass).identify();
	//if it's already selected, go away
	if(this[p].selected==e)
		return;
	$('video_player').loadPlaylist(this[p][e_id].playlist + '/playlist');
	//remove classnames
	e.addClassName(this.activeclass);
	//might start w/ non being selected
	if(this[p].selected)
		this[p].selected.removeClassName(this.activeclass);
	this[p].selected=e;
};

PlayerControls.claim=function(evt)
{
	return (evt.type)?this.traverse(Event.element(evt),this.itemclass):evt;
};