﻿var userImagePath, userImageWidth, userImageHeight;
var siteID;

function setUserImageData(path, width, height) {
	userImagePath = path;
	userImageWidth = width;
	userImageHeight = height;
}
var jsTabArray;
function setJsTabArray(array) {
	jsTabArray = array;
}
function showTotal(id){
  new Ajax.Request('/section/user/common/TopUserAjax.ashx', { method: 'get', onSuccess: showResponse});
  EntryTabSelect(jsTabArray, id);
}
function showVideo(id){
  new Ajax.Request('/section/user/common/TopUserAjax.ashx?type=2', { method: 'get', onSuccess: showResponse});
  EntryTabSelect(jsTabArray, id);
}
function showPhoto(id){
  new Ajax.Request('/section/user/common/TopUserAjax.ashx?type=1', { method: 'get', onSuccess: showResponse});
  EntryTabSelect(jsTabArray, id);
}
var mediaUploadsLangTag;
function setMediaUploadsLangTag(tag) {
	mediaUploadsLangTag = tag;
}
function getUploadInfo(user){
  var container = document.createElement('div');
  container.style.marginTop = '10px';
  
  container.appendChild(document.createTextNode(mediaUploadsLangTag + ': '));
  var uploadLabel = document.createElement('span');
  uploadLabel.style.fontWeight = 'bold';
  uploadLabel.appendChild(document.createTextNode(formatNumber(user.MediaCount)));
  container.appendChild(uploadLabel);
  
  return container;
}
function getImageCell(user){
  var imageCell = document.createElement('td');
  
  var imageLink = document.createElement('a');
  imageLink.setAttribute("href", '/' + user.Username)
  imageLink.title = user.Username;
  var thumbnail = new Image();
  if(user.GotPicture){
    thumbnail.src = userImagePath + user.PictureID.substr(0,3) + "/" + user.PictureID + ".jpg?updated=" + escape(user.PictureUpdatedTime);
  }
  else{
      thumbnail.src = '/gfx/design/user/user_nopicture_48_' + siteID + '.gif';
  }
  thumbnail.style.width = userImageWidth + 'px';
  thumbnail.style.height = userImageHeight + 'px';
  thumbnail.style.borderWidth = '0px';
  imageLink.appendChild(thumbnail);
  
  imageCell.appendChild(imageLink);
  return imageCell;
}
function getUsernameLink(user){
  var container = document.createElement('div');
  
  var statusIcon = document.createElement('div');
  statusIcon.className = user.Online ? 'useronline' : 'useroffline';
  statusIcon.style.cssFloat = statusIcon.style.styleFloat = 'left';
  statusIcon.style.paddingRight = '5px';
  statusIcon.style.marginTop = '3px';
  container.appendChild(statusIcon);
  
  var usernameContainer = document.createElement('div');
  usernameContainer.style.width = '100px';
  usernameContainer.style.overflow = 'hidden';
  var usernameLink = document.createElement('a');
  usernameLink.setAttribute('href', '/' + user.Username);
  usernameLink.title = user.Username;
  usernameLink.appendChild(document.createTextNode(user.Username));
  usernameContainer.appendChild(usernameLink);
  
  var sexAge = user.Gender ? 'M' : 'K';
  sexAge += user.Age;
  usernameContainer.appendChild(document.createTextNode(' ' + sexAge));
  
  container.appendChild(usernameContainer);
  
  return container;
}
onload = function() {
  showTotal(0);
};
function showResponse(response){
  var list = eval('(' + response.responseText +')');
  if(list != null && list.length > 0){
    populateContainer(list);
  }
}
function populateContainer(userList){
  var mainTable = document.createElement("table");
  mainTable.setAttribute("cellspacing", "10");
  mainTable.style.marginTop = '0px';
  var mainTableBody = document.createElement("tbody");
  
  for(i = 0; i < userList.length; i++){
    mainTableBody.appendChild(getUserRow(userList[i]));
  }
  
  mainTable.appendChild(mainTableBody);
  $('entriesContainer').innerHTML = '';
  $('entriesContainer').appendChild(mainTable);
}
function getUserRow(user){
  var userRow = document.createElement('tr');
  userRow.appendChild(getImageCell(user));
  userRow.appendChild(getInfoCell(user));
  return userRow;
}
function getInfoCell(user){
  var infoCell = document.createElement('td');
  infoCell.setAttribute('valign', 'top');
  
  infoCell.appendChild(getUsernameLink(user));
  infoCell.appendChild(getUploadInfo(user));
  
  return infoCell;
}