

/**
 * Generowanie aktualnie granej pozycji w Radiu Polska Live!
 * @autor Paweł Psztyć <p.psztyc@polskalive.com.pl>
 * @version 1.0
 */
// definicje źródeł
var dataSource = 'http://www.polskalive.com.pl/nowplaying/';
var waitImgSource = 'http://www.polskalive.com.pl/images/spin.gif';
//funkcje pobierające dane z serwera
function JSONscriptRequest(fullUrl) {
    this.fullUrl = fullUrl; 
    this.noCacheIE = '&noCacheIE=' + (new Date()).getTime();
    this.headLoc = document.getElementsByTagName("head").item(0);
    this.scriptId = 'JscriptId' + JSONscriptRequest.scriptCounter++;
}
JSONscriptRequest.scriptCounter = 1;
JSONscriptRequest.prototype.buildScriptTag = function () {
    this.scriptObj = document.createElement("script");
    this.scriptObj.setAttribute("type", "text/javascript");
    this.scriptObj.setAttribute("charset", "utf-8");
    this.scriptObj.setAttribute("src", this.fullUrl +this.noCacheIE );
    this.scriptObj.setAttribute("id", this.scriptId);
}
JSONscriptRequest.prototype.removeScriptTag = function () {
    this.headLoc.removeChild(this.scriptObj);  
}
JSONscriptRequest.prototype.addScriptTag = function () {
    this.headLoc.appendChild(this.scriptObj);
}
//obiekt obsługujący pobieranie danych i ich wyświetlanie
NowPlaying = {
	box:null,
	inited:false,
	XMLfile:'',
	waitImg:'',
	bnow:{},
	bnext:{},
	bx:200,
	by:150,
	bborder:'none',
	brefresh:120000,
	runTimeout:0,
	waitDiv:null,
	dsp:'block',
	fsize:'auto',
	spanbreak:5,
	nowTxt:'Teraz',
	nextTxt:'Następnie',
	waitTxt:'ładowanie',
	init:function(){
		if( typeof _pb!="undefined"){
			this.box=$(_pb);
			this.getConfig();
			this.configureBox();
			this.inited=true;
		}
		this.start();
	},
	getConfig:function(){
		this.XMLfile=dataSource;
		if(typeof _pbwaitimg!="undefined")this.waitImg=_pbwaitimg; else this.waitImg=waitImgSource;
		if(typeof _pbnowtxt!="undefined") this.nowTxt=_pbnowtxt;
		if(typeof _pbnexttxt!="undefined") this.nextTxt=_pbnexttxt;
		if(typeof _pbwaittxt!="undefined") this.waitTxt=_pbwaittxt;
		if(typeof _pbwidth!="undefined") {if(!isNaN(_pbwidth)||_pbwidth=='auto') this.bx=_pbwidth;}
		if(typeof _pbheight!="undefined") {if(!isNaN(_pbheight)||_pbheight=='auto') this.by=_pbheight;}
		if(typeof _pbborder!="undefined") this.bborder=_pbborder;
		if(typeof _pbtime!="undefined") { if(!isNaN(_pbtime)) this.brefresh=_pbtime;}
		if(typeof _pbdisplay!="undefined") {if(_pbdisplay=='inline'||_pbdisplay=='block') this.dsp=_pbdisplay;}
		if(typeof _pbfsize!="undefined") { if(!isNaN(_pbfsize)) this.fsize=_pbfsize;}
		if(typeof _pbsep!="undefined") { if(!isNaN(_pbsep)) this.spanbreak=_pbsep;}
	},
	configureBox:function(){
		if(this.bx!='auto')	this.box.style.width=this.bx+'px';
		if(this.by!='auto') this.box.style.height=this.by+'px';
		this.box.style.border=this.bborder;
		if(this.fsize!='auto') this.box.style.fontSize=this.fsize+='px';
	},
	waitInfo:function(){
		if(this.waitDiv==null){
			this.waitDiv=document.createElement('div');
			this.waitDiv.setAttribute("id", 'waitDiv');
			var img=document.createElement('img');
			img.setAttribute("id", 'spingif');
			img.setAttribute("src", this.waitImg);
			img.setAttribute("alt", '...');
			this.waitDiv.innerHTML=this.waitTxt+' ';
			this.waitDiv.appendChild(img);
			this.box.appendChild(this.waitDiv);
		}
		this.waitDiv.style.display='inline';
	},
	endWait:function(){
		this.waitDiv.style.display='none';
	},
	start:function(){
		if(!this.inited) return null;
		this.waitInfo();
		bObj = new JSONscriptRequest(this.XMLfile); 
		bObj.buildScriptTag();
		bObj.addScriptTag();
		this.run();
	},
	build:function(data){
		this.createContent();
		this.bnow.innerHTML='<strong>'+this.nowTxt+':</strong> '+data.no.t;
		this.bnext.innerHTML='<strong>'+this.nextTxt+':</strong> '+data.ne.t;
		this.endWait();
		this.drow();
		bObj.removeScriptTag();
	},
	createContent:function(){
		var element = (this.dsp=='block')?'div':'span';
		this.bnow=document.createElement(element);
		this.bnow.setAttribute("id", 'bnow');
		if(element=='span') this.bnow.style.marginRight=this.spanbreak+'px';
		this.bnext=document.createElement(element);
		this.bnext.setAttribute("id", 'bnext');
	},
	drow:function(){
		this.box.appendChild(this.bnow);
		this.box.appendChild(this.bnext);
	},
	clean:function(){
		if(typeof this.bnow.id=="undefined") return null;
		this.box.removeChild(this.bnow);
		this.box.removeChild(this.bnext);
		this.bnow={};
		this.bnext={};
		for(var i=0;i<this.box.childNodes.length;i++){
			if(this.box.childNodes[i].id!='waitDiv')
				this.box.removeChild(this.box.childNodes[i]);
		}
	},
	run:function(){
		this.runTimeout=window.setTimeout(NowPlaying.rebuild,this.brefresh);
	},
	rebuild:function(){
		window.clearTimeout(this.runTimeout);
		NowPlaying.clean();
		NowPlaying.waitInfo();
		NowPlaying.start();
	}
}
if(typeof $!='function'){
	function $(element) {
	  if (arguments.length > 1) {
	    for (var i = 0, elements = [], length = arguments.length; i < length; i++)
	      elements.push($(arguments[i]));
	    return elements;
	  }
	  if (typeof element == "string")
	    element = document.getElementById(element);
	  return element;
	}
}
NowPlaying.init();
