var currRnd = "";
var rnd_images = new Array();

function rnd_callbackDB(docInfo) {
  var ids = currRnd.split(';');
  rnd_images[parseInt(ids[0])].callbackDB(ids[1], docInfo);
}

function RandomImage(id) {

  this.setup = function() {
    this.id = id;
    this.maindiv = $(id);
    this.maindiv.innerHTML = "";

    var response = Master.GetRandomImage(_pageId, this.id);
    var response2 = NFN.BasePage.GetPermission(this.id, "PageProperty", "edit");

    this.imgdiv = document.createElement("div");
    if (response.error) {
      alert(response.error.Message);
      return;
    }
    else if (response.value.length > 0) {
      var vals = response.value.split('|');
      var html = "<img src='" + vals[0] + "' alt='' border='0' />";
      if (vals[1].length > 0)
        html = "<a href='" + vals[1] + "' onfocus='this.blur()'>" + html + "</a>";
      this.imgdiv.innerHTML = html;
    }
    else if (response2.value)
      this.imgdiv.innerHTML = "Random image";

    if (response2.value) {
      this.editdiv = document.createElement("div");
      this.editdiv.style.position = "absolute";
      this.editdiv.style.display = "none";
      this.editdiv.innerHTML = this.getEditHtml();
      this.maindiv.appendChild(this.editdiv);
      this.maindiv.onmouseover = function() { this.firstChild.style.display = "block" };
      this.maindiv.onmouseout = function() { this.firstChild.style.display = "none" };


      this.editordiv = document.createElement("div");
      this.editordiv.style.position = "absolute";
      this.editordiv.style.display = "none";
      this.maindiv.appendChild(this.editordiv);

      this.editordiv.innerHTML = this.getEditorHtml();
    }

    this.maindiv.appendChild(this.imgdiv);

  }

  this.TL = function(txt) {
    if (!this.tl) {
      var tls = ['Redigera slumpbilder','Publicera ändringar','Publicera','Redigera','Återgå till publicerade värden','Lägg till ny slumpbild','Ny','Bild','Stäng','Länk','Ta bort','Är du säker på att du vill ångra alla ändingar sedan senaste publiceringen','Spara','Avbryt','Dokumentbank','Är du säker på att du vill ta bort bilden'];
      var tt = translateArr(tls);
      this.tl = new Object();
      for (var i=0; i < tt.length; i++)
        this.tl[tls[i]] = tt[i];
    }
    return this.tl[txt];
  }

  this.getEditHtml = function() {
    var response = NFN.BasePage.IsPagePropPublished(_pageId, this.id);
    var html = "<div style='background:#fff;border:solid 1px #000;padding:5px'><a href='javascript:rnd_images[" + this.arrindex + "].showEdit()' title='" + this.TL('Redigera slumpbilder') + "'><img src='admin/gfx/edit.gif' alt='" + this.TL('Redigera') + "' /></a>";
    if (!response.value)
      html += "&nbsp;&nbsp;<a href='javascript:rnd_images[" + this.arrindex + "].publish()' title='" + this.TL('Publicera ändringar') + "'><img src='admin/gfx/publish.gif' alt='" + this.TL('Publicera') + "' /></a>&nbsp;&nbsp;<a href='javascript:rnd_images[" + this.arrindex + "].unpublish()' title='" + this.TL('Återgå till publicerade värden') + "'><img src='admin/gfx/unpublish.gif' alt='Unpublish' /></a>";
    html += "</div>";
    return html;
  }

  this.getEditorHtml = function() {
    var response = Master.GetImages(_pageId, this.id);
    var html = "<div style='background:#eee;border:solid 1px #000;padding:10px;width:280px'>" +
      "<div><a href='javascript:rnd_images[" + this.arrindex + "].newImage()' title='" + this.TL('Lägg till ny slumpbild') + "'><img src='admin/gfx/new.gif' alt='" + this.TL('Ny') + "' /><a href='javascript:rnd_images[" + this.arrindex + "].closeEdit()' style='margin-left:250px' title='" + this.TL('Stäng') + "'><img src='admin/gfx/closewind.gif' alt='" + this.TL('Stäng') + "' /></div>" +
      "<table id='rndtable_" + this.id + "'><tr><th></th><th></th><th>" + this.TL('Bild') + "</th><th>" + this.TL('Länk') + "</th></tr>";
    for (var i=0; i < response.value.length; i++) {
      var param = response.value[i].split('|');
      html +=
        "<tr>" +
        "<td><a href='javascript:void(0)' onclick='rnd_images[" + this.arrindex + "].editOne(this)' title='" + this.TL('Redigera') + "'><img src='admin/gfx/edit.gif' alt='" + this.TL('Redigera') + "' /></a><a href='javascript:void(0)' onclick='rnd_images[" + this.arrindex + "].deleteOne(" + i + ")' title='" + this.TL('Ta bort') + "'><img src='admin/gfx/delete.gif' alt='" + this.TL('Ta bort') + "' /></a></td>" +
        "<td></td>" +
        "<td><span id='s_" + this.id + "_path_" + i + "'><img src='admin/DocumentBank/thumbnail.aspx?image=" + param[1] + "&width=50&height=50' /></span></td>" +
        "<td><span id='s_" + this.id + "_link_" + i + "'>" + param[2] + "</span></td>" +
        "</tr>" +

        "<tr style='display:none'>" +
        "<td><a href='javascript:void(0)' onclick='rnd_images[" + this.arrindex + "].saveOne(this, " + i + ")' title='" + this.TL('Spara') + "'><img src='admin/gfx/save.gif' alt='" + this.TL('Spara') + "' /></a><a href='javascript:void(0)' onclick='rnd_images[" + this.arrindex + "].undoOne(this, " + i + ")' title='" + this.TL('Avbryt') + "'><img src='admin/gfx/cancel.gif' alt='" + this.TL('Avbryt') + "' /></a><a href='javascript:void(0)' onclick='rnd_images[" + this.arrindex + "].openDocBank(" + i + ")' title='" + this.TL('Dokumentbank') + "'><img src='admin/gfx/documentbank.gif' alt='" + this.TL('Dokumentbank') + "' /></a></td>" +
        "<td><input id='" + this.id + "_id_" + i + "' value='" + param[0] + "' type='hidden' /></td>" +
        "<td><input id='" + this.id + "_path_" + i + "' style='width:100px' value='" + param[1] + "' type='text' /></td>" +
        "<td><input id='" + this.id + "_link_" + i + "' style='width:100px' value='" + param[2] + "' type='text' /></td>" +
        "</tr>";
    }
    html +=
      "<tr id='newrow_" + this.id + "' style='display:none'>" +
      "<td><a href='javascript:void(0)' onclick='rnd_images[" + this.arrindex + "].saveOneNew(this)' title='" + this.TL('Spara') + "'><img src='admin/gfx/save.gif' alt='" + this.TL('Spara') + "' /></a><a href='javascript:void(0)' onclick='rnd_images[" + this.arrindex + "].undoOneNew(this)' title='" + this.TL('Avbryt') + "'><img src='admin/gfx/cancel.gif' alt='" + this.TL('Avbryt') + "' /></a><a href='javascript:void(0)' onclick='rnd_images[" + this.arrindex + "].openDocBank(\"new\")' title='" + this.TL('Dokumentbank') + "'><img src='admin/gfx/documentbank.gif' alt='" + this.TL('Dokumentbank') + "' /></a></td>" +
      "<td></td>" +
      "<td><input id='" + this.id + "_path_new' style='width:100px' type='text' /></td>" +
      "<td><input id='" + this.id + "_link_new' style='width:100px' type='text' /></td>" +
      "</tr>";
    html += "</table></div>";
    return html;
  }

  this.openDocBank = function(idx) {
    currRnd = this.arrindex + ";" + idx;
    window.open('/admin/DocumentBank/DocumentBank.aspx?callback=rnd_callbackDB', 'MB', 'width=900,height=600,resizable=1')
  }

  this.callbackDB = function(idx, docInfo) {
    $(this.id + "_path_" + idx).value = docInfo.url;
  }

  this.editOne = function(a) {
    var tr = a.parentNode.parentNode;
    var edtr = tr.nextSibling;
    tr.style.display = "none";
    edtr.style.display = "";
  }

  this.deleteOne = function(idx) {
    if (confirm(this.TL("Är du säker på att du vill ta bort bilden") + "?")) {
      var imgid = $(this.id + '_id_' + idx).value;
      var response = Master.DeleteRnd(_pageId, this.id, imgid);
      this.editdiv.innerHTML = this.getEditHtml();
      this.editordiv.innerHTML = this.getEditorHtml();
    }
  }

  this.saveOne = function(a, idx) {
    var imgid = $(this.id + '_id_' + idx).value;
    var vals = $(this.id + '_path_' + idx).value + "|" + $(this.id + '_link_' + idx).value;
    var response = Master.UpdateRnd(_pageId, this.id, imgid, vals);
    this.editdiv.innerHTML = this.getEditHtml();
    this.editordiv.innerHTML = this.getEditorHtml();

    this.undoOne(a, idx);
  }

  this.undoOne = function(a, idx) {
    var tr = a.parentNode.parentNode;
    var sttr = tr.previousSibling;
    tr.style.display = "none";
    sttr.style.display = "";
  }

  this.newImage = function() {
    var tr = $("newrow_" + this.id);
    tr.style.display = "";
  }

  this.saveOneNew = function() {
    var vals = $(this.id + '_path_new').value + "|" + $(this.id + '_link_new').value;
    var response = Master.SaveNewRnd(_pageId, this.id, vals);
    this.editdiv.innerHTML = this.getEditHtml();
    this.editordiv.innerHTML = this.getEditorHtml();
  }

  this.undoOneNew = function() {
    var tr = $("newrow_" + this.id);
    tr.style.display = "none";
  }

  this.showEdit = function() {
    this.editdiv.style.display = "none";
    this.editordiv.style.display = "block";
    this.maindiv.onmouseover = null;
    this.maindiv.onmouseout = null;
  }

  this.closeEdit = function() {
    this.editdiv.style.display = "block";
    this.editordiv.style.display = "none";
    this.maindiv.onmouseover = function() { this.firstChild.style.display = "block" };
    this.maindiv.onmouseout = function() { this.firstChild.style.display = "none" };
  }

  this.publish = function() {
    Master.PublishRnd(_pageId, this.id);
    this.editdiv.innerHTML = this.getEditHtml();
    this.editordiv.innerHTML = this.getEditorHtml();
  }

  this.unpublish = function() {
    if (confirm(this.TL("Är du säker på att du vill ångra alla ändingar sedan senaste publiceringen") + "?")) {
      Master.UnpublishRnd(_pageId, this.id);
      this.editdiv.innerHTML = this.getEditHtml();
      this.editordiv.innerHTML = this.getEditorHtml();
    }
  }

  this.arrindex = rnd_images.length;
  rnd_images.push(this);

  this.setup();
}
