// vim600: fdm=marker
///////////////////////////////////////////
// author     : Ben Kietzman
// begin      : 06/15/2009
// copyright  : kietzman.org
// email      : ben@kietzman.org
///////////////////////////////////////////
// {{{ variables
var bAnchored = true;
var bRunningCentralMessages = false;
var periodicalCentralMessages = null;
var f = null;
var json = null;
// }}}
// {{{ buildGraph()
function buildGraph()
{
  if ($('future').selectedIndex > 0 || $('ratio').selectedIndex > 0)
  {
    $('frequency').selectedIndex = 0;
  }
  $j.ajax
  ({
    url: '/include/buildGraph.php',
    type: 'POST',
    data: $j('#formgraph').serialize(),
    success: function(html)
    {
      //$j('#raw').html(html);
      var data = $j.parseJSON(html);
      var options =
      {
        legend: {show: false},
        xaxis: {tickFormatter: formatTicker},
        selection: {mode: "xy"}
      };
      var plot = $j.plot($j('#graph'), data, options);
      var overviewOptions =
      {
        legend: {show: true, container: $j('#overviewLegend')},
        xaxis: {show: false},
        yaxis: {show: false},
        selection: {mode: "xy"}
      };
      var overview = $j.plot($j('#overview'), data, overviewOptions);
      $j('#graph').bind('plotselected', function (event, ranges)
      {
        if (ranges.xaxis.to - ranges.xaxis.from < 0.00001)
        {
          ranges.xaxis.to = ranges.xaxis.from + 0.00001;
        }
        if (ranges.yaxis.to - ranges.yaxis.from < 0.00001)
        {
          ranges.yaxis.to = ranges.yaxis.from + 0.00001;
        }
        plot = $j.plot($j('#graph'), data, $j.extend(true, {}, options, {xaxis: {min: ranges.xaxis.from, max: ranges.xaxis.to}, yaxis: {min: ranges.yaxis.from, max: ranges.yaxis.to}}));
        overview.setSelection(ranges, true);
      });
      $j('#overview').bind('plotselected', function (event, ranges)
      {
        plot.setSelection(ranges);
      });
    }
  });
}
// }}}
// {{{ toggleGraph()
function toggleGraph(bLarge)
{
  if (bAnchored || bLarge)
  {
    var nHeight = $j(window).width() / 2;
    if (nHeight > $j(window).height())
    {
      nHeight = $j(window).height();
    }
    $j('#graphcontainer').css({position: 'absolute', left: 10, top: 40, width: $j(window).width() - 40, height: nHeight - 80});
    $j('#graph').css({width: $j(window).width() - 60 - 166, height: nHeight - 100 - 100});
    buildGraph();
    $j('#graphtoggle').html('Pop-In');
    bAnchored = false;
  }
  else
  {
    $j('#graph').css({width: 530, height: 250});
    buildGraph();
    $j('#graphcontainer').css({position: 'relative', left: '', top: '', width: '', height: ''});
    $j('#graphtoggle').html('Pop-Out');
    bAnchored = true;
  }
}
// }}}
// {{{ formatTicker()
function formatTicker(val)
{
  var strResult = val;
  var strFrequency = $j('#frequency').val();

  if (strFrequency == 'monthly' || strFrequency == 'daily' || strFrequency == 'hourly')
  {
    var m = new Date();
    m.setTime(val*1000);
    strResult = m.getFullYear() + '-' + (m.getMonth()+1);
    if (strFrequency == 'daily' || strFrequency == 'hourly')
    {
      strResult = strResult + '-' + m.getDate();
      if (strFrequency == 'hourly')
      {
        strResult = strResult + ' ' + (m.getHours()+1) + ':00:00';
      }
    }
  }

  return strResult;
}
// }}}
// {{{ getApplicationList()
function getApplicationList(nPopup, strSortBy, strSortOrder, cLetter, nPage)
{
  $j('#result').html('<b>Fetching information...</b>');
  $j.ajax({url: "/include/getApplicationList.php", type: "POST", data: {popup: nPopup, sortby: strSortBy, sortorder: strSortOrder, letter: cLetter, page: nPage}, success: function(html){$j("#result").html(html);}});
}
// }}}
// {{{ getCentralMessages()
function getCentralMessages(strSite)
{
  if (periodicalCentralMessages != null)
  {
    periodicalCentralMessages.stop();
    delete periodicalCentralMessages;
    periodicalCentralMessages = null;
  }
  new Ajax.Updater('central_messages', '/include/getCentralMessages.php', {method: 'post', parameters: {site: strSite}});
  periodicalCentralMessages = new PeriodicalExecuter(function (pe) {if (!bRunningCentralMessages) {bRunningCentralMessages = true; new Ajax.Updater('central_messages', '/include/getCentralMessages.php', {onSuccess: function(transport) {bRunningCentralMessages = false;}, onFailure: function(transport) {bRunningCentralMessages = false;}, method: 'post', parameters: {site: strSite}});}}, 120);
}
// }}}
// {{{ getHistorical()
function getHistorical()
{
  $('historical').innerHTML = 'Fetching...';
  new Ajax.Updater('historical', '/include/getHistorical.php');
}
// }}}
// {{{ getPersonInfo()
function getPersonInfo(nPopup, strUserID)
{
  $('central_popup_' + nPopup).innerHTML = '<b>Fetching information...</b>';
  new Ajax.Updater('central_popup_' + nPopup, '/include/getPersonInfo.php', {method: 'post', parameters: {userid: strUserID}, onFailure: function(transport) {var response = transport.responseText || 'Internal AJAX Error'; $('central_popup_' + nPopup).innerHTML = response;}});
}
// }}}
// {{{ getServerList()
function getServerList(nPopup, strSortBy, strSortOrder, cLetter, nPage)
{
  $('result').innerHTML = '<b>Fetching information...</b>';
  new Ajax.Updater('result', '/include/getServerList.php', {method: 'post', parameters: {popup: nPopup, sortby: strSortBy, sortorder: strSortOrder, letter: cLetter, page: nPage}, onFailure: function(transport) {var response = transport.responseText || 'Internal AJAX Error'; $('result').innerHTML = response;}});
}
// }}}
// {{{ getWeather()
function getWeather(strCountry, strCity)
{
  $('weather').innerHTML = 'Fetching...';
  new Ajax.Updater('weather', '/include/getWeather.php', {method: 'post', parameters: {country: strCountry, city: strCity}});
  new PeriodicalExecuter(function (pe) {new Ajax.Updater('weather', '/include/getWeather.php', {method: 'post', parameters: {country: strCountry, city: strCity}});}, 600);
}
// }}}

