wrong files comitted previously
[enigma2-plugins.git] / webinterface / src / web-data / tools.js
1 var url_getvolume = '/web/vol?set=state'; 
2 var url_setvolume = '/web/vol?set=set'; // plus new value eq. set=set15
3 var url_volumeup = '/web/vol?set=up';
4 var url_volumedown = '/web/vol?set=down';
5 var url_volumemute = '/web/vol?set=mute';
6
7 var url_epgservice = "/web/epgservice?ref="; // plus serviceRev
8 var url_epgsearch = "/web/epgsearch?search="; // plus serviceRev
9 var url_epgnownext = "/web/epgnownext?ref="; // plus serviceRev
10
11 var DBG = true;
12
13 function openWindow(title, inner, width, height, id){
14                         if(id == null) id = new Date().toUTCString();
15                         
16                         //debug(id);
17                         var win = new Window(id, {className: "alphacube", title: title, width: width, height: height});
18                         win.getContent().innerHTML = inner;
19                         win.setDestroyOnClose();
20                         win.showCenter();
21                         debug("opening Window: "+title);
22                         return win;
23 }
24
25 function messageBox(t, m){
26         Dialog.alert(m, {windowParameters: {title: t, className: "alphacube", width:200}, okLabel: "Close"});
27 }
28
29 function getHTTPObject( ){
30     var xmlHttp = false;
31             
32     // try to create a new instance of the xmlhttprequest object        
33     try{
34         // Internet Explorer
35         if( window.ActiveXObject ){
36             for( var i = 5; i; i-- ){
37                 try{
38                     // loading of a newer version of msxml dll (msxml3 - msxml5) failed
39                     // use fallback solution
40                     // old style msxml version independent, deprecated
41                     if( i == 2 ){
42                         xmlHttp = new ActiveXObject( "Microsoft.XMLHTTP" );    
43                     }
44                     // try to use the latest msxml dll
45                     else{
46                         
47                         xmlHttp = new ActiveXObject( "Msxml2.XMLHTTP." + i + ".0" );
48                     }
49                     break;
50                 }
51                 catch( excNotLoadable ){                        
52                     xmlHttp = false;
53                 }
54             }
55         }
56         // Mozilla, Opera und Safari
57         else if( window.XMLHttpRequest ){
58             xmlHttp = new XMLHttpRequest();
59         }
60     }
61     // loading of xmlhttp object failed
62     catch( excNotLoadable ){
63         xmlHttp = false;
64     }
65     return xmlHttp ;
66 }
67
68 //RND Template Function
69 function RND(tmpl, ns) {
70         var fn = function(w, g) {
71           g = g.split("|");
72           var cnt = ns[g[0]];
73
74           //Support for filter functions
75           for(var i=1; i < g.length; i++) {
76                 cnt = eval(g[i])(cnt);
77           }
78           return cnt || w;
79         };
80         return tmpl.replace(/%\(([A-Za-z0-9_|.]*)\)/g, fn);
81 }
82
83 function debug(text){
84         if(DBG){
85                 try{
86                         debugWin.getContent().innerHTML += "DEBUG: "+text+"<br>";
87                 } catch (windowNotPresent) {}
88         }
89 }
90
91 function showhide(id){
92         o = document.getElementById(id).style;
93         o.display = (o.display!="none")? "none":"";
94 }
95
96 function set(what, value){
97         //debug(what+"-"+value);
98         element = document.getElementById(what);
99         element.innerHTML = value;
100         //$('scriptzone').innerHTML = ""; // deleting set() from page, to keep the page short and to save memory
101 }
102
103 function doRequest(url, readyFunction){
104
105         new Ajax.Request(url,
106                 {
107                         method: 'get', 
108                         onSuccess: readyFunction
109                 });
110 }
111
112 function getXML(request){
113         if (document.implementation && document.implementation.createDocument){
114                 debug("using responseXML");
115                 var xmlDoc = request.responseXML
116         }
117         else if (window.ActiveXObject){
118                 debug("Creating XML for IE");
119                 var xmlInsert = document.createElement('xml');
120
121                 xmlInsert.setAttribute('innerHTML',request.responseText);
122                 xmlInsert.setAttribute('id','_MakeAUniqueID');
123                 document.body.appendChild(xmlInsert);
124                 xmlDoc = document.getElementById('_MakeAUniqueID');
125                 document.body.removeChild(document.getElementById('_MakeAUniqueID'));
126         } else {
127                 debug("Your Browser Sucks!");
128         }
129         return xmlDoc;
130 }
131
132 function zap(li){
133         var url = "/web/zap?ZapTo=" + escape(li.id);
134         //debug("requesting "+url);
135         new Ajax.Request( url,
136                 {
137                         method: 'get'                           
138                 });
139 }
140
141
142 //+++++++++++++++++++++++++++++++++++++++++++++++++++++
143 //+++++++++++++++++++++++++++++++++++++++++++++++++++++
144 //++++ EPG functions                               ++++
145 //+++++++++++++++++++++++++++++++++++++++++++++++++++++
146 //+++++++++++++++++++++++++++++++++++++++++++++++++++++
147 var EPGList = Class.create();
148 EPGList.prototype = {
149         //contructor
150         initialize: function(){
151                 debug("init class EPGList");
152         },
153         getBySearchString: function(string){
154                 debug("requesting "+ url_epgsearch+string);
155                 new Ajax.Request( url_epgsearch+string,
156                         {
157                                 method: 'get', 
158                                 onComplete: this.incomingEPGrequest
159                         });
160                 
161         },
162         getByServiceReference: function(serviceRef){
163                 new Ajax.Request(url_epgservice+serviceRef,
164                         {
165                                 method: 'get', 
166                                 onComplete: this.incomingEPGrequest
167                         });
168                 
169         },
170         
171         
172         renderTable: function(epglist){
173                 debug("rendering Table with "+epglist.length+" events");
174                 var html='<table width="100%" border="0" cellspacing="1" cellpadding="0" border="1">';
175                 for (var i=0; i < epglist.length; i++){
176                         try{
177                                 var item = epglist[i];
178                                 
179                                 //Create JSON Object for Template
180                                 var namespace = {       'date': item.getTimeDay(), 
181                                                                         'servicename': item.getServiceName(), 
182                                                                         'title': item.getTitle(),
183                                                                         'titleESC': escape(item.getTitle()),
184                                                                         'starttime': item.getTimeStartString(), 
185                                                                         'duration': (item.getDuration()/60000), 
186                                                                         'description': item.getDescription(), 
187                                                                         'endtime': item.getTimeEndString(), 
188                                                                         'extdescription': item.getDescriptionExtended()
189                                                                 };
190                                 //Fill template with data and add id to our result
191                                 html += RND(tplEPGListItem, namespace);
192                         } catch (blubb) {
193                                 //debug("Error rendering: "+blubb);
194                         }
195                 }
196                 
197                 html +="</table>";
198                 //element.innerHTML = html;
199                 openWindow("Electronic Program Guide", html, 900, 500);
200                 
201         },
202         incomingEPGrequest: function(request){
203                 debug("incoming request" +request.readyState);          
204                 if (request.readyState == 4)
205                 {
206                         var EPGItems = getXML(request).getElementsByTagName("e2eventlist").item(0).getElementsByTagName("e2event");
207                         
208                         debug("have "+EPGItems.length+" e2events");
209                         if(EPGItems.length > 0){
210                         
211                                 epglist = new Array();
212                                 for(var i=0; i < EPGItems.length; i++){         
213                                         epglist.push(new EPGEvent(EPGItems.item(i)));
214                                 }
215                                 debug("Calling prototype.renderTable(epglist)");
216                                 EPGList.prototype.renderTable(epglist);
217                                 
218                         } else {
219                                 messageBox('No Items found!', 'Sorry but i could not find any EPG Content containing your search value');
220                         }
221                         
222                 }
223         }
224         
225 }
226
227 function EPGEvent(element){     
228         // parsing values from xml-element
229         try{
230                 this.eventID = element.getElementsByTagName('e2eventid').item(0).firstChild.data;
231                 this.startTime = element.getElementsByTagName('e2eventstart').item(0).firstChild.data;
232                 this.duration = element.getElementsByTagName('e2eventduration').item(0).firstChild.data;
233                 this.title = element.getElementsByTagName('e2eventtitle').item(0).firstChild.data;
234                 this.description = element.getElementsByTagName('e2eventdescription').item(0).firstChild.data;
235                 this.descriptionE = element.getElementsByTagName('e2eventdescriptionextended').item(0).firstChild.data;
236                 this.serviceRef = element.getElementsByTagName('e2eventservicereference').item(0).firstChild.data;
237                 this.serviceName = element.getElementsByTagName('e2eventservicename').item(0).firstChild.data;
238         } catch (bullshit) {
239                 //debug("Bullshit is:"+bullshit);
240         }
241         this.getEventId = function ()
242         {
243                 return this.eventID;
244         }
245         this.getTimeStart = function ()
246         {
247                 var date = new Date(parseInt(this.startTime)*1000);
248                 return date;
249         }
250         this.getTimeStartString = function ()
251         {
252                 var h = this.getTimeStart().getHours();
253                 var m = this.getTimeStart().getMinutes();
254                 if (m < 10){
255                         m="0"+m;
256                 }
257                 return h+":"+m;
258         }
259         this.getTimeDay = function ()
260         {
261                 var Wochentag = new Array("So", "Mo", "Di", "Mi", "Do", "Fr", "Sa");
262                 var wday = Wochentag[this.getTimeStart().getDay()];
263                 var day = this.getTimeStart().getDate();
264                 var month = this.getTimeStart().getMonth()+1;
265                 var year = this.getTimeStart().getFullYear();
266                 
267                 return wday+".&nbsp;"+day+"."+month+"."+year;
268         }
269         this.getTimeEnd = function ()
270         {
271                 var date = new Date((parseInt(this.startTime)+parseInt(this.duration))*1000);
272                 return date;
273         }
274         this.getTimeEndString = function ()
275         {
276                 var h = this.getTimeEnd().getHours();
277                 var m = this.getTimeEnd().getMinutes();
278                 if (m < 10){
279                         m="0"+m;
280                 }
281                 return h+":"+m;
282         }
283         this.getDuration = function ()
284         {
285                 return  new Date(parseInt(this.duration)*1000);
286         }
287         this.getTitle = function ()
288         {
289                 return this.title;
290         }
291         this.getDescription = function ()
292         {
293                 return this.description;
294         }
295         this.getDescriptionExtended = function ()
296         {
297                 return this.descriptionE;
298         }
299         this.getServiceReference = function ()
300         {
301                 return this.serviceRef;
302         }
303         this.getServiceName = function ()
304         {
305                 return this.serviceName.replace(" ","&nbsp;");
306         }
307 }//END class EPGEvent
308
309 //+++++++++++++++++++++++++++++++++++++++++++++++++++++
310 //+++++++++++++++++++++++++++++++++++++++++++++++++++++
311 //++++ GUI functions                               ++++
312 //+++++++++++++++++++++++++++++++++++++++++++++++++++++
313 //+++++++++++++++++++++++++++++++++++++++++++++++++++++
314 var currentBodyMainElement = null
315
316 function setBodyMainContent(newelementname){
317         newelement =document.getElementById(newelementname);
318         if(currentBodyMainElement != null){
319                 currentBodyMainElement.style.display = "none";
320                 
321         }
322         newelement.style.display = "";
323         currentBodyMainElement = newelement;
324 }
325
326 //+++++++++++++++++++++++++++++++++++++++++++++++++++++
327 //+++++++++++++++++++++++++++++++++++++++++++++++++++++
328 //++++ volume functions                            ++++
329 //+++++++++++++++++++++++++++++++++++++++++++++++++++++
330 //+++++++++++++++++++++++++++++++++++++++++++++++++++++
331
332 function initVolumePanel(){
333         document.getElementById('VolumePanel').innerHTML = tplVolumePanel;
334         getVolume(); 
335 }
336 function getVolume()
337 {
338         doRequest(url_getvolume,handleVolumeRequest);
339 }
340 function volumeSet(newvalue)
341 {
342         doRequest(url_setvolume+newvalue,handleVolumeRequest);
343 }
344 function volumeUp()
345 {
346         doRequest(url_volumeup,handleVolumeRequest);
347 }
348 function volumeDown()
349 {
350         doRequest(url_volumedown,handleVolumeRequest);  
351 }
352 function volumeMute()
353 {
354         doRequest(url_volumemute,handleVolumeRequest);
355 }
356 function handleVolumeRequest(request){
357         if (request.readyState == 4) {
358                 var b = getXML(request).getElementsByTagName("e2volume");
359                 debug(b.item(0).getElementsByTagName('e2current').length); 
360                 
361                 var newvalue = b.item(0).getElementsByTagName('e2current').item(0).firstChild.data;
362                 var mute = b.item(0).getElementsByTagName('e2ismuted').item(0).firstChild.data;
363                 debug("volume"+newvalue+";"+mute);
364                 
365                 for (var i = 1; i <= 10; i++)
366                 {
367                         if ( (newvalue/10)>=i){
368                                 $("volume"+i).src = "/webdata/gfx/led_on.png";
369                         }else{
370                                 $("volume"+i).src = "/webdata/gfx/led_off.png";
371                         }
372                 }
373                 if (mute == "False"){
374                         $("speaker").src = "/webdata/gfx/speak_on.png";
375                 }else{
376                         $("speaker").src = "/webdata/gfx/speak_off.png";
377                 }
378         }       
379 }
380 //+++++++++++++++++++++++++++++++++++++++++++++++++++++
381 //+++++++++++++++++++++++++++++++++++++++++++++++++++++
382 //++++ bouquet managing functions                   ++++
383 //+++++++++++++++++++++++++++++++++++++++++++++++++++++
384 //+++++++++++++++++++++++++++++++++++++++++++++++++++++
385 function loadRootTVbouquet(){
386         url = '/web/fetchchannels?ServiceListBrowse='+escape('1:7:1:0:0:0:0:0:0:0:(type == 1) || (type == 17) || (type == 195) || (type == 25)  FROM BOUQUET &quot;bouquets.tv&quot; ORDER BY bouquet');
387         loadBouquet(url);
388 }
389
390 function loadBouquet(url){
391         doRequest(url, incomingBouquet);
392 }
393
394 function incomingBouquet(request){
395         if((request.readyState == 4) && (t.status == 200)) {
396         // perfekt!
397                 
398                 var b = getXML(request).getElementsByTagName("e2servicelist").item(0).getElementsByTagName("e2service");
399
400                 bouquets = new Array();
401                 for ( var i=0; i < b.length; i++){
402                         bRef = b.item(i).getElementsByTagName('e2servicereference').item(0).firstChild.data;
403                         bName = b.item(i).getElementsByTagName('e2servicename').item(0).firstChild.data;
404                         
405                         bu = new Array(bName,bRef);
406                         bouquets.push(bu)
407                 }
408                 refreshSelect(bouquets);
409         } else {
410         // die Anfrage enthielt Fehler;
411         // die Antwort war z.B. 404 (nicht gefunden)
412         // oder 500 (interner Server-Fehler)
413         }
414         
415 }
416
417 // to add the bouquetts to the list
418 function refreshSelect(arraybouquet){
419         doRequest(url, incomingChannellist);
420         sel = document.getElementById("accordionMenuebouquetContent");
421         // options neu eintragen
422         html = "<table>";
423                 for ( var i = 0 ; i < arraybouquet.length ; i++ ){
424                 if(arraybouquet[i][0] && arraybouquet[i][1]){
425                         html+="<tr><td>";
426                         html+="<a  onclick=\"bouquetSelected(this); setBodyMainContent('BodyContentChannellist');\" id='";
427                         html+= arraybouquet[i][1];
428                         html+="'>";
429                         html+= arraybouquet[i][0];
430                         html+="</a>";
431                         html+="</td></tr>";
432                 }
433         }
434         html+="</table>";
435         sel.innerHTML=html;
436         refreshChannellist(arraybouquet[0][0],arraybouquet[0][1]);
437 }
438
439 //++++++++++++++++++++++
440 function bouquetSelected(element){
441         refreshChannellist(element.value,element.id)
442 }
443
444 function refreshChannellist(bname, bref){
445         var url = '/web/fetchchannels?ServiceListBrowse='+escape(bref);
446         doRequest(url, incomingChannellist);
447 }
448                         
449 function incomingChannellist(request){
450         if(request.readyState == 4){
451
452                 services = getXML(request).getElementsByTagName("e2servicelist").item(0).getElementsByTagName("e2service");
453                 
454                 listerHtml      = tplServiceListHeader;
455                 
456                 debug("got "+services.length+" Services");
457                 
458                 for ( var i = 0; i < (services.length ); i++){
459                         sRef = services.item(i).getElementsByTagName('e2servicereference').item(0).firstChild.data;
460                         sName = services.item(i).getElementsByTagName('e2servicename').item(0).firstChild.data;
461                                 
462                         var namespace = {       'serviceref': sRef,
463                                                                 'servicerefESC': escape(sRef), 
464                                                                 'servicename': sName 
465                                                         };
466                                                         
467                         listerHtml += RND(tplServiceListItem, namespace);
468                         
469                 }
470                 
471                 listerHtml += tplServiceListFooter;
472                 document.getElementById('BodyContentChannellist').innerHTML = listerHtml;
473                 setBodyMainContent('BodyContentChannellist');
474         }
475 }
476
477
478
479
480