/* =================    Random Select Array value    ======================== */

Array.prototype.rand = function() {
  return this[Math.floor(Math.random() * this.length)];
}


/* ================================================================= */
function $(e){if(typeof e=='string')e=document.getElementById(e);return e};

function rand(n) {
  if (typeof n == "number") n = Math.floor(Math.random() * n);
  return n;
}

var addEvent = (function() {
  if ( window.addEventListener ) {
    return function(el, type, fn) {
      el.addEventListener(type, fn, false);
    };
  } else if ( window.attachEvent ) {
    return function(el, type, fn) {
      var f = function() {
        fn.call(el, window.event);
      };
      el.attachEvent('on'+type, f);
    };
  } else {
    return function(el, type, fn) {
      element['on'+type] = fn;
    }
  }
})();

/* -------------------- Yahoo! Pipes JOSNP Request -------------------------- */

function PipesRequest() {
  this.base = "http://pipes.yahoo.com/pipes/pipe.run?";
  this.query = {
  id: "",
  run: 1,
  render: "json",
  callback: ""
  };
  this.inputQuery = {};
}

PipesRequest.prototype.makeURL = function() {
  var q = [];
  for (var k in this.query) {
    q.push("_" + k + "=" + this.query[k]);
  }
  for (var k in this.inputQuery) {
    q.push(k + "=" + this.inputQuery[k]);
  }
  this.url = this.base + q.join("&");
}

PipesRequest.prototype.set = function() {
  this.makeURL();
  var script = document.createElement("script");
  script.type = "text/javascript";
  script.charset = "utf-8";
  script.src = this.url + "&noCache=" + new Date().getTime();
  setTimeout(function() {
    document.getElementsByTagName("head")[0].appendChild(script);
  }, 500);
  setTimeout(function() {script.parentNode.removeChild(script);}, 5000);
}

// ============================================================================
function JSONPRequest(url, cb) {
  var callback = cb || "callback";
  var script = document.createElement("script");
  script.type = "text/javascript";
  script.charset = "utf-8";
  script.src = url + callback + "&noCache=" + new Date().getTime();
  document.getElementsByTagName("head")[0].appendChild(script);
  setTimeout(function() {script.parentNode.removeChild(script);}, 5000);
}


