ctimer = null;
cbusy = false;
busywidth = 18;
busyheight = 18;
ctrlwidth = 40;
ctrlheight = 30;
ctrlstate = 0;
vlevels = 14; // do not include the zero level
vgrad = 0; // volume per gradient
vcurrent = 0;
playlist = 0;


$(document).ready(function() {
  vgrad = 100 / vlevels;
  vcurrent = GetCookieVolume();

  // precache images
  var images = new Array();
  images.push('button-stop.png');
  images.push('mozilla_blu.gif');
  for(i = 0; i < 14; i++) {
    if( i < 10 ) vimg = 'vol0' + i + '.png';
    else vimg = 'vol' + i + '.png';
    images.push(vimg);
  }
  var itags = new Array();
  for(i = 0; i < images.length; i++) {
    itags[i] = new Image();
    itags[i].src = 'images/' + images[i];
  };
  $("img#control").attr('src', 'images/button-play.png');

  // control button click
  $("img#control").click(function() {
    if( !cbusy ) {
      switch(ctrlstate) {
        case 0:
        HandleAction('playnow');
        break;

        case 3:
        HandleAction('stop');
        break;
      }
    }
  });

  // volume slider
  $("#volslider").slider({
    min: 0,
    max: 100,
    value: GetCookieVolume(),
    stop: function(e, ui) {
      vcurrent = $("#volslider").slider("value");
      VolumeAbs(vcurrent, true);
    }
  });

  timedUpdate();

  getplaylist();
});

function timedUpdate() {
  ctrl = $("img#control");
  ctrlstate = GetPlayState();
/*
0 = stopped
1 = waiting
2 = buffering
3 = playing
7 = connecting
*/
  switch(ctrlstate) {
    case 0:
    ctrl.attr('src', 'images/button-play.png');
    if( cbusy ) busyout();
    break;

    case 1:
    case 2:
    case 7:
    busyin(ctrl);
    break;

    case 3:
    ctrl.attr('src', 'images/button-stop.png');
    if( cbusy ) busyout();
    break;

  }

  grads = Math.round(vcurrent / vgrad);
  if( grads < 10 ) vimg = 'vol0' + grads + '.png';
  else vimg = 'vol' + grads + '.png';
  $("div#volslider").css('background-image', 'url("images/' + vimg + '")');
  checkplaylist();

  ctimer = setTimeout('timedUpdate()', 1000);
}

function busyin(elm) {
  bleft = elm.offset().left + (ctrlwidth - busywidth) / 2;
  btop = elm.offset().top + (ctrlheight - busyheight) / 2;

  $("img#busyicon").offset({left: bleft, top: btop});
  cbusy = true;
}

function busyout() {
  $("img#busyicon").offset({left:0, top:-400});
  cbusy = false;
}

function checkplaylist() {
  if( playlist && playlist.runtime && playlist.refresh ) {
      // determine if time is past refresh
      d = new Date();
      dutc = Date.UTC(d.getUTCFullYear(), d.getUTCMonth(), d.getUTCDate(), d.getUTCHours(), d.getUTCMinutes(), d.getUTCSeconds()) / 1000;

      if( parseInt(playlist.runtime) + parseInt(playlist.refresh) < dutc ) getplaylist();
  }
}

function getplaylist() {
  $.getJSON('pl.php', function(data) {
    playlist = data;

  if( playlist && playlist.playlist ) {
    plhtml = '';
    for(i = 0; i < playlist.playlist.length; i++) {
      if( i == 0 ) {
        if( playlist.playlist[i].visual.length > 0 ) $("div#albumcover").html('<img src="' + playlist.playlist[i].visual + '">');
        else $("div#albumcover").html('<img src="images/cover-default.jpg">');

        plhtml += '<div id="currenttrack">';
      }
      else if( i == playlist.playlist.length - 1 ) {
        plhtml += '<div id="lastnexttrack">';
      }
      else {
        plhtml += '<div class="nexttrack">';
      }

      plhtml += playlist.playlist[i].artist + ' / ' + playlist.playlist[i].title + ' / ' + playlist.playlist[i].album + '</div>';
    }
    $("div#playlist").html(plhtml);
  }
  });
}


