
/////////////////////////////////////////////////////////////////////////////////
//         JukeBox Widget Renderer
////////////////////////////////

// Add widget to library:
library["JJUKEBOX"]=widgetInfo("JJUKEBOX","new jJukeBox", "Pinchadiscos") 


function jJukeBox_Render(config,id)
{
	
	this.num=jJukeBox_Render.count++
	this.config=config
	this.id=id
	this.playList="";

}

function jJukeBox_RenderRender()
{
	if(this.num>=1)
		return false; //kill this extra jukebox object
	
	var div
	
	div="jJukeBoxDIV" + this.num
	
	this.checkCtrlFrameLoaded();

	return true;	 //stay alive

}

function jJukeBox_RenderCheckCtrlFrameLoaded()
{

	if (! (window.parent.frames["commonctrl"].document.readyState=="complete"))
	{
		window.setTimeout("a=widgets[" + this.id + "];a.checkCtrlFrameLoaded();",1000);
		return;
	}
	

	cfg=this.config

	var st=cfg["PLAYLIST"]
	if(!st)
		st="";
	
	var pl=st.split(",")
	
	this.ctrl=window.parent.frames["commonctrl"]
	this.ctrl.jbInitialize();
	
	if (st=="")
	{
			this.ctrl.jbClearPlayList()
			this.ctrl.jbStop();
	}

	callback=new Function("info","a=widgets[" + this.id + "];a.resourceInfoCallback(info);")
	
	this.playList=new Array()
	
	for (i in pl)
		if (pl[i]!="")
		{
			obj=new Object();
			obj.IDResource=pl[i];
			obj.loaded=false;
			this.playList[i]=obj;
		}

	for (i in this.playList)
			getResourceInfoAsyncPack(this.playList[i].IDResource,callback)

}



function jJukeBox_RenderDestroy()
{
	this.config=null
	delete this.config
}

function jJukeBox_RenderResourceInfoCallback(info)
{

	var obj
	
	for(i in this.playList)
	{
		obj=this.playList[i]
		if (obj.IDResource==info.IDResource)
		{
			obj.loaded=true;
			obj.info=info;
		}
	}

	for(i in this.playList)
	{
		obj=this.playList[i]
		if(!obj.loaded)
			return;
	}

	this.ctrl.jbClearPlayList()
	this.ctrl.jbStop();
	
	for(i in this.playList)
	{
		obj=this.playList[i]
		this.ctrl.jbAddSong(relativizeURL(obj.info.url));
	}
	
	if( this.config["RNDORDER"]== -1 )
		this.ctrl.jbShuffle();

	this.ctrl.jbPlayRepeat= this.config["REPEAT"]== -1
	
	this.ctrl.jbPlayNext();
	

}

function relativizeURL(url)
{
	//var n=url.indexOf("/",7);
	
	//return url.substr(n);
	return url;

}

jJukeBox_Render.prototype.resourceInfoCallback=jJukeBox_RenderResourceInfoCallback
jJukeBox_Render.prototype.render=jJukeBox_RenderRender
jJukeBox_Render.prototype.destroy=jJukeBox_RenderDestroy
jJukeBox_Render.prototype.checkCtrlFrameLoaded=jJukeBox_RenderCheckCtrlFrameLoaded

jJukeBox_Render.count=0
