function pickRandom(userArray) {
  var ran = 60/userArray.length;
  currentdate = new Date();
  core = currentdate.getSeconds();
  core = Math.floor(core/ran);
  return(userArray[core]);
}

function pick_random_img(images) {
    imgsrc = pickRandom(images);
    return imgsrc;
}

function getElem(elem) {
    if (document.getElementById) {
        if (typeof elem == "string") {
            elem = document.getElementById(elem);
            if (elem===null) throw 'cannot get element: element does not exist';
        } else if (typeof elem != "object") {
            throw 'cannot get element: invalid datatype';
        }
    } else throw 'cannot get element: unsupported DOM';
    return elem;
}

function load_random_img(replaceid, images) {
    imgsrc = pick_random_img(images);
    replacer = getElem(replaceid);
    replacer.src = imgsrc;
    replacer.style.display='block';
}

function load_random_flag(theid, theimages) {
    imgsrc = pick_random_img(theimages);
    thehtml = "<img src=\""+imgsrc+"\" alt=\"\" />";
    
    container = getElem(theid);
    container.innerHTML = thehtml;
    container.style.display='inline';
}

function blog_randomize_flag() {
    //load_random_img(flag_replacement_id, flag_images);
    load_random_flag(flag_replacement_id, flag_images);
}

function addLoadEvent(func) {
  var oldonload = window.onload;
  if (typeof window.onload != 'function') {
    window.onload = func;
  } else {
    window.onload = function() {
      oldonload();
      func();
    }
  }
}

function writeStyle(anID, property, value) {
    document.write("<style type=\"text/css\">");
    document.write("#"+anID+" { "+property+": "+value+";");
    document.write("</style>");
}
