]> git.sesse.net Git - vlc/blob - share/http-lua/js/functions.js
Add "search as you type" to the HTTP interface.
[vlc] / share / http-lua / js / functions.js
1 /*****************************************************************************
2  * functions.js: VLC media player web interface
3  *****************************************************************************
4  * Copyright (C) 2005-2006 the VideoLAN team
5  * $Id: functions.js 21264 2007-08-19 17:48:28Z dionoea $
6  *
7  * Authors: Antoine Cellerier <dionoea -at- videolan -dot- org>
8  *
9  * This program is free software; you can redistribute it and/or modify
10  * it under the terms of the GNU General Public License as published by
11  * the Free Software Foundation; either version 2 of the License, or
12  * (at your option) any later version.
13  *
14  * This program is distributed in the hope that it will be useful,
15  * but WITHOUT ANY WARRANTY; without even the implied warranty of
16  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
17  * GNU General Public License for more details.
18  *
19  * You should have received a copy of the GNU General Public License
20  * along with this program; if not, write to the Free Software
21  * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111, USA.
22  *****************************************************************************/
23
24 /**********************************************************************
25  * Global variables
26  *********************************************************************/
27
28 var old_time = 0;
29 var pl_cur_id;
30 var albumart_id = -1;
31
32 /**********************************************************************
33  * Slider functions
34  *********************************************************************/
35  
36 var slider_mouse_down = 0;
37 var slider_dx = 0;
38
39 /* findPosX() from http://www.quirksmode.rg/js/indpos.html */
40 function findPosX(obj)
41 {
42     var curleft = 0;
43     if (obj.offsetParent)
44     {
45         while (obj.offsetParent)
46         {
47             curleft += obj.offsetLeft
48             obj = obj.offsetParent;
49         }
50     }
51     else if (obj.x)
52         curleft += obj.x;
53     return curleft;
54 }
55
56 function slider_seek( e, bar )
57 {
58     seek(Math.floor(( e.clientX + document.body.scrollLeft - findPosX( bar )) / 4)+"%25");
59 }
60 function slider_down( e, point )
61 {
62     slider_mouse_down = 1;
63     slider_dx = e.clientX - findPosX( point );
64 }
65 function slider_up( e, bar )
66 {
67     slider_mouse_down = 0;
68     /* slider_seek( e, bar ); */
69 }
70 function slider_move( e, bar )
71 {
72     if( slider_mouse_down == 1 )
73     {
74         var slider_position  = Math.floor( e.clientX - slider_dx + document.body.scrollLeft - findPosX( bar ));
75         document.getElementById( 'main_slider_point' ).style.left = slider_position+"px";
76         slider_seek( e, bar );
77     }
78 }
79
80 /**********************************************************************
81  * Misc utils
82  *********************************************************************/
83
84 /* XMLHttpRequest wrapper */
85 function loadXMLDoc( url, callback )
86 {
87   // branch for native XMLHttpRequest object
88   if ( window.XMLHttpRequest )
89   {
90     req = new XMLHttpRequest();
91     req.onreadystatechange = callback;
92     req.open( "GET", url, true );
93     req.send( null );
94   // branch for IE/Windows ActiveX version
95   }
96   else if ( window.ActiveXObject )
97   {
98     req = new ActiveXObject( "Microsoft.XMLHTTP" );
99     if ( req )
100     {
101       req.onreadystatechange = callback;
102       req.open( "GET", url, true );
103       req.send();
104     }
105   }
106 }
107
108 /* fomat time in second as hh:mm:ss */
109 function format_time( s )
110 {
111     var hours = Math.floor(s/3600);
112     var minutes = Math.floor((s/60)%60);
113     var seconds = Math.floor(s%60);
114     if( hours < 10 ) hours = "0"+hours;
115     if( minutes < 10 ) minutes = "0"+minutes;
116     if( seconds < 10 ) seconds = "0"+seconds;
117     return hours+":"+minutes+":"+seconds;
118 }
119
120 /* delete all a tag's children and add a text child node */
121 function set_text( id, val )
122 {
123     var elt = document.getElementById( id );
124     while( elt.hasChildNodes() )
125         elt.removeChild( elt.firstChild );
126     elt.appendChild( document.createTextNode( val ) );
127 }
128
129 /* set item's 'element' attribute to value */
130 function set_css( item, element, value )
131 {
132     for( var j = 0; j < document.styleSheets.length; j++ )
133     {
134         var cssRules = document.styleSheets[j].cssRules;
135         if( !cssRules ) cssRules = document.styleSheets[j].rules;
136         for( var i = 0; i < cssRules.length; i++)
137         {
138             if( cssRules[i].selectorText == item )
139             {
140                 if( cssRules[i].style.setProperty )
141                     cssRules[i].style.setProperty( element, value, null );
142                 else
143                     cssRules[i].style.setAttribute( toCamelCase( element ), value );
144                 return;
145             }
146         }
147     }
148 }
149
150 /* get item's 'element' attribute */
151 function get_css( item, element )
152 {
153     for( var j = 0; j < document.styleSheets.length; j++ )
154     {
155         var cssRules = document.styleSheets[j].cssRules;
156         if( !cssRules ) cssRules = document.styleSheets[j].rules;
157         for( var i = 0; i < cssRules.length; i++)
158         {
159             if( cssRules[i].selectorText == item )
160             {
161                 if( cssRules[i].style.getPropertyValue )
162                     return cssRules[i].style.getPropertyValue( element );
163                 else
164                     return cssRules[i].style.getAttribute( toCamelCase( element ) );
165             }
166         }
167     }
168 }
169
170 function toggle_show( id )
171 {
172     var element = document.getElementById( id );
173     if( element.style.display == 'block' || element.style.display == '' )
174     {
175         element.style.display = 'none';
176     }
177     else
178     {
179         element.style.display = 'block';
180     }
181 }
182 function toggle_show_node( id )
183 {
184     var element = document.getElementById( 'pl_'+id );
185     var img = document.getElementById( 'pl_img_'+id );
186     if( element.style.display == 'block' || element.style.display == '' )
187     {
188         element.style.display = 'none';
189         img.setAttribute( 'src', 'images/plus.png' );
190         img.setAttribute( 'alt', '[+]' );
191     }
192     else
193     {
194         element.style.display = 'block';
195         img.setAttribute( 'src', 'images/minus.png' );
196         img.setAttribute( 'alt', '[-]' );
197     }
198 }
199
200 function show( id ){ document.getElementById( id ).style.display = 'block'; }
201 function showinline( id ){ document.getElementById( id ).style.display = 'inline'; }
202
203 function hide( id ){ document.getElementById( id ).style.display = 'none'; }
204
205 function checked( id ){ return document.getElementById( id ).checked; }
206
207 function value( id ){ return document.getElementById( id ).value; }
208
209 function setclass( obj, value )
210 {
211     obj.setAttribute( 'class', value ); /* Firefox */
212     obj.setAttribute( 'className', value ); /* IE */
213 }
214
215 function radio_value( name )
216 {
217     var radio = document.getElementsByName( name );
218     for( var i = 0; i < radio.length; i++ )
219     {
220         if( radio[i].checked )
221         {
222             return radio[i].value;
223         }
224     }
225     return "";
226 }
227
228 function check_and_replace_int( id, val )
229 {
230     var objRegExp = /^\d+$/;
231     if( value( id ) != ''
232         && ( !objRegExp.test( value( id ) )
233              || parseInt( value( id ) ) < 1 ) )
234         return document.getElementById( id ).value = val;
235     return document.getElementById( id ).value;
236 }
237
238 function addslashes( str ){ return str.replace(/\'/g, '\\\''); }
239 function escapebackslashes( str ){ return str.replace(/\\/g, '\\\\'); }
240
241 function toCamelCase( str )
242 {
243     str = str.split( '-' );
244     var cml = str[0];
245     for( var i=1; i<str.length; i++)
246         cml += str[i].charAt(0).toUpperCase()+str[i].substring(1);
247     return cml;
248 }
249
250 function disable( id ){ document.getElementById( id ).disabled = true; }
251
252 function enable( id ){ document.getElementById( id ).disabled = false; }
253
254 function button_over( element ){ element.style.border = "1px solid #000"; }
255
256 function button_out( element ){ element.style.border = "1px solid #fff"; }
257 function button_out_menu( element ){ element.style.border = "1px solid transparent"; }
258
259 function show_menu( id ){ document.getElementById(id).style.display = 'block'; }
260 function hide_menu( id ){ document.getElementById(id).style.display = 'none'; }
261
262 /* toggle show help under the buttons */
263 function toggle_btn_text()
264 {
265     if( get_css( '.btn_text', 'display' ) == 'none' )
266     {
267         set_css( '.btn_text', 'display', 'block' );
268     }
269     else
270     {
271         set_css( '.btn_text', 'display', 'none' );
272     }
273 }
274
275 function clear_children( elt )
276 {   
277     if( elt )
278         while( elt.hasChildNodes() )
279             elt.removeChild( elt.firstChild );
280 }
281
282 /**********************************************************************
283  * Interface actions
284  *********************************************************************/
285 /* input actions */
286 function in_play()
287 {
288     var input = value('input_mrl');
289     if( value('sout_mrl') != '' )
290         input += ' '+value('sout_mrl');
291     var url = 'requests/status.xml?command=in_play&input='+encodeURIComponent( addslashes(escapebackslashes(input)) );
292     loadXMLDoc( url, parse_status );
293     setTimeout( 'update_playlist()', 1000 );
294 }
295 function in_enqueue()
296 {
297     var input = value('input_mrl');
298     if( value('sout_mrl') != '' )
299         input += ' '+value('sout_mrl');
300     var url = 'requests/status.xml?command=in_enqueue&input='+encodeURIComponent( addslashes(escapebackslashes(input)) );
301     loadXMLDoc( url, parse_status );
302     setTimeout( 'update_playlist()', 1000 );
303 }
304
305 /* playlist actions */
306 function pl_play( id )
307 {
308     loadXMLDoc( 'requests/status.xml?command=pl_play&id='+id, parse_status );
309     pl_cur_id = id;
310     setTimeout( 'update_playlist()', 1000 );
311 }
312 function pl_pause()
313 {
314     loadXMLDoc( 'requests/status.xml?command=pl_pause&id='+pl_cur_id, parse_status );
315 }
316 function pl_stop()
317 {
318     loadXMLDoc( 'requests/status.xml?command=pl_stop', parse_status );
319     setTimeout( 'update_playlist()', 1000 );
320 }
321 function pl_next()
322 {
323     loadXMLDoc( 'requests/status.xml?command=pl_next', parse_status );
324     setTimeout( 'update_playlist()', 1000 );
325 }
326 function pl_previous()
327 {
328     loadXMLDoc( 'requests/status.xml?command=pl_previous', parse_status );
329     setTimeout( 'update_playlist()', 1000 );
330 }
331 function pl_delete( id )
332 {
333     loadXMLDoc( 'requests/status.xml?command=pl_delete&id='+id, parse_status );
334     setTimeout( 'update_playlist()', 1000 );
335 }
336 function pl_empty()
337 {
338     loadXMLDoc( 'requests/status.xml?command=pl_empty', parse_status );
339     setTimeout( 'update_playlist()', 1000 );
340 }
341 function pl_sort( sort, order )
342 {
343     loadXMLDoc( 'requests/status.xml?command=pl_sort&id='+order+'&val='+sort, parse_status );
344     setTimeout( 'update_playlist()', 1000 );
345 }
346 function pl_shuffle()
347 {
348     loadXMLDoc( 'requests/status.xml?command=pl_random', parse_status );
349     setTimeout( 'update_playlist()', 1000 );
350 }
351 function pl_loop()
352 {
353     loadXMLDoc( 'requests/status.xml?command=pl_loop', parse_status );
354 }
355 function pl_repeat()
356 {
357     loadXMLDoc( 'requests/status.xml?command=pl_repeat', parse_status );
358 }
359 function pl_sd( value )
360 {
361     loadXMLDoc( 'requests/status.xml?command=pl_sd&val='+value, parse_status );
362 }
363
364 /* misc actions */
365 function volume_down()
366 {
367     loadXMLDoc( 'requests/status.xml?command=volume&val=-20', parse_status );
368 }
369 function volume_up()
370 {
371     loadXMLDoc( 'requests/status.xml?command=volume&val=%2B20', parse_status );
372 }
373 function seek( pos )
374 {
375     loadXMLDoc( 'requests/status.xml?command=seek&val='+pos, parse_status );
376 }
377 function fullscreen()
378 {
379     loadXMLDoc( 'requests/status.xml?command=fullscreen', parse_status );
380 }
381 function snapshot()
382 {
383     loadXMLDoc( 'requests/status.xml?command=snapshot', parse_status );
384 }
385 function hotkey( str )
386 {
387     /* Use hotkey name (without the "key-" part) as the argument to simulate a hotkey press */
388     loadXMLDoc( 'requests/status.xml?command=key&val='+str, parse_status );
389 }
390 function update_status()
391 {
392     loadXMLDoc( 'requests/status.xml', parse_status );
393 }
394 function update_playlist()
395 {
396     loadXMLDoc( 'requests/playlist.xml', parse_playlist );
397 }
398 function update_playlist_search(key)
399 {
400     loadXMLDoc( 'requests/playlist.xml?search='+encodeURIComponent(key), parse_playlist )
401 }
402 function reset_search()
403 {
404     var search = document.getElementById('search')
405     if( search )
406     {
407         search.value = '<search>'
408         update_playlist_search('')
409     }
410 }
411
412 /**********************************************************************
413  * Parse xml replies to XMLHttpRequests
414  *********************************************************************/
415 /* parse request/status.xml */
416 function parse_status()
417 {
418     if( req.readyState == 4 )
419     {
420         if( req.status == 200 )
421         {
422             var status = req.responseXML.documentElement;
423             var timetag = status.getElementsByTagName( 'time' );
424             if( timetag.length > 0 )
425             {
426                 var new_time = timetag[0].firstChild.data;
427             }
428             else
429             {
430                 new_time = old_time;
431             }
432             var lengthtag = status.getElementsByTagName( 'length' );
433             var length;
434             if( lengthtag.length > 0 )
435             {
436                 length = lengthtag[0].firstChild.data;
437             }
438             else
439             {
440                 length = 0;
441             }
442             var slider_position;
443             positiontag = status.getElementsByTagName( 'position' );
444             if( length < 100 && positiontag.length > 0 )
445             {
446                 slider_position = ( positiontag[0].firstChild.data * 4 ) + "px";
447             }
448             else if( length > 0 )
449             {
450                 /* this is more precise if length > 100 */
451                 slider_position = Math.floor( ( new_time * 400 ) / length ) + "px";
452             }
453             else
454             {
455                 slider_position = 0;
456             }
457             if( old_time > new_time )
458                 setTimeout('update_playlist()',50);
459             old_time = new_time;
460             set_text( 'time', format_time( new_time ) );
461             set_text( 'length', format_time( length ) );
462             if( status.getElementsByTagName( 'volume' ).length != 0 )
463                 set_text( 'volume', Math.floor(status.getElementsByTagName( 'volume' )[0].firstChild.data/5.12)+'%' );
464             var statetag = status.getElementsByTagName( 'state' );
465             if( statetag.length > 0 )
466             {
467                 set_text( 'state', statetag[0].firstChild.data );
468             }
469             else
470             {
471                 set_text( 'state', '(?)' );
472             }
473             if( slider_mouse_down == 0 )
474             {
475                 document.getElementById( 'main_slider_point' ).style.left = slider_position;
476             }
477             var statustag = status.getElementsByTagName( 'state' );
478             if( statustag.length > 0 ? statustag[0].firstChild.data == "playing" : 0 )
479             {
480                 document.getElementById( 'btn_pause_img' ).setAttribute( 'src', 'images/pause.png' );
481                 document.getElementById( 'btn_pause_img' ).setAttribute( 'alt', 'Pause' );
482                 document.getElementById( 'btn_pause' ).setAttribute( 'title', 'Pause' );
483             }
484             else
485             {
486                 document.getElementById( 'btn_pause_img' ).setAttribute( 'src', 'images/play.png' );
487                 document.getElementById( 'btn_pause_img' ).setAttribute( 'alt', 'Play' );
488                 document.getElementById( 'btn_pause' ).setAttribute( 'title', 'Play' );
489             }
490
491             var randomtag = status.getElementsByTagName( 'random' );
492             if( randomtag.length > 0 ? randomtag[0].firstChild.data == "1" : 0)
493                 setclass( document.getElementById( 'btn_shuffle'), 'on' );
494             else
495                 setclass( document.getElementById( 'btn_shuffle'), 'off' );
496                
497             var looptag = status.getElementsByTagName( 'loop' );
498             if( looptag.length > 0 ? looptag[0].firstChild.data == "1" : 0)
499                 setclass( document.getElementById( 'btn_loop'), 'on' );
500             else
501                 setclass( document.getElementById( 'btn_loop'), 'off' );
502
503             var repeattag = status.getElementsByTagName( 'repeat' );
504             if( repeattag.length > 0 ? repeattag[0].firstChild.data == "1" : 0 )
505                 setclass( document.getElementById( 'btn_repeat'), 'on' );
506             else
507                 setclass( document.getElementById( 'btn_repeat'), 'off' );
508
509             var tree = document.createElement( "ul" );
510             var categories = status.getElementsByTagName( 'category' );
511             var i;
512             for( i = 0; i < categories.length; i++ )
513             {
514                 var item = document.createElement( "li" );
515                 item.appendChild( document.createTextNode( categories[i].getAttribute( 'name' ) ) );
516                 var subtree = document.createElement( "dl" );
517                 var infos = categories[i].getElementsByTagName( 'info' );
518                 var j;
519                 for( j = 0; j < infos.length; j++ )
520                 {
521                     var subitem = document.createElement( "dt" );
522                     subitem.appendChild( document.createTextNode( infos[j].getAttribute( 'name' ) ) );
523                     subtree.appendChild( subitem );
524                     if( infos[j].hasChildNodes() )
525                     {
526                         var subitem = document.createElement( "dd" );
527                         subitem.appendChild( document.createTextNode( infos[j].firstChild.data ) );
528                         subtree.appendChild( subitem );
529                     }
530                 }
531                 item.appendChild( subtree );
532                 tree.appendChild( item );
533             }
534             var infotree = document.getElementById('infotree' );
535             clear_children( infotree );
536             infotree.appendChild( tree );
537             
538         }
539         else
540         {
541             /*alert( 'Error! HTTP server replied: ' + req.status );*/
542         }
543     }
544 }
545
546 /* parse playlist.xml */
547 function parse_playlist()
548 {
549     if( req.readyState == 4 )
550     {
551         if( req.status == 200 )
552         {
553             var answer = req.responseXML.documentElement;
554             var playtree = document.getElementById( 'playtree' );
555             var pos = document.createElement( "div" );
556             var pos_top = pos;
557             var elt = answer.firstChild;
558             
559             pl_cur_id = 0;  /* changed to the current id is there actually
560                              * is a current id */
561             while( elt )
562             {
563                 if( elt.nodeName == "node" )
564                 {
565                     if( pos.hasChildNodes() )
566                         pos.appendChild( document.createElement( "br" ) );
567                     var nda = document.createElement( 'a' );
568                     nda.setAttribute( 'href', 'javascript:toggle_show_node(\''+elt.getAttribute( 'id' )+'\');' );
569                     var ndai = document.createElement( 'img' );
570                     ndai.setAttribute( 'src', 'images/minus.png' );
571                     ndai.setAttribute( 'alt', '[-]' );
572                     ndai.setAttribute( 'id', 'pl_img_'+elt.getAttribute( 'id' ) );
573                     nda.appendChild( ndai );
574                     pos.appendChild( nda );
575                     pos.appendChild( document.createTextNode( ' ' + elt.getAttribute( 'name' ) ) );
576
577                     if( elt.getAttribute( 'ro' ) == 'rw' )
578                     {
579                         pos.appendChild( document.createTextNode( ' ' ) );
580                         var del = document.createElement( "a" );
581                         del.setAttribute( 'href', 'javascript:pl_delete('+elt.getAttribute( 'id' )+')' );
582                             var delimg = document.createElement( "img" );
583                             delimg.setAttribute( 'src', 'images/delete_small.png' );
584                             delimg.setAttribute( 'alt', '(delete)' );
585                         del.appendChild( delimg );
586                         pos.appendChild( del );
587                     }
588
589                     var nd = document.createElement( "div" );
590                     setclass( nd, 'pl_node' );
591                     nd.setAttribute( 'id', 'pl_'+elt.getAttribute( 'id' ) );
592                     pos.appendChild( nd );
593                 }
594                 else if( elt.nodeName == "leaf" )
595                 {
596                     if( pos.hasChildNodes() )
597                     pos.appendChild( document.createElement( "br" ) );
598                     var pl = document.createElement( "a" );
599                     setclass( pl, 'pl_leaf' );
600                     pl.setAttribute( 'href', 'javascript:pl_play('+elt.getAttribute( 'id' )+');' );
601                     pl.setAttribute( 'id', 'pl_'+elt.getAttribute( 'id' ) );
602                     if( elt.getAttribute( 'current' ) == 'current' )
603                     {
604                         pl.style.fontWeight = 'bold';
605                         var nowplaying = document.getElementById( 'nowplaying' );
606                         clear_children( nowplaying );
607                         nowplaying.appendChild( document.createTextNode( elt.getAttribute( 'name' ) ) );
608                         pl.appendChild( document.createTextNode( '* '));
609                         pl_cur_id = elt.getAttribute( 'id' );
610                     }
611                     pl.setAttribute( 'title', elt.getAttribute( 'uri' ));
612                     pl.appendChild( document.createTextNode( elt.getAttribute( 'name' ) ) );
613                     var duration = elt.getAttribute( 'duration' );
614                     if( duration > 0 )
615                         pl.appendChild( document.createTextNode( " (" + format_time( elt.getAttribute( 'duration' ) / 1000000 ) + ")" ) );
616                     pos.appendChild( pl );
617
618                     if( elt.getAttribute( 'ro' ) == 'rw' )
619                     {
620                         pos.appendChild( document.createTextNode( ' ' ) );
621                         var del = document.createElement( "a" );
622                         del.setAttribute( 'href', 'javascript:pl_delete('+elt.getAttribute( 'id' )+')' );
623                             var delimg = document.createElement( "img" );
624                             delimg.setAttribute( 'src', 'images/delete_small.png' );
625                             delimg.setAttribute( 'alt', '(delete)' );
626                         del.appendChild( delimg );
627                         pos.appendChild( del );
628                     }
629                 }
630                 if( elt.firstChild )
631                 {
632                     elt = elt.firstChild;
633                     pos = pos.lastChild;
634                 }
635                 else if( elt.nextSibling )
636                 {
637                     elt = elt.nextSibling;
638                     pos = pos;
639                 }
640                 else
641                 {
642                     while( ! elt.parentNode.nextSibling )
643                     {
644                         elt = elt.parentNode;
645                         if( ! elt.parentNode ) break;
646                     }
647                     if( ! elt.parentNode ) break;
648                     elt = elt.parentNode.nextSibling;
649                     pos = pos.parentNode;
650                 }
651             }
652             clear_children( playtree );
653             playtree.appendChild( pos_top );
654         }
655         else
656         {
657             /*alert( 'Error! HTTP server replied: ' + req.status );*/
658         }
659     }
660 }
661
662 /* parse browse.xml */
663 function parse_browse_dir( )
664 {
665     if( req.readyState == 4 )
666     {
667         if( req.status == 200 )
668         {
669             var answer = req.responseXML.documentElement;
670             if( !answer ) return;
671             var browser = document.getElementById( 'browser' );
672             var pos = document.createElement( "div" );
673             var elt = answer.firstChild;
674             while( elt )
675             {
676                 if( elt.nodeName == "element" )
677                 {
678                     var item = document.createElement( "a" );
679                     setclass( item, 'browser' );
680                     if( elt.getAttribute( 'type' ) == 'dir' )
681                     {
682                         item.setAttribute( 'href', 'javascript:browse_dir(\''+addslashes(escapebackslashes(elt.getAttribute( 'path' )))+'\');');
683                     }
684                     else
685                     {
686                         item.setAttribute( 'href', 'javascript:browse_path(\''+addslashes(escapebackslashes(elt.getAttribute( 'path' )))+'\');' );
687                     }
688                     item.appendChild( document.createTextNode( elt.getAttribute( 'name' ) ) );
689                     pos.appendChild( item );
690                     if( elt.getAttribute( 'type' ) == 'dir' )
691                     {
692                         pos.appendChild( document.createTextNode( ' ' ) );
693                         var item = document.createElement( "a" );
694                         setclass( item, 'browser' );
695                         item.setAttribute( 'href', 'javascript:browse_path(\''+addslashes(escapebackslashes(elt.getAttribute( 'path' )))+'\');');
696                         item.appendChild( document.createTextNode( '(select)' ) );
697                         pos.appendChild( item );
698                     }
699                     pos.appendChild( document.createElement( "br" ) );
700                 }
701                 elt = elt.nextSibling;
702             }
703             clear_children( browser );
704             browser.appendChild( pos );
705         }
706         else
707         {
708             /*alert( 'Error! HTTP server replied: ' + req.status );*/
709         }
710     }
711 }
712
713 /**********************************************************************
714  * Input dialog functions
715  *********************************************************************/
716 function hide_input( )
717 {
718     document.getElementById( 'input_file' ).style.display = 'none';
719     document.getElementById( 'input_disc' ).style.display = 'none';
720     document.getElementById( 'input_network' ).style.display = 'none';
721     document.getElementById( 'input_fake' ).style.display = 'none';
722 }
723
724 /* update the input MRL using data from the input file helper */
725 /* FIXME ... subs support */
726 function update_input_file()
727 {
728     var mrl = document.getElementById( 'input_mrl' );
729
730     mrl.value = value( 'input_file_filename' );
731 }
732
733 /* update the input MRL using data from the input disc helper */
734 function update_input_disc()
735 {
736     var mrl     = document.getElementById( 'input_mrl' );
737     var type    = radio_value( "input_disc_type" );
738     var device  = value( "input_disc_dev" );
739
740     var title   = check_and_replace_int( 'input_disc_title', 0 );
741     var chapter = check_and_replace_int( 'input_disc_chapter', 0 );
742     var subs    = check_and_replace_int( 'input_disc_subtrack', '' );
743     var audio   = check_and_replace_int( 'input_disc_audiotrack', 0 );
744
745     mrl.value = "";
746
747     if( type == "dvd" )
748     {
749         mrl.value += "dvd://";
750     }
751     else if( type == "dvdsimple" )
752     {
753         mrl.value += "dvdsimple://";
754     }
755     else if( type == "vcd" )
756     {
757         mrl.value += "vcd://";
758     }
759     else if( type == "cdda" )
760     {
761         mrl.value += "cdda://";
762     }
763
764     mrl.value += device;
765
766     if( title )
767     {
768         mrl.value += "@"+title;
769         if( chapter && type != "cdda" )
770             mrl.value += ":"+chapter;
771     }
772
773     if( type != "cdda" )
774     {
775         if( subs != '' )
776             mrl.value += " :sub-track="+subs;
777         if( audio != '' )
778             mrl.value += " :audio-track="+audio;
779     }
780
781 }
782
783 /* update the input MRL using data from the input network helper */
784 function update_input_net()
785 {
786     var mrl = document.getElementById( 'input_mrl' );
787     var type = radio_value( "input_net_type" );
788     
789     check_and_replace_int( 'input_net_udp_port', 1234 );
790     check_and_replace_int( 'input_net_udpmcast_port', 1234 );
791
792     mrl.value = "";
793
794     if( type == "udp" )
795     {
796         mrl.value += "udp://";
797         if( checked( 'input_net_udp_forceipv6' ) )
798             mrl.value += "[::]";
799         if( value( 'input_net_udp_port' ) )
800             mrl.value += ":"+value( 'input_net_udp_port' );
801     }
802     else if( type == "udpmcast" )
803     {
804         mrl.value += "udp://@"+value( 'input_net_udpmcast_address');
805         if( value( 'input_net_udpmcast_port' ) )
806             mrl.value += ":"+value( 'input_net_udpmcast_port' );
807     }
808     else if( type == "http" )
809     {
810         var url = value( 'input_net_http_url' );
811         if( url.substring(0,7) != "http://"
812             && url.substring(0,8) != "https://"
813             && url.substring(0,6) != "ftp://"
814             && url.substring(0,6) != "mms://"
815             && url.substring(0,7) != "mmsh://" )
816             mrl.value += "http://";
817         mrl.value += url;
818     }
819     else if( type == "rtsp" )
820     {
821         var url = value( 'input_net_rtsp_url' );
822         if( url.substring(0,7) != "rtsp://" )
823             mrl.value += "rtsp://";
824         mrl.value += url;
825     }
826
827     if( checked( "input_net_timeshift" ) )
828         mrl.value += " :access-filter=timeshift";
829 }
830
831 /* update the input MRL using data from the input fake helper */
832 function update_input_fake()
833 {
834     var mrl = document.getElementById( 'input_mrl' );
835
836     mrl.value = "fake:";
837     mrl.value += " :fake-file=" + value( "input_fake_filename" );
838
839     if( value( "input_fake_width" ) )
840         mrl.value += " :fake-width=" + value( "input_fake_width" );
841     if( value( "input_fake_height" ) )
842         mrl.value += " :fake-height=" + value( "input_fake_height" );
843     if( value( "input_fake_ar" ) )
844         mrl.value += " :fake-ar=" + value( "input_fake_ar" );
845 }
846
847 /**********************************************************************
848  * Sout dialog functions
849  *********************************************************************/
850 /* toggle show the full sout interface */
851 function toggle_show_sout_helper()
852 {
853     var element = document.getElementById( "sout_helper" );
854     if( element.style.display == 'block' )
855     {
856         element.style.display = 'none';
857         document.getElementById( "sout_helper_toggle" ).value = 'Full sout interface';
858     }
859     else
860     {
861         element.style.display = 'block';
862         document.getElementById( "sout_helper_toggle" ).value = 'Hide sout interface';
863     }
864 }
865
866 /* update the sout MRL using data from the sout_helper */
867 function update_sout()
868 {
869     var mrl = document.getElementById( 'sout_mrl' );
870     mrl.value = "";
871
872     check_and_replace_int( 'sout_http_port', 8080 );
873     check_and_replace_int( 'sout_mmsh_port', 8080 );
874     check_and_replace_int( 'sout_rtp_port', 1234 );
875     check_and_replace_int( 'sout_udp_port', 1234 );
876     check_and_replace_int( 'sout_ttl', 1 );
877
878     if( checked( 'sout_soverlay' ) )
879     {
880         disable( 'sout_scodec' );
881         disable( 'sout_sub' );
882     }
883     else
884     {
885         enable( 'sout_scodec' );
886         enable( 'sout_sub' );
887     }
888
889     var transcode =  checked( 'sout_vcodec_s' ) || checked( 'sout_acodec_s' )
890                   || checked( 'sout_sub' )      || checked( 'sout_soverlay' );
891
892     if( transcode )
893     {
894         mrl.value += ":sout=#transcode{";
895         var alot = false; /* alot == at least one transcode */
896         if( checked( 'sout_vcodec_s' ) )
897         {
898             mrl.value += "vcodec="+value( 'sout_vcodec' )+",vb="+value( 'sout_vb' )+",scale="+value( 'sout_scale' );
899             alot = true;
900         }
901         if( checked( 'sout_acodec_s' ) )
902         {
903             if( alot ) mrl.value += ",";
904             mrl.value += "acodec="+value( 'sout_acodec' )+",ab="+value( 'sout_ab' );
905             if( value( 'sout_channels' ) )
906                 mrl.value += ",channels="+value( 'sout_channels' );
907             alot = true;
908         }
909         if( checked( 'sout_soverlay' ) )
910         {
911             if( alot ) mrl.value += ",";
912             mrl.value += "soverlay";
913             alot = true;
914         }
915         else if( checked( 'sout_sub' ) )
916         {
917             if( alot ) mrl.value += ",";
918             mrl.value += "scodec="+value( 'sout_scodec' );
919             alot = true;
920         }
921         mrl.value += value( 'sout_transcode_extra' );
922             
923         mrl.value += "}";
924     }
925
926     var output = checked( 'sout_display' ) + checked( 'sout_file' )
927                + checked( 'sout_http' )    + checked( 'sout_mmsh' )
928                + checked( 'sout_rtp' )     + checked( 'sout_udp' );
929
930     if( output )
931     {
932         if( transcode )
933             mrl.value += ":";
934         else
935             mrl.value += ":sout=#";
936         var aloo = false; /* aloo == at least one output */
937         var mux = radio_value( 'sout_mux' );
938         var ttl = parseInt( value( 'sout_ttl' ) );
939         if( output > 1 ) mrl.value += "duplicate{";
940         if( checked( 'sout_display' ) )
941         {
942             if( output > 1 ) mrl.value += "dst="
943             mrl.value += "display";
944             aloo = true;
945         }
946         if( checked( 'sout_file' ) )
947         {
948             if( aloo ) mrl.value += ",";
949             if( output > 1 ) mrl.value += "dst="
950             mrl.value += "std{access=file,mux="+mux+",dst="+value( 'sout_file_filename' )+"}";
951             aloo = true;
952         }
953         if( checked( 'sout_http' ) )
954         {
955             if( aloo ) mrl.value += ",";
956             if( output > 1 ) mrl.value += "dst="
957             mrl.value += "std{access=http,mux="+mux+",dst="+value( 'sout_http_addr' );
958             if( value( 'sout_http_port' ) )
959                 mrl.value += ":"+value( 'sout_http_port' );
960             mrl.value += "}";
961             aloo = true;
962         }
963         if( checked( 'sout_mmsh' ) )
964         {
965             if( aloo ) mrl.value += ",";
966             if( output > 1 ) mrl.value += "dst="
967             mrl.value += "std{access=mmsh,mux="+mux+",dst="+value( 'sout_mmsh_addr' );
968             if( value( 'sout_mmsh_port' ) )
969                 mrl.value += ":"+value( 'sout_mmsh_port' );
970             mrl.value += "}";
971             aloo = true;
972         }
973         if( checked( 'sout_rtp' ) )
974         {
975             if( aloo ) mrl.value += ",";
976             if( output > 1 ) mrl.value += "dst="
977             mrl.value += "std{access=rtp";
978             if( ttl ) mrl.value += "{ttl="+ttl+"}";
979             mrl.value += ",mux="+mux+",dst="+value( 'sout_rtp_addr' );
980             if( value( 'sout_rtp_port' ) )
981                 mrl.value += ":"+value( 'sout_rtp_port' );
982             if( checked( 'sout_sap' ) )
983             {
984                 mrl.value += ",sap";
985                 if( value( 'sout_sap_group' ) != '' )
986                 {
987                     mrl.value += ",group=\""+value( 'sout_sap_group' )+"\"";
988                 }
989                 mrl.value += ",name=\""+value( 'sout_sap_name' )+"\"";
990             }
991             mrl.value += "}";
992             aloo = true;
993         }
994         if( checked( 'sout_udp' ) )
995         {
996             if( aloo ) mrl.value += ",";
997             if( output > 1 ) mrl.value += "dst="
998             mrl.value += "std{access=udp";
999             if( ttl ) mrl.value += "{ttl="+ttl+"}";
1000             mrl.value += ",mux="+mux+",dst="+value( 'sout_udp_addr' );
1001             if( value('sout_udp_port' ) )
1002                 mrl.value += ":"+value( 'sout_udp_port' );
1003             if( checked( 'sout_sap' ) )
1004             {
1005                 mrl.value += ",sap";
1006                 if( value( 'sout_sap_group' ) != '' )
1007                 {
1008                     mrl.value += ",group=\""+value( 'sout_sap_group' )+"\"";
1009                 }
1010                 mrl.value += ",name=\""+value( 'sout_sap_name' )+"\"";
1011             }
1012             mrl.value += "}";
1013             aloo = true;
1014         }
1015         if( output > 1 ) mrl.value += "}";
1016     }
1017
1018     if( ( transcode || output ) && checked( 'sout_all' ) )
1019         mrl.value += " :sout-all";
1020 }
1021
1022 /* reset sout mrl value */
1023 function reset_sout()
1024 {
1025     document.getElementById('sout_mrl').value = value('sout_old_mrl');
1026 }
1027
1028 /* save sout mrl value */
1029 function save_sout()
1030 {
1031     document.getElementById('sout_old_mrl').value = value('sout_mrl');
1032 }
1033
1034 /**********************************************************************
1035  * Browser dialog functions
1036  *********************************************************************/
1037 /* only browse() should be called directly */
1038 function browse( dest )
1039 {
1040     document.getElementById( 'browse_dest' ).value = dest;
1041     document.getElementById( 'browse_lastdir' ).value;
1042     browse_dir( document.getElementById( 'browse_lastdir' ).value );
1043     show( 'browse' );
1044 }
1045 function browse_dir( dir )
1046 {
1047     document.getElementById( 'browse_lastdir' ).value = dir;
1048     loadXMLDoc( 'requests/browse.xml?dir='+encodeURIComponent(dir), parse_browse_dir );
1049 }
1050 function browse_path( p )
1051 {
1052     document.getElementById( value( 'browse_dest' ) ).value = p;
1053     hide( 'browse' );
1054     document.getElementById( value( 'browse_dest' ) ).focus();
1055 }
1056 function refresh_albumart( force )
1057 {
1058     if( albumart_id != pl_cur_id || force )
1059     {
1060         var now = new Date();
1061         var albumart = document.getElementById( 'albumart' );
1062         albumart.src = '/art?timestamp=' + now.getTime();
1063         albumart_id = pl_cur_id;
1064     }
1065 }
1066 /**********************************************************************
1067  * Periodically update stuff in the interface
1068  *********************************************************************/
1069 function loop_refresh_status()
1070 {
1071     setTimeout( 'loop_refresh_status()', 1000 );
1072     update_status();
1073 }
1074 function loop_refresh_playlist()
1075 {
1076     /* setTimeout( 'loop_refresh_playlist()', 10000 ); */
1077     update_playlist();
1078 }
1079 function loop_refresh_albumart()
1080 {
1081     setTimeout( 'loop_refresh_albumart()', 1000 );
1082     refresh_albumart( false );
1083 }
1084 function loop_refresh()
1085 {
1086     setTimeout( 'loop_refresh_status()', 1 );
1087     setTimeout( 'loop_refresh_playlist()', 1 );
1088     setTimeout( 'loop_refresh_albumart()', 1 );
1089 }
1090