var trace = 0;

function GoTo(url,frm) {
  if (trace) {
    url += (url.indexOf('?') >= 0) ? '&' : '?';
    url += 'debug='+trace;
  }
  if (frm) {
    if (frm.redir) frm.redir.value = url;
    if (frm.cfg) frm.cfg.value = '';
    frm.submit();
  }
  else {
    location.href = url;
  }
  return false;
}

var lastStatus = '';
function ChangeAvailability(obj,id,title) {
  if (obj.value == 'del' && !confirm('Delete '+title+'?')) {
    obj.value = lastStatus;
    return false;
  }
  location = document.getElementById('avail'+id).value + obj.value;
  return true;
}

function ShowImage(id,filename) {
  var elem = document.getElementById(id);
  if (filename.indexOf('/') == -1)
      filename = 'images/'+filename;
  if (elem) elem.src = filename;
}

function ShowControls(id) {
  if (arguments.length < 1) id = curPhoto;
  var elem = document.getElementById('thumb'+id+'Ctrls');
  if (elem) elem.className = "showit";
}
function HideControls(id) {
  if (arguments.length < 1) id = curPhoto;
  var elem = document.getElementById('thumb'+id+'Ctrls');
  if (elem) elem.className = "hideit";
}
function ToggleControls(id) {
  if (arguments.length < 1) id = curPhoto;
  var elem;
  if (elem = document.getElementById('thumb'+id+'Ctrls')) {
    if (elem.className == "showit")
      elem.className = "hideit";
    else
      elem.className = "showit";
  }
}

var curPhoto = 0;
function ShowPhoto(obj,id) {
  obj.src = thumbDir+photos[id];
  var elem = document.getElementById('mainphoto');
  if (elem) elem.src = photoDir+photos[id];
  if (id == curPhoto) {
    ToggleControls(curPhoto);
  } else {
    HideControls(curPhoto);
    ShowControls(id);
    if (top.UploadFile) top.UploadFile.SetPhotoNum(id);
    elem = document.getElementById('photonum');
    if (elem) elem.innerHTML = id+1;
    elem = document.getElementById('UploadImage');
    if (elem && elem.className == 'showit') {
      ClearMove('UploadImage','UploadTitle');
      elem.className = 'hideit';
    }
    curPhoto = id;
  }
  elem = document.getElementById('moveLeft');
  if (elem) elem.disabled = (id == 0);
  elem = document.getElementById('moveRight');
  if (elem) elem.disabled = (id == photos.length-1);

  return false;
}

