/*
 * Copyright 2007 Jeffrey Palm
 */

var arr;
var min = +999999;
var max = -999999;

function doLoad(_arr) {

  arr = _arr;

  // find a min and max
  for (i in arr) {
    if (arr[i]>max) max = arr[i];
    if (arr[i]<min) min = arr[i];
  }
  
  // now do the lookup
  lookup();
}

function note(s) {
  document.getElementById("note").innerHTML = s;
}

function lookup(name) {

  // choose a random name
  // pick a random numer between min and max
  // and choose a random name with a value higher

  if (!name) {
    
    var lst = new Array();
    while (lst.length == 0) {
      var rand = Math.floor(Math.random()*(max-min));
    for (i in arr) {
      if (arr[i]>rand) lst.push(i);
    }
    }
    var name = lst[Math.floor(Math.random()*lst.length)];
  }

  var url = '/yourtube/lookup.php';
  var params = 'name=' + urlencode(name);
  var callback = function(res) {
    if (!res) return;
    var txt = res.responseText;
    ar = txt.split(':');

    link = ar[0];
    time = ar[1];
    band = ar[2];
   
    href = '/yourtube/show.php?id=' + urlencode(link);
    alink = "http://youtube.com/watch?v=" + link;
    bandInner = band + " (<a href='" + alink + "'>" + link + "</a>)";

    document.getElementById("band").innerHTML = bandInner;
    document.getElementById("mainDiv").src = href;

    document.getElementById("mainDiv").innerHTML = document.getElementById("main").src;
    document.getElementById("same").href = 'javascript:lookup("' + urlencode(name) + '");';
    document.getElementById("same").innerHTML = 'another <em>' + name + '</em>';
    secs = parseInt(time)+2;

    note("Showing " + band + " for " + secs + " seconds");

    if ((""+secs) == "NaN") {
      note("Reloading...");
      lookup();
    } else {
      setTimeout('lookup()',1000 * secs);
    }
    

  }
  note("Looking up " + name);
  new Ajax.Request (url,
                    {   method: 'get', 
                        parameters: params, 
                        onSuccess: callback
                        });    

}

// --------------------------------------------------
// misc
// --------------------------------------------------

function $n(tag,on) {
  var e = document.createElement(tag);
  if (on) on.appendChild(e);
  return e;
}

function $t(text,on) {
  var e = document.createTextNode(text);
  if (on) on.appendChild(e);
  return e;
}

function insertBefore(newNode,target) {
  // http://lists.xml.org/archives/xml-dev/200201/msg00873.html
  var parent   = target.parentNode;
  var refChild = target; //target.nextSibling;  
  if(refChild) parent.insertBefore(newNode, refChild);
  else parent.appendChild(newNode);  
}

function insertAfter(parent, node, referenceNode) {
  // http://javascript.internet.com/snippets/insertafter.html
  parent.insertBefore(node, referenceNode.nextSibling);
}

// ====================================================================
//       URLEncode and URLDecode functions
//
// Copyright Albion Research Ltd. 2002
// http://www.albionresearch.com/
//
// You may copy these functions providing that 
// (a) you leave this copyright notice intact, and 
// (b) if you use these functions on a publicly accessible
//     web site you include a credit somewhere on the web site 
//     with a link back to http://www.albionresarch.com/
//
// If you find or fix any bugs, please let us know at albionresearch.com
//
// SpecialThanks to Neelesh Thakur for being the first to
// report a bug in URLDecode() - now fixed 2003-02-19.
// ====================================================================
function urlencode( plaintext )
{
  // The Javascript escape and unescape functions do not correspond
  // with what browsers actually do...
  var SAFECHARS = "0123456789" +          // Numeric
          "ABCDEFGHIJKLMNOPQRSTUVWXYZ" +  // Alphabetic
          "abcdefghijklmnopqrstuvwxyz" +
          "-_.!~*'()";          // RFC2396 Mark characters
  var HEX = "0123456789ABCDEF";

  var encoded = "";
  for (var i = 0; i < plaintext.length; i++ ) {
    var ch = plaintext.charAt(i);
      if (ch == " ") {
        encoded += "+";        // x-www-urlencoded, rather than %20
    } else if (SAFECHARS.indexOf(ch) != -1) {
        encoded += ch;
    } else {
        var charCode = ch.charCodeAt(0);
      if (charCode > 255) {
          alert( "Unicode Character '" 
                        + ch 
                        + "' cannot be encoded using standard URL encoding.\n" +
                  "(URL encoding only supports 8-bit characters.)\n" +
              "A space (+) will be substituted." );
        encoded += "+";
      } else {
        encoded += "%";
        encoded += HEX.charAt((charCode >> 4) & 0xF);
        encoded += HEX.charAt(charCode & 0xF);
      }
    }
  } // for

  return encoded;
};


