// Define the common objects for the site

function Filter(strName, strId, boolCustom, arrIndicatorIds) {
  this.name = strName;
  this.id = strId;
  this.custom = boolCustom;
  this.indicatorIds = arrIndicatorIds;
}

function Indicator(strName, strDescription, strId) {
  this.name = strName;
  this.description = strDescription;
  this.id = strId;
}

var runnableIndicators = new Array();
var filters = new Array();

function getRunnableIndicatorById(strId) {
  for (var i = 0; i < runnableIndicators.length; i++) {
    if (runnableIndicators[i].id == strId) {
      return runnableIndicators[i];
    }
  }
  return null;
}

function showIndicatorInfo(strId) {
  var ind = getRunnableIndicatorById(strId);
  if (ind != null) {
    alert(ind.description);
  } else {
    alert("Sorry, no description of this indicator yet.");
  }
}

function buildIndicatorsTable(arrIndicators, objDiv, strTitle) {
  var strHTML = "<table class=\"indicatorstable\" cellpadding=\"0\" cellspacing=\"0\"><tr><th class=\"selectcol\">&nbsp;</th><th>" + strTitle + "<br><span class=\"titledescr\">(Mouse over name to read description)</span></th><th class=\"infocol\">Info</th></tr>";
  for (var i = 0; i < arrIndicators.length; i++) {
    strHTML += "<tr class='indicatorstablerow" + ((i % 2) + 1) + "'>\n";
    strHTML += "    <td><input type='checkbox' name='indicator[]' value='" + arrIndicators[i].id + "' id='indicator" + arrIndicators[i].id + "'></td><td title='" + arrIndicators[i].description + "'>" + arrIndicators[i].name + "</td><td style='text-align: center'><a href=\"javascript:void(0);\" onclick=\"showIndicatorInfo('" + arrIndicators[i].id + "')\"><img src='img/icon_info.gif' border='0'></a></td>\n";
    strHTML += "</tr>\n";
  }
  strHTML += "</table>";
  objDiv.innerHTML = strHTML;  
}

/**
 * Move an object horizontally.
 */
function moveHorizontal(strDivId, intTo, intTarget, intIncrement) {
  var timeout = 20;
  var objDiv = document.getElementById(strDivId);
  if (objDiv == null) {
    return;
  }
  objDiv.style.left = intTo;
  if (intIncrement > 0) {
    if (intTo < intTarget) {
      var newX = intTo + intIncrement;
      setTimeout("moveHorizontal('" + strDivId + "', " + newX + ", " + intTarget + ", " + intIncrement + ")", timeout);
    } else {
      objDiv.style.left = intTarget;
    }
  } else {
    if (intTo > intTarget) {
      var newX = intTo + intIncrement;
      setTimeout("moveHorizontal('" + strDivId + "', " + newX + ", " + intTarget + ", " + intIncrement + ")", timeout);
    } else {
      objDiv.style.left = intTarget;
    }
  }
}

/**
 * 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 [curleft,curtop];
}

/**
 * Build the quote table and return it as HTML.
 */
function buildQuoteTable(quote) {
  var html = "<h3>" + quote.company + "</h3><table class='statstable' cellspacing='0' cellpadding='0'>";
  html += "<tr><th>Latest Price</th><td>" + quote.price.toFixed(2) + "</td></tr>";
  html += "<tr><th>Change</th><td " + addColor(quote.change, 0) + ">" + quote.change.toFixed(2) + "</td></tr>";
  html += "<tr><th>Open</th><td>" + quote.open.toFixed(2) + "</td></tr>";
  html += "<tr><th>High</th><td>" + quote.high.toFixed(2) + "</td></tr>";
  html += "<tr><th>Low</th><td>" + quote.low.toFixed(2) + "</td></tr>";
  html += "<tr><th>Prev Close</th><td>" + quote.prevClose.toFixed(2) + "</td></tr>";
  html += "<tr><th>50-Day MA</th><td>" + quote.fiftyDayAverage.toFixed(2) + "</td></tr>";
  html += "<tr><th>Volume</th><td>" + quote.volume + "</td></tr>";
  html += "<tr><th>Avg Daily Volume</th><td>" + quote.avgDailyVolume + "</td></tr>";
  html += "<tr><th>EPS</th><td>" + quote.eps.toFixed(3) + "</td></tr>";
  html += "<tr><th>P/E</th><td>" + quote.pe.toFixed(2) + "</td></tr>";
  html += "<tr><th>PEG</th><td>" + quote.peg.toFixed(2) + "</td></tr>";
  html += "<tr><th>1y Price Target</th><td>" + quote.oneyPriceTarget.toFixed(2) + "</td></tr>";
  html += "</table>";
  return html;
}

/**
 * Show a popup box with filter information.  Expects a DIV id filterInfo.
 * Will position somewhere near objElement.
 */
function showFilterInfo(intFilterIndex, objElement) {
  var objInfo = document.getElementById("filterInfo");
  var arrXY = findObjPosition(objElement);
  objInfo.style.top = arrXY[1] - 100;

  var filter = filters[intFilterIndex];

  var html = "<div id='filterInfoClose'><a href='javascript:void(0)' onclick='hideFilterInfo()'>close</a></div><h3>Filter Details</h3><h4>" + filter.name + "</h4><p><b>Indicators:</b><ul>";
  for (var i = 0; i < filter.indicatorIds.length; i++) {
    var indicator = getRunnableIndicatorById(filter.indicatorIds[i]);
    if (indicator != null) {
      html += "<li>" + indicator.name + "</li>";
    }
  }
  html += "</ul></p>";

  objInfo.innerHTML = html;
  objInfo.style.visibility = "visible";
  
}

function hideFilterInfo() {
  var objInfo = document.getElementById("filterInfo");
  objInfo.style.visibility = "hidden";
}

function showStatusWorking(strMessage) {
  var objDiv = document.getElementById("statusDiv");
  if (objDiv == null) {
    return;
  }
  objDiv.style.top = document.body.scrollTop + 20;
  objDiv.innerHTML = "<div class='loadingspan'>" + strMessage + "</div>";
  objDiv.style.visibility = "visible";
}

function showStatusComplete(strMessage) {
  var objDiv = document.getElementById("statusDiv");
  if (objDiv == null) {
    return;
  }
  objDiv.style.top = document.body.scrollTop + 20;
  objDiv.innerHTML = "<div class='loadingspandone'>" + strMessage + "</div>";
  objDiv.style.visibility = "visible";
  window.setTimeout("hideStatus()", 2000); 
}

function showStatusError(strMessage) {
  var objDiv = document.getElementById("statusDiv");
  if (objDiv == null) {
    return;
  }
  objDiv.style.top = document.body.scrollTop + 20;
  objDiv.innerHTML = "<div class='loadingspanerror'>" + strMessage + "</div>";
  objDiv.style.visibility = "visible";
  window.setTimeout("hideStatus()", 2000); 
}

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