9db050bf6a1d06615b7c350bb7e55918011cc8c6
[living-lab-site.git] / javascript / NextSharePC-interface.js
1 // NextSharePC plugin controls\r
2 // Author: Calin-Andrei Burloiu, calin.burloiu@gmail.com\r
3 // 2011\r
4 \r
5 updateInterval = 500;\r
6 \r
7 function getURLParam( name )\r
8 {\r
9         name = name.replace(/[\[]/,"\\\[").replace(/[\]]/,"\\\]");\r
10         var regexS = "[\\?&]"+name+"=([^&#]*)";\r
11         var regex = new RegExp( regexS );\r
12         var results = regex.exec( window.location.href );\r
13         if( results == null )\r
14                 return "";\r
15         else\r
16                 return results[1];\r
17 }\r
18 \r
19 function getPluginWidth()\r
20 {\r
21         return 800;\r
22 }\r
23 \r
24 function getPluginHeight()\r
25 {\r
26         return 600;\r
27 }\r
28 \r
29 function getSliderWidth()\r
30 {\r
31         return $("#nsSlider").width();\r
32 }\r
33 \r
34 function updateTime(afterSlider)\r
35 {\r
36         var len = document.vlc.input.length;\r
37         var pos;\r
38         if(afterSlider)\r
39         {\r
40                 var val = 1.0 * $("#nsSlider").slider("option", "value");\r
41                 pos = val / (getSliderWidth() - 1);\r
42         }\r
43         else\r
44                 pos = document.vlc.input.position;\r
45         var time = Math.round(pos * len);\r
46         var crt_s = Math.floor(time / 1000 % 60);\r
47         var crt_min = Math.floor((time / 1000) / 60);\r
48         var tot_s = Math.floor(len / 1000 % 60);\r
49         var tot_min = Math.floor((len / 1000) / 60);\r
50 \r
51         if(isNaN(crt_s)) crt_s = 0;\r
52         if(isNaN(crt_min)) crt_min = 0;\r
53         if(isNaN(tot_s)) tot_s = 0;\r
54         if(isNaN(tot_min)) tot_min = 0;\r
55 \r
56         $("#nsTime").html("" + pad(crt_min, 2) + ":" + pad(crt_s, 2)\r
57                 + " / " + pad(tot_min, 2) + ":" + pad(tot_s, 2));\r
58 }\r
59 \r
60 function update()\r
61 {\r
62         var val = Math.round(document.vlc.input.position * (getSliderWidth() - 1));\r
63 \r
64         $("#nsSlider").slider({ value: val });\r
65         \r
66         updateTime(false);\r
67         \r
68         timerHandle = setTimeout("update()", updateInterval);\r
69 }\r
70 \r
71 function displayNextSharePC(torrent, html5)\r
72 {\r
73         if(html5 == true)\r
74         {\r
75                 document.write('<video src="'+torrent+'" controls="controls" width="800" height="600">');\r
76                 document.write('    Error: Your browser does not support HTML5!');\r
77                 document.write('</video>');\r
78                 document.write('<br /><br /><p><a href="' + window.location.href.replace('&html5=1', '') + '">');\r
79                 document.write('Play video using VLC with NextSharePC</a></p>');\r
80 \r
81                 return true;\r
82         }\r
83 \r
84         if (navigator.appName == "Netscape")\r
85         {\r
86                 document.write('<embed type="application/x-ns-stream"');\r
87                 document.write('name="vlc"');\r
88                 document.write('id="vlc"');\r
89                 document.write('autoplay="no" loop="no" width="');\r
90                 document.write('' + getPluginWidth());\r
91                 document.write('" height="');\r
92                 document.write('' + getPluginHeight());\r
93                 document.write('"');\r
94                 document.write('target="');\r
95                 document.write(torrent);\r
96                 document.write('" />');\r
97         }\r
98         else\r
99         {\r
100                 document.write('<object classid="clsid:1800B8AF-4E33-43C0-AFC7-894433C13538" ');\r
101                 document.write('codebase="http://trial.p2p-next.org/download/SwarmPlugin_IE_1.0.4.cab"');\r
102                 document.write('width="');\r
103                 document.write(getPluginWidth());\r
104                 document.write('" height="');\r
105                 document.write(getPluginHeight());\r
106                 document.write('" id="vlc" name="vlc" events="True" target="">');\r
107                 document.write('<param name="Src" value="');\r
108                 document.write(torrent);\r
109                 document.write('" />');\r
110                 document.write('<param name="ShowDisplay" value="True" />');\r
111                 document.write('<param name="Loop" value="False" />');\r
112                 document.write('<param name="AutoPlay" value="True" />');\r
113                 document.write('<param name="Toolbar" value="True" />');\r
114                 document.write('</object>');\r
115         }\r
116         \r
117         document.write('<table id="nsTable"><tr>'\r
118                 + '<td id="nsPlaybackCell"><input type=button value="Play" onClick="play();" />'\r
119                 + '<input type=button value="Pause" onClick="pause();" />'\r
120                 + '<input type=button value="Stop" onclick="stop();" /></td>'\r
121                 + '<td id="nsTimeCell"><span id="nsTime">-</span></td>'\r
122                 + '<td><div id="nsVol"></div></td>'\r
123                 + '<td><input type=button value="Fullscreen" onclick="fullscreen();" /></td></tr>'\r
124                 + '<tr><td colspan="4"><div id="nsSlider"></div></td></tr>'\r
125                 + '</table>');\r
126                 \r
127         $("nsTable").css("width", getPluginWidth());\r
128 \r
129         document.write('<br /><p><a href="' + window.location.href + '&html5=1">');\r
130         document.write('Play video using HTML5 with SwarmPlayer</a></p>');\r
131 \r
132         return true;\r
133 }\r
134 \r
135 function onSliderStop(event, ui)\r
136 {\r
137         var val = $("#nsSlider").slider("option", "value");\r
138         var s = 1.0 * val / (getSliderWidth() - 1);\r
139         \r
140         //document.vlc.playlist.seek(s, false);\r
141         //document.vlc.video.toggleFullscreen();\r
142         \r
143         try {\r
144                 if(s == 0)\r
145                         document.vlc.input.position = 0.0000001;\r
146                 else if(s == 1)\r
147                         document.vlc.input.position = 0.9999999;\r
148                 else\r
149                         document.vlc.input.position = s;\r
150         }\r
151         catch(err) {\r
152                 alert(err.message + ": " + err.description);\r
153         }\r
154         \r
155         timerHandle = setTimeout("update()", updateInterval);\r
156 }\r
157 \r
158 function pad(number, length) \r
159 {\r
160         var str = '' + number;\r
161         while (str.length < length) \r
162         {\r
163                 str = '0' + str;\r
164         }\r
165 \r
166         return str;\r
167 }\r
168 \r
169 function onSliderSlide(event, ui)\r
170 {\r
171         updateTime(true);\r
172         \r
173         clearTimeout(timerHandle);\r
174 }\r
175 \r
176 function onVolChange(event, ui)\r
177 {\r
178         var val = $("#nsVol").slider("option", "value");\r
179         \r
180         document.vlc.audio.volume = val;\r
181 }\r
182 \r
183 $(document).ready(function() \r
184 {\r
185         if(document.vlc == null)\r
186                 return;\r
187         \r
188         // Progress Slider\r
189         $("#nsSlider").slider({ animate: true });\r
190         $("#nsSlider").slider({ min: 0 });\r
191         $("#nsSlider").slider({ max: (getPluginWidth() - 1) });\r
192         $("#nsSlider").slider({\r
193                 stop: onSliderStop,\r
194                 slide: onSliderSlide\r
195         });\r
196         $("#nsSlider").css("width", getPluginWidth());// TODO\r
197         $("#nsSlider").slider();\r
198         \r
199         // Volume Slider\r
200         $("#nsVol").slider({ animate: true });\r
201         $("#nsVol").slider({ min: 0 });\r
202         $("#nsVol").slider({ max: 200 });\r
203         $("#nsVol").slider({ value: document.vlc.audio.volume });\r
204         $("#nsVol").slider({\r
205                 change: onVolChange,\r
206         });\r
207         $("#nsVol").css("width", 72);\r
208         $("#nsVol").slider();\r
209         \r
210         timerHandle = setTimeout("update()", updateInterval);\r
211 });  \r
212 \r
213 function play()\r
214 {\r
215         document.vlc.playlist.play();\r
216         timerHandle = setTimeout("update()", updateInterval);\r
217 }\r
218 \r
219 function pause()\r
220 {\r
221         document.vlc.playlist.togglePause();\r
222         clearTimeout(timerHandle);\r
223 }\r
224 \r
225 function stop()\r
226 {\r
227         document.vlc.playlist.stop();\r
228         clearTimeout(timerHandle);\r
229 }\r
230 \r
231 function fullscreen()\r
232 {\r
233         document.vlc.video.toggleFullscreen();\r
234 }\r