
﻿
var jPLogic={version:'v2',channelNames:[""],ajaxRequestURL:"localhost",useProxy:false,proxyDomainURL:'localhost',pictureHOST:'localhost',pictureDirectory:null,pictureDefault:null,maxQueue:4,sourceDelay:16}
var jPLogicPrivate={regiaFetchIntervalTime:16,regiaFetchIntervalID:null,regiaAjaxSettings:null,fetchedData:null}
function StartRegiaUpdate()
{if(jPLogicPrivate.regiaFetchIntervalID!=null)
clearInterval(jPLogicPrivate.regiaFetchIntervalID);jPLogicPrivate.fetchedData=null;jPLogicPrivate.regiaFetchIntervalID=null;var requestURL="";if(jPLogic.ajaxRequestURL!="localhost")
requestURL=jPLogic.ajaxRequestURL+"/";if(jPLogic.useProxy)
{requestURL+="RegiaService.aspx?ch=";for(var i=0;i<jPLogic.channelNames.length;i++)
{if(jPLogic.version==undefined||jPLogic.version=='v1')
requestURL+=jPLogic.channelNames[i]+(i<jPLogic.channelNames.length-1?",":"");else if(jPLogic.version=='v2')
requestURL+='v2'+jPLogic.channelNames[i]+(i<jPLogic.channelNames.length-1?",":"");}
requestURL+="&domain="+jPLogic.proxyDomainURL;if(jPLogic.channelNames.length>1)
requestURL+="&pcn=yes";}
else
{if(jPLogic.version==undefined||jPLogic.version=='v1')
requestURL+="regia"+jPLogic.channelNames[0]+".txt";else if(jPLogic.version=='v2')
requestURL+="regia"+"v2"+jPLogic.channelNames[0]+".txt";}
jPLogicPrivate.regiaAjaxOptions={type:"GET",url:requestURL,dataType:"text",cache:false,success:function(data){regiaFetchSuccess(data)}};regiaFetch();setTimeout(function(){regiaFetchIntervalID=setInterval(regiaFetch,jPLogicPrivate.regiaFetchIntervalTime*1000);},5000);}
function regiaFetch()
{$.ajax(jPLogicPrivate.regiaAjaxOptions);}
function regiaFetchSuccess(data)
{if(data.indexOf("##########")==-1)
return;if(jPLogicPrivate.fetchedData==null)
{jPLogicPrivate.fetchedData=data;regiaPublish();}
else if(jPLogicPrivate.fetchedData!=data)
{jPLogicPrivate.fetchedData=data;var delay=jPLogic.sourceDelay*1000-jPLogicPrivate.regiaFetchIntervalTime*1000;if(delay<=0)
regiaPublish();else
setTimeout(regiaPublish,delay);}}
function regiaPublish()
{var fetchedData=jPLogicPrivate.fetchedData;if((fetchedData==null)||(fetchedData==""))
return;var linesAux=fetchedData.split(new RegExp("\n|\r"));var lines=new Array();var i=0;var j=0;for(;i<linesAux.length;i++)
if(linesAux[i].length>=3)
lines[j++]=linesAux[i];if(lines.length==0)
return;i=0;j=0;for(;i<lines.length;j++)
{var channelName="";var pictureURL="";var pictureDefault="";if(j<jPLogic.channelNames.length)
channelName=jPLogic.channelNames[j];if(lines[i].indexOf("##########")==-1)
channelName=lines[i++];if(jPLogic.pictureHOST!='localhost')
pictureURL=jPLogic.pictureHOST+"/";if(jPLogic.pictureDirectory!=null)
pictureURL+=jPLogic.pictureDirectory;else
{if(channelName=="")
pictureURL+="Images/AlbumPictures";else
pictureURL+="Images/AlbumPictures/"+channelName;}
if(jPLogic.pictureDefault!=null)
pictureDefault=jPLogic.pictureDefault;else
pictureDefault="Logo"+channelName+".jpg";var regiaContents=lines[i++];var history=null;var queue=null;var splits=regiaContents.split("##########");if(splits[1]==undefined)
continue;history=splits[0].split("a@@@@@@@@z");queue=splits[1].split("a@@@@@@@@z");var songTitle;var songArtist;var songPicture;var songDuration=Number.NaN;var songNext=Number.NaN;var songAlbum;var songAlbumYear;var j_onair_title;var j_onair_artist;var j_onair_picture;var jselector_suffix="";if(jPLogic.channelNames.length!=1)
jselector_suffix="_"+channelName;else if($('#onair_title'+"_"+channelName).length!=0)
jselector_suffix="_"+channelName;j_onair_title=$('#onair_title'+jselector_suffix);j_onair_artist=$('#onair_artist'+jselector_suffix);j_onair_picture=$('#onair_picture'+jselector_suffix);j_onair_album=$('#onair_album'+jselector_suffix);j_onair_albumyear=$('#onair_albumyear'+jselector_suffix);if(j_onair_title.length!=0)
{songTitle=history[0];songArtist=history[1];songDuration=history[3];songPicture=history[4];if(history.length>=6)
songAlbum=history[5];if(history.length>=7)
songAlbumYear=history[6];var blankSong=(songTitle=="__")&&(songArtist=="__");try
{var played=str2sec(history[2].substring(11));var duration=str2sec(songDuration);if(duration==0)
duration=Number.NaN;songNext=played+duration;}
catch(e)
{songNext=Number.NaN;songDuration=Number.NaN;}
if(blankSong)
{songTitle="";songArtist="";songPicture="";songAlbum="";songAlbumYear="";}
j_onair_title.text(songTitle);j_onair_artist.text(songArtist);j_onair_album.text(songAlbum);j_onair_albumyear.text(songAlbumYear);if(j_onair_picture.length!=0)
{var fullPictureURL;if(songPicture!="")
fullPictureURL=pictureURL+"/"+songPicture;else
fullPictureURL=pictureURL+"/"+pictureDefault;j_onair_picture.attr("src",fullPictureURL);}}
if((jPLogic.maxQueue!=0)&&(queue.length>0)&&((queue.length-1)%4==0))
{var jcuwrapper;if(jPLogic.channelNames.length!=1)
jcuwrapper=$(".ComingUpWrapper."+channelName);else
jcuwrapper=$(".ComingUpWrapper");var iq=0;var ip=0;for(;(iq*4<queue.length-1)&&(ip<jPLogic.maxQueue);iq++)
{songTitle=queue[iq*4];songArtist=queue[iq*4+1];songPicture=queue[iq*4+3];var songScheduleStr="-:-";blankSong=(songTitle=="__")&&(songArtist=="__");if(blankSong)
{songTitle="";songArtist="";songPicture="";songScheduleStr="";}
if(!blankSong&&!isNaN(songNext))
{try
{songDuration=str2sec(queue[iq*4+2]);if(songDuration==0)
songDuration=Number.NaN;songScheduleStr=sec2str(songNext);songNext+=songDuration;}
catch(e)
{songScheduleStr="-:-";songNext=Number.NaN;songDuration=Number.NaN;}}
if(songDuration>45||isNaN(songNext)||blankSong)
{jcuwrapper.find(".title_"+ip).text(songTitle);jcuwrapper.find(".artist_"+ip).text(songArtist);jcuwrapper.find(".schedule_"+ip).text(songScheduleStr);jcuwrapper.find(".row_"+ip).show();ip++;}}
for(;ip<jPLogic.maxQueue;ip++)
jcuwrapper.find(".row_"+ip).hide();}}}
function sec2str(sec)
{var hh=Math.floor(sec/3600)%24;sec=sec%3600;var mm=Math.floor(sec/60)
sec=sec%60;return hh+":"+(mm<=9?"0"+mm:mm);}
function str2sec(str)
{if(str.indexOf(".")!=-1)
splits=str.split(".");else
splits=str.split(":");if(splits.length==3)
{hh=parseInt(splits[0],10);mm=parseInt(splits[1],10);ss=parseInt(splits[2],10);}
else if(splits.length==2)
{hh=0;mm=parseInt(splits[0],10);ss=parseInt(splits[1],10);}
else
return Number.NaN;return hh*3600+mm*60+ss;}
