var ajaxPending = false;
var postQuickAddCallback;

function showStatusWorking(strMessage) {
  var objDiv = document.getElementById("statusDiv");
  if (objDiv == null) {
    return;
  }
  $("#statusDiv").css("top", (getScrollXY()[1] + 50) + "px");
  $("#statusDiv").html("<div class='loadingspan'>" + strMessage + "</div>");
  $("#statusDiv").css("visibility", "visible");
  $("#statusDiv").show();
}

function showStatusComplete(strMessage) {
  var objDiv = document.getElementById("statusDiv");
  if (objDiv == null) {
    return;
  }
  $("#statusDiv").css("top", (getScrollXY()[1] + 50) + "px");
  $("#statusDiv").html("<div class='loadingspandone'>" + strMessage + "</div>");
  $("#statusDiv").css("visibility", "visible");
  $("#statusDiv").show();
  $("#statusDiv").fadeOut(6000);
}

function showStatusError(strMessage) {
  var objDiv = document.getElementById("statusDiv");
  if (objDiv == null) {
    return;
  }
  $("#statusDiv").css("top", (getScrollXY()[1] + 50) + "px");
  $("#statusDiv").html("<div class='loadingspanerror'>" + strMessage + "</div>");
  $("#statusDiv").css("visibility", "visible");
  $("#statusDiv").show();
  $("#statusDiv").fadeOut(6000);
}

function hideStatus() {
  var objDiv = document.getElementById("statusDiv");
  if (objDiv == null) {
    return;
  }
  $("#statusDiv").css("visibility", "hidden");
}

/**
 * Populate all loaded solutions into the select box with the given Id
 */
function populateSolutionsInSelect(sSelectId) {
    // Put all the solutions in the dropdown list
    var select = document.getElementById(sSelectId);
    for (var i = 0; i < solutions.length; i++) {
	select.options[select.options.length] = new Option(solutions[i].name, solutions[i].id);
    }
}

/**
 * Find the current window/document scrolled X, Y position.
 */
function getScrollXY() {
  var scrOfX = 0, scrOfY = 0;
  if( typeof( window.pageYOffset ) == 'number' ) {
    //Netscape compliant
    scrOfY = window.pageYOffset;
    scrOfX = window.pageXOffset;
  } else if( document.body && ( document.body.scrollLeft || document.body.scrollTop ) ) {
    //DOM compliant
    scrOfY = document.body.scrollTop;
    scrOfX = document.body.scrollLeft;
  } else if( document.documentElement && ( document.documentElement.scrollLeft || document.documentElement.scrollTop ) ) {
    //IE6 standards compliant mode
    scrOfY = document.documentElement.scrollTop;
    scrOfX = document.documentElement.scrollLeft;
  }
  return [ scrOfX, scrOfY ];
}

/**
 * A screen coordinate.
 */
function Coordinates(intX, intY) {
  this.x = intX;
  this.y = intY;
}

/**
 * Find an objects absolute X, Y screen coordinates.
 */
function findObjPosition(obj) {
  var curleft = curtop = 0;
  if (obj.offsetParent) {
    curleft = obj.offsetLeft
    curtop = obj.offsetTop
    while (obj = obj.offsetParent) {
      curleft += obj.offsetLeft
      curtop += obj.offsetTop
    }
  }
  return new Coordinates(curleft, curtop);
}

function kgToPounds(kg) {
    var pounds = kg * 2.2;
    return pounds.toFixed(0);
}

/**
 * Quick-add a custom one-time impact.
 */
function quickAddMemberImpact(strDescription) {
  if (strDescription == "") {
    showStatusError("Nothing to add."); 
    return;
  }

  showStatusWorking("Adding impact...");

  // TODO hardcoded ref to custom one-time action as ID 25
  var sUrl = websiteRoot + "/ajax/add_impact.php?name=" + strDescription + "&co2_savings=0&cash_savings=0&solution_id=25";
  $.ajax({
      url: sUrl,
      type: 'GET',
      dataType: 'html',
      timeout: 10000,
      error: function(request, errMsg, errObj) {
	 showStatusError("Error adding impact."); 
      },
      success: function(html){
	  showStatusComplete("Impact added.");
          if (postQuickAddCallback != null) {
              postQuickAddCallback();
          }
      }
  });
}

function getElementPosition(theElementId){
  var theElement = document.getElementById(theElementId);
  var selectedPosX = 0;
  var selectedPosY = 0;

  while(theElement != null){
    selectedPosX += theElement.offsetLeft;
    selectedPosY += theElement.offsetTop;
    theElement = theElement.offsetParent;
  }

  return new Coordinates(selectedPosX,selectedPosY);
}

function scrollToElement(theElementId){
  var coords = getElementPosition(theElementId);
  window.scrollTo(coords.x, coords.y);
}

