/* mp = miniprofile */
mpOnTimeout = mpOffTimeout = false;
mpData = [];

mpUid = 0;

var mpAjax = [];

function mpOn(a) {
  //alert(a.tagName);
  var href = a.href;
  if (!href.match(/^.*\/users\/\d+.*$/)) return;
  var pos = href.lastIndexOf('/');
  var uid = href.substr(pos + 1);
  pos = uid.lastIndexOf('#');
  if (pos > -1) {
    uid = uid.substr(0, pos);
  }

  // corrent miniprofile already showing - cancel off timeout if any
  if (uid == mpUid) {
    if (mpOffTimeout) clearTimeout(mpOffTimeout);
    return;
  }

  // wrong miniprofile showing - remove it
  if (uid != mpUid && mpUid > 0) {
    removeMpNow();
  }

  mpUid = uid;
  mpX = getLeftPos(a);
  mpY = getTopPos(a) + a.offsetHeight;
  if (mpData[uid]) {
    // cached
    mpOnTimeout = setTimeout('mpShow(' + uid + ')', 500);
  } else {
    mpOnTimeout = setTimeout('mpDoAjax(' + uid + ')', 1000);
  }

}

function mpOff() {
  // mouseout before miniprofile was shown
  if (mpUid == 0) {
    removeMpNow();
    return;
  }

  // miniprofile visible - remove after timeout
  mpOffTimeout = setTimeout('removeMpNow()', 200);
}

function mpDoAjax(uid) {
  var ajax = new sack();
  var index = mpAjax.length;
  ajax.requestFile = '/ajax/mp_ajax.php?uid=' + uid;
  ajax.onCompletion = function() { mpCallback(index, uid); };
  mpAjax[index] = ajax;
  ajax.runAJAX();
}

function mpCallback(index, uid) {
  var ajax = mpAjax[index];
  var json = eval("(" + ajax.xmlhttp.responseText + ")");
  if (json.uid) mpData[json.uid] = json;
  mpShow(json.uid);
}

