scripts/ajax.js
changeset 10 d3059e20b0fa
parent 6 5f35ebc4f9bb
child 11 0faea3a6c881
--- a/scripts/ajax.js	Mon Mar 24 00:12:21 2008 -0400
+++ b/scripts/ajax.js	Mon Mar 24 02:53:42 2008 -0400
@@ -14,6 +14,39 @@
 var ajax;
 var is_playing = false, current_track = -1, current_track_length, current_track_pos, ct_advance_timeout = false, ct_counter = false, playlist_md5 = false, first_load = true;
 
+var onload_hooks = new Array();
+
+function addOnloadHook(func)
+{
+  if ( typeof ( func ) == 'function' )
+  {
+    if ( typeof(onload_hooks.push) == 'function' )
+    {
+      onload_hooks.push(func);
+    }
+    else
+    {
+      onload_hooks[onload_hooks.length] = func;
+    }
+  }
+}
+
+function runOnloadHooks(e)
+{
+  var _errorTrapper = 0;
+  for ( var _oLc = 0; _oLc < onload_hooks.length; _oLc++ )
+  {
+    _errorTrapper++;
+    if ( _errorTrapper >= 1000 )
+      break;
+    var _f = onload_hooks[_oLc];
+    if ( typeof(_f) == 'function' )
+    {
+      _f(e);
+    }
+  }
+}
+
 function ajaxGet(uri, f)
 {
   if (window.XMLHttpRequest)
@@ -147,7 +180,7 @@
         if ( ct_counter )
           clearInterval(ct_counter);
         update_clock();
-        if ( is_playing )
+        if ( is_playing && time_remaining > 0 )
         {
           ct_advance_timeout = setTimeout(refresh_playlist, ( 1000 * time_remaining ));
           ct_counter = setInterval(update_clock, 1000);
@@ -228,8 +261,26 @@
     });
 }
 
+function set_playback_position(pos)
+{
+  pos = Math.round(( pos / 100 ) * current_track_length);
+  setAjaxLoading();
+  if ( !allow_control )
+    return false;
+  ajaxGet('/action.json/seek/' + pos, function()
+    {
+      if ( ajax.readyState == 4 && ajax.status == 200 )
+      {
+        unsetAjaxLoading();
+        current_track_pos = pos;
+        update_clock();
+      }
+    });
+}
+
 function update_clock()
 {
+  posslide_set_position((100 * (current_track_pos / current_track_length)));
   var str = secs_to_string(current_track_pos) + '/' + secs_to_string(current_track_length);
   $('playmeter').object.innerHTML = str;
   current_track_pos++;
@@ -256,6 +307,12 @@
   return str;
 }
 
-window.onload = refresh_playlist;
 setInterval(refresh_playlist, 10000);
 
+window.onload = function(e)
+{
+  runOnloadHooks(e);
+}
+
+addOnloadHook(refresh_playlist);
+