//var uploadedPhotos = new Array();
function UploadPhoto(id) {
  if (arguments.length < 1) id = curPhoto;
  if (top.UploadFile) {
    var f = top.UploadFile.document.f;
    if (f.filename.value.replace(/ +/,'')) {
      UploadingPhoto();
      f.submit();
      return false;
    }
  }
  var elem = document.getElementById('UploadImage');
  if (elem.className == 'hideit') {
    elem.className = 'showit';
    elem = document.getElementById('UploadFrame');
    elem.src = elem.src.replace(/&img=\d+/,'') + '&img=' + id;
    InitMove('UploadImage','UploadTitle');
  } else {
    ClearMove('UploadImage','UploadTitle');
    elem.className = 'hideit';
  }
  return false;
}
function UploadingPhoto(id) {
  if (arguments.length < 1) id = curPhoto;
  var elem = document.getElementById('mainphoto');
  if (elem) elem.src = 'images/uploading.png';
  elem = document.getElementById('thumb'+id);
  if (elem) elem.src = 'images/uploadingsm.png';
  HideControls(id);
}
function UploadedPhoto(id,filename) {
  //alert('UploadedPhoto('+id+',"'+filename+'")');
  UploadPhoto(id);
  //if (id >= photos.length) {
  //  var photo = filename;
  //  uploadedPhotos[uploadedPhotos.length] = filename;
  //}
  HideControls(id);
  UpdatePhoto(id,filename);
}
function UpdatePhoto(id,filename) {
  if (arguments.length < 1) id = curPhoto;
  if (arguments.length < 2)
    filename = photos[id];
  else
    photos[id] = filename;
  var elem = document.getElementById('mainphoto');
  if (elem) elem.src = photoDir+filename;
  elem = document.getElementById('thumb'+id);
  if (elem) elem.src = thumbDir+filename;
}
function DeletePhoto(id) {
  if (arguments.length < 1) id = curPhoto;
  if (confirm('Delete image #'+(id+1))) {
    var elem = document.getElementById('ActionFrame');
    if (elem) {
      var opts = trace ? '&debug='+trace : '';
      elem.src = elem.src.replace(/&action=.*/,'') + '&action=del&img='+id+opts;
      if (trace) elem.className = 'showit';
    }
  }
  DeletingPhoto(id);
  return false;
}
function DeletingPhoto(id) {
  if (arguments.length < 1) id = curPhoto;
  HideControls(id);
  //UpdatePhoto(id,noimage);
}
function DeletedPhoto(id) {
  if (arguments.length < 1) id = curPhoto;
  UpdatePhoto(id,noimage);
}
function AddPhoto(id,n) {
  if (arguments.length < 1) {
    id = curPhoto;
    if (document.getElementById('before').checked)
      n = -1;
    else if (document.getElementById('after').checked)
      n = +1;
    else
      n = 0;
  }
  else if (arguments.length < 2)
    n = 0;

  if (n < 0)
    msg = 'Insert new image before #'+(id+1);
  else if (n > 0)
    msg = 'Insert new image after #'+(id+1);
  else
    msg = 'Add image at end';
  alert(msg);

  return false;
}
function MovePhoto(id,dx,dy) {
  if (arguments.length < 1) id = curPhoto;
  else if (id == null) id = curPhoto;
  if (arguments.length < 2) dx = 0;
  if (arguments.length < 3) dy = 0;

  if (trace) {
    if (dx < 0)
      msg = 'Move image #'+(id+1)+' to the left';
    else if (dx > 0)
      msg = 'Move image #'+(id+1)+' to the right';
    else if (dy < 0)
      msg = 'Move image #'+(id+1)+' up';
    else if (dy > 0)
      msg = 'Move image #'+(id+1)+' down';
    else
      msg = 'Move image #'+(id+1);
    alert(msg);
  }

  var id2 = id + dx + 2*dy;
  //if (trace) alert('MovePhoto('+id+','+dx+','+dy+'): '+id2);

  var elem = document.getElementById('ActionFrame');
  if (elem) {
    var opts = trace ? '&debug='+trace : '';
    elem.src = elem.src.replace(/&action=.*/,'') + '&action=swap&img='+id+'&img2='+id2+opts;
    if (trace) elem.className = 'showit';
  }

  MovingPhotos(id,id2);
  return false;
}
function MovingPhotos(id1,id2) {
  HideControls(id1);
  var elem = document.getElementById('mainphoto');
  if (elem) elem.src = 'images/blank.png';
  var elem = document.getElementById('thumb'+id1);
  if (elem) elem.src = 'images/blanksm.png';
  elem = document.getElementById('thumb'+id2);
  if (elem) elem.src = 'images/blanksm.png';
}
function MovedPhotos(id1,id2) {
  var file1 = photos[id1];
  var file2 = photos[id2];
  UpdatePhoto(id2,file1);
  UpdatePhoto(id1,file2);
}

function GetFile(cbox,name) {
  var elem = document.getElementById(name);
  var eold = document.getElementById(name+'_filename');
  if (cbox.checked) {
    eold.className = 'hideit';
    elem.className = 'showit';
  }
  else {
    eold.className = 'showit';
    elem.className = 'hideit';
    elem.value     = '';
  }
}

var MoveInfo = { el:null, dx:0, dy:0 };
function MoveElement(e) {
  if (!e) e = window.event;
  var cursor = GetCursorPos(e);
  var elem = MoveInfo.el;
  elem.style.left = cursor.x + MoveInfo.dx;
  elem.style.top  = cursor.y + MoveInfo.dy;
}
function StartMove(e) {
  if (!e) var e = window.event;
  var cursor = GetCursorPos(e);
  var elem = MoveInfo.el;
  var pos = GetPos(elem);
  MoveInfo.dx = pos.x - cursor.x;
  MoveInfo.dy = pos.y - cursor.y;
  document.onmousemove = MoveElement;
  //if (document.captureEvents)
  //  document.captureEvents(Event.MOUSEMOVE);
}
function StopMove(e) {
  document.onmousemove = null;
}
function InitMove(name,trigger) {
  MoveInfo.el = document.getElementById(name);
  var elem = document.getElementById(trigger);
  AddEvent(elem,'mousedown',StartMove);
  AddEvent(elem,'mouseup',StopMove);
}
function ClearMove(name,trigger) {
  var elem = document.getElementById(trigger);
  RemoveEvent(elem,'mousedown',StartMove);
  RemoveEvent(elem,'mouseup',StopMove);
  MoveInfo.el = null;
  MoveInfo.dx = 0;
  MoveInfo.dy = 0;
}