function mpShow(uid) {
  mpUid = uid;
  var user = mpData[uid];

  /*var innerSize = getWindowInnerSize();
  var scrollTop = getScrollTop();
  if (mpY + 50 > innerSize.height + scrollTop) {
    // miniprofile wont fit on screen - what to do? can't figure out
    // it's height in advance to position it neatly on top of the nick :|
  }*/

  var div = document.createElement('div');
  div.id = 'mp';
  div.style.left = mpX + 'px';
  div.style.top = mpY + 'px';
  div.onmouseover = function(e) { cancelMpRemoval(); };
  div.onmouseout = function(e) { mpOff(); };
  if (uid == 9) {
    div.style.backgroundImage = "url('/v3/img/nmdbg.png')";
  }


  /* FdFace */
  if (user.face == 1) {
    var table = createPhotoWithShadow('/pictures/faces/' + user.uid + '.gif', '/users/' + user.uid);
    div.appendChild(table);
  } else {
    var noFace = div.appendChild(document.createElement('div'));
    noFace.id = 'mp_noface';
    noFace.innerHTML = mpTexts.noFaceFound;
  }

  /* Info */
  var header = div.appendChild(document.createElement('div'));
  header.id = 'mp_header';
  var html = '<a class="mp" href="/users/' + user.uid + '"><b>'
           + unescape(user.nick);
  if (user.donated >= 5) {
    html += '<img src="/img/star.gif" />';
  }

  html += '</b> (' + unescape(user.name) + ', ' + user.age;

  if (user.age >= 16 && user.status) {
    html += ', ' + unescape(user.status);
  }

  html += ')</a>';

  header.innerHTML = html;

  /*span = div.appendChild(document.createElement('div'));

  html = '<a onclick="ajaxGet(\'/ajax/register_click.php?uid=0&target=silja&source=miniprofile%20'
       + escape(user.nick) + '\');" href="http://clk.tradedoubler.com/click?p=55332&a=1296902&g=16700640" target="_new">'
       + mpTexts.silja.replace('{0}', unescape(user.nick))
       + '</a>';
  span.innerHTML = html;*/

  //div.appendChild(document.createElement('br'));

  var span = div.appendChild(document.createElement('span'));
  html = '<b>' + mpTexts.lastSeen + ':</b> ';
  if (user.lastSeen) {
    html += unescape(user.lastSeen);
    if (user.lastSeenTs) {
      html += ' ' + unescape(user.lastSeenTs) + ' ' + mpTexts.ago;
    }
  } else {
    html += '-';
  }
  span.innerHTML = html;

  div.appendChild(document.createElement('br'));

  var ward = div.appendChild(document.createElement('img'));
  ward.id = 'mp_ward_img';
  ward.src = '/img/ward_small.gif';

  span = div.appendChild(document.createElement('span'));
  html = '<a class="mp" href="'
       + '/users/search.php?nick=&name=&sex=x&status=all&bornafter=1950&bornbefore=1995&country=&stake=&ward='
       + user.wardId + '">' + unescape(user.ward) + '</a>';
  if (user.stakeId != '') {
    html += ', <a class="mp" href="'
         + '/users/search.php?nick=&name=&sex=x&status=all&bornafter=1950&bornbefore=1995&country=&ward=-3&stake='
         + user.stakeId + '">' + unescape(user.stake);
  }
  html += '</a>';
  span.innerHTML = html;
  div.appendChild(document.createElement('br'));

  var span = div.appendChild(document.createElement('span'));
  span.innerHTML = '<b>' + mpTexts.latest + ': ...</b>';

  div.appendChild(document.createElement('br'));

  var comm = div.appendChild(document.createElement('img'));
  comm.id = 'mp_comments_img';
  comm.src = '/img/comments_small.gif';

  span = div.appendChild(document.createElement('span'));
  html = '<b>' + mpTexts.comment + ':</b> ';

  if (user.lastCommentUrl) {
    html += '<a class="mp" href="' + unescape(user.lastCommentUrl) + '">' + unescape(user.lastComment) + '</a>';
  } else {
    html += '-';
  }
  span.innerHTML = html;

  div.appendChild(document.createElement('br'));

  var photo = div.appendChild(document.createElement('img'));
  photo.id = 'mp_photos_img';
  photo.src = '/img/photo_small.gif';

  span = div.appendChild(document.createElement('span'));
  html = '<b>' + mpTexts.photoAppearance + ':</b> ';
  if (user.lastPhotoUrl) {
    html += '<a class="mp" href="' + unescape(user.lastPhotoUrl) + '">' + unescape(user.lastPhotoIn) + '</a>';
  } else {
    html += '-';
  }
  span.innerHTML = html;

  div.appendChild(document.createElement('br'));

  var msg = div.appendChild(document.createElement('img'));
  msg.id = 'mp_message_img';
  msg.src = '/img/message_small.gif';

  span = div.appendChild(document.createElement('span'));
  html = '<b>' + mpTexts.onlineMessage + ':</b> ';
  if (user.lastMsg) {
    html += '<a class="mp" href="javascript:showDialogue(' + user.uid + ', ' + user.lastMsgToUid + ', ' + user.lastMsgId + ');">'
         +  unescape(user.lastMsg) + '</a>';
  } else {
    html += '-';
  }
  span.innerHTML = html;

  div.appendChild(document.createElement('br'));

  /*span = div.appendChild(document.createElement('span'));

  html = '<a onclick="ajaxGet(\'/ajax/register_click.php?uid=0&target=www.fi&source=miniprofile%20'
       + escape(user.nick) + '\');" href="http://clk.tradedoubler.com/click?p=52099&a=1296902&epi='
       + escape('www.fi_via_miniprofile_[' + user.nick + ']') + '&url=http://www.fi/WebSearch?advancedPage=1&advancedPage=1&safeSearch=true&doQuery=true&world=true&query=' + escape(user.name) + '" target="_new">'
       + mpTexts.wwwFi.replace('{0}', unescape(user.name))
       + '</a>';
  span.innerHTML = html;*/


  /*var ad = document.getElementById('mp_ad');
  var iframe = ad.getElementsByTagName('iframe')[0];
  var mpaddiv = div.appendChild(document.createElement('div'));
  mpaddiv.id = 'mp_ad_div';
  mpaddiv.style.clear = 'both';
  mpaddiv.style.paddingTop = '20px';
  mpaddiv.appendChild(iframe);*/

  document.body.appendChild(div);
}

function removeMpNow() {
  var div = byId('mp');
  /*if (div) {
    var ad = document.getElementById('mp_ad_div').getElementsByTagName('iframe')[0];
    document.getElementById('mp_ad').appendChild(ad);
  }*/
  if (div) div.parentNode.removeChild(div);
  if (mpOnTimeout) clearTimeout(mpOnTimeout);
  if (mpOffTimeout) clearTimeout(mpOffTimeout);
  mpUid = 0;
}

function cancelMpRemoval() {
  if (mpOffTimeout) clearTimeout(mpOffTimeout);
}
function modifyLinksMp() {
  links = document.getElementsByTagName("a");
  for (i = 0; i < links.length; i++) {
    var href = links[i].href;
    if (href.indexOf('/users/') < 0) continue;
    links[i].onmouseover = function() { mpOn(this); };
    links[i].onmouseout = function() { mpOff(); }
    links[i].onclick = function() { removeMpNow(); };
  }
}

