//
// Javascript functions to handle dynamic Ajax interaction
// @author bjoern
//

// ajax request
request = (navigator.appName.search("Microsoft") > -1) ? new ActiveXObject("MSXML2.XMLHTTP") : new XMLHttpRequest();

// capture mouse move event
if (window.Event) {
  document.captureEvents(Event.MOUSEMOVE);
}
document.onmousemove = getXY;

/**
 * Move the ajax mouse trail next to the cursor.
 * This is needed to display dynamic content next to the cursor.
 * @param e {Event} Mouse move event
 */
function getXY(e) {
  var posx = 0;
  var posy = 0;
  if (!e) var e = window.event;
  if (e.pageX || e.pageY)   {
    posx = e.pageX;
    posy = e.pageY;
  } else if (e.clientX || e.clientY) {
    posx = e.clientX + document.body.scrollLeft + document.documentElement.scrollLeft;
    posy = e.clientY + document.body.scrollTop + document.documentElement.scrollTop;
  }
  el = document.getElementById("ajaxMouseTrail");
  if(el.style.visibility == "visible") {
    //el.innerHTML = "" + posx + "/" + posy;
    el.style.left = "" + (posx + 5) + "px";
    el.style.top = "" + (posy + 5) + "px";
  }
}

/**
 * Request user information and show them next to the cursor.
 * @param userId {Integer} UserId to read the info for
 */
function showUserInfo(userId) {
  el = document.getElementById("ajaxMouseTrail");
  el.innerHTML = "&nbsp;";
  el.style.visibility = "visible";
  request.open('get', '?action=AjaxShowUserInfo&user_id=' + userId, true);
  request.onreadystatechange = fillAjaxMouseTrail;
  request.send(null);
}

/**
 * Request bet information and show them next to the cursor.
 * @param betId {Integer} BetId to read the info for
 */
function showBetInfo(betId) {
  el = document.getElementById("ajaxMouseTrail");
  el.innerHTML = "&nbsp;";
  el.style.visibility = "visible";
  request.open('get', '?action=AjaxShowBetInfo&bet_id=' + betId, true);
  request.onreadystatechange = fillAjaxMouseTrail;
  request.send(null);
}

/**
 * Fill the mouse trail with the response of the ajax request.
 */
function fillAjaxMouseTrail() {
  if (self.request.readyState == 4) {
    el = document.getElementById("ajaxMouseTrail");
    el.innerHTML = "&nbsp;" + self.request.responseText;
  }
}

/**
 * Hide the mouse trail that has been enabled by ajax.
 */
function hideAjaxMouseTrail() {
  el = document.getElementById("ajaxMouseTrail");
  el.innerHTML = "&nbsp;";
  el.style.visibility = "hidden";
}

/**
 * Request watcher information of a bet and show that on screen.
 * @param betId {Integer} BetId to read the watchers for
 * @param resultTitle {String} Title of the window to show
 */
function showWatcherInfo(betId, resultTitle) {
  request.open('get', '?action=AjaxShowWatcher&bet_id=' + betId, true);
  request.onreadystatechange = new Function('fai', 'fillAjaxInfobox("' + resultTitle + '")');
  request.send(null);
}

/**
 * Request agreement for the bet
 * @param betId {Integer} BetId to read the watchers for
 * @param resultTitle {String} Title of the window to show
 */
function showAgreement(betId, resultTitle) {
  request.open('get', '?action=AjaxShowAgreement&bet_id=' + betId, true);
  request.onreadystatechange = new Function('fai', 'fillAjaxInfoboxWide("' + resultTitle + '")');
  request.send(null);
}

/**
 * Request winner/loser information of a bet in score.
 * @param betId {Integer} BetId to show the winner/loser for
 * @param resultTitle {String} Title of the window to show
 */
function showWinLoseUserInfo(betId, isWinner, resultTitle) {
  request.open('get', '?action=AjaxShowWinLoseUser&bet_id=' + betId+'&isWinner='+ isWinner, true);
  request.onreadystatechange = new Function('fai', 'fillAjaxInfobox("' + resultTitle + '")');
  request.send(null);
}

/**
 * Request a new window to change the joiners of a bet.
 * @param betId {Integer} BetId to change the joiners for
 * @param resultTitle {String} Title of the window to show
 */
function showChangeJoiner(betId, resultTitle) {
  request.open('get', '?action=AjaxShowChangeJoiner&bet_id=' + betId);
  request.onreadystatechange = new Function('fai', 'fillAjaxInfobox("' + resultTitle + '")');
  request.send(null);
}

/**
 * Request a password change.
 * @param resultTitle {String} Title of the window to show
 */
function changePassword(resultTitle) {
  oldPassword = document.getElementById("old_password").value;
  newPassword1 = document.getElementById("new_password1").value;
  newPassword2 = document.getElementById("new_password2").value;
  url = '?action=AjaxChangePassword&old_password=' + oldPassword + '&new_password1=' + newPassword1 + '&new_password2=' + newPassword2;
  //window.alert(url);
  request.open('get', url, true);
  request.onreadystatechange = new Function('fai', 'fillAjaxInfobox("' + resultTitle + '")');
  request.send(null);
}

/**
 * Request a window to propose an opposer.
 * @param resultTitle {String} Title of the window to show
 */
function proposeOpposer(resultTitle) {
  opposer = document.getElementById("new_bet_add_opposer").value;
  url = '?action=AjaxProposeOpposer&name=' + opposer;
  request.open('get', url, true);
  request.onreadystatechange = new Function('fai', 'fillAjaxInfobox("' + resultTitle + '")');
  request.send(null);
}

/**
 * Request a window to propose a joiner.
 * @param resultTitle {String} Title of the window to show
 */
function proposeJoiner(resultTitle) {
  joiner = document.getElementById("new_bet_add_joiner").value;
  url = '?action=AjaxProposeJoiner&name=' + joiner;
  request.open('get', url, true);
  request.onreadystatechange = new Function('fai', 'fillAjaxInfobox("' + resultTitle + '")');
  request.send(null);
}

// Dialog to fill with content and show to the user.
var $dialog;

/**
 * Fill a dialog with the response of the ajax request.
 * @param wintitle {String} Title of the window to show
 */
function fillAjaxInfobox(wintitle) {
  if (self.request.readyState == 4) {
    $dialog = $('<div></div>')
      .html(self.request.responseText)
      .dialog({
        autoOpen: false,
        height: 'auto',
        title: wintitle
       });
    $dialog.dialog('open');
  }
}

/**
 * Fill a dialog with the response of the ajax request.
 * @param wintitle {String} Title of the window to show
 */
function fillAjaxInfoboxWide(wintitle) {
  if (self.request.readyState == 4) {
    $dialog = $('<div></div>')
      .html(self.request.responseText)
      .dialog({
        autoOpen: false,
        title: wintitle,
        width: 400
       });
    $dialog.dialog('open');
  }
}

/**
 * Hide an opened dialog with content from an ajax request.
 */
function hideAjaxInfobox() {
  $dialog.dialog('close');
}

/**
 * Request a an info whether a bet has news.
 * Writes the News-Tag directly to the given HTML-Element.
 * @param betId {Integer} Bet Id to check for news
 * @param resultId {String} HTML-Element-Id to set the results within
 */
function showBetNews(betId, resultId) {
  $.ajax({
    type: 'get',
    url: '?action=AjaxShowBetNewsStatus&bet_id=' + betId,
    async: true,
    success: function(result) {
      $('#'+resultId).html("&nbsp;" + result);
    }
  });
}

/**
 * Request a an HTML string with action links that can be performed on a bet.
 * Writes the Actions-Tag directly to the given HTML-Element.
 * @param betId {Integer} Bet Id to check for actions
 * @param resultId {String} HTML-Element-Id to set the results within
 */
function showBetActions(betId, resultId) {
  $.ajax({
    type: 'get',
    url: '?action=AjaxShowBetAction&bet_id=' + betId,
    async: true,
    success: function(result) {
      $('#'+resultId).html("&nbsp;" + result);
    }
  });
}